gpt4 book ai didi

python - Pygame 由于 Sprite 而滞后?

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

我用 Sprite 制作了这样的游戏来制作平台游戏。

以前还好,但现在有点滞后,所以当施加重力时,它会移动一格,看起来不自然。

一开始还不错,因为平台是矩形的。

我更改了平台,还插入了敌人列表作为 Sprite 。

它确实工作得很好,但它非常滞后而且非常慢。我不喜欢这个角色的“口吃”。我能做些什么?

import pygame, sys
from pygame.locals import *
pygame.init()

class Poppy(pygame.sprite.Sprite):
def __init__(self): #making the player
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('POPPY.png')
self.rect = self.image.get_rect()
self.grav = .5
self.y_vel = 0
self.jumping = False

def jump_update(self): #checking the jumps
if self.jumping:
self.y_vel += self.grav
self.rect.y += self.y_vel

def jump(self): #the jump initializer
if not self.jumping:
self.y_vel = -50
self.jumping = True

def keys(self): #the keys
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_RIGHT]: # right key
self.rect.x += dist # move right
elif key[pygame.K_LEFT]: # left key
self.rect.x -= dist

def collide_check(self, platform_list): #check if it hit the ground or the platforms
for blocks in platform_list:
if pygame.sprite.collide_rect(self, blocks) == True:
self.rect.y = blocks.top

if self.rect.y >= 600-self.rect.height: # BALL HITS TOP AND BOTTOM
self.grav = 0
self.y_vel = 0
self.rect.y = 600 - self.rect.height
self.jumping = False

def move(self):
self.rect.x += 0
self.rect.y += 10

def draw(self, surface):
surface.blit(self.image, (self.rect.x, self.rect.y))

class Platform(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('levoneplatform.png')
self.rect = self.image.get_rect()

class Enemy(pygame.sprite.Sprite):

def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('enemy.png')
self.rect = self.image.get_rect()

class Menu():
def setup(self):
background_image = pygame.image.load('menu.png').convert_alpha()
screen.blit(background_image, [0,0])


class LevOne():
def setup(self):

background_image = pygame.image.load('night.png').convert_alpha()
screen.blit(background_image, [0, 0])


platforms_one = [ (200,300),
(50,500),
(550,650),
(300,200),
(120,100)
]
for k,v in platforms_one:
platform = Platform()
enemy = Enemy()
platform.rect.x = k
enemy.rect.x = k
platform.rect.y = v
enemy.rect.y = v - 44
platform_list.add(platform)
enemy_list.add(enemy)

screen = pygame.display.set_mode((800,600))
enemy_list = pygame.sprite.Group()
platform_list = pygame.sprite.Group()
Poppy = Poppy()
Menu = Menu()
LevOne = LevOne()


clock = pygame.time.Clock()
level = 0

while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_SPACE:
Poppy.jump()
Poppy.jump_update()

Menu.setup()
if event.type == MOUSEBUTTONDOWN:
x,y = pygame.mouse.get_pos()
if x >= 544.86 and x <= 700.86 and y <= 385.02 and y >= 340.03:
level = 1

if level == 1:

first_lives = 5
LevOne.setup()

Poppy.keys()
Poppy.move()


Poppy.draw(screen)
platform_list.draw(screen)
enemy_list.draw(screen)


Poppy.draw(screen)

pygame.display.update()

最佳答案

我注意到您在每次迭代中加载 LevOne 背景,并初始化各种 Sprite 。这是一次性操作,因此没有必要在每次迭代中都执行此操作,并且可能会导致您遇到的口吃。您应该在 __init__ 中加载背景您的 LevOne 类以及关卡中将使用的任何 Sprite ,然后在设置方法中进行位图传输时访问背景变量。

而不是这个;

class LevOne():
def setup(self):

background_image = pygame.image.load('night.png').convert_alpha()
screen.blit(background_image, [0, 0])


platforms_one = [ (200,300),
(50,500),
(550,650),
(300,200),
(120,100)
]
for k,v in platforms_one:
platform = Platform()
enemy = Enemy()
platform.rect.x = k
enemy.rect.x = k
platform.rect.y = v
enemy.rect.y = v - 44
platform_list.add(platform)
enemy_list.add(enemy)

试试这个:

class LevOne():
def __init__(self):
self.background_image = pygame.image.load('night.png').convert_alpha()
platforms_one = [ (200,300),
(50,500),
(550,650),
(300,200),
(120,100)
]
for k,v in platforms_one:
platform = Platform()
enemy = Enemy()
platform.rect.x = k
enemy.rect.x = k
platform.rect.y = v
enemy.rect.y = v - 44
platform_list.add(platform)
enemy_list.add(enemy)
def setup(self):
screen.blit(self.background_image, [0, 0])

我注意到 Menu 类也存在同样的问题。应进行相同的更改,如下所示。

class Menu():
def __init__(self):
self.background_image = pygame.image.load('menu.png').convert_alpha()
def setup(self):
screen.blit(self.background_image, [0,0])

我只是想重申我认为运行缓慢的原因是它必须在每次迭代时从系统打开一个文件,这是一个缓慢的操作,并且您还不断为第一级创建 Sprite 列表,只需要做一次。

关于python - Pygame 由于 Sprite 而滞后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504394/

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