import math import random, pygame Screen_Width = 800 Screen_Height = 600 class Ball(pygame.sprite.Sprite): def __init__(self): super(Ball, self).__init__() self.image = pygame.image.load('images/ballblue.png').convert() self.rect = self.image.get_rect() self.rect.x = 10 self.rect.y = 300 self.x = self.rect.x self.y = self.rect.y # random direction of the ball self.dx = random.randint(4, 6) self.dy = random.randint(4, 6) if self.dx != 0: pass else: self.dx = 5 if self.dy != 0: pass else: self.dy = 1 self.width = 15 self.height = 15 def move(self, lives): self.x = self.x + self.dx self.y = self.y + self.dy # Check border collision if self.x >= Screen_Width - self.width / 2: self.dx = -self.dx if self.x <= 0: self.dx = -self.dx if self.y <= 0: self.dy = -self.dy if self.y >= Screen_Height - self.height / 2: self.dy = -self.dy lives -= 1 return lives def collpaddle(self, a): collide = pygame.sprite.spritecollide(self, a, False) return collide def collbrick(self, a): collide = pygame.sprite.spritecollide(self, a, False) return collide