-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris_ui.py
129 lines (107 loc) · 3.78 KB
/
tetris_ui.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Authors: Sharay Gao, Ori Yoked, Neil Thistlethwaite
(add your name above if you contribute to this file)
The Agency, Reinforcement Learning for Tetris
This file provides a UI that can be used to play the Tetris game (using the
engine in {tetris_engine.py}), and possibly in the future to watch our agents
play the game. Uses pygame and supports arrow keys and WASD controls.
"""
from random import Random
import pygame
import sys
from tetris_engine import *
from reward_functions import *
from game_agent import *
pygame.init()
game = GameState()
# Sharay: some basic information, makes adapting the program easier
CELL_SIZE = 30
GAME_WIDTH = game.width
GAME_HEIGHT = game.height
GAME_TICK_DELAY = 100
IS_KEYBOARD_MODE = False
AGENT_TYPE = ModelAgent
screen = pygame.display.set_mode((CELL_SIZE * GAME_WIDTH, CELL_SIZE * GAME_HEIGHT))
# Sharay: Actions currently supported
action_lookup = {
pygame.K_LEFT: Action.LEFT,
pygame.K_RIGHT: Action.RIGHT,
pygame.K_UP: Action.ROTATE_CW,
pygame.K_DOWN: Action.ROTATE_CCW,
pygame.K_a: Action.LEFT,
pygame.K_d: Action.RIGHT,
pygame.K_w: Action.ROTATE_CW,
pygame.K_s: Action.ROTATE_CCW,
pygame.K_r: Action.RESET
}
# Ori - Colors:
COLORS = [
(255, 255, 0), # Yellow
(255, 0, 255), # Magenta
(0, 255, 255), # Cyan
(255, 0, 0), # Red
(0, 0, 255), # Blue
(0, 255, 0), # Green
(255, 53, 184) # Pink
]
LOCKED_COLORS = [
(250, 249, 223),
(223, 185, 208),
(163, 220, 228),
(255, 203, 165),
(167, 183, 222),
(206, 240, 161),
(244, 204, 203)
]
# Ori - Game Over message:
font = pygame.font.Font('freesansbold.ttf', 15)
if not IS_KEYBOARD_MODE:
agent = AGENT_TYPE()
lines_cleared = 0
agent = RandomAgent()
#agent = ModelAgent(torch.load("./model-epoch-140.pt"))
#agent = BruteAgent2(depth=2, reward_func=HeightPenaltyReward(multiplier=0.1))
frame_count = 0
while True:
# Sharay: this just waits a bit before running, temporary
pygame.time.wait(GAME_TICK_DELAY)
action = Action.IDLE
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key in action_lookup:
action = action_lookup[event.key]
# Sharay: Updates the game with the key action
if IS_KEYBOARD_MODE:
lines_cleared += game.update(action)
else:
lines_cleared += game.update(agent.get_move(game))
frame_count += 1
board = game.game_board
# Sharay and Ori: alters colors in the board
screen.fill((0, 0, 0))
if game.stop:
text = font.render("Game Over - Score: " + str(lines_cleared) + " - R to restart", True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center = (150, 100)
screen.blit(text, textRect)
print("Game ended at",frame_count,"frames")
else:
for i in range(len(board)):
for j in range(len(board[0])):
# Ori - change color when locked
if board[i][j] > 0:
pygame.draw.rect(screen, LOCKED_COLORS[board[i][j] - 1],
pygame.Rect(CELL_SIZE * i,
CELL_SIZE * (GAME_HEIGHT - j - 1),
CELL_SIZE,
CELL_SIZE))
elif board[i][j] < 0:
pygame.draw.rect(screen, COLORS[(board[i][j] * -1) - 1],
pygame.Rect(CELL_SIZE * i,
CELL_SIZE * (GAME_HEIGHT - j - 1),
CELL_SIZE,
CELL_SIZE))
pygame.display.flip()