diff --git a/Car.py b/Car.py index 7a945a6..fefcd80 100644 --- a/Car.py +++ b/Car.py @@ -3,32 +3,48 @@ import random from Bubble import Bubble class Car: - def __init__(self, posX, posY, height, width, game_objects, color=(0, 255,0), directionX=1): + def __init__(self, posX, posY, game_objects, color=(0, 255,0), directionX=1): self.posX = posX self.posY = posY self.color = color self.directionX = directionX - self.width = width - self.height = height + self.width = 173 + self.height = 66 self.game_objects = game_objects self.bubble_spawn_time_ms = 1000 self.time_of_last_spawn = 0 self.BUBBLE_IMAGE = pygame.image.load("sprites/bubble.png") + self.image = pygame.image.load("sprites/car.png") + + if directionX < 0: + self.image = pygame.transform.flip(self.image, True, False) def draw(self, screen): - pygame.draw.rect(screen, self.color, pygame.Rect(self.posX, self.posY, self.width, self.height)) + screen.blit(self.image, (self.posX, self.posY)) def update(self, screen): self.posX += self.directionX screen_width, screen_height = screen.get_size() - if self.posX > screen_width: - self.posX = -self.width + if self.directionX > 0: + if self.posX > screen_width: + self.posX = -self.width + else: + if self.posX + self.width < 0: + self.posX = screen_width + self.width + + if self.bubble_spawn_time_ms + self.time_of_last_spawn < pygame.time.get_ticks(): bubble_y_direction = random.uniform(-1, 1) - bubble_x_speeddiff = random.uniform(0, 0.9) bubble_radius = 16 - self.game_objects.append(Bubble(self.posX, self.posY + self.height - bubble_radius, + if self.directionX > 0: + bubble_x_speeddiff = random.uniform(0, 0.9) + bubbleX = self.posX + else: + bubble_x_speeddiff = random.uniform(-0.9, 0) + bubbleX = self.posX + self.width + + self.game_objects.append(Bubble(bubbleX, self.posY + self.height - bubble_radius, self.BUBBLE_IMAGE, radius=bubble_radius, direction=(-self.directionX * bubble_x_speeddiff,bubble_y_direction))) diff --git a/main.py b/main.py index e9a1e87..337421e 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,7 @@ FONT = pygame.font.SysFont('Comic Sans MS', 128) TEXT_SCREEN = FONT.render('Game over!', False, (255, 255, 255)) TEXT_RECT = TEXT_SCREEN.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)) + BUBBLE_LIMIT = 100 SCREEN = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) @@ -25,8 +26,10 @@ CLOCK = pygame.time.Clock() game_objects = [] bubble_objects = [] -for i in range(10): - game_objects.append(Car(random.uniform(0, 200), i * 50, 30, 50, bubble_objects, directionX=random.uniform(1, 5))) +for i in range(4): + game_objects.append(Car(random.uniform(0, SCREEN_WIDTH), i * 80 + 10, bubble_objects, directionX=random.uniform(1, 5))) +for i in range(4, 7): + game_objects.append(Car(random.uniform(0, SCREEN_WIDTH), i * 80 + 10, bubble_objects, directionX= - random.uniform(1, 5))) game_objects.append(Vacuum(bubble_objects)) diff --git a/sprites/car.png b/sprites/car.png new file mode 100644 index 0000000..7e862b4 Binary files /dev/null and b/sprites/car.png differ