我用精灵制作了这样的游戏来制作平台游戏。
以前很好,但现在它已经滞后了,所以当重力被施加时,它会移动一个并且看起来不自然。
首先,当平台是矩形时,它很好。
我改变了平台并且还插入了一个敌人列表作为精灵。
它确实工作正常,但它非常迟缓,而且非常慢。 我不喜欢这个角色是如何“口吃”的。 我能做什么?
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()
答案 0 :(得分:1)
我注意到你在每次迭代时加载你的LevOne背景,以及初始化各种精灵。这是一次性操作,因此在每次迭代时都这样做是不必要的,并且可能导致您经历的口吃。您应该在LevOne类的__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 Menu():
def __init__(self):
self.background_image = pygame.image.load('menu.png').convert_alpha()
def setup(self):
screen.blit(self.background_image, [0,0])
我只想重申我认为运行缓慢的原因是它必须在每次迭代时从系统中打开一个文件,这是一个很慢的操作,你还要继续为第一级创建精灵列表,只需要做一次。