Pygame游戏中的大滞后

时间:2015-09-16 23:01:21

标签: python pygame

我在弗雷迪的比赛中做了五个晚上的比赛,我所拥有的一切都应该如此运作。问题是按下灯按钮时会出现延迟。当你向左或向右移动时,还有一个非常轻微的滞后(我在添加灯光功能之前没有在版本中注意到它)。感谢您的帮助。

这是我的代码的简化版本:

import pygame, random
pygame.init()

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

office_closed = pygame.image.load('images/office.png')
left_win = pygame.image.load('images/left_win.png')
right_win = pygame.image.load('images/right_win.png')

building_map = pygame.image.load('images/map.png')
building_map.convert_alpha()
camera_switch = pygame.image.load('images/camera_switch.png')
turn_left = pygame.image.load('images/turn_left.png')
turn_right = pygame.image.load('images/turn_right.png')
turn_left.convert_alpha()
turn_right.convert_alpha()
light_button = pygame.image.load('images/light_button.png')

v = 0
a = 0
position = 'door'

def door_lights(mouse_x, mouse_y, click, position):

    global left_win, right_win

    if position == 'left_win':    
        if click == (1,0,0) and (mouse_x >= 20) and (mouse_x <= 95) and (mouse_y >= 363) and (mouse_y <= 437):
            left_win = pygame.image.load('images/left_win_light.png')       
        else:
          left_win = pygame.image.load('images/left_win.png')


    if position == 'right_win':     
        if click == (1,0,0) and (mouse_x >= 1185) and (mouse_x <= 1260) and (mouse_y >= 363) and (mouse_y <= 437):
            right_win = pygame.image.load('images/right_win_light.png')         
        else:
            right_win = pygame.image.load('images/right_win.png')



def office(mouse_x, mouse_y, click):
    global position, a

    if position == 'door':
        if (mouse_x <= 95)   and (mouse_y >= 100) and (mouse_y <= 500) and (a==0):
            position = 'left_win'
        if (mouse_x >= 1185) and (mouse_y >= 100) and (mouse_y <= 500) and (a==0):
            position = 'right_win'

    if a == 1:    #IF THE MOUSE IS NOT OVER A BUTTON
        if not ((mouse_x <= 95) and (mouse_y >= 100) and (mouse_y <= 500)) and not ((mouse_x >= 1185) and (mouse_y >= 100) and (mouse_y <= 500)):
            a = 0    #RESET TO 0

    if position == 'left_win':
        if (mouse_x >= 1185)   and (mouse_y >= 100) and (mouse_y <= 500) and (a==0):
            position = 'door'
            a = 1

    if position == 'right_win':
        if (mouse_x <= 95)  and (mouse_y >= 100)  and (mouse_y <= 500) and (a==0):
            position = 'door'
            a = 1

    door_lights(mouse_x, mouse_y, click, position)


    if position == 'door':
        screen.blit(office_closed, (0,0))
        screen.blit(turn_left, (20,100))
        screen.blit(turn_right, (1185,100))

    if position == 'left_win':
        screen.blit(left_win, (0,0))
        screen.blit(light_button,(20, 363))
        screen.blit(turn_right, (1185,100))

    if position == 'right_win':
        screen.blit(right_win, (0,0))
        screen.blit(light_button,(1185, 363))
        screen.blit(turn_left, (20,100))


def main():

    global v

    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, mouse_y, click)            

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



main()

1 个答案:

答案 0 :(得分:5)

每次单击灯光时,都会加载图像。这需要一段时间,只需要完成一次。将图像加载到字典或其他内容中,然后将(例如)right_win指向加载的图像。