我正在尝试制作一个游戏,你必须点击苹果,然后它会移动到随机位置。你有5秒钟(现在),你必须多次点击苹果图像,因为你不能。我的游戏有效,但我有两个问题:
1)游戏结束后,程序不响应
2)时间不断更新(它继续闪烁)我不希望这样。
我该如何纠正这两个错误?
这是我的代码:
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()
this stackoverlow answer是我的食物形象。
答案 0 :(得分:0)
对我来说,程序正常退出(我在linux上运行它,python 2.6)。 要删除植绒:
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
Flip用于更新曲面。在绘制完所有内容后,您需要更新曲面。你做的是填充所有白色,更新,然后绘制文本,更新。所以你每个刻度都有文字绘制和填充白色