0

I have a problem with the following code. I am creating a game, bubble shooter. The bubble are the sprites. The problem I have is that when the bubbles appear on the screen, they go OFF the screen. Instead I want them to bounce of the screen and move in a random direction. I don't know how to do this. I've tried so hard but no luck.

My Code:

import pygame
import random


class Bubble(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        # Load the bubble images from the bubbles folder. The os.path.join is required to open from a folder.
        # Convert_alpha fixes the transparency in the image.
        self.image = pygame.image.load(os.path.join("assets","bubble1.png")).convert_alpha()
        # This resizes the bubbles so that they are smaller.
        self.image = pygame.transform.scale(self.image, (50, 50))

        self.rect = self.image.get_rect()

        self.area = screen.get_rect()
        self.y = random.randrange(1, 1000)

        self.rect.topleft = 500, self.y
        self.rect.topright = 600, self.y


        self.move = random.randrange(1, 5)

    def _move(self):
            newpos = self.rect.move((-self.move, 0))

            self.rect = newpos

    def update(self):
        self.rect.centerx += self.vx
        self.rect.centery += self.vy
        w, h = self.screen.get_size()
        if self.rect.left < 0 or self.rect.right > w:
            self.vx *= -1
        if self.rect.top < 0 or self.rect.bottom > h:
            self.vy *= -1     

    def update(self):
            self._move()
Martin Gergov
  • 1,556
  • 4
  • 20
  • 29

0 Answers0