Naposledy aktivní 1 month ago

A ancient code that I did when I was at university. A friend reminded about it. It was posted ib my tumblr: http://letshackit.energylabs.com.br/?fbclid=IwAR2-rzRJeC39HP48EPAk4EvLQfe_LKgbQMcQIiHjrYnqMRsYyPXw_yNAX1k

Revize eb79ffd3b12ed1e9a954403a8916f22648b37621

tetris.py Raw
1#!/usr/bin/python
2
3from sys import stdout
4from PySFML import *
5from random import random
6import time as timec
7import copy
8
9'''
10 _ _ _ _ _ _ _ _
11| | ___| |_ ___ | | | | __ _ ___| | __ (_) |_| |
12| | / _ \ __/ __| | |_| |/ _` |/ __| |/ / | | __| |
13| |__| __/ |_\__ \ | _ | (_| | (__| < | | |_|_|
14|_____\___|\__|___/ |_| |_|\__,_|\___|_|\_\ |_|\__(_)
15
16http://letshackit.energylabs.com.br
17
18Lucas Teske
19'''
20
21emptypiece = [ " ", " ", " ",
22 " ", " ", " ",
23 " ", " ", " " ]
24
25pieces = [
26 [ "#", " ", " ",
27 "#", " ", " ",
28 "#", "#", "#" ],
29
30 [ "#", "#", " ",
31 " ", "#", " ",
32 " ", "#", "#" ],
33
34 [ " ", "#", "#",
35 " ", "#", " ",
36 "#", "#", " " ],
37
38 [ " ", "#", " ",
39 " ", "#", " ",
40 " ", "#", " " ],
41
42 [ "#", "#", "#",
43 "#", "#", "#",
44 "#", "#", "#" ]
45]
46
47colors = [
48 "\033[31m",
49 "\033[32m",
50 "\033[33m",
51 "\033[34m",
52 "\033[35m",
53 "\033[36m",
54 "\033[37m"
55]
56
57screen_text = '''
58 _____ _____ _____ ____ ___ ____
59|_ _| ____|_ _| _ \|_ _/ ___|
60 | | | _| | | | |_) || |\___ \
61 | | | |___ | | | _ < | | ___) |
62 |_| |_____| |_| |_| \_\___|____/
63
64TTTTTTTTTTTXTTTTTTTTTTTTTTTTTTTTTTTTX
65T T T
66T PPP T T
67T PPP T T
68T PPP T T
69T T T
70T T T
71T T T
72T T T
73T T T
74T T T
75T SCORE T T
76T 00000000 T T
77T T T
78T T T
79TTTTTTTTTTTXTTTTTTTTTTTTTTTTTTTTTTTTX
80'''
81ctr = screen_text.split("\n")
82scr = [ [ (ctr[y][x] if (len(ctr) > (y) ) and (len(ctr[y]) > x) and not "P" in ctr[y][x] else " ") for x in range(38) ] for y in range(23) ]
83scr[0][0] = "\033[0m\033[31m"
84scr[6][0] = "\033[0m"
85
86window = sf.RenderWindow(sf.VideoMode(100, 100), "TETRIS")
87input = window.GetInput()
88acpxy = [ 0 , -2 ]
89actualpiece = (pieces[int(round(random()*(len(pieces)-1)))], colors[int(round(random()*(len(colors)-1)))])
90lasttime = round(timec.time()*1000)
91deltatime = 0
92
93gamecoord = [ (0,0),(0,0),(0,0),(0,0) ]
94x = 0
95y = 0
96xmax = 37
97p = 0
98
99screen_base = copy.deepcopy(scr)
100
101time = round(timec.time()*1000)
102
103nextpiece = (pieces[int(round(random()*(len(pieces)-1)))], colors[int(round(random()*(len(colors)-1)))])
104
105starttime = round(timec.time()*1000)
106stdout.write("\033[2J\033[;H")
107
108def prnscr(screen):
109 for line in screen:
110 for column in line:
111 stdout.write(column)
112 stdout.write("\n")
113
114
115for line in scr:
116 for char in line:
117 if char == "X":
118 gamecoord[p] = (x,y)
119 p = p + 1
120 x = x + 1
121 if x > xmax:
122 x = 0
123 y = y + 1
124
125for y in range(len(screen_base)):
126 for x in range(len(screen_base[y])):
127 if "X" in screen_base[y][x] or screen_base[y][x] == "T":
128 screen_base[y][x] = "\033[107mO\033[0m"
129
130def SetXY( screen , x , y , char, color = None ):
131 y = y + gamecoord[0][1]
132 x = x + gamecoord[0][0] + 1
133
134 if y < len(screen) and x < len(screen[0]) and x > 1 and y > 7:
135 if not screen[y][x] == ['O']:
136 if not color == None:
137 screen[y][x] = "%s%s%s" % (color, char, "\033[0m")
138 else:
139 screen[y][x] = char
140 return screen
141
142def SetRawXY( screen, x, y, char, color=None):
143 if y < len(screen) and x < len(screen[0]):
144 if not color == None:
145 screen[y][x] = "%s%s%s" % (color, char, "\033[0m")
146 else:
147 screen[y][x] = char
148 return screen
149
150def WritePiece(screen, x, y, piece, f=SetXY, color=None, Force=False):
151 x = x
152 y = y
153 ax = 0
154 ay = 0
155 for c in piece:
156 if ((y+ay >= 0) or (x+ax > 0)) and not (c ==' ' and not Force):
157 f(screen, x+ax, y+ay, c, color)
158 if ax == 2:
159 ay = ay + 1
160 ax = 0
161 else:
162 ax = ax + 1
163 return screen
164
165def RotateRight(piece):
166 return [ piece[6], piece[3], piece[0],
167 piece[7], piece[4], piece[1],
168 piece[8], piece[5], piece[2] ]
169
170def RotateLeft(piece):
171 return [ piece[2], piece[5], piece[8],
172 piece[1], piece[4], piece[7],
173 piece[0], piece[3], piece[6] ]
174
175def CheckPieceCollision(scr, acpxy):
176 '''
177 0 1 2
178 3 4 5
179 6 7 8
180 (px , py ) (px+1 , py ) (px+2 , py )
181 (px , py+1) (px+1 , py+1) (px+2 , py+1)
182 (px , py+2) (px+1 , py+2) (px+2 , py+2)
183
184 '''
185 px = int(acpxy[0]) + gamecoord[0][0]+1
186 py = int(acpxy[1]) + gamecoord[0][1]+1
187 print (px,py,acpxy)
188 return (( not scr[py][px] == " " and not ( scr[py][px] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][0] )) or \
189 (( not scr[py][px+1] == " " and not ( scr[py][px+1] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][1] )) or \
190 (( not scr[py][px+2] == " " and not ( scr[py][px+2] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][2] )) or \
191 (( not scr[py+1][px] == " " and not ( scr[py+1][px] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][3] )) or \
192 (( not scr[py+1][px+1] == " " and not ( scr[py+1][px+1] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][4] )) or \
193 (( not scr[py+1][px+2] == " " and not ( scr[py+1][px+2] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][5] )) or \
194 (( not scr[py+2][px] == " " and not ( scr[py+2][px] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][6] )) or \
195 (( not scr[py+2][px+1] == " " and not ( scr[py+2][px+1] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][7] )) or \
196 (( not scr[py+2][px+2] == " " and not ( scr[py+2][px+2] == "O" and (acpxy[1] <= 4) ) ) and ("#" in actualpiece[0][8] ))
197
198def CheckLineBreak(scr):
199 '''
200 x : 0 -> 24
201 y : 0 -> 13
202 '''
203 lbreak = [ False for i in range(13) ]
204 for y in range(24):
205 lbreak[y] = True
206 for x in range(13):
207 lbreak[y] = lbreak[y] and ("#" in scr[y][x])
208
209
210
211while True:
212 deltatime = round(timec.time()*1000) - lasttime
213 lasttime = round(timec.time()*1000)
214 keypressed = False
215 event = sf.Event()
216 while window.GetEvent(event):
217 if event.Type == sf.Event.KeyPressed:
218 # Process Keyboard P1 Inputs
219 keypressed = True
220 if event.Key.Code == sf.Key.Left:
221 acpxy[0] = acpxy[0] - 1 if (acpxy[0] > 0) and not (CheckPieceCollision(screen_base, [acpxy[0]-1, acpxy[1]] )) else acpxy[0]
222 if event.Key.Code == sf.Key.Right:
223 acpxy[0] = acpxy[0] + 1 if (acpxy[0] < 21) and not (CheckPieceCollision(screen_base, [acpxy[0]+1, acpxy[1]] )) else acpxy[0]
224 if event.Key.Code == sf.Key.Z:
225 actualpiece = (RotateRight(actualpiece[0]),actualpiece[1])
226
227 screen = copy.deepcopy(screen_base)
228 screen = WritePiece(screen, 4, 9, emptypiece, SetRawXY, None, True)
229 screen = WritePiece(screen, 4, 9, nextpiece[0], SetRawXY, nextpiece[1])
230 screen = WritePiece(screen, int(acpxy[0]) , int(acpxy[1]), actualpiece[0], SetXY, actualpiece[1])
231 stdout.write("\033[2J\033[;H\033[0m")
232
233 prnscr(screen)
234
235 stdout.flush()
236 window.Display()
237 if CheckPieceCollision(screen_base, acpxy):
238 screen_base = WritePiece(screen, int(acpxy[0]) , int(acpxy[1]), actualpiece[0], SetXY, actualpiece[1])
239 acpxy = [ 0 , 0 ]
240 actualpiece = nextpiece
241 nextpiece = (pieces[int(round(random()*(len(pieces)-1)))], colors[int(round(random()*(len(colors)-1)))])
242 else:
243 acpxy[1] = acpxy[1] + deltatime / 300.0
244
245 timec.sleep(0.1)