im creating a flappy bird game and after making the pipe sprite class my game is just extremely slow and unplayable i delete the class and its normal its driving me crazy can someone help please
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()
im creating a flappy bird game and after making the pipe sprite class my game is just extremely slow and unplayable i delete the class and its normal its driving me crazy can someone help please
You run your game at 60 frames per second. That means you create 120 Pipe instances per second. That also means you load the pipe image 120 times per second.
You should only load the image once, only create Pipe instances when they should actually spawn (like once per second or something like that), and kill those instances once they leave the screen (or simply move them to the right instead of creating new instances).