Home > other >  Access sprite/class position outside the class
Access sprite/class position outside the class

Time:07-08

I want to access my player position, I tried following Moving a Sprite Class from outside of the original Class in pygame but then I got "AttributeError: 'GroupSingle' object has no attribute 'rect'" as my error. I need to access player position inside my obstacle class.

This 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

OBSTACLE_INTERVAL = 1000
AWAY_FROM_BIRD = 100

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(0.1)

    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
        print(int(self.gravity_store), int(clock.get_fps()))
    
    def collision(self):
        if self.rect.colliderect(ground_rect):
            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):
        #I want to access player position here
        print(player.rect.x)
        self.rect = self.image.get_rect(midleft = (0, 0))

    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")
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()

    SCREEN.blit(background_surf, (0,0))

    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)

I tried to see if it would access the player position using "print(player.rect.x)". And got this error:

Traceback (most recent call last):
  File "c:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird\main.py", line 98, 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 65, in update
    self.obstacle_spawn()
  File "c:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird\main.py", line 61, in obstacle_spawn
    print(player.rect.x)
AttributeError: 'GroupSingle' object has no attribute 'rect'
PS C:\Users\46722\Documents\Programmering\Python\yesman\Pygame\Mama Bird>

CodePudding user response:

player is an instance of pygame.sprite.Group():

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

Use the sprite property to access the pygame.sprite.Sprite in a pygame.sprite.GroupSingle:

print(player.rect.x)

print(player.sprite.rect.x)
  • Related