forked from s444519/Waiter_group
hello
This commit is contained in:
commit
d79d503d03
143
my_grid3.py
Normal file
143
my_grid3.py
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
import pygame
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
# Colors:
|
||||||
|
# Define some colors
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
GREEN = (0, 255, 0)
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
BLUE = (0, 0, 240)
|
||||||
|
|
||||||
|
#Width and Height of each square:
|
||||||
|
WIDTH = 20
|
||||||
|
HEIGHT = 20
|
||||||
|
|
||||||
|
#Margin:
|
||||||
|
MARGIN = 5
|
||||||
|
grid = [[0 for x in range(16)] for y in range(16)]
|
||||||
|
|
||||||
|
def change_value(i, j, width, n):
|
||||||
|
for r in range (i, i+width):
|
||||||
|
for c in range (j, j+width):
|
||||||
|
grid[r][c] = n
|
||||||
|
|
||||||
|
class Table:
|
||||||
|
def __init__(self, coordinate_i, coordinate_j):
|
||||||
|
self.coordinate_i = coordinate_i
|
||||||
|
self.coordinate_j = coordinate_j
|
||||||
|
change_value(coordinate_i, coordinate_j, 2, 1)
|
||||||
|
|
||||||
|
class Kitchen:
|
||||||
|
def __init__(self, coordinate_i, coordinate_j):
|
||||||
|
self.coordinate_i = coordinate_i
|
||||||
|
self.coordinate_j = coordinate_j
|
||||||
|
change_value(coordinate_i, coordinate_j,3 , 2)
|
||||||
|
|
||||||
|
class Agent:
|
||||||
|
def __init__(self,orig_coordinate_i, orig_coordinate_j):
|
||||||
|
self.orig_coordinate_i = orig_coordinate_i
|
||||||
|
self.orig_coordinate_j = orig_coordinate_j
|
||||||
|
self.state = np.array([1,2])
|
||||||
|
change_value(orig_coordinate_j, orig_coordinate_j, 1, 3)
|
||||||
|
self.state_update(orig_coordinate_i, orig_coordinate_j)
|
||||||
|
def state_update(self, c1, c2):
|
||||||
|
self.state[0] = c1
|
||||||
|
self.state[1] = c2
|
||||||
|
print(self.state)
|
||||||
|
def leave(self):
|
||||||
|
change_value(self.state[0], self.state[1], 1, 0)
|
||||||
|
def move_up (self):
|
||||||
|
self.leave()
|
||||||
|
self.state_update(x-1, y)
|
||||||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||||||
|
def move_down(self):
|
||||||
|
self.leave()
|
||||||
|
change_value(self.state[0], self.state[1], 1, 0)
|
||||||
|
self.state_update(x+1, y)
|
||||||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||||||
|
def move_right(self):
|
||||||
|
self.leave()
|
||||||
|
self.state_update(x, y+1)
|
||||||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||||||
|
def move_left(self):
|
||||||
|
self.leave()
|
||||||
|
self.state_update(x, y-1)
|
||||||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||||||
|
## default positions of the agent:
|
||||||
|
x = 11
|
||||||
|
y = 11
|
||||||
|
agent = Agent(x, y)
|
||||||
|
|
||||||
|
table1 = Table(2, 2)
|
||||||
|
table2 = Table (2,7)
|
||||||
|
table3 = Table(2, 12)
|
||||||
|
table4 = Table(7, 2)
|
||||||
|
table5 = Table(7, 7)
|
||||||
|
table6 = Table(7, 12)
|
||||||
|
table7 = Table(12, 2)
|
||||||
|
table8 = Table(12, 7)
|
||||||
|
#class Kitchen:
|
||||||
|
kitchen = Kitchen(13, 13)
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
WINDOW_SIZE = [405, 405]
|
||||||
|
screen = pygame.display.set_mode(WINDOW_SIZE)
|
||||||
|
|
||||||
|
pygame.display.set_caption("Waiter_Grid3")
|
||||||
|
|
||||||
|
done = False
|
||||||
|
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# -------- Main Program Loop -----------
|
||||||
|
while not done:
|
||||||
|
x = agent.state[0]
|
||||||
|
y = agent.state[1]
|
||||||
|
pygame.time.delay(100)
|
||||||
|
for event in pygame.event.get(): # Checking for the event
|
||||||
|
if event.type == pygame.QUIT: # If the program is closed:
|
||||||
|
done = True # To exit the loop
|
||||||
|
screen.fill(BLACK) # Background color
|
||||||
|
for row in range(16): # Drawing the grid
|
||||||
|
for column in range(16):
|
||||||
|
color = WHITE
|
||||||
|
if grid[row][column] == 1:
|
||||||
|
color = GREEN
|
||||||
|
if grid[row][column] == 2:
|
||||||
|
color = RED
|
||||||
|
if grid[row][column] == 3:
|
||||||
|
color = BLUE
|
||||||
|
pygame.draw.rect(screen,
|
||||||
|
color,
|
||||||
|
[(MARGIN + WIDTH) * column + MARGIN,
|
||||||
|
(MARGIN + HEIGHT) * row + MARGIN,
|
||||||
|
WIDTH,
|
||||||
|
HEIGHT])
|
||||||
|
keys = pygame.key.get_pressed() # Moving the agent:
|
||||||
|
if keys[pygame.K_LEFT]: # Left:
|
||||||
|
# Checking if not a table, a kitchen, or a wall:
|
||||||
|
if y-1 < 0 or grid[x][y-1] == 1:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
agent.move_left() # If okay, the move
|
||||||
|
if keys[pygame.K_RIGHT]: # The same procedure with right:
|
||||||
|
if y+1> 15 or grid[x][y+1] == 1 or grid[x][y+1] == 2:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
agent.move_right()
|
||||||
|
if keys[pygame.K_UP]: # The same procedure with up:
|
||||||
|
if x-1<0 or grid[x-1][y] == 1 or grid[x-1][y] == 2:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
agent.move_up()
|
||||||
|
if keys[pygame.K_DOWN]: # The same procedure with down
|
||||||
|
if x +1 >15 or grid[x+1][y] == 1 or grid[x+1][y] == 2:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
agent.move_down()
|
||||||
|
clock.tick(60) # Limit to 60 frames per second
|
||||||
|
pygame.display.flip() # Updating the screen
|
||||||
|
|
||||||
|
pygame.quit()
|
Loading…
Reference in New Issue
Block a user