added agent and bombs

This commit is contained in:
barmal4 2021-03-16 18:53:26 +01:00
parent d689651e10
commit 9f1e95fdd2
10 changed files with 57 additions and 28 deletions

View File

@ -1,20 +1,22 @@
import pygame
from Constants import ROWS, COLS, UP, LEFT, DOWN, RIGHT
class Agent(object):
def __init__(self, column, row):
self.column = column
self.row = row
def __init__(self, y, x):
self.y = y
self.x = x
self.image = pygame.image.load('Engine/agent.png')
def ifNotOnEdge(self, destination):
if destination == UP:
if self.row - 1 <= 0:
return False
if destination == DOWN:
if self.row + 1 > ROWS:
return False
if destination == LEFT:
if self.column - 1 <= 0:
return False
if destination == RIGHT:
if self.column + 1 > COLS:
return False
return True
# def ifNotOnEdge(self, destination):
# if destination == UP:
# if self.x - 1 <= 0:
# return False
# if destination == DOWN:
# if self.x + 1 > ROWS:
# return False
# if destination == LEFT:
# if self.y - 1 <= 0:
# return False
# if destination == RIGHT:
# if self.y + 1 > COLS:
# return False
# return True

View File

@ -3,6 +3,7 @@ from Constants import ROWS, COLS, SQUARE_SIZE, GREEN, BLACK, WIDTH
class Board:
def __init__(self):
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)
def drawAgent(self, win, agent):
dis = WIDTH // ROWS
pygame.draw.rect(win, self.color, (agent.row * dis + 1, agent.column * dis + 1, dis - 2, dis - 2))
rect = agent.image.get_rect()
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)

View File

@ -1,7 +1,7 @@
import pygame
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN
from Board import Board
import Agent
from Constants import SQUARE_SIZE, GREEN, RIGHT, LEFT, UP, DOWN, COLS, ROWS
from Engine.Board import Board
from Engine.Agent import Agent
class Game:
def __init__(self, win):
@ -12,15 +12,31 @@ class Game:
self.board = Board()
self.agent = Agent(0, 0)
self.turn = GREEN
self.goingDown = True
def update(self):
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()
def move(self, destination):
if destination == UP and self.agent.ifNotOnEdge(UP):
self.agent.row -= 1
def move(self):
if self.goingDown:
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

Binary file not shown.

BIN
Engine/agent.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
Engine/bomb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

View File

@ -9,17 +9,19 @@ FPS = 60
def main():
run = True
clock = pygame.time.Clock()
game = Game(WIN)
while run:
pygame.init()
game = Game(WIN)
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.time.delay(200)
game.move()
game.update()
pygame.display.update()
main()