-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.py
executable file
·249 lines (195 loc) · 7.05 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/python3
from kivy.config import Config
Config.set('graphics', 'resizable', 0)
Config.write()
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.properties import NumericProperty
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from piece import Piece
from board import Board
from constants import WINDOW_WIDTH, WINDOW_HEIGHT, TEXTURES, \
BLOCK_SIZE, SCORE_PER_LINE
Window.size = WINDOW_WIDTH, WINDOW_HEIGHT
class Menu(Screen):
"""The class to handle the game menu."""
def __init__(self, **kwargs):
super().__init__(**kwargs)
btn_jogar = MenuButton(
text="Play!",
pos_hint={
'center_x': .5,
'center_y': .4
}
)
titulo = Label(
text="TETRIS",
font_size=190,
pos_hint={
'center_x': .5,
'center_y': .8
}
)
self.add_widget(titulo)
btn_jogar.bind(on_press=self.start_game)
self.add_widget(btn_jogar)
# self.add_widget(MenuButton(text="Ajuda"))
def start_game(self, btn):
self.manager.current = 'game'
game = self.manager.current_screen
game.start()
class MenuButton(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.size_hint_y = .3
self.size_hint_x = 1
self.background_normal = ""
self.background_color = (0.1, .5, 0, 1)
self.font_size = 70
class Game(Screen):
"""The screen that everything of the game happens."""
loops_per_second = NumericProperty(3)
score = NumericProperty(0)
level = NumericProperty(0)
lines = NumericProperty(0)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.add_widget(GameLabel(text="Score:", y=19))
self.score_label = GameLabel(text='0', y=18)
self.add_widget(self.score_label)
self.level_label = GameLabel(text='Level: 0 ', y=15)
self.add_widget(self.level_label)
self.lines_label = GameLabel(text='Lines: 0', y=12)
self.add_widget(self.lines_label)
self.add_widget(GameLabel(text="Next:", y=8))
# Draw the margin
with self.canvas.before:
Rectangle(
pos=(0, 0),
size=Window.size,
texture=TEXTURES['margin']
)
def start(self):
"""Start the game."""
self.setup()
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self.draw_next_piece()
def stop(self):
# Release the keyboard, and unschedule the game loop.
self._keyboard.release()
Clock.unschedule(self.event)
# Go back to menu screen.
self.manager.current = 'menu'
def setup(self):
"""Setup the game to start."""
# Set the loops per second.
self.loops_per_second = 3
# Clear the canvas
self.canvas.after.clear()
# Initialize the board
self.board = Board(self)
# Put the falling piece on the game.
self.falling_piece = Piece(self)
self.next_piece = self.falling_piece.next
self.next_rotation = self.falling_piece.next_rotation
# Store the score, level and cleaned lines.
self.score = 0
self.level = 0
self.lines = 0
self.level_lines = 0
# Schedule the game_loop.
self.event = Clock.schedule_interval(
self.update, 1 / self.loops_per_second
)
def update(self, dt=0):
"""Function called for updating the game."""
# Check if the piece was placed or not. If it is, put a new
# falling_piece.
# If it's not, it will move down every time it's updated.
if not self.falling_piece.move_down():
self.falling_piece.set_new()
self.next_piece = self.falling_piece.next
self.next_rotation = self.falling_piece.next_rotation
self.draw_next_piece()
self.draw()
def update_score(self, lines):
self.lines += lines
self.level_lines += lines
if self.level_lines >= 10:
self.level += 1
self.level_lines %= 10
self.score += SCORE_PER_LINE[lines] * (self.level + 1)
def _keyboard_closed(self):
"""Kivy's keyboard close callback."""
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
"""Handle key down"""
# If the game is not running, don't handle any key.
# The arrow keys will move the piece, except for the up arrow key that
# will rotate the piece.
if keycode[1] == 'down':
self.update()
elif keycode[1] == 'left':
self.falling_piece.move_left()
elif keycode[1] == 'right':
self.falling_piece.move_right()
elif keycode[1] == 'up':
self.falling_piece.rotate()
# Return True to accept the key. Otherwise, it will be used by
# the system.
return True
def draw(self):
# Clear the canvas
self.canvas.after.clear()
with self.canvas.after:
self.board.draw()
self.falling_piece.draw()
def draw_next_piece(self):
center_x = 4
bottom_y = 3
with self.canvas.before:
Color(0, 0, 0, 1)
Rectangle(
pos=(BLOCK_SIZE, BLOCK_SIZE*2),
size=(BLOCK_SIZE*6, BLOCK_SIZE*6)
)
Color(1, 1, 1, 1)
for x, y in self.next_piece['rotations'][self.next_rotation]:
Rectangle(
size=(BLOCK_SIZE, BLOCK_SIZE),
pos=((x + center_x)*BLOCK_SIZE, (y + bottom_y)*BLOCK_SIZE),
texture=self.next_piece['texture']
)
def on_score(self, prefix, score):
self.score_label.text = str(score)
def on_level(self, instance, level):
self.level_label.text = 'Level: ' + str(level)
def on_lines(self, instance, lines):
self.lines_label.text = 'Lines: ' + str(lines)
class GameLabel(Label):
"""Custom labels for the game."""
def __init__(self, x=1, y=1, **kwargs):
super().__init__(**kwargs)
self.size_hint = None, None
self.size = BLOCK_SIZE * 6, BLOCK_SIZE
self.pos = x*BLOCK_SIZE, y*BLOCK_SIZE
self.font_size = 36
with self.canvas.before:
Color(0, 0, 0, 1)
Rectangle(size=self.size, pos=self.pos)
class TetrisApp(App):
"""Class to handle the application."""
def build(self):
"""Method for building the application."""
sm = ScreenManager()
sm.add_widget(Menu(name="menu"))
sm.add_widget(Game(name="game"))
return sm
if __name__ == '__main__':
TetrisApp().run()