0
import pygame
import os
import random
import time
import sys

pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game Window")
white = (255, 255, 255)
black = (0, 0, 0)
FPS = 60
vel = 5
fireball_speed = 10
background = pygame.image.load(os.path.join('Assets', 'background.jpg'))
bird = pygame.image.load(os.path.join('Assets', 'bird.png'))
fireball_size = (45, 30)
fireball = pygame.image.load(os.path.join('Assets', 'fireball.png'))
fireball = pygame.transform.scale(fireball, fireball_size)
fireball = pygame.transform.rotate(fireball, -90)
bird_image = pygame.transform.scale(bird, (70, 45))
directionx = random.randrange(0, 255)
directiony = random.randrange(0, 255)
choice = random.randrange(1, 10)
border = pygame.Rect(WIDTH, HEIGHT, 1, 1)
fireballentity = pygame.Rect(directionx, directiony, 35, 30)
fireball_y = 0
fireball_x = 0
lives = 5
pygame.mixer.init()
pygame.mixer.music.load("gamemusic.mp3")
font = pygame.font.SysFont('Comic Sans MS', 30)

def draw_window(bird):
    WIN.blit(background, (0,0))
    pygame.draw.rect(WIN, black, border)
    WIN.blit(bird_image, (bird.x, bird.y))
    WIN.blit(fireball, (fireballentity.x, fireballentity.y))
    pygame.display.update()


def main():
    pygame.mixer.music.play(10)
    while pygame.mixer.music.get_busy():

        bird = pygame.Rect(450, 250, 70, 45)

        clock = pygame.time.Clock()
        run = True
        while run:
            draw_window(bird)
            text = font.render("Lives: 5", False, (255, 255, 255))
            WIN.blit(text, (25, 100))
            pygame.display.update()
            clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    try:
                        pygame.mixer.Channel.pause()
                        run = False
                    except Exception:
                        return
            if bird.colliderect(fireballentity):
                lives -= 1
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_a] or keys_pressed[pygame.K_LEFT] and bird.x - vel > 0: #left
                bird.x -= vel
            if keys_pressed[pygame.K_d] or keys_pressed[pygame.K_RIGHT] and bird.x + vel + bird.width < border.x: #right
                bird.x += vel
            if keys_pressed[pygame.K_w] or keys_pressed[pygame.K_UP] and bird.y - vel > 0: #up
                bird.y -= vel
            if keys_pressed[pygame.K_s] or keys_pressed[pygame.K_DOWN] and bird.y + vel + bird.height < HEIGHT: #down
                bird.y += vel
            
          
def event_handler(bird, fireballsprite):
    if bird.colliderect(fireballsprite):
        lives -=1
        text = font.render(f"Lives: {lives}", False, (255, 255, 255))
    if lives == 0:
        WIN.fill(white)
        gameover = font.render("Game Over", False, (0, 0, 0))
        WIN.blit(gameover, (450, 250))




if __name__ == "__main__":
    main()

I'm trying to set a variable lives as 5 and when the bird sprite collides with the fireball, the player loses lives, However I'm getting an error "lives" referenced before assignment

I was trying to make lives a global variable or even use pygame events but I still got the error I also tried to add the lives variable in the main game function loop but the text will flicker or I will get this error

Traceback (most recent call last):
  File "/Users/username/Desktop/Code/main.py", line 98, in <module>
    main()
  File "/Users/username/Desktop/Code/main.py", line 52, in main
    if bird.colliderect(fireballrect):
TypeError: Argument must be rect style object
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

0 Answers0