[Python] Pygame sprite replication issue [duplicate]

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

  1. Stack

    Stack Membro Participativo

    I am making a game and implementing sprites. I have my main (drives game), game_objects (contains sprite classes), game_manager (generates game_objects and adds them to sprite groups), and settings (contains all constant game values). I have managed to generate all the sprite objects that I need and put them into the correct groups. I started to implement the behavior, starting with the paddle sprite, but I am having an issue with the paddle's image. The paddle is drawn at its initial position. The paddle moves with the mouse position. Everytime the paddle moves, it redraws a new image but also keeps the previous images on screen. What I end up getting is a bunch of ghost images that ends up painting across the screen at the paddle's x level. Below is the code I am working with, I am very new to Pygame.

    This is the main body:

    pygame.init()
    active_bricks = []
    brick_colors = []
    brick_bodies = []

    game_screen = pygame.display.set_mode((settings.WIDTH, settings.HEIGHT))
    pygame.display.set_caption("SmashCore")
    clock = pygame.time.Clock()
    game_surface = pygame.draw.rect(game_screen,
    (0,0,0), (0,0,settings.WIDTH,settings.HEIGHT))
    game_screen.blit(settings.background_img, game_surface)

    game_manager.generate_ball()
    game_manager.generate_paddle()
    game_manager.generate_bricks(active_bricks, brick_colors, brick_bodies)

    running = True

    while running:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    running = False

    settings.all_sprites.draw(game_screen)
    settings.all_sprites.update()

    pygame.display.flip()
    clock.tick(settings.fps)

    pygame.quit()


    This is how the paddle is generated and placed into a group:

    def generate_paddle():

    paddle_face = settings.paddle
    paddle_body = paddle_face.get_rect()
    paddle_body.x = settings.WIDTH // 2 - paddle_body.width // 2
    paddle_body.y = settings.HEIGHT - 150
    paddle_obj = Paddles(paddle_face, paddle_body)
    settings.paddles.add(paddle_obj)
    settings.all_sprites.add(paddle_obj)

    return paddle_face, paddle_body


    This is the paddle sprite class:

    class Paddles(pygame.sprite.Sprite):
    def __init__(self, paddle_face, paddle_body):
    pygame.sprite.Sprite.__init__(self)
    self.image = paddle_face
    self.rect = paddle_body
    self.x = paddle_body.x
    self.y = paddle_body.y
    self.centerx = paddle_body.centerx
    self.centery = paddle_body.centery
    self.left = paddle_body.left
    self.right = paddle_body.right
    self.top = paddle_body.top
    self.bottom = paddle_body.bottom
    self.width = paddle_body.width
    self.height = paddle_body.height

    def update(self):
    pos = pygame.mouse.get_pos()
    self.rect.x = pos[0]


    These are the values in settings that pertain to the paddle file:

    paddles = pygame.sprite.Group()
    all_sprites = pygame.sprite.Group()

    art_dir = 'assets/art/'
    paddle = pygame.image.load(os.path.join(art_dir, paddle_img_path))

    WIDTH = 1200
    HEIGHT = 900
    fps = 60



    Below is what is happening in game every time I move the mouse.

    [​IMG]

    I do not know why it is doing this and would really appreciate the help. The movement is fine, I just need it to not keep the old images on the screen. If you could offer a brief explanation of what I messed up that would be greatly appreciated. I am not sure where to even begin.

    Continue reading...

Compartilhe esta Página