SIP/container/board.py
2021-03-24 00:20:41 +01:00

111 lines
3.8 KiB
Python

import pygame
import random
#sth goes wrong with imports so its for preventing errors
"""
try:
from .constans import GREEN,GREEN_2,WHITE,ROWS,COLUMNS,SQUARE_SIZE,MARGIN
except (ModuleNotFoundError, ImportError):
from constans import GREEN,GREEN_2,WHITE,ROWS,COLUMNS,SQUARE_SIZE,MARGIN
#sth goes wrong with imports so its for preventing errors
try:
from .piece import *
except (ModuleNotFoundError, ImportError):
from piece import *
from agent import *
#from .constans import GREEN,GREEN_2,WHITE,ROWS,COLUMNS,SQUARE_SIZE
"""
from container.constans import *
from container.tree import Tree
from container.mushroom import Mushroom
from container.agent import Agent
class Board:
def __init__(self, x = 0, y = 0, width = WIDTH, height = HEIGHT, row = ROWS, col = COLUMNS):
self.x = x
self.y = y
self.width = width
self.height = height
self.row = row
self.col = col
self.square_size = (self.width // self.col, self.height // self.row)
self.board = [[False for j in range(self.col)] for i in range(self.row)]
self.free_spaces = []
self.update_free_spaces()
self.agent = Agent(self.free_spaces.pop())
self.surface = pygame.Surface((self.width, self.height))
for i in range(TREES):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Tree(x_y, random.randint(0, 8))
for i in range(MUSHROOMS_START):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2))
for i in range(POISON_MUSHROOMS_MAX):
x_y = self.free_spaces.pop()
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True)
def update_free_spaces(self):
self.free_spaces = []
for x in range(self.row):
for y in range(self.col):
if self.board[x][y] == False: self.free_spaces.append((x,y))
random.shuffle(self.free_spaces)
def get_piece(self,x,y):
#print(self.board[x][y], x, y)
return self.board[x][y]
def draw_squares(self,win):
self.surface.fill(GREEN_2)
for row in range(self.row):
for col in range(self.col):
if (row+col) % 2 == 0:
pygame.draw.rect(self.surface,GREEN,(col*self.square_size[0], row*self.square_size[1], self.square_size[0], self.square_size[1]))
win.blit(self.surface, (self.x, self.y))
def draw_agent(self,win):
self.agent.draw(self.surface, self.square_size)
win.blit(self.surface, (self.x, self.y))
def draw_pieces(self,win):
for row in range(self.row):
for col in range(self.col):
if self.board[row][col] != False:
self.board[row][col].draw(self.surface, self.square_size)
win.blit(self.surface, (self.x, self.y))
def draw_info(self,win):
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render(str(self.agent.points), False, (0, 0, 0))
win.blit(textsurface,(self.x+self.width-textsurface.get_width()-5,self.y-40))
def move(self, x, y):
if 0 <= self.agent.row + x < self.row and 0 <= self.agent.col + y < self.col:
if self.board[self.agent.row + x][self.agent.col + y] == False or self.board[self.agent.row + x][self.agent.col + y].name != "Tree":
self.agent.col += y
self.agent.row += x
if self.board[self.agent.row][self.agent.col] != False:
self.agent.points += self.board[self.agent.row][self.agent.col].points
self.board[self.agent.row][self.agent.col] = False