From f70bbf7c1ba90b8edecc339daa6da34219138e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Drzewi=C5=84ski?= Date: Wed, 24 Mar 2021 00:20:41 +0100 Subject: [PATCH] reprezentacja wiedzy 2 --- container/board.py | 105 ++++++++++++++++++++++++++++++++++------ container/constans.py | 14 ++++-- container/detective.png | Bin 1891 -> 1252 bytes container/piece.py | 12 +++-- main.py | 40 +++++++-------- 5 files changed, 132 insertions(+), 39 deletions(-) diff --git a/container/board.py b/container/board.py index 72b6a30..4706e16 100644 --- a/container/board.py +++ b/container/board.py @@ -1,33 +1,110 @@ 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 + 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 + 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): - self.board = [[0 for j in range(COLUMNS)] for i in range(ROWS)] + 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) - for x in range(ROWS): - for y in range(COLUMNS): - self.board[x][y]=Piece(x,y) def get_piece(self,x,y): - print(self.board[x][y], x, y) + #print(self.board[x][y], x, y) return self.board[x][y] - + def draw_squares(self,win): - win.fill(GREEN_2) - for row in range(ROWS): - for col in range(row % 2, ROWS, 2): - pygame.draw.rect(win,GREEN,(row*SQUARE_SIZE, col*SQUARE_SIZE, SQUARE_SIZE,SQUARE_SIZE)) + 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 + + + + + + + diff --git a/container/constans.py b/container/constans.py index b5fdf0c..f51af46 100644 --- a/container/constans.py +++ b/container/constans.py @@ -1,11 +1,19 @@ import pygame #board size variables -WIDTH = 700 +WIDTH = 1000 HEIGHT = 700 ROWS = 7 -COLUMNS = 7 -SQUARE_SIZE = WIDTH // COLUMNS +COLUMNS = 10 + +#number of mushrooms + +MUSHROOMS_START = 5 +MUSHROOMS_MAX = 10 +POISON_MUSHROOMS_START = 5 +POISON_MUSHROOMS_MAX = 10 +TREES = 5 + #colors in game in rgb GREEN = (0,230,0) diff --git a/container/detective.png b/container/detective.png index a3eadbe8f089652e0db22dccc4bab6570ed9e2a4..a3482b1afc279fd59a1b37ed385d5a3e52e55fec 100644 GIT binary patch delta 1224 zcmV;(1ULKR4&(`t85#xv003~}l~e!#00v@9M??Vs0RI60puMM)kvx#mEU zZX3lyB4`oIiy#~%#iqZjUAZ=;kW?17M%FYI5y68TqTO7z;(19-;_S?u+4taqh{eo& z^L}S%WSny#!6Oze0RbU@As_@qh#VaqK`a*Ir>CdUA;LLFs{p_Q-P+nhJyubQ%0imW z=JU^tvFG%!SS+&5%}vN=v%8EjK8~)guGYyyM$l5J^p8b^sjlmywGf7-4rw-lq`i;IiTio66GD9U3%cV^vXBv>FvfVb zTGg*ocU>2-RpO#mt4iG@cO|pBCzmB?^KEQwd`0cDOm|nkG@VY@-@R@GM65cFGfgqX zpm;Jt5n%X5u2!pm&}y}y-EQx8yWJE3!0hboua%XRll}dDF&mjIM1CekS(X5i^|2mV z2w8}JlFQ}37|~obMUu(niL@d!4438mJ}fLOkjugX#KamTkx0PJ%?%?U5wjBEF~qj* zuBov^#zqrvAyH_VglQ=j!N4_7N_&w(5e8&$Z;vPm@`qA?&Og%WbRFni{Y{;a0Gyc` zW5NjTp~Vo_b=m6yW4k^!E`&f)yR2L;mxCcLy+DMIXX(O@hSj=d;-tcqcs$OH0|Ee0 zE|;~0GBq{zN2}Hexh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7Zw&$;wrtpy?+#GB-Ju>clnEp zhA}=qPLUS&5esLd?dGQgE?>SpH8n+v zdXn`lZMR<}Q8duUlF(URQ3{}+>S?SbAK|!kag50)ar>tsFb? zdsd>G_Qs-T=8EGi#((deX#+FY($W$o%827E&)LdC^|H9vS@1JO9z^xBxL2&s&Q6NF z$%_{+C{jRF8G2~8N`i_{7zI(z^3cSPgxj}oQ^ZY_SYu;j6nUBM?rue$D@rWC-%pXp zQS^Y=ZqG`OhGn6mbL~1y&tBBi>>VA_hI{w!QN+h?8Lq9Z8CddR1Q+ZB{jRf?mzOC@ z7bYhsDPm$*EHnR@v<%{#0gUX5wZ6Vi(E$mF8XeMaLHzFl5555+&Mle)5|Or}13!lj Z{R^d-7lzYSM1BAO002ovPDHLkV1idwj`jcm diff --git a/container/piece.py b/container/piece.py index 64428dc..c06467a 100644 --- a/container/piece.py +++ b/container/piece.py @@ -1,8 +1,12 @@ +import pygame + class Piece: - def __init__(self,row,col): - self.row = row - self.col = col + def __init__(self,x_y): + self.name = "" + self.row = x_y[0] + self.col = x_y[1] + self.img = '' self.mushroom = False self.isSbThere = False @@ -10,6 +14,8 @@ class Piece: print(self.isSbThere, self.row, self.col) self.isSbThere = not self.isSbThere + def draw(self, win, square_size): + win.blit(pygame.transform.scale(self.img, square_size), (self.col*square_size[0], self.row*square_size[1])) diff --git a/main.py b/main.py index d3a406b..82c8e2b 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ import pygame -from container.constans import WIDTH, HEIGHT,SQUARE_SIZE,ROWS,COLUMNS +from container.constans import WIDTH, HEIGHT, ROWS, COLUMNS, GREEN from container.board import Board FPS = 8 @@ -8,23 +8,23 @@ FPS = 8 WIN = pygame.display.set_mode((WIDTH,HEIGHT)) #setting name pygame.display.set_caption('Forest') +pygame.font.init() - -detective =pygame.image.load(r'container\detective.png') +#detective = pygame.image.load(r'container\detective.png') def main(): run = True clock = pygame.time.Clock() #for fps - board = Board() + board = Board(0,40,WIDTH,HEIGHT-40) #board.manage_rows_and_col(ROWS,COLUMNS) #managing agent location - current_column = 0 - current_row = 0 - current_piece = board.get_piece(current_row,current_column) - current_piece.change_status() + #current_column = 0 + #current_row = 0 + #current_piece = board.get_piece(current_row,current_column) + #current_piece.change_status() #calculate desireable location to pixels def claculate_position(x,y): @@ -42,25 +42,27 @@ def main(): run = False #managing arrow click + key_input = pygame.key.get_pressed() - if key_input[pygame.K_LEFT] and current_column != 0: - current_column -= 1 - if key_input[pygame.K_UP] and current_row != 0: - current_row -= 1 - if key_input[pygame.K_RIGHT] and current_column != COLUMNS - 1: - current_column += 1 - if key_input[pygame.K_DOWN] and current_row != 6 - 1: - current_row += 1 - + if key_input[pygame.K_LEFT]: board.move(0,-1) + if key_input[pygame.K_UP]: board.move(-1,0) + if key_input[pygame.K_RIGHT]: board.move(0,1) + if key_input[pygame.K_DOWN]: board.move(1,0) + """ #managing agent location current_piece.change_status() current_piece = board.get_piece(current_row,current_column) current_piece.change_status() + """ - #drawing map and detective + #drawing map and detective + WIN.fill(GREEN) board.draw_squares(WIN) - WIN.blit(detective, (claculate_position(current_column,current_row)[0], claculate_position(current_column,current_row)[1])) + board.draw_pieces(WIN) + board.draw_agent(WIN) + board.draw_info(WIN) + #WIN.blit(detective, (claculate_position(current_column,current_row)[0], claculate_position(current_column,current_row)[1])) pygame.display.update() pygame.quit()