-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetup.py
107 lines (97 loc) · 2.8 KB
/
Setup.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
__author__ = 'Robin'
""" Settings file, for the GUI of pyTetris """
import pygame, sys
from pygame.locals import *
from tetris import *
from collections import namedtuple
# Instantiate pyTetris engine
# ---------------------------
model = Model()
viewer = Viewer(model)
controller = Controller(model, viewer)
pygame.init()
# Colors
# ------
Color = namedtuple("Color", ['red', 'green', 'blue'])
CYAN = Color(0,200,200)
GREEN = Color(0,200,0)
RED = Color(200,0,0)
BLUE = Color(0,0,255)
DARKBLUE = Color(0,0,128)
WHITE = Color(255,255,255)
BLACK = Color(0,0,0)
MAGENTA = Color(255,200,200)
YELLOW = Color(255,255,0)
ORANGE = Color(255,165,0)
# pygame setup
# ------------
# display size
DX = 20
DY = 20
WIDTH = int((model.GRID_COLS+2)*DX)
HEIGHT = int((model.GRID_ROWS+2)*DX)
# refresh rate
RATE = 30
TETRAMINOS = ('I','J','L','O','T','Z','S')
COLORCODE = {
'c': CYAN,
'C': CYAN,
'y': YELLOW,
'Y': YELLOW,
'r': RED,
'R': RED,
'g': GREEN,
'G': GREEN,
'b': BLUE,
'B': BLUE,
'o': ORANGE,
'O': ORANGE,
'm': MAGENTA,
'M': MAGENTA,
'.': BLACK
}
screen = pygame.display.set_mode((WIDTH,HEIGHT))
myfont = pygame.font.SysFont("monospace", 15)
# Helper functions
# ----------------
def draw_block(position, color):
""" Draws a rectangle of the given color in the grid
:param position: Position in the grid where the block should be placed
:param color: Color of the block
:return: None
"""
x = position.col*DX+DX+2
y = position.row*DY+DY+2
width = DX-4
height = DY-4
pygame.draw.rect(screen, color, (x,y,width,height), 0)
def draw_grid():
screen.fill(BLACK)
# Setup background grid
for col in range(model.GRID_COLS+1):
line_color = WHITE
pygame.draw.line(screen, line_color, (DX*(col+1), DY), (DX*(col+1), HEIGHT-DY), (1))
for row in range(model.GRID_ROWS+1):
if row == 2:
line_color = RED
else:
line_color = WHITE
pygame.draw.line(screen, line_color, (DX, DY*(row+1)), (WIDTH-DX, DY*(row+1)), (1))
# draw filled blocks
for row in range(model.GRID_ROWS):
for col in range(model.GRID_COLS):
if model.grid[row,col] != '.':
draw_block(Position(row,col), COLORCODE[model.grid[row,col]])
if model.active_grid[row,col] != '.':
draw_block(Position(row,col), COLORCODE[model.active_grid[row,col]])
# draw score
draw_score(model.score, model.level)
# refresh console, for debugging
controller.exec_command('p')
def draw_score(score, level):
if model.game_mode == 'game over':
text = myfont.render("Game Over!", 1, (255,255,255))
else:
text = myfont.render("".join(["Score: ", str(score), " Level: ", str(level)]),
1, (255,255,255))
screen.blit(text, (1, 1))