added agent and bombs
This commit is contained in:
parent
d689651e10
commit
9f1e95fdd2
@ -1,20 +1,22 @@
|
|||||||
|
import pygame
|
||||||
from Constants import ROWS, COLS, UP, LEFT, DOWN, RIGHT
|
from Constants import ROWS, COLS, UP, LEFT, DOWN, RIGHT
|
||||||
class Agent(object):
|
class Agent(object):
|
||||||
def __init__(self, column, row):
|
def __init__(self, y, x):
|
||||||
self.column = column
|
self.y = y
|
||||||
self.row = row
|
self.x = x
|
||||||
|
self.image = pygame.image.load('Engine/agent.png')
|
||||||
|
|
||||||
def ifNotOnEdge(self, destination):
|
# def ifNotOnEdge(self, destination):
|
||||||
if destination == UP:
|
# if destination == UP:
|
||||||
if self.row - 1 <= 0:
|
# if self.x - 1 <= 0:
|
||||||
return False
|
# return False
|
||||||
if destination == DOWN:
|
# if destination == DOWN:
|
||||||
if self.row + 1 > ROWS:
|
# if self.x + 1 > ROWS:
|
||||||
return False
|
# return False
|
||||||
if destination == LEFT:
|
# if destination == LEFT:
|
||||||
if self.column - 1 <= 0:
|
# if self.y - 1 <= 0:
|
||||||
return False
|
# return False
|
||||||
if destination == RIGHT:
|
# if destination == RIGHT:
|
||||||
if self.column + 1 > COLS:
|
# if self.y + 1 > COLS:
|
||||||
return False
|
# return False
|
||||||
return True
|
# return True
|
@ -3,6 +3,7 @@ from Constants import ROWS, COLS, SQUARE_SIZE, GREEN, BLACK, WIDTH
|
|||||||
|
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.board = []
|
self.board = []
|
||||||
|
|
||||||
@ -13,5 +14,13 @@ class Board:
|
|||||||
pygame.draw.rect(win, BLACK, pygame.Rect(row * SQUARE_SIZE, col * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE), 2)
|
pygame.draw.rect(win, BLACK, pygame.Rect(row * SQUARE_SIZE, col * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE), 2)
|
||||||
|
|
||||||
def drawAgent(self, win, agent):
|
def drawAgent(self, win, agent):
|
||||||
dis = WIDTH // ROWS
|
rect = agent.image.get_rect()
|
||||||
pygame.draw.rect(win, self.color, (agent.row * dis + 1, agent.column * dis + 1, dis - 2, dis - 2))
|
rect.center = (agent.x * SQUARE_SIZE + SQUARE_SIZE / 2, agent.y * SQUARE_SIZE + SQUARE_SIZE / 2 - 5)
|
||||||
|
win.blit(agent.image, rect)
|
||||||
|
|
||||||
|
def drawBombs(self,win, x, y):
|
||||||
|
image = pygame.image.load('Engine/bomb.png')
|
||||||
|
image = pygame.transform.scale(image, (SQUARE_SIZE, SQUARE_SIZE))
|
||||||
|
rect = image.get_rect()
|
||||||
|
rect.center = (x * SQUARE_SIZE + SQUARE_SIZE/2 + 5, y * SQUARE_SIZE + SQUARE_SIZE/2 - 5)
|
||||||
|
win.blit(image, rect)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN
|
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN, COLS, ROWS
|
||||||
from Board import Board
|
from Engine.Board import Board
|
||||||
import Agent
|
from Engine.Agent import Agent
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, win):
|
def __init__(self, win):
|
||||||
@ -12,15 +12,31 @@ class Game:
|
|||||||
self.board = Board()
|
self.board = Board()
|
||||||
self.agent = Agent(0, 0)
|
self.agent = Agent(0, 0)
|
||||||
self.turn = GREEN
|
self.turn = GREEN
|
||||||
|
self.goingDown = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.board.drawSquares(self.win)
|
self.board.drawSquares(self.win)
|
||||||
self.board.drawAgent(self,self.win,self.agent)
|
self.board.drawBombs(self.win, 1, 1)
|
||||||
|
self.board.drawBombs(self.win, 2, 3)
|
||||||
|
self.board.drawBombs(self.win, 5, 6)
|
||||||
|
self.board.drawAgent(self.win,self.agent)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
|
|
||||||
def move(self, destination):
|
def move(self):
|
||||||
if destination == UP and self.agent.ifNotOnEdge(UP):
|
if self.goingDown:
|
||||||
self.agent.row -= 1
|
if self.agent.y + 1 < ROWS:
|
||||||
|
self.agent.y += 1
|
||||||
|
elif self.agent.x + 1 < COLS:
|
||||||
|
self.agent.x += 1
|
||||||
|
self.goingDown = not self.goingDown
|
||||||
|
else:
|
||||||
|
if self.agent.y - 1 >= 0:
|
||||||
|
self.agent.y -= 1
|
||||||
|
elif self.agent.x + 1 < COLS:
|
||||||
|
self.agent.x += 1
|
||||||
|
self.goingDown = not self.goingDown
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
BIN
Engine/__pycache__/Agent.cpython-39.pyc
Normal file
BIN
Engine/__pycache__/Agent.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Engine/agent.png
Normal file
BIN
Engine/agent.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.1 KiB |
BIN
Engine/bomb.png
Normal file
BIN
Engine/bomb.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
6
main.py
6
main.py
@ -9,17 +9,19 @@ FPS = 60
|
|||||||
def main():
|
def main():
|
||||||
run = True
|
run = True
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
game = Game(WIN)
|
||||||
|
|
||||||
while run:
|
while run:
|
||||||
pygame.init()
|
pygame.init()
|
||||||
game = Game(WIN)
|
|
||||||
clock.tick(FPS)
|
clock.tick(FPS)
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
run = False
|
run = False
|
||||||
|
|
||||||
|
pygame.time.delay(200)
|
||||||
|
game.move()
|
||||||
game.update()
|
game.update()
|
||||||
pygame.display.update()
|
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user