forked from Infatoshi/rl-from-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
89 lines (76 loc) · 3.26 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pygame
import numpy as np
import random
# Initialize Pygame
pygame.init()
# Setting up the display
screen_size = 400
tile_size = screen_size // 4
screen = pygame.display.set_mode((screen_size, screen_size))
# Colors
background_color = (0, 0, 0)
player_color = (255, 0, 0) # Player color changed to red
goal_color = (0, 255, 0) # Goal color changed to green
line_color = (0, 0, 0)
class GridGame:
def __init__(self):
self.board = np.zeros((4, 4))
self.player_pos = self.random_position()
self.goal_pos = self.random_position()
while self.goal_pos == self.player_pos:
self.goal_pos = self.random_position()
self.done = False
def random_position(self):
return random.randint(0, 3), random.randint(0, 3)
def move_player(self, action):
if action == 'up' and self.player_pos[0] > 0:
self.player_pos = (self.player_pos[0] - 1, self.player_pos[1])
elif action == 'down' and self.player_pos[0] < 3:
self.player_pos = (self.player_pos[0] + 1, self.player_pos[1])
elif action == 'left' and self.player_pos[1] > 0:
self.player_pos = (self.player_pos[0], self.player_pos[1] - 1)
elif action == 'right' and self.player_pos[1] < 3:
self.player_pos = (self.player_pos[0], self.player_pos[1] + 1)
if self.player_pos == self.goal_pos:
self.done = True
def render(self):
screen.fill(background_color)
for row in range(4):
for col in range(4):
if (row, col) == self.player_pos:
pygame.draw.rect(screen, player_color, (col * tile_size, row * tile_size, tile_size, tile_size))
elif (row, col) == self.goal_pos:
pygame.draw.rect(screen, goal_color, (col * tile_size, row * tile_size, tile_size, tile_size))
pygame.draw.rect(screen, line_color, (col * tile_size, row * tile_size, tile_size, tile_size), 1)
pygame.display.flip()
def is_done(self):
return self.done
# Example usage in a Jupyter Notebook
def run_game():
while True:
game = GridGame()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
game.move_player('up')
elif event.key == pygame.K_DOWN:
game.move_player('down')
elif event.key == pygame.K_LEFT:
game.move_player('left')
elif event.key == pygame.K_RIGHT:
game.move_player('right')
elif event.key == pygame.K_q: # Quit game when 'Q' is pressed
running = False
if game.is_done():
print("Goal Reached!")
running = False
game.render()
pygame.time.wait(1) # Slow down the game loop
if __name__ == "__main__":
run_game()
# Note: To run this in a Jupyter Notebook, place the run_game() call in a cell.
# However, keep in mind the limitations of Pygame in Jupyter as mentioned earlier.