Pygame 因为精灵而滞后?
Pygame lagging because of sprites?
我用精灵制作了这样的游戏来制作平台游戏。
之前还好,现在就卡了,所以重力一动一动,看起来不自然。
刚开始还不错,当时平台是长方形的。
我更改了平台并插入了一个敌人列表作为精灵。
它确实工作正常,但它非常滞后而且非常慢。
我不喜欢这个角色 "stuttering"。
我能做什么?
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 背景,并初始化了各种精灵。这是一次性操作,因此没有必要在每次迭代时都这样做,并且可能会导致您遇到卡顿。您应该将背景加载到 LevOne class 的 __init__
中,以及将在关卡中使用的任何精灵,然后在设置方法中 blitting 时访问背景变量。
而不是这个;
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])
我注意到菜单 class 也存在同样的问题。应进行如下相同的更改。
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])
我只是想重申我认为这是 运行 缓慢的原因是它必须在每次迭代时从系统中打开一个文件,这是一个缓慢的操作,而且你还不断地创建你的一级精灵列表,只需要做一次。
我用精灵制作了这样的游戏来制作平台游戏。
之前还好,现在就卡了,所以重力一动一动,看起来不自然。
刚开始还不错,当时平台是长方形的。
我更改了平台并插入了一个敌人列表作为精灵。
它确实工作正常,但它非常滞后而且非常慢。 我不喜欢这个角色 "stuttering"。 我能做什么?
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 背景,并初始化了各种精灵。这是一次性操作,因此没有必要在每次迭代时都这样做,并且可能会导致您遇到卡顿。您应该将背景加载到 LevOne class 的 __init__
中,以及将在关卡中使用的任何精灵,然后在设置方法中 blitting 时访问背景变量。
而不是这个;
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])
我注意到菜单 class 也存在同样的问题。应进行如下相同的更改。
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])
我只是想重申我认为这是 运行 缓慢的原因是它必须在每次迭代时从系统中打开一个文件,这是一个缓慢的操作,而且你还不断地创建你的一级精灵列表,只需要做一次。