From cfc9a20de523e3fb61f775896d3d1c00a34ed162 Mon Sep 17 00:00:00 2001 From: "Hannes F. Kuchelmeister" Date: Fri, 5 Nov 2021 19:35:05 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + Bubble.py | 36 ++++++++++++++++++++++++++++++++++++ Car.py | 30 ++++++++++++++++++++++++++++++ main.py | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100644 Bubble.py create mode 100644 Car.py create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/Bubble.py b/Bubble.py new file mode 100644 index 0000000..5800c43 --- /dev/null +++ b/Bubble.py @@ -0,0 +1,36 @@ +import pygame +import random + +class Bubble: + def __init__(self, posX, posY, radius = 1, color=(0,0,0), direction=(1,1)): + self.posX = posX + self.posY = posY + self.radius = radius + self.color = color + self.directionX = direction[0] + self.directionY = direction[1] + + def draw(self, screen): + pygame.draw.circle(screen, self.color, (self.posX, self.posY), self.radius) + + def update(self, screen): + + screen_width, screen_height = screen.get_size() + + self.move_x(screen_width) + self.move_y(screen_height) + + def move_x(self, screen_width): + if self.posX + self.radius + self.directionX < screen_width and self.posX - self.radius + self.directionX > 0: + self.posX += self.directionX + else: + self.directionX *= -1 + + + def move_y(self, screen_height): + if self.posY + self.radius + self.directionY < screen_height and self.posY - self.radius + self.directionY > 0: + self.posY += self.directionY + else: + self.directionY *= -1 + + diff --git a/Car.py b/Car.py new file mode 100644 index 0000000..826a7e6 --- /dev/null +++ b/Car.py @@ -0,0 +1,30 @@ +import pygame +import random +from Bubble import Bubble + +class Car: + def __init__(self, posX, posY, height, width, game_objects, color=(0,0,0), directionX=1): + self.posX = posX + self.posY = posY + self.color = color + self.directionX = directionX + self.width = width + self.height = height + self.game_objects = game_objects + self.bubble_spawn_time_ms = 1000 + self.time_of_last_spawn = 0 + + def draw(self, screen): + pygame.draw.rect(screen, self.color, pygame.Rect(self.posX, self.posY, self.width, self.height)) + + 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.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 = random.uniform(5, 10) + self.game_objects.append(Bubble(self.posX, self.posY, radius=bubble_radius, direction=(-self.directionX * bubble_x_speeddiff,bubble_y_direction))) + self.time_of_last_spawn = pygame.time.get_ticks() diff --git a/main.py b/main.py new file mode 100644 index 0000000..89aeb9d --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +import pygame +import random +import sys +from pygame.locals import * + +from Bubble import Bubble +from Car import Car + +pygame.init() + +SCREEN = pygame.display.set_mode((800,600)) +BACKGROUND = (255,255,255) +SCREEN.fill(BACKGROUND) +CLOCK = pygame.time.Clock() + +car_objects = [] +bubble_objects = [] +for i in range(10): + car_objects.append(Car(random.uniform(0, 200), i * 50, 30, 50, car_objects, directionX=random.uniform(1, 5))) + +while True: + SCREEN.fill(BACKGROUND) + for game_object in car_objects: + game_object.update(SCREEN) + for game_object in car_objects: + game_object.draw(SCREEN) + + for game_object in bubble_objects: + game_object.update(SCREEN) + for game_object in bubble_objects: + game_object.draw(SCREEN) + pygame.display.flip() + + for event in pygame.event.get(): + if event.type == QUIT: + pygame.quit() + sys.exit() + CLOCK.tick(60)