我遇到以下代码问题。我正在创造一个游戏,泡泡射手。泡沫是精灵。我遇到的问题是,当屏幕上出现气泡时,它们会关闭屏幕。相反,我希望他们反弹屏幕并随机移动。我不知道该怎么做。我努力但没有运气。
我的代码:
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()