reprezentacja wiedzy 2

This commit is contained in:
Patryk Drzewiński 2021-03-24 00:20:41 +01:00
parent 2353c4a5a2
commit f70bbf7c1b
5 changed files with 132 additions and 39 deletions

View File

@ -1,33 +1,110 @@
import pygame import pygame
import random
#sth goes wrong with imports so its for preventing errors #sth goes wrong with imports so its for preventing errors
"""
try: 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): 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 #sth goes wrong with imports so its for preventing errors
try: try:
from .piece import * from .piece import *
except (ModuleNotFoundError, ImportError): except (ModuleNotFoundError, ImportError):
from piece import * from piece import *
from agent import *
#from .constans import GREEN,GREEN_2,WHITE,ROWS,COLUMNS,SQUARE_SIZE #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: class Board:
def __init__(self): def __init__(self, x = 0, y = 0, width = WIDTH, height = HEIGHT, row = ROWS, col = COLUMNS):
self.board = [[0 for j in range(COLUMNS)] for i in range(ROWS)] 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): 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] return self.board[x][y]
def draw_squares(self,win): def draw_squares(self,win):
win.fill(GREEN_2) self.surface.fill(GREEN_2)
for row in range(ROWS): for row in range(self.row):
for col in range(row % 2, ROWS, 2): for col in range(self.col):
pygame.draw.rect(win,GREEN,(row*SQUARE_SIZE, col*SQUARE_SIZE, SQUARE_SIZE,SQUARE_SIZE)) 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

View File

@ -1,11 +1,19 @@
import pygame import pygame
#board size variables #board size variables
WIDTH = 700 WIDTH = 1000
HEIGHT = 700 HEIGHT = 700
ROWS = 7 ROWS = 7
COLUMNS = 7 COLUMNS = 10
SQUARE_SIZE = WIDTH // COLUMNS
#number of mushrooms
MUSHROOMS_START = 5
MUSHROOMS_MAX = 10
POISON_MUSHROOMS_START = 5
POISON_MUSHROOMS_MAX = 10
TREES = 5
#colors in game in rgb #colors in game in rgb
GREEN = (0,230,0) GREEN = (0,230,0)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -1,8 +1,12 @@
import pygame
class Piece: class Piece:
def __init__(self,row,col): def __init__(self,x_y):
self.row = row self.name = ""
self.col = col self.row = x_y[0]
self.col = x_y[1]
self.img = ''
self.mushroom = False self.mushroom = False
self.isSbThere = False self.isSbThere = False
@ -10,6 +14,8 @@ class Piece:
print(self.isSbThere, self.row, self.col) print(self.isSbThere, self.row, self.col)
self.isSbThere = not self.isSbThere 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]))

38
main.py
View File

@ -1,5 +1,5 @@
import pygame 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 from container.board import Board
FPS = 8 FPS = 8
@ -8,23 +8,23 @@ FPS = 8
WIN = pygame.display.set_mode((WIDTH,HEIGHT)) WIN = pygame.display.set_mode((WIDTH,HEIGHT))
#setting name #setting name
pygame.display.set_caption('Forest') 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(): def main():
run = True run = True
clock = pygame.time.Clock() #for fps clock = pygame.time.Clock() #for fps
board = Board() board = Board(0,40,WIDTH,HEIGHT-40)
#board.manage_rows_and_col(ROWS,COLUMNS) #board.manage_rows_and_col(ROWS,COLUMNS)
#managing agent location #managing agent location
current_column = 0 #current_column = 0
current_row = 0 #current_row = 0
current_piece = board.get_piece(current_row,current_column) #current_piece = board.get_piece(current_row,current_column)
current_piece.change_status() #current_piece.change_status()
#calculate desireable location to pixels #calculate desireable location to pixels
def claculate_position(x,y): def claculate_position(x,y):
@ -42,25 +42,27 @@ def main():
run = False run = False
#managing arrow click #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
key_input = pygame.key.get_pressed()
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 #managing agent location
current_piece.change_status() current_piece.change_status()
current_piece = board.get_piece(current_row,current_column) current_piece = board.get_piece(current_row,current_column)
current_piece.change_status() current_piece.change_status()
"""
#drawing map and detective #drawing map and detective
WIN.fill(GREEN)
board.draw_squares(WIN) 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.display.update()
pygame.quit() pygame.quit()