commit a732c83f0527707d5bdf7f2ee7bc26eb1f724313 Author: Krzysztof Surażyński Date: Sat Mar 13 19:04:01 2021 +0100 Init - base stuff diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82195aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv +.idea \ No newline at end of file diff --git a/assets/garbage-truck.png b/assets/garbage-truck.png new file mode 100644 index 0000000..aad1384 Binary files /dev/null and b/assets/garbage-truck.png differ diff --git a/assets/trashes/can.png b/assets/trashes/can.png new file mode 100644 index 0000000..721c22f Binary files /dev/null and b/assets/trashes/can.png differ diff --git a/assets/trashes/plastic-bottle.png b/assets/trashes/plastic-bottle.png new file mode 100644 index 0000000..e6ef146 Binary files /dev/null and b/assets/trashes/plastic-bottle.png differ diff --git a/requirments.txt b/requirments.txt new file mode 100644 index 0000000..cd9b26e --- /dev/null +++ b/requirments.txt @@ -0,0 +1,2 @@ +pkg-resources==0.0.0 +pygame==2.0.1 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..f2b13b6 --- /dev/null +++ b/src/main.py @@ -0,0 +1,133 @@ +import os +import random +import time +import threading + +import pygame + +FPS = 60 + +WINDOW_HEIGHT = 720 +WINDOW_WIDTH = 1280 +PADDING = 64 + +DEFAULT_ASSET_SIZE = 128 + +TRASHES_COUNT = 4 + +WHITE_COLOR = (255, 255, 255) + +TRASH_BOTTLE = "TRASH_BOTTLE" +TRASH_CAN = "TRASH_CAN" + + +class Trash: + def __init__(self): + self.generate_random_trash() + self.get_random_coordinates() + + def generate_random_trash(self): + random_index = random.randint(1, 2) + + if random_index == 1: + self.type = TRASH_BOTTLE + asset_path = os.path.join(os.path.dirname(__file__), '..', 'assets', 'trashes', 'plastic-bottle.png') + else: + self.type = TRASH_CAN + asset_path = os.path.join(os.path.dirname(__file__), '..', 'assets', 'trashes', 'can.png') + + self.asset = pygame.image.load(asset_path) + coordinates = self.get_random_coordinates() + self.object = pygame.Rect(coordinates["x"], coordinates["y"], DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE) + + def get_random_coordinates(self): + return { + "x": random.randint(PADDING, WINDOW_WIDTH - PADDING), + "y": random.randint(PADDING, WINDOW_HEIGHT - PADDING) + } + + +class Truck: + def __init__(self): + asset_path = os.path.join(os.path.dirname(__file__), '..', 'assets', 'garbage-truck.png') + self.asset = pygame.image.load(asset_path) + self.object = pygame.Rect(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2, DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE) + self.velocity = 5 + + def move_up(self): + self.object.y -= self.velocity + + def move_down(self): + self.object.y += self.velocity + + def move_right(self): + self.object.x += self.velocity + + def move_left(self): + self.object.x -= self.velocity + + +class Environment: + def __init__(self): + self.truck = Truck() + self.run = True + self.clock = pygame.time.Clock() + self.WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) + self.trashes_list = [] + self.generate_trashes(TRASHES_COUNT) + self.trashes_collected = [] + + while self.run: + self.clock.tick(FPS) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.run = False + + self.keys_pressed = pygame.key.get_pressed() + self.draw_objects() + self.handle_keyboard() + self.handle_collisions() + + def draw_objects(self): + self.WINDOW.fill(WHITE_COLOR) + self.draw_trashes() + self.WINDOW.blit(self.truck.asset, (self.truck.object.x, self.truck.object.y)) + pygame.display.update() + + def draw_trashes(self): + for trash in self.trashes_list: + self.WINDOW.blit(trash.asset, (trash.object.x, trash.object.y)) + + def handle_keyboard(self): + if self.keys_pressed[pygame.K_w]: + self.truck.move_up() + if self.keys_pressed[pygame.K_s]: + self.truck.move_down() + if self.keys_pressed[pygame.K_d]: + self.truck.move_right() + if self.keys_pressed[pygame.K_a]: + self.truck.move_left() + + def generate_trashes(self, count): + for i in range(count): + trash = Trash() + self.WINDOW.blit(trash.asset, (trash.object.x, trash.object.y)) + self.trashes_list.append(trash) + + def add_trashes(self): + time.sleep(1) + trash = Trash() + self.WINDOW.blit(trash.asset, (trash.object.x, trash.object.y)) + self.trashes_list.append(trash) + + def handle_collisions(self): + for trash in self.trashes_list: + if self.truck.object.colliderect(trash.object): + self.trashes_collected.append(trash) + self.trashes_list.remove(trash) + timer = threading.Thread(target=self.add_trashes) + timer.daemon = True + timer.start() + + +environment = Environment()