I'm having trouble with spawning in the pipes from flappy bird, I'm trying to use a class for the information (surfaces, drawing rects, movement). However, I can't seem to figure out how to draw rectangles with it.
In my flappy bird clone, I'm using 2 pipes (one coming down from the top, and the other to the bottom). But what I've gathered so far is for drawing rects, you must use self.rect, which seems like it can only hold one value at a time, so I can't exactly access information for both the top and bottom pipes.
import pygame
from sys import exit
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__() # import files
bird1 = pygame.image.load('graphics/bird/Bird1.png').convert_alpha()
bird2 = pygame.image.load('graphics/bird/Bird2.png').convert_alpha()
self.bird_fly = [bird1, bird2] # sets up animation index
self.bird_index = 0
self.gravity = 0
self.image = self.bird_fly[self.bird_index]
self.rect = self.image.get_rect(midbottom = (100,300))
def animation(self):
self.bird_index += 0.1
if self.bird_index >= len(self.bird_fly): self.bird_index = 0
self.image = self.bird_fly[int(self.bird_index)]
def apply_gravity(self):
self.rect.y += 5
def input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.rect.y -= 15
def update(self): # gives access
self.animation()
self.apply_gravity()
self.input()
class Pipes(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
top_pipe = pygame.image.load('graphics/pipes/top_green_pipe.png')
bottom_pipe = pygame.image.load('graphics/pipes/bottom_green_pipe.png')
pipes_list = [top_pipe, bottom_pipe]
self.image = pipes_list[]
self.rect = self.image.get_rect(midbottom = (800, y))
pygame.init()
screen = pygame.display.set_mode((800, 400))
clock = pygame.time.Clock()
pygame.display.set_caption('Flabby Pird')
#groups
player = pygame.sprite.GroupSingle()
player.add(Player())
pipes = pygame.sprite.Group()
pipes.add (Pipes())
#environment
sky = pygame.image.load('graphics/sky.png')
ground = pygame.image.load('graphics/ground.png')
fork = pygame.image.load('graphics/fork.png')
fork_rect = fork.get_rect(midbottom = (80, 200))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(ground, (0, 300))
screen.blit(sky, (0,0))
screen.blit(fork, fork_rect)
# draws player from "# groups"
player.draw(screen)
player.update()
screen.blit()
pygame.display.update()
clock.tick(60) # limits FPS to 60
pygame.quit()
here is my current code, im experimenting with drawing an entire list, but im not sure how to change the Y-value of each item in list