-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetris.py
204 lines (168 loc) · 5.45 KB
/
tetris.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from random import *
from Shapes import Shape
from Area import Area
from Display import Hud
from Menu import Menu
from Gameover import Gameover
from Bot import Bot
import pygame
import sys
# checks the user input and implements proper tests and movements in correspondance
# with the user's input
def check_input():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
shape.rotate(area.matrix())
if event.key == pygame.K_DOWN:
shape.test_y(area.matrix())
if shape.collision != 1:
shape.move_down(area.matrix())
if event.key == pygame.K_LEFT:
shape.move_left(area.matrix())
if event.key == pygame.K_RIGHT:
shape.move_right(area.matrix())
if event.key == pygame.K_SPACE:
shape.test_y(area.matrix())
while shape.collision != 1:
shape.move_down(area.matrix())
shape.draw_shape(area.matrix(), screen)
area.draw(shape, area.matrix(), screen)
return area
# updates the menu screen based on user inputs
def menu_input():
while not menu.gameStart:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
menu.move_cursor(-1)
menu.update_menu(screen)
pygame.display.flip()
if event.key == pygame.K_UP:
menu.move_cursor(1)
menu.update_menu(screen)
pygame.display.flip()
if event.key == pygame.K_RETURN:
menu.gameStart=1
################### MAIN ####################
# some initialization procedures
pygame.init()
bot = Bot()
# initializes the screen, window name and the key repeater
# if the key is held the key is repeated
size = width, height = 300, 440
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('TETRIS')
pygame.key.set_repeat(75)
while 1:
# build a matrix for from the width and height
area = Area(width - 100, height)
# initializes the shape queue (first number is current shape, second is upcoming)
# randomizes the shape color
shape_queue = []
for i in range(5):
shape_queue.append(randint(0,6))
shape_color=(randint(1,255), randint(1,255), randint(1,255))
# creates a new shape: 0 is the shape id
shape = Shape(shape_color, shape_queue[0])
# init Heads up Display
hud = Hud(width, height)
gameOver=Gameover(width,height,area.score)
# redraws the screen with the black background hud and menu
screen.fill((0,0,0))
hud.draw(screen)
menu=Menu(width,height)
menu.draw_menu(screen)
menu.update_menu(screen)
pygame.display.flip()
# moves the selector box around the menu
menu_input()
# initializes the ticker and starts moving the blocks after ~1s elapsed.
init = 1000
start_time = pygame.time.get_ticks()
# Single Player Looop
while menu.singlePlayer and shape.game_state:
check_input()
time = pygame.time.get_ticks() - start_time
# deactivate shape if state become one, pop shape off the queue and
# add a new shape to the list and
if shape.state == 1:
next_shape.deactivate()
shape_queue.pop(0)
shape_queue.append(randint(0,6))
shape = Shape(shape_color,shape_queue[0], 0,5,0)
shape_color=(randint(1,255), randint(1,255), randint(1,255))
# Delay (increase to increase the shape drop delay)
if init < time:
shape.move_down(area.matrix())
init += 300
# redraws the screen to update everything
screen.fill((0,0,0))
shape.draw_shape(area.matrix(), screen)
area.draw(shape, area.matrix(), screen)
area.print_game_info(screen)
next_shape = Shape(shape_color,shape_queue[1],0,12,0)
next_shape.update_shape(area.matrix())
area.draw_next_shape(next_shape,screen)
hud.draw(screen)
pygame.display.flip()
# first loop helps bot with the first iteration of the bot loop
first_loop = 1
# Main Bot loop
while menu.demo and shape.game_state:
check_input()
time = pygame.time.get_ticks() - start_time
next_shape = Shape(shape_color,shape_queue[1],0,12,0)
if shape.state == 1 or first_loop == 1:
next_shape.deactivate()
shape_queue.pop(0)
shape_queue.append(randint(0,6))
shape = Shape(shape_color, shape_queue[0])
shape_color=(randint(1,255), randint(1,255), randint(1,255))
location = bot.check_spot_score(shape, area.matrix())
shape.track_x = location[1]
shape.rotation = location[2]
first_loop = 0
# Delay (increase to increase the shape drop delay)
if init < time:
shape.move_down(area.matrix())
init += 0
# redraws the screen to update everything
screen.fill((0,0,0))
shape.draw_shape(area.matrix(), screen)
area.draw(shape, area.matrix(), screen)
area.print_game_info(screen)
next_shape.update_shape(area.matrix())
area.draw_next_shape(next_shape,screen)
hud.draw(screen)
pygame.display.flip()
# display the info screen
while menu.info and not menu.infoDone:
menu.draw_info(screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
menu.infoDone=1
# displays the loss screen
while not menu.info and not gameOver.pressContinue:
gameOver.draw_Gameover(screen)
gameOver.press_continue(screen)
pygame.display.flip()
# waits for a keystroke to move to main menu
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
gameOver.pressContinue=1
# resets the game variables.
menu.reset_game()