Home > Net >  Can't spawn an copy of the rect but just rotated on top
Can't spawn an copy of the rect but just rotated on top

Time:07-09

I need to have a pipe spawning system in pygame for my "Flappy Bird". I have coded it so that I can spawn one pipe, but I need to have a pipe on top rotaded 180 with a little space beetween so that my bird can fly between. Right now I only get an error. (I will add collisions and a list of the pipes so they remain on the screen).

Here is my code:

import pygame
import os
import random
from sys import exit
pygame.init()
os.system("cls")

WIDTH = 288
HEIGHT = 512
FPS = 60

JUMP_POWER = 60
GRAVITY = 0.15
GAME_ACTIVE = 1
VOLUME = 0.15

OBSTACLE_INTERVAL = 500
OBSTACLE_SPACE = 100
AWAY_FROM_BIRD = 150

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("assets/images/player/bird.png").convert_alpha()
        self.rect = self.image.get_rect(center = (WIDTH/4, HEIGHT/2))
        self.gravity_store = 0

        self.jump_sfx = pygame.mixer.Sound("assets/audio/jump.wav")
        self.jump_sfx.set_volume(VOLUME)

        self.hit_sfx = pygame.mixer.Sound("assets/audio/hit.wav")
        self.hit_sfx.set_volume(VOLUME)

    def player_input(self):
        for event in event_list:
            if event.type == pygame.KEYDOWN:
                if self.rect.bottom <= 0:
                    pass
                else:
                    if event.key == pygame.K_SPACE:
                        self.gravity_store = 0
                        self.rect.y -= JUMP_POWER 
                        self.jump_sfx.play()

    def gravity(self):
        self.gravity_store  = GRAVITY
        self.rect.y  = self.gravity_store
    
    def collision(self):
        global GAME_ACTIVE
        if self.rect.colliderect(ground_rect):
            self.hit_sfx.play()
            GAME_ACTIVE = 0

    def update(self):
        self.player_input()
        self.gravity()
        self.collision()

class Obstacles(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")

    def obstacle_spawn(self):
        player_x = player.sprite.rect.x
        obstacle_height1 = random.randint((HEIGHT/2)-150, HEIGHT-150)
        self.rect = self.image.get_rect(midtop = (player_x AWAY_FROM_BIRD, obstacle_height1))

        obstacle.draw(SCREEN)

        self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")
        obstacle_height2 = self.rect.midtop
        heightlist = list(obstacle_height2)
        y = heightlist[1]
        y = y - OBSTACLE_SPACE
        del heightlist[1]
        heightlist.append(y)
        obstacle_height2 = tuple(heightlist)
        self.image = pygame.transform.rotate(self.image, 180)
        self.rect = self.image.get_rect(bottom = (player_x AWAY_FROM_BIRD, obstacle_height2))

        obstacle.draw(SCREEN)

    def update(self):
        self.obstacle_spawn()

SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mama Bird")
clock = pygame.time.Clock()

background_surf = pygame.image.load("assets/images/background/background-day.png")
background_rect = background_surf.get_rect()
ground_surf = pygame.image.load("assets/images/background/base.png")
ground_rect = ground_surf.get_rect(topleft = (0, HEIGHT-112))

player = pygame.sprite.GroupSingle()
player.add(Player())

obstacle = pygame.sprite.Group()
obstacle.add(Obstacles())

OBSTACLESPAWN = pygame.USEREVENT   1
pygame.time.set_timer(OBSTACLESPAWN, OBSTACLE_INTERVAL)

while True:
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    if GAME_ACTIVE == 1:
        SCREEN.blit(background_surf, background_rect)

        player.draw(SCREEN)
        player.update()

        for event in event_list:
            if event.type == OBSTACLESPAWN and GAME_ACTIVE == 1:
                obstacle.update()
                #obstacle.draw(SCREEN)

        SCREEN.blit(ground_surf, ground_rect)


    pygame.display.update()
    clock.tick(FPS)

Here is the error:

Traceback (most recent call last):
  File "C:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird\main.py", line 121, in <module>
    obstacle.update()
  File "C:\Users\46722\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame\sprite.py", line 539, in update
    sprite.update(*args, **kwargs)
  File "C:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird\main.py", line 86, in update
    self.obstacle_spawn()
  File "C:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird\main.py", line 81, in obstacle_spawn     
    self.rect = self.image.get_rect(bottom = (player_x AWAY_FROM_BIRD, obstacle_height2))
TypeError: invalid rect assignment
PS C:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird> 

CodePudding user response:

The bottom argument only affects the y-coordinate of the rectangle. Therefore you can only assign a single value, but not a tuple. If you want to set the x and y coordinate, you must use bottomleft, midbottom, or bottomright (see pygame.Rect). Also obstacle_height2 needs to be s single value not a list. e.g. self.rect.top

obstacle_height2 = self.rect.top
self.image = pygame.transform.rotate(self.image, 180)
self.rect = self.image.get_rect(midbottom = (player_x AWAY_FROM_BIRD, obstacle_height2))

Additionally you need to flip the pipe image along the y axis with pygame.transform.flip:

self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")
self.image = pygame.transform.flip(self.image, False, True)
  • Related