Pygame 时钟不准确

Pygame clock not accurate

我正在尝试制作玩具熊的五夜后宫 "fan game",但时钟部分不工作。我有一个名为 'time' 的函数,每帧设置一次 运行。它计算程序运行的帧数,当它等于 40(我以 40 fps 的速度 运行ning)时,它向变量 'seconds' 加 1,当秒数等于一定数量时,小时将改变。有3个问题:

当摄像头升起时(将鼠标移动到屏幕底部),新的时间图像将覆盖旧图像,但是一旦您将摄像头放回原处(只需将鼠标移到屏幕底部)屏幕底部)它恢复正常。

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

最后一个问题是 6:00 图像永远不会被块化。该程序没有用 6:00 图像替换 5:00 图像,而是简单地 blitts 没有图像来显示 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()

如果您想每 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')
...