From 7617ff82202de0fa5a7b72b4bded92085b0506ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Cupa=C5=82?= Date: Tue, 16 Mar 2021 10:06:56 +0100 Subject: [PATCH] init app --- .gitignore | 3 +++ Readme.md | 16 ++++++++++++ app/__init__.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++ app/agent.py | 32 +++++++++++++++++++++++ app/board.py | 32 +++++++++++++++++++++++ config.py | 25 ++++++++++++++++++ main.py | 6 +++++ requirements.txt | Bin 0 -> 32 bytes 8 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 Readme.md create mode 100644 app/__init__.py create mode 100644 app/agent.py create mode 100644 app/board.py create mode 100644 config.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2767817 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv +.idea +*__pycache__ \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..aa50c70 --- /dev/null +++ b/Readme.md @@ -0,0 +1,16 @@ +## Tractor +### 1. Requirements +python version 3.9 or higher +```bash +python -v +``` +### 2. Create virtual environments and install libs +```bash +virtualenv venv +source activate +pip3 install -r requirements.txt +``` +### 3. Run application +```bash +python3 main.py +``` \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e72c192 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 + +import pygame + +from config import * +from app.board import Board +from app.agent import Agent + + +class App: + def __init__(self): + self.__running = True + self.__screen = None + self.__clock = None + self.__board = Board() + self.__agent = Agent() + + def initialize(self): + pygame.init() + pygame.display.set_caption(CAPTION) + self.__screen = pygame.display.set_mode((WIDTH, HEIGHT)) + + self.__clock = pygame.time.Clock() + + def event_handler(self, event: pygame.event.Event): + if event.type == pygame.QUIT: + self.__running = False + + def loop_handler(self): + self.__board.draw(self.__screen) + self.__agent.draw(self.__screen) + + def keys_pressed_handler(self): + keys = pygame.key.get_pressed() + + if keys[pygame.K_UP]: + self.__agent.move_up() + if keys[pygame.K_DOWN]: + self.__agent.move_down() + if keys[pygame.K_LEFT]: + self.__agent.move_left() + if keys[pygame.K_RIGHT]: + self.__agent.move_right() + + def update_screen(self): + pygame.display.flip() + + def quit(self): + pygame.quit() + + def run(self): + self.initialize() + + while self.__running: + self.__clock.tick(FPS) + + for event in pygame.event.get(): + self.event_handler(event) + + self.keys_pressed_handler() + + self.loop_handler() + self.update_screen() + + self.quit() diff --git a/app/agent.py b/app/agent.py new file mode 100644 index 0000000..69f1f01 --- /dev/null +++ b/app/agent.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +import pygame + +from config import * + + +class Agent: + def __init__(self): + self.__pos_x = (int(HORIZONTAL_NUM_OF_FIELDS/2)-1)*FIELD_SIZE + self.__pos_y = (int(VERTICAL_NUM_OF_FIELDS/2)-1)*FIELD_SIZE + self.__move = FIELD_SIZE + + def draw(self, screen: pygame.Surface): + rect = pygame.Rect(self.__pos_x, self.__pos_y, FIELD_SIZE, FIELD_SIZE) + pygame.draw.rect(screen, C_BLACK, rect) + + def move_up(self): + if self.__pos_y - self.__move >= 0: + self.__pos_y -= self.__move + + def move_down(self): + if self.__pos_y + self.__move + FIELD_SIZE <= HEIGHT: + self.__pos_y += self.__move + + def move_left(self): + if self.__pos_x - self.__move >= 0: + self.__pos_x -= self.__move + + def move_right(self): + if self.__pos_x + self.__move + FIELD_SIZE <= WIDTH: + self.__pos_x += self.__move diff --git a/app/board.py b/app/board.py new file mode 100644 index 0000000..fdddf0d --- /dev/null +++ b/app/board.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +import random +import pygame + +from config import * + + +class Board: + def __init__(self): + self.__fields = [] + self.generate_board() + self.fill() + # print(self.__fields) + + def generate_board(self): + for i in range(HORIZONTAL_NUM_OF_FIELDS): + self.__fields.append([]) + for j in range(VERTICAL_NUM_OF_FIELDS): + self.__fields[i].append(None) + + def fill(self): + colors = [C_RED, C_GREEN, C_BLUE] + for i in range(len(self.__fields)): + for j in range(len(self.__fields[i])): + self.__fields[i][j] = random.choice(colors) + + def draw(self, screen: pygame.Surface): + for i in range(len(self.__fields)): + for j in range(len(self.__fields[i])): + rect = pygame.Rect(FIELD_SIZE*i, FIELD_SIZE*j, FIELD_SIZE, FIELD_SIZE) + pygame.draw.rect(screen, self.__fields[i][j], rect) diff --git a/config.py b/config.py new file mode 100644 index 0000000..9f96548 --- /dev/null +++ b/config.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 + +__all__ = ( + 'WIDTH', 'HEIGHT', 'FIELD_SIZE', + 'VERTICAL_NUM_OF_FIELDS', 'HORIZONTAL_NUM_OF_FIELDS', + 'C_RED', 'C_GREEN', 'C_BLUE', 'C_BLACK', + 'FPS','CAPTION' +) + +# Board settings: +VERTICAL_NUM_OF_FIELDS = 14 +HORIZONTAL_NUM_OF_FIELDS = 20 +FIELD_SIZE = 32 +WIDTH = HORIZONTAL_NUM_OF_FIELDS * FIELD_SIZE +HEIGHT = VERTICAL_NUM_OF_FIELDS * FIELD_SIZE + +# Colors of Fields +C_RED = (255, 0, 0) +C_GREEN = (0, 255, 0) +C_BLUE = (0, 0, 255) +C_BLACK = (0, 0, 0) + +# Other settings +FPS = 10 +CAPTION = 'Tractor' diff --git a/main.py b/main.py new file mode 100644 index 0000000..0829046 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +#!/usr/bin/python3 +from app import App + +if __name__ == '__main__': + app = App() + app.run() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..d72704e182addadda087750a8dcd0f56b883094f GIT binary patch literal 32 kcmezWuYjSFA)O(SA(tVQ!4?RO81xtnfY^|Mmw}4`0FSB$0RR91 literal 0 HcmV?d00001