[Python] Pygame Sprite Referencing

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project with Pygame to try to understand it more for an upcoming assignment. I decided to do the classic Breakout game. I am running into an issue with trying to figure out how to reference a sprite class appropriately to blit the sprite onto a specified surface.

    I thought I could just make a standard getter type class that would just return whatever image object was created but it won't let me. I am not understanding how to reference and call this correctly

    In the main game handler I was trying to do something like this(sc is the window surface for the game):

    sc.blit(getbrick(), wall)


    wall is a surface that I was designating as a container for the brick sprites. Basically, the game will randomly choose a brick from the specified asset list bricks[] and then iterate through some nested for loops to fill the wall. The bricks sprite class is super basic at the moment. I am just trying to figure how I can get it to populate a brick correctly before I move into anything else. Below is the bricks sprite class that I am working on:

    import pygame
    import random

    class Bricks(pygame.sprite.Sprite):

    yellow_brick = pygame.image.load('assets/art/yellow_brick.png')
    blue_brick = pygame.image.load('assets/art/blue_brick.png')
    green_brick = pygame.image.load('assets/art/green_brick.png')
    red_brick = pygame.image.load('assets/art/red_brick.png')

    bricks = [yellow_brick, blue_brick, green_brick, red_brick]

    def __init__(self, x, y,):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load(random.choice(self.bricks))
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y

    def getbrick(self):
    return self.image, self.rect


    I am really new to pygame, so I am still trying to figure out a lot. I have been reading docs and testing thus far. I just cannot figure out how to get this function to work as intended. I had tried doing the same getbrick() function like this as well:

    def getbrick():
    return self.image, self.rect


    If this is any help, the main body is below. Ignore any of the commented stuff, that was me experimenting with blit and finding spacing to write the for loops.

    import pygame
    import bricks

    pygame.display.set_caption("Smash Core")
    WIDTH, HEIGHT = 1200, 800
    fps = 60

    # paddle settings
    paddle_w = 330
    paddle_h = 35
    paddle_speed = 15
    paddle = pygame.Rect(WIDTH // 2 - paddle_w // 2, HEIGHT - paddle_h - 10, paddle_w, paddle_h)

    # ball settings
    ball_radius = 15
    ball_speed = 6
    dx, dy = 1, -1

    pygame.init()

    sc = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()

    paddle_face = pygame.image.load('assets/art/paddle.png')
    paddle_obj = paddle_face.get_rect()
    paddle_obj.center = (WIDTH // 2 - paddle_w // 2, HEIGHT - paddle_h - 10)

    ball_face = pygame.image.load('assets/art/ball.png')
    ball_obj = ball_face.get_rect()
    ball_obj.center = (WIDTH // 2 - paddle_w // 2, HEIGHT - paddle_h - 10)

    #ybrick_face = pygame.image.load('assets/art/yellow_brick.png')
    #ybrick_obj = ybrick_face.get_rect()
    #ybrick_obj.center = (61, HEIGHT // 2)
    #ybrick_obj.top = HEIGHT // 2 - ybrick_obj.height // 2

    #ybrick_face1 = pygame.image.load('assets/art/red_brick.png')
    #ybrick_obj1 = ybrick_face1.get_rect()
    #ybrick_obj1.center = (61 + 120, HEIGHT // 2)

    #ybrick_face2 = pygame.image.load('assets/art/blue_brick.png')
    #ybrick_obj2 = ybrick_face2.get_rect()
    #ybrick_obj2.center = (61, HEIGHT // 2 - 20)

    wall = pygame.Rect(0, 130, 1200, 280)

    # Hide the mouse cursor
    pygame.mouse.set_visible(False)

    while True:
    # fill the screen with black.
    sc.fill((0, 0, 0))

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    exit()

    pygame.draw.rect(sc, (255, 255, 255), wall)

    sc.blit(trying to retrieve random brick using getbrick(), blit to wall surface)

    # drawing
    sc.blit(paddle_face, paddle_obj)
    sc.blit(ball_face, ball_obj)
    #sc.blit(ybrick_face, ybrick_obj)
    #sc.blit(ybrick_face1, ybrick_obj1)
    #sc.blit(ybrick_face2, ybrick_obj2)

    # ball movement
    ball_obj.x += ball_speed * dx
    ball_obj.y += ball_speed * dy

    # ball collision left/right
    if ball_obj.centerx < ball_radius or ball_obj.centerx > WIDTH - ball_radius:
    dx = -dx

    # ball collision top
    if ball_obj.centery < ball_radius:
    dy = -dy

    # ball exits the bottom of the screen, game over
    if ball_obj.centery > HEIGHT - ball_radius:
    dy = -dy
    #pygame.quit()

    # ball collision paddle
    if ball_obj.colliderect(paddle_obj) and paddle_obj.center + (10, 61) > ball_obj.center:
    dx, dy = -1, -dy

    if ball_obj.colliderect(paddle_obj) and paddle_obj.center + (10, -61) < ball_obj.center:
    dx, dy = 1, -dy

    #if ball_obj.colliderect(ybrick_obj):
    #dy = -dy

    #if ball_obj.colliderect(ybrick_obj1):
    #dy = -dy


    # paddle control (mouse)
    mouse_pos = pygame.mouse.get_pos()
    paddle_obj.centerx = mouse_pos[0]

    # Keep paddle within screen bounds
    if paddle_obj.left < 0:
    paddle_obj.left = 0
    if paddle_obj.right > WIDTH:
    paddle_obj.right = WIDTH

    # update screen
    pygame.display.flip()
    clock.tick(fps)


    When attempting to use getbrick() with no parameters, I cannot even execute the code because Method must have a first parameter, usually called 'self' when I use getbrick(self) and leave it blank it gives Parameter 'self' unfilled if I have tried using keywords bricks, Bricks, bricks.Bricks, bricks.self, and self when calling. They all state Expected type 'Bricks', got 'Type[Bricks]' instead in different ways based off the parameter.

    Continue reading...

Compartilhe esta Página