python游戏结束显示分数代码_当游戏循环在Python中运行时,多线程来显示游戏分数?...
我想根據(jù)比賽時(shí)間的推移得分。為此,我想讓兩個(gè)循環(huán)同時(shí)運(yùn)行。游戲循環(huán)和得分循環(huán),每1.5秒加1。當(dāng)我運(yùn)行程序時(shí),分?jǐn)?shù)不會(huì)出現(xiàn)。我是否正確使用多線程?這是最好的辦法嗎?為了簡單起見,我只發(fā)布了相關(guān)代碼。謝謝!在def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def displayScore(text):
while(True):
textStyle = pygame.font.Font('freesansbold.ttf', 25)
# pop up window defining window and textRect to align text properly
textWindow, textRect = text_objects(text, textStyle)
textRect.center = ((display_width/2)), ((display_height/2))
gameDisplay.blit(textWindow, textRect)
pygame.display.update()
time.sleep(1.5)
text = int(text)
text +=1
text = str(text)
def gameStart():
x_change = 0
y_change = 0
x = (display_width * 0.39)
y = (display_height * 0.7)
car_width = 200
x_brick = (display_width * random.uniform(.05, .95))
y_brick = display_height - 925
# exception for the game crashing and the game loop
gameExit = False
while not gameExit:
# capturing user actions
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# key-down events for car movements
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
# cancelling out key-up events
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
# crash conditions
if x > display_width - car_width or x < 0:
crash()
if y > display_height - 175 or y < 0 - 10:
crash()
# updating car movements
x += x_change
y += y_change
# generating display
gameDisplay.fill(white)
car(x, y)
brick(x_brick, y_brick)
# moving the brick
y_brick += brick_speed
# updates for parameter given or entire surface
pygame.display.update()
# frames per second, the more frames per second the faster and smoother things move, taxing on processing speed
clock.tick(60)
t1 = Thread(target=gameStart())
t2 = Thread(target=displayScore('0'))
t1.start()
t2.start()
編輯:最后是這個(gè)。。在
^{pr2}$
總結(jié)
以上是生活随笔為你收集整理的python游戏结束显示分数代码_当游戏循环在Python中运行时,多线程来显示游戏分数?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想e580没有声音_现在你可以购买通过
- 下一篇: with as python_pytho