Pygame时钟不准确

时间:2015-08-20 19:25:18

标签: python pygame

我正试图在弗雷迪的“粉丝游戏”中度过五夜,但时钟部分无法正常工作。我有一个叫做'time'的函数,设置为每帧运行一次。它计算程序所在的帧以及何时等于40(我以40 fps运行),它为变量'seconds'加1,当秒等于某个量时,小时将改变。有3个问题:

当相机处于启动状态时(将鼠标移动到屏幕底部),新时间图像会在旧图像上显示,但只要将相机放回原位(只需将鼠标移到底部即可)它再次恢复正常。

时间不准确。就像我之前提到的那样,我有它所以时间会每10秒钟改变一次。问题是,当我计时时,小时之间的时间不一致(即改变为1:00需要15秒,然后改为2:00需要18秒,然后改为3:00需要17秒,等等。)

最后一个问题是6:00的图像永远不会被搞砸。该程序不会将6:00图像替换为5:00图像,而是在5:00之后不会显示任何图像以显示小时。

我的代码如下:

import pygame, random
pygame.init()

screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN, 32)
pygame.display.toggle_fullscreen()
clock = pygame.time.Clock()

twelve = pygame.image.load('images/12.png')
one = pygame.image.load('images/1.png')
two = pygame.image.load('images/2.png')
three = pygame.image.load('images/3.png')
four = pygame.image.load('images/4.png')
five = pygame.image.load('images/5.png')
six = pygame.image.load('images/5.png')

office_pic = pygame.image.load('images/office.png')
stage = pygame.image.load('images/stage.jpg')
building_map = pygame.image.load('images/map.png')
building_map.convert_alpha()

breathing = pygame.mixer.Sound('sounds/breathing.wav')

screen_width = 1280
screen_height = 800

x = 0
x_move = 0

camera_up = False
v = 0
camera = stage

fps_tick = 0
seconds = 0
hour = 1


def time():
    global fps_tick, seconds, hour
    fps_tick += 1

    if fps_tick == 40:
        seconds += 1
        fps_tick = 0

    if seconds == 10:
        hour += 1
        seconds = 0


    #blit the hour
    if hour == 1:
        screen.blit(twelve, ((screen_width-150), 30))
    elif hour == 2:
        screen.blit(one, ((screen_width-150), 30))
    elif hour == 3:
        screen.blit(two, ((screen_width-150), 30))
    elif hour == 4:
        screen.blit(three, ((screen_width-150), 30))
    elif hour == 5:
        screen.blit(four, ((screen_width-150), 30))
    elif hour == 6:
        screen.blit(five, ((screen_width-150), 30))
    elif hour == 7:
        screen.blit(six, ((screen_width-150), 30))



def office(mouse_x, click):
    global x, x_move

    if camera_up == False:
        if mouse_x >= (screen_width/2) + 200 and mouse_x <= (screen_width/2) + 400:
            x_move = 6
        elif mouse_x <= (screen_width/2) - 200 and mouse_x >= (screen_width/2) - 400:
            x_move = -6
        elif mouse_x > (screen_width/2) + 400:
            x_move = 12
        elif mouse_x < (screen_width/2) - 400:
            x_move = -12
        else:
            x_move = 0

    x -= x_move

    if camera_up == False:
        screen.blit(office_pic, (x,0))



def cameras(mouse_x, mouse_y, click):

    global camera_up, v

    if mouse_y >= (screen_height - (screen_height / 6)) and camera_up == False and v == 0:
        camera_up = True
    if not mouse_y >= (screen_height - (screen_height / 6)) and camera_up == True:
        v = 1
    if mouse_y >= (screen_height - (screen_height / 6)) and camera_up == True and v == 1:
        screen.fill((0,0,0))
        camera_up = False
    if not mouse_y >= (screen_height - (screen_height / 6)) and camera_up == False:
        v = 0

    if camera_up == True:
        screen.blit(camera, (0,0))
        screen.blit(building_map, (screen_width-650, screen_height-426))




def main():

    global x, x_move, camera_up, v, click

    while True:       

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_ESCAPE:
                   pygame.quit()
                   quit()

        mouse_xy = pygame.mouse.get_pos()
        mouse_x = mouse_xy[0]
        mouse_y = mouse_xy[1]
        click = pygame.mouse.get_pressed()


        office(mouse_x, click)            

        cameras(mouse_x, mouse_y, click)

        time()


        pygame.display.update()
        clock.tick(40)



main()

1 个答案:

答案 0 :(得分:3)

如果你想每10秒做一次,最好使用pygame的事件系统,只需用pygame.time.set_timer安排一个用户事件。

例如,

pygame.time.set_timer(pygame.USEREVENT, 10000)

每10秒触发一次pygame.USEREVENT事件。您可以像主循环中的每个事件一样监听它:

...
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
       if event.key == pygame.K_ESCAPE:
            pygame.quit()
            quit()
    if event.type == pygame.USEREVENT:
        hour += 1
...

如果你仔细观察,就会发现为什么图像在fivesix之间没有变化:你使用相同的图像。

...
five = pygame.image.load('images/5.png')
six = pygame.image.load('images/5.png')
...