如何修复pygame中的自动时间更新?

2024-03-28 11:07:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一个游戏,你必须点击苹果,然后它就会变成随机的位置。你呢有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()

Here是我的食物形象。你知道吗


Tags: importevent游戏getiftimedisplayrandom
1条回答
网友
1楼 · 发布于 2024-03-28 11:07:05

对我来说,程序正常退出(我在linux、python2.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 

翻转用于更新曲面。你需要更新表面后,你有所有的抽屉。你要做的是把所有东西都填成白色,更新,然后画一个文本,更新。所以你的每一个记号都是白色的

相关问题 更多 >