添加第二个精灵类后,我的游戏变得非常冻结

问题描述 投票:0回答:1

我正在创建一个飞翔的小鸟游戏,在制作了管道精灵类之后,我的游戏非常慢且无法玩,我删除了该类,这很正常,它让我发疯,有人可以帮忙吗

import pygame
import time
from pygame.locals import *



pygame.init()
WIDTH =600
HEIGHT =650
screen = pygame.display.set_mode((WIDTH,HEIGHT))

run = True
clock = pygame.time.Clock()
background_img = pygame.image.load('img/bg.png')
ground_img = pygame.image.load('img/ground.png')
ground_scroll = -10
flying = False
game_over = False
pipe_gap = 140

class Bird(pygame.sprite.Sprite) :
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.images = []
        self.index = 0
        self.speed = 0
        for num in range(1,3):
            imag = pygame.image.load(f'img/bird{num}.png')
            self.images.append(imag)
        self.image = self.images[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = [x,y]
        self.vel = 0
        self.clicked = False



    def update(self) :
        if flying== True  :
            self.vel +=0.5
            if self.vel > 8:
                self.vel = 8
            if self.rect.bottom < 530:
                self.rect.y += int(self.vel)
        if game_over == False:
            if pygame.mouse.get_pressed()[0] ==1 and self.clicked== False :
                self.clicked = True
                self.vel =-10
                self.rect.y += int(self.vel)



            if pygame.mouse.get_pressed()[0] == 0  :
                self.clicked = False

            self.speed+=1
            self.ceiling = 7
            if self.speed > self.ceiling :
                self.speed = 0
                self.index+=1
                if self.index >= len(self.images):
                    self.index =0

            self.image = self.images[self.index]
            self.image=pygame.transform.rotate(self.images[self.index],self.vel *-2)
        else:
            self.image=pygame.transform.rotate(self.images[self.index],-180)

class Pipe(pygame.sprite.Sprite):
    def __init__(self,x,y,pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('img/pipe.png')
        self.rect = self.image.get_rect()

        if pos == -1 :
            pygame.transform.flip(self.image,False,True)
            self.rect.bottomleft = [x,y]
        else:
            self.rect.topleft = [x,y]


bird_group = pygame.sprite.Group()
pipe_group = pygame.sprite.Group()

flappy = Bird(100,320)

bird_group.add(flappy)

while run :

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

    if game_over == False  :
        ground_scroll -=4
        if abs(ground_scroll) >= 35 :
            ground_scroll = 0
        btm_pipe = Pipe(300,250,1)
        top_pipe = Pipe(300,250,-1)
        bird_group.add(btm_pipe)
        bird_group.add(top_pipe)


    if event.type == pygame.MOUSEBUTTONDOWN and flying == False and game_over==False:
        flying = True
    if flappy.rect.bottom >530 :
        game_over = True
        flying= False



    clock.tick(60)
    screen.blit(background_img,(0,-230))
    screen.blit(ground_img,(ground_scroll,530))
    bird_group.draw(screen)
    bird_group.update()
    pygame.display.update()

我正在创建一个飞翔的小鸟游戏,在制作了管道精灵类之后,我的游戏非常慢且无法玩,我删除了该类,这很正常,它让我发疯,有人可以帮忙吗

python python-3.x pygame flappy-bird-clone
1个回答
0
投票

您以每秒 60 帧的速度运行游戏。这意味着您每秒创建 120 个 Pipe 实例。这也意味着您每秒加载管道图像 120 次。

您应该只加载图像一次,只在管道实例实际生成时创建它们(例如每秒一次或类似的东西),并在它们离开屏幕后杀死这些实例(或者只是将它们移动到右侧而不是创建新的)实例)。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.