Home > Back-end >  Sprite not showing, and collision with the sprite not working
Sprite not showing, and collision with the sprite not working

Time:07-10

I am making "Flappy Bird" and want to have a coin between the pipes that you need to fly into to get score. The problem is that my coin is not showing up on the screen and the collision with the coin that gives out score is not working. Since I dont get an error I have nothing that can help us solve the problem, maybe the problem is that I am drawing over something else?

Here is my current code:

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

WIDTH = 288 #288
HEIGHT = 512 #512
FPS = 60 #60

JUMP_POWER = 50 #55
GRAVITY = 0.2 #0.2
GAME_STAGE = 1 #1
VOLUME = 0.1 #0.1
GAME_SPEED = 4 #5
COLLISION = True
NIGHT = 18
BIRD_SPAWN = HEIGHT/2-150
OBSTACLE_DESPAWN = -300

OBSTACLE_INTERVAL = 800 #800
OBSTACLE_SPACE = 100 #100
AWAY_FROM_BIRD = WIDTH

obstacle_stage = 0
coins = 0

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        global hit_sfx

        self.image = pygame.image.load("assets/images/player/bird.png").convert_alpha()
        self.rect = self.image.get_rect(center = (WIDTH/4, BIRD_SPAWN))

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

        self.gravity_store = 0

    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_STAGE
        if COLLISION == True:
            if self.rect.colliderect(ground_rect):
                hit_sfx.play()
                GAME_STAGE = 2
        else:
            pass
        
    def update(self):
        self.player_input()
        self.gravity()
        self.collision()

class Obstacles(pygame.sprite.Sprite):
    def __init__(self, obstacle_height):
        super().__init__()
        global obstacle_stage
        global last_height
        global OBSTACLE_DESPAWN

        if obstacle_stage == 0:
            player_x = player.sprite.rect.x
            self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")
            self.rect = self.image.get_rect(midtop = (player_x AWAY_FROM_BIRD, obstacle_height))
            last_height = self.rect.midtop
            obstacle_stage = 1

        elif obstacle_stage == 1:
            player_x = player.sprite.rect.x
            heightlist = list(last_height)
            y = heightlist[1]
            y = y - OBSTACLE_SPACE
            obstacle_height = y
            self.image = pygame.image.load("assets/images/obstacles/pipe-green.png")            
            self.image = pygame.transform.flip(self.image, False, True)
            self.rect = self.image.get_rect(midbottom = (player_x AWAY_FROM_BIRD, obstacle_height))
            obstacle_stage = 0

    def destroy(self):
        if self.rect.x <= OBSTACLE_DESPAWN:
            self.kill()

    def update(self):
        self.rect.x -= GAME_SPEED
        self.destroy()

class Coins(pygame.sprite.Sprite):
    def __init__(self, obstacle_height):
        super().__init__()
        global OBSTACLE_DESPAWN
        if obstacle_stage == 0:
            player_x = player.sprite.rect.x
            self.image = pygame.image.load("assets/images/obstacles/coin.png")
            self.rect = self.image.get_rect(midbottom = (player_x AWAY_FROM_BIRD, obstacle_height))

    def destroy(self):
        if self.rect.x >= OBSTACLE_DESPAWN:
            self.kill()

    def update(self):
        self.rect.x -= GAME_SPEED
        self.destroy()

def sprite_collision():
    global GAME_STAGE
    global COLLISION
    global coins
    if COLLISION == True:
        if pygame.sprite.spritecollide(player.sprite, obstacle_group, False):
            hit_sfx.play()
            obstacle_group.empty()
            GAME_STAGE = 2
            return GAME_STAGE
        else:
            return GAME_STAGE
    else:
        if pygame.sprite.spritecollide(player.sprite, coins_group, False):
            score_sfx.play()
            coins  = 1
        else:
            return GAME_STAGE

def timechecker():
    global background_surf
    global background_rect
    t = time.localtime()
    current_time = int(time.strftime("%H", t))
    if current_time >= NIGHT:
        background_surf = pygame.image.load("assets/images/background/background-night.png")
        background_rect = background_surf.get_rect()
    else:
        background_surf = pygame.image.load("assets/images/background/background-day.png")
        background_rect = background_surf.get_rect()

SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Life as a milf")
clock = pygame.time.Clock()

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

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

score_sfx = pygame.mixer.Sound("assets/audio/score.wav")
score_sfx.set_volume(VOLUME)

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

coins_group = pygame.sprite.Group()
obstacle_group = pygame.sprite.Group()

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_STAGE == 1:
        timechecker()
        SCREEN.blit(background_surf, background_rect)
        for event in event_list:
            if event.type == OBSTACLESPAWN and GAME_STAGE == 1:

                obstacle_height = random.randint((HEIGHT/2)-100, HEIGHT-200)
                obstacle_group.add(Obstacles(obstacle_height))
                obstacle_group.add(Obstacles(obstacle_height))
                coins_group.add(Coins(obstacle_height))
        
        obstacle_group.update()
        obstacle_group.draw(SCREEN)
        coins_group.update()
        coins_group.draw(SCREEN)
        player.update()
        player.draw(SCREEN)
        print(coins)

        SCREEN.blit(ground_surf, ground_rect)
            
        GAME_STAGE = sprite_collision()

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

CodePudding user response:

The condition if obstacle_level == 0: is in the wrong place. Do not test obstacle_stage in the constructor. Test the condition before spawning the coin.
Also, the condition if self.rect.x >= OBSTACLE_DESPAWN: is always True because OBSTACLE_DESPAWN is -300. The condition must be if self.rect.x < OBSTACLE_DESPAWN::

class Coins(pygame.sprite.Sprite):
    def __init__(self, obstacle_height):
        super().__init__()
        player_x = player.sprite.rect.x
        self.image = pygame.image.load("assets/images/obstacles/coin.png")
        self.rect = self.image.get_rect(midbottom = (player_x AWAY_FROM_BIRD, obstacle_height))

    def destroy(self):
        if self.rect.x < OBSTACLE_DESPAWN:
            self.kill()

    def update(self):
        self.rect.x -= GAME_SPEED
        self.destroy()
while True:
    # [...]

    if GAME_STAGE == 1:
        # [...]
 
        for event in event_list:
            if event.type == OBSTACLESPAWN and GAME_STAGE == 1:
                obstacle_height = random.randint((HEIGHT/2)-100, HEIGHT-200)
                obstacle_group.add(Obstacles(obstacle_height))
                obstacle_group.add(Obstacles(obstacle_height))

                if obstacle_stage == 0: 
                    coins_group.add(Coins(obstacle_height))
  • Related