This commit is contained in:
Dominik Cupał 2021-03-16 10:06:56 +01:00
commit 7617ff8220
8 changed files with 179 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
venv
.idea
*__pycache__

16
Readme.md Normal file
View File

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

65
app/__init__.py Normal file
View File

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

32
app/agent.py Normal file
View File

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

32
app/board.py Normal file
View File

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

25
config.py Normal file
View File

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

6
main.py Normal file
View File

@ -0,0 +1,6 @@
#!/usr/bin/python3
from app import App
if __name__ == '__main__':
app = App()
app.run()

BIN
requirements.txt Normal file

Binary file not shown.