Python中如何在游戏循环运行时使用多线程显示游戏得分?

3

我希望得分基于游戏中经过的时间。为此,我想同时运行两个循环。游戏循环和分数循环,在分数循环中,每1.5秒将1加到分数上。当我运行程序时,分数不会出现。我是否正确使用了多线程?这是最好的方法吗?为简单起见,我只发布了相关代码。谢谢!

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()

编辑:最终得到了这个结果...

import time

score = 0

def timer(oldsec):
    currentsec = time.time()
    if abs(currentsec - oldsec > 3):
        return True
    else:
        False


oldsec = time.time() # what time is it now

while True:
    if timer(oldsec) == True:
        score += 1
        oldsec = time.time()
        print (score)

1
不,你做得不对。你应该只在单独的线程中执行实际需要时间完成的任务(比如程序化世界生成)。此外,像许多其他GUI /渲染库一样,我认为Pygame不支持多线程。也就是说,你不能从除主事件循环线程以外的线程渲染任何东西到屏幕上。 - EvilTak
好的,谢谢您解释这个。您有关于如何同时运行两个循环的建议吗? - John Fisher
1
如果你想运行第二个循环以便每1.5秒更新一次分数,请不要这样做。你可以在更新循环中通过简单地检查自上次得分更新以来是否已经过了1.5秒来完成此操作。如果是,则更新分数,否则不更新。理想情况下,你应该将其抽象成一个“计时器”或“定时事件”类,该类每隔x秒调用一次方法,并在每次事件循环运行时检查x秒是否已经过去。 - EvilTak
太棒了,谢谢! - John Fisher
1
只需使用其中一个计时器(我指的是 pygame.time.get_ticksset_timer 和我的 delta 时间解决方案)而不是创建新线程。 - skrx
1个回答

0
你可以使用 _thread。
from _thread import *

count = 0

def Your_other_loop():
    global count
    while True:
        #Do loop stuff



def loop():
    global count
    while True:
        print(count)

start_new_thread(Your_other_loop,())
loop()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接