2017-11-14 5 views
2

ゲームに合格した時間に基づいてスコアを求めます。これを行うには、2つのループを同時に実行したいと思います。ゲームのループと、1.5秒ごとにスコアに1を加えるスコアループ。プログラムを実行すると、スコアは表示されません。私はマルチスレッドを正しく使用していますか?それはこれを行うための最善の方法ですか?簡単にするため、私は関連するコードだけを掲載しました。ありがとう!ゲームループがPythonで実行されている間、マルチスレッドでゲームのスコアを表示しますか?

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

編集:これで終わった..あなたは_threadを使用することができ

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

+0

さて、これを説明していただきありがとうございます。同時に2つのループを実行する方法についての提案はありますか? –

+1

1.5秒ごとにスコアを更新する目的で2回目のループを実行する場合は、実行しないでください。最後のスコアの更新から1.5秒が経過したかどうかを確認するだけで、更新ループでこれを行うことができます。彼らが持っている場合、そうでなければスコアを更新しないでください。理想的には、これを 'Timer'または' TimedEvent'クラスに抽象化し、 'x'秒ごとにメソッドを呼び出し、イベントループが実行されるたびに' x'秒が経過したかどうかをチェックします。 – EvilTak

答えて

0

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() 
関連する問題