Projekt_AI-Automatyczny_saper/Engine/Game.py
2021-04-13 19:32:08 +02:00

104 lines
3.1 KiB
Python

import random
import pygame
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN, COLS, ROWS, DECOY, ATOMIC_BOMB, CHEMICAL_BOMB, \
CLAYMORE, LAND_MINE
from Engine.Board import Board
from Engine.Agent import Agent
from Engine.BombFactory import BombFactory
from Engine.Point import Point
from Engine.Stone import Stone
from Engine.PathFinder import PathFinder
class Game:
def __init__(self, win):
self._init(win)
self.win = win
self.randomizeObject()
pygame.display.update()
def _init(self, win):
self.board = Board(win)
self.agent = Agent(Point(0, 0))
self.turn = GREEN
self.goingDown = True
def update(self):
self.agent.defuse(self.board.getBomb(self.agent.getPoint()))
self.board.drawSquares(self.win)
self.board.drawBombs()
self.board.drawStones()
self.board.drawAgent(self.win, self.agent)
pygame.display.update()
def move(self):
point = self.agent.getPoint()
if self.goingDown:
if point.getY() + 1 < ROWS:
point.y += 1
elif point.getX() + 1 < COLS:
point.x += 1
self.goingDown = not self.goingDown
else:
if point.getY() - 1 >= 0:
point.y -= 1
elif point.getX() + 1 < COLS:
point.x += 1
self.goingDown = not self.goingDown
self.agent.rotateImage(point)
self.agent.point = point
#self.moveSequence()
def moveSequence(self):
pathfinder = PathFinder(self.board)
for point in pathfinder.findPath(Point(0,0), Point(5,5)):
self.agent.point = point
self.update()
def randomizeObject(self):
i = 0
while i < 5:
point = Point(random.randint(0, 9), random.randint(0, 9))
if(point.getX() == 0 and point.getY() == 0):
continue;
if point not in self.board.bombMap:
object = self.pickObject(random.randint(0, 4))
self.board.bombMap[point] = object
i += 1
r = 5
j = 0
while j < r:
point = Point(random.randint(0, 9), random.randint(0, 9))
if (point.getX() == 0 and point.getY() == 0):
continue;
if point not in self.board.bombMap and point not in self.board.stoneMap:
object = Stone()
self.board.stoneMap[point] = object
j += 1
def pickObject(self, rand):
if rand == 0:
return BombFactory.create(DECOY)
elif rand == 1:
return BombFactory.create(CHEMICAL_BOMB)
elif rand == 2:
return BombFactory.create(ATOMIC_BOMB)
elif rand == 3:
return BombFactory.create(CLAYMORE)
elif rand == 4:
return BombFactory.create(LAND_MINE)
def findBomb(self):
pass
def finalState(self):
j = 0
for key in self.board.bombMap:
bomb = self.board.bombMap[key]
if not bomb.isDefused:
j += 1
return j == 0