gpt4 book ai didi

python - Pygame Sprite 太慢

转载 作者:行者123 更新时间:2023-12-04 10:48:18 25 4
gpt4 key购买 nike

我最近一直在使用 Pygame 开发游戏。它在两个不同的背景中加载(用于将来的视差滚动),然后在可以用箭头键控制并可以左右跳跃和移动的 Sprite 中加载广告。当我运行它时,一切都正常加载,直到我尝试控制 Sprite 。它 react 缓慢,我必须不断地重新按下箭头键才能让它移动。我用来让 sprite 移动的代码与我之前在另一个程序中使用的代码相同,该程序只是在背景和 sprite 中加载并控制 sprite 的移动。对于我的新程序,我只是添加了屏幕调整大小和全屏选项。那是它变慢的时候。是什么减慢了它的速度?我是否需要更好地优化我的代码?

如果这有帮助,我还注意到,当我快速按下另一个键和我的箭头键时,它会以正确的速度移动。就像这样更新它。是否需要进行某种更新?此外,我已经在我的 if 语句中使用了“Pygame.display.update()”来控制 Sprite 键。


import pygame
from pygame.locals import *

pygame.init()

#two backgrounds
bg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Llama game/Llama imaging/backgrounds concepts/Mountains/PPP_BG1.png')
fg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Llama game/Llama imaging/backgrounds concepts/Mountains/PPP_fg1.png')

clock = pygame.time.Clock()

def Sresize():
#screen resize:
pygame.event.pump()

event=pygame.event.wait()

if event.type==QUIT:
pygame.quit()

elif event.type==VIDEORESIZE:
sprite.screen=pygame.display.set_mode(event.dict['size'],HWSURFACE|DOUBLEBUF|RESIZABLE)
sprite.screen.blit(pygame.transform.scale(bg,event.dict['size']),(0,0))
sprite.screen.blit(pygame.transform.scale(fg,event.dict['size']),(0,0))
pygame.display.update()


#PLAYER CLASS

class player(object):
def __init__(self,x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.velocity = 15
self.isJump = False
self.jumpCount = 7
self.right = False
self.left = False
self.walkCount = 0
self.screen = pygame.display.set_mode((1280, 800), HWSURFACE|DOUBLEBUF|RESIZABLE)
self.walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Examples/sprite-Game/sprite characters main/R %s.png' % frame) for frame in range(1, 9)]
self.walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Examples/sprite-Game/sprite characters main/L %s.png' % frame) for frame in range(14, 21)]
self.char = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/Examples/sprite-Game/sprite characters main/character 25.png')


def draw(self, screen):

if self.walkCount + 1 >= 8:
self.walkCount = 0

if self.left:
self.screen.blit(self.walkLeft[self.walkCount//1], (self.x, self.y))
self.walkCount += 1

elif self.right:
self.screen.blit(self.walkRight[self.walkCount//1], (self.x, self.y))
self.walkCount += 1

else:
self.screen.blit(self.char, (self.x, self.y))



sprite = player(4, 480, 16, 16)

def redrawGamescreen():

sprite.screen.blit(pygame.transform.scale(bg,(1280, 800)),(0,0))
sprite.screen.blit(pygame.transform.scale(fg,(1280, 800)),(0,0))
sprite.draw(sprite.screen)
pygame.display.update()



#mainloop:-----------------------------------------------------------------
run = True

while run:
keys = pygame.key.get_pressed()
Sresize()
clock.tick(20)
#left

if keys[pygame.K_LEFT] and sprite.x > sprite.velocity:
sprite.x -= sprite.velocity
sprite.left = True
sprite.right = False

#right

elif keys[pygame.K_RIGHT] and sprite.x < 1280 - sprite.width - sprite.velocity:
sprite.x += sprite.velocity
sprite.right = True
sprite.left = False

#otherwise, stand still

else:
sprite.right = False
sprite.left = False
sprite.walkCount = 0

#jumping


if not(sprite.isJump):

if keys[pygame.K_SPACE]:
sprite.isJump = True
sprite.right = False
sprite.left = False

else:
if sprite.jumpCount >= -7:
neg = 1
pygame.display.update()
if sprite.jumpCount < 0:
neg = -1

sprite.y -= (sprite.jumpCount ** 2) * 0.5 * neg
sprite.jumpCount -= 1

else:
sprite.isJump = False
sprite.jumpCount = 7
sprite.velocity = 15

redrawGamescreen()

pygame.quit()








最佳答案

问题是对 event=pygame.event.wait() 的调用。备注pygame.event.wait()等待队列中的单个事件。如果队列为空,此函数将等待,直到创建队列。这会让你的游戏滞后。 pygame.event.pump() 使问题更加复杂之前被调用。

pygame.event.pump()                                                                 
event=pygame.event.wait()

改用 pygame.event.get():

events=pygame.event.get()

pygame.event.get() gets 从事件队列中删除所有消息。

例如:

def HandleEvents():

events = pygame.event.get()
for event in events:
if event.type==QUIT:
pygame.quit()

elif event.type==VIDEORESIZE:

sprite.screen=pygame.display.set_mode(event.dict['size'],HWSURFACE|DOUBLEBUF|RESIZABLE)
sprite.screen.blit(pygame.transform.scale(bg,event.dict['size']),(0,0))
sprite.screen.blit(pygame.transform.scale(fg,event.dict['size']),(0,0))
pygame.display.update()

请注意,为了获得良好的控制流,您必须在应用程序循环中实现事件循环。事件循环必须处理所有用户输入。

关于python - Pygame Sprite 太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597330/

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