2016-05-21 8 views
0

私はあなたがリンゴをクリックしなければならないゲームを作ろうとしています。そして、それはランダムポジションになります。今のところ5秒あります。リンゴイメージを何回もクリックしなければなりません。ゲームは動作しますが、私は2つの問題があります:
1)ゲーム終了後、プログラムは応答しません。
2)時間は自分自身を更新し続けます(それはちらつきを続けます)。私はそれを望んでいません。 これらの2つのバグを修正するにはどうすればよいですか?pygameで時間の自動更新を修正するには?

import pygame 
import time 
import random 
pygame.init() 

foodimg=pygame.image.load("food.png") 
foodrect=foodimg.get_rect() 

#Colours 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 

#Game Display 
display_width = 1080 
display_height = 720 
gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('Click It! ~ Snapnel Productions') 
gameDisplay.fill((white)) 

font=pygame.font.SysFont("Arial",30) 

a=6 
running=True 
while running: 
    gameDisplay.fill((white)) 
    time=pygame.time.get_ticks()/1000 
    if a==time: 
     print "Game over" 
     break 

    for event in pygame.event.get(): 
     if event.type==pygame.QUIT: 
      running=False 
      pygame.quit() 
      quit() 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      # Set the x, y postions of the mouse click 
      x, y = event.pos 
      if foodrect.collidepoint(x, y): 
       foodrect.center=(random.randint(5,1060),random.randint(5,700)) 
       print "New Position: ",foodrect.center 
       continue 

    gameDisplay.blit(foodimg,foodrect) 
    pygame.display.flip() 
    showtime=font.render("Time: "+str(time),0,(0,0,0)) 
    gameDisplay.blit(showtime,(950,10)) 
    pygame.display.flip() 

Hereは私の食料画像です:

は、ここに私のコードです。

+0

私にとっては、プログラムは正常に終了します –

答えて

0

私にとっては、プログラムは正常に終了します(私はLinux上で実行しました、Python 2.6)。 flockeringを削除する :

gameDisplay.blit(foodimg,foodrect) 
#pygame.display.flip() < -- remove that 
showtime=font.render("Time: "+str(time),0,(0,0,0)) 
gameDisplay.blit(showtime,(950,10)) 
pygame.display.flip() # because you should do it after the text is drawed 

フリップ表面を更新するために使用されます。すべての描画が完了したら、サーフェスを更新する必要があります。あなたがしていることは、すべてを白く塗りつぶし、更新してテキストを描画し、更新することです。だからあなたはテキストを描画し、白い塗りつぶしごとに塗りつぶします

関連する問題