initial commit

This commit is contained in:
2021-11-05 19:35:05 +01:00
commit cfc9a20de5
4 changed files with 105 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
__pycache__

36
Bubble.py Normal file
View File

@@ -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

30
Car.py Normal file
View File

@@ -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()

38
main.py Normal file
View File

@@ -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)