-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (40 loc) · 1.65 KB
/
main.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
from snake import *
from utility import *
from cube import *
import pygame
import numpy as np
from tkinter import messagebox
from snake import Snake
def main():
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
snake_1 = Snake((255, 0, 0), (15, 15), SNAKE_1_Q_TABLE)
snake_2 = Snake((255, 255, 0), (5, 5), SNAKE_2_Q_TABLE)
snake_1.addCube()
snake_2.addCube()
snack = Cube(randomSnack(ROWS, snake_1), color=(0, 255, 0))
clock = pygame.time.Clock()
while True:
reward_1 = 0
reward_2 = 0
pygame.time.delay(25)
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
if messagebox.askokcancel("Quit", "Do you want to save the Q-tables?"):
save(snake_1, snake_2)
pygame.quit()
exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
np.save(SNAKE_1_Q_TABLE, snake_1.q_table)
np.save(SNAKE_2_Q_TABLE, snake_2.q_table)
pygame.time.delay(1000)
state_1, new_state_1, action_1 = snake_1.move(snack, snake_2)
state_2, new_state_2, action_2 = snake_2.move(snack, snake_1)
snack, reward_1, win_1, win_2 = snake_1.calc_reward(snack, snake_2)
snack, reward_2, win_2, win_1 = snake_2.calc_reward(snack, snake_1)
snake_1.update_q_table(state_1, action_1, new_state_1, reward_1)
snake_2.update_q_table(state_2, action_2, new_state_2, reward_2)
redrawWindow(snake_1, snake_2, snack, win)
if __name__ == "__main__":
main()