Traktor/app/board.py

59 lines
1.8 KiB
Python
Raw Normal View History

2021-03-16 10:06:56 +01:00
#!/usr/bin/python3
2021-04-13 09:55:19 +02:00
import copy
import pygame
2021-03-16 10:06:56 +01:00
import random
from app.fields import *
from app.utils import get_class
2021-03-16 10:06:56 +01:00
2021-04-13 09:55:19 +02:00
2021-03-16 10:06:56 +01:00
class Board:
2021-04-13 09:55:19 +02:00
def __init__(self, fields=None):
if fields is None:
fields = []
self.__fields = fields
self.create_board()
2021-03-16 10:06:56 +01:00
self.fill()
self.generate_board()
2021-03-16 10:06:56 +01:00
# print(self.__fields)
2021-04-13 09:55:19 +02:00
def get_fields(self) -> list:
return self.__fields
2021-04-27 21:40:59 +02:00
def get_field(self, x: int, y: int) -> Field:
2021-04-13 09:55:19 +02:00
return self.__fields[x][y]
2021-04-27 21:40:59 +02:00
def create_board(self) -> None:
2021-03-16 10:06:56 +01:00
for i in range(HORIZONTAL_NUM_OF_FIELDS):
self.__fields.append([])
for j in range(VERTICAL_NUM_OF_FIELDS):
self.__fields[i].append(None)
2021-04-27 21:40:59 +02:00
def fill(self) -> None:
2021-03-16 10:06:56 +01:00
for i in range(len(self.__fields)):
for j in range(len(self.__fields[i])):
self.__fields[i][j] = random.choice(FIELD_TYPES).capitalize()
2021-03-16 12:20:06 +01:00
2021-04-27 21:40:59 +02:00
def generate_board(self) -> None:
for x in range(len(self.__fields)):
for y in range(len(self.__fields[x])):
field_type = self.__fields[x][y]
c = get_class("app.fields", field_type)
field = c()
self.__fields[x][y] = field
2021-03-16 10:06:56 +01:00
2021-04-27 21:40:59 +02:00
def draw(self, screen: pygame.Surface) -> None:
2021-03-16 12:20:06 +01:00
for x in range(len(self.__fields)):
for y in range(len(self.__fields[x])):
field = self.__fields[x][y]
pos_x = x * FIELD_SIZE
pos_y = y * FIELD_SIZE
field.draw_field(screen, pos_x, pos_y)
2021-04-13 09:55:19 +02:00
2021-04-27 21:40:59 +02:00
def print_board(self) -> None:
2021-04-13 09:55:19 +02:00
for i in range(HORIZONTAL_NUM_OF_FIELDS):
for j in range(VERTICAL_NUM_OF_FIELDS):
print(f"{j} - {type(self.__fields[i][j]).__name__}", end=" | ")
2021-04-27 21:40:59 +02:00
print()