-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
99 lines (77 loc) · 2.13 KB
/
game.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
import wasabi2d as w2d
from wasabi2d.keyboard import keys
from model import Piece, ALL_PIECES, TetrisBoard
from random import choice
scene = w2d.Scene()
rect = scene.layers[0].add_line(
vertices=[(50, 50), (50, 540), (300, 540), (300, 50)],
color='red',
)
def grid_to_screen(x, y):
return x*24+66, y*24+66
#for j in range(0, 20):
# for i in range(0, 10):
# x, y = grid_to_screen(i, j)
# scene.layers[1].add_sprite("tile", pos=(x, y), color='green', scale=0.75)
b = TetrisBoard((10, 20))
active_piece_sprites = []
active_piece = None
COLOURS = [
'red',
'green',
'blue',
'yellow',
]
def create_piece():
global active_piece
type = choice(ALL_PIECES)
active_piece = Piece("*", type, (4, 0), b)
active_piece_sprites.clear()
colour = choice(COLOURS)
for p in active_piece:
sprite = scene.layers[1].add_sprite(
"tile",
pos=grid_to_screen(*p),
color=colour,
scale=0.75
)
active_piece_sprites.append(sprite)
def update_active_piece():
"""Make the sprites for the active piece reflect their grid coordinates."""
for sprite, pos in zip(active_piece_sprites, active_piece):
w2d.animate(
sprite,
duration=0.1,
pos=grid_to_screen(*pos)
)
def drop_tick():
if active_piece.can_drop():
active_piece.drop()
update_active_piece()
else:
for point in active_piece:
b[point] = True
create_piece()
w2d.clock.schedule_interval(drop_tick, 1)
@w2d.event
def on_key_down(key):
updated = False
if key == keys.Z:
active_piece.rotate_left()
updated = True
elif key == keys.X:
active_piece.rotate_right()
updated = True
if key == keys.LEFT and active_piece.can_go_left():
active_piece.move_left()
updated = True
elif key == keys.RIGHT and active_piece.can_go_right():
active_piece.move_right()
updated = True
elif key == key.DOWN:
drop_tick()
updated = True
if updated:
update_active_piece()
create_piece()
w2d.run()