-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1 Block Tetris.py
183 lines (145 loc) · 5.26 KB
/
1 Block 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
# http://www.codeskulptor.org/#user45_OPKChitwnV_126.py
# Yu Sheng 22/09/18
# Images and mechanics taken and based from https://www.tetrisfriends.com/games/Ultra/game.php
import simplegui
import math
image1 = simplegui.load_image('https://i.imgur.com/JxGJa4B.png') # Background
image2 = simplegui.load_image('https://i.imgur.com/BWf2RTF.png') # Blocks
image3 = simplegui.load_image('https://i.imgur.com/7S4W6wa.png') # Ghost Blocks
S_Block_Flip = 0
S_Block_Flip_Ghost = 0
S_Block_Rotation = 0
S_Block_X = 81
S_Block_Y = 0
S_Block_Y_Ghost = 342
fall = 1
freeze = 0
def timer1_handler():
global S_Block_Y
if S_Block_Y <= 324:
S_Block_Y += 18
def timer2_handler():
global S_Block_X
if S_Block_X > 27:
S_Block_X -= 18
def timer3_handler():
global S_Block_X
if S_Block_X < 153:
S_Block_X += 18
def timer4_handler():
global S_Block_Y
if S_Block_Y <= 324:
S_Block_Y += 18
def timer5_handler():
global freeze
freeze = 1
def reset_timer5():
timer5.stop()
timer5.start()
def draw_handler(canvas):
# Draw Background
canvas.draw_image(image1, (181 / 2, 361 / 2), (181, 361), (181 / 2, 361 / 2), (181, 361))
# Freeze block when it reaches the bottom
if S_Block_Y <= 324:
freeze = 0
if S_Block_Y >= 342:
timer5.start()
#canvas.draw_image(image2, (144 / 2, 198 / 2), (144, 198), (144 / 2, 198 / 2), (144, 198))
#canvas.draw_image(image, center_source, width_height_source, center_dest, width_height_dest)
# J Block
#canvas.draw_image(image2, (27, 18), (54, 36), (300, 300), (27, 18))
# I Block
#canvas.draw_image(image2, (36, 81), (72, 18), (300, 300), (72, 18))
# O Block
#canvas.draw_image(image2, (108, 72), (36, 36), (300, 300), (36, 36))
# L Block
#canvas.draw_image(image2, (27, 126), (54, 36), (300, 300), (54, 36))
# Z Block
#canvas.draw_image(image2, (117, 126), (54, 36), (300, 300), (54, 36))
# T Block
#canvas.draw_image(image2, (27, 180), (54, 36), (300, 300), (54, 36))
# S Block Ghost
canvas.draw_image(image3, (117, 180), (54, 36), (S_Block_X, S_Block_Y_Ghost - S_Block_Flip_Ghost), (54, 36), S_Block_Rotation)
# S Block
canvas.draw_image(image2, (117, 180), (54, 36), (S_Block_X, S_Block_Y), (54, 36), S_Block_Rotation)
# Keys
def keydown(key):
global S_Block_Rotation, S_Block_X, S_Block_X_Vel, S_Block_Y
global freeze
if freeze == 0:
if key == simplegui.KEY_MAP['left']:
reset_timer5()
if S_Block_X > 27:
S_Block_X -= 18
timer2.start()
if key == simplegui.KEY_MAP['right']:
reset_timer5()
if S_Block_X < 153:
S_Block_X += 18
timer3.start()
if key == simplegui.KEY_MAP['up']:
reset_timer5()
# Declare global variable
global S_Block_Flip, S_Block_Flip_Ghost
# Rotate Block by 180
S_Block_Rotation += math.pi / 2
# Prevent Block from rotating out of the map
if S_Block_X == 18:
S_Block_X += 18
if S_Block_Flip == 0:
S_Block_Flip = 1
S_Block_X += 9
S_Block_Y += 9
if S_Block_Y >= 342:
S_Block_Y -= 18
S_Block_Flip_Ghost = 9
elif S_Block_Flip == 1:
if S_Block_X == 171:
S_Block_X -= 18
S_Block_X -= 9
S_Block_Y += 9
S_Block_Flip = 2
S_Block_Flip_Ghost = 0
elif S_Block_Flip == 2:
S_Block_X -= 9
S_Block_Y -= 9
S_Block_Flip = 3
S_Block_Flip_Ghost = 9
elif S_Block_Flip == 3:
# Prevent Block from rotating out of the map
if S_Block_X == 162:
S_Block_X -= 18
S_Block_X +=9
S_Block_Y -= 9
S_Block_Flip = 0
S_Block_Flip_Ghost = 0
if key == simplegui.KEY_MAP['down']:
if S_Block_Y < 325:
S_Block_Y += 18
timer4.start()
if key == simplegui.KEY_MAP['space']:
if S_Block_Flip_Ghost == 0:
S_Block_Y = 342
elif S_Block_Flip_Ghost == 9:
S_Block_Y = 333
freeze = 1
def keyup(key):
global freeze
if freeze == 0 :
if key == simplegui.KEY_MAP['left']:
timer2.stop()
if key == simplegui.KEY_MAP['right']:
timer3.stop()
if key == simplegui.KEY_MAP['down']:
timer4.stop()
frame = simplegui.create_frame('1 Block Tetris', 181, 361)
frame.set_draw_handler(draw_handler)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
timer1 = simplegui.create_timer(1000, timer1_handler)
timer2 = simplegui.create_timer(75, timer2_handler)
timer3 = simplegui.create_timer(75, timer3_handler)
timer4 = simplegui.create_timer(100, timer4_handler)
timer5 = simplegui.create_timer(1000, timer5_handler)
timer1.start()
frame.start()