gpt4 book ai didi

python - 为什么我的简单 pygame 会滞后?

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:14 25 4
gpt4 key购买 nike

<分区>

我一直在使用 pygame 制作一个简单的 python 游戏,在我添加切换枪支的功能后,游戏开始滞后。我不知道为什么它会滞后。我试过重新启动,但没有用。代码真的很短,所以也许它只是我的电脑,但如果有什么可以帮助它更快地运行,请告诉我。这是代码:

import sys, pygame, pygame.mixer
from pygame.locals import *

pygame.init()

size = width, height = 600, 400

screen = pygame.display.set_mode(size)

pygame.display.set_caption('Blue Screen of Death')

#variables
x = 100
y = 200
gun_type = "gun1"
gun = pygame.image.load("gun1.png")
gun = pygame.transform.scale(gun,(500,250))
gun_sound = pygame.mixer.Sound("gun_sound.wav")
clock = pygame.time.Clock()

while 1:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:sys.exit()

elif event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
gun_sound.play()
elif event.type == KEYDOWN and event.key == K_1:
gun = pygame.image.load("gun1.png")
gun = pygame.transform.scale(gun,(500,250))
gun_type = "gun2"
elif event.type == KEYDOWN and event.key == K_2:
gun = pygame.image.load("gun2.png")
gun = pygame.transform.scale(gun,(500,250))
gun_type = "gun2"
elif event.type == KEYDOWN and event.key == K_TAB:
if gun_type == "gun2":
gun_type = "gun2_aimed"
elif gun_type == "gun2_aimed":
gun_type = "gun2"
elif gun_type == "gun2_aimed":
gun = pygame.image.load("gun2_aimed.png")
gun = pygame.transform.scale(gun,(500,250))



#frames per second
clock.tick(60)

hallway = pygame.image.load("hallway.png")
hallway = pygame.transform.scale(hallway,(600,400))
screen.blit(hallway,(0,0))

screen.blit(gun,(mx-100,y))

pygame.display.flip()

感谢您的帮助。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com