Lucas Teske ревизій цього gist 6 years ago. До ревизії
1 file changed, 245 insertions
tetris.py(файл створено)
| @@ -0,0 +1,245 @@ | |||
| 1 | + | #!/usr/bin/python | |
| 2 | + | ||
| 3 | + | from sys import stdout | |
| 4 | + | from PySFML import * | |
| 5 | + | from random import random | |
| 6 | + | import time as timec | |
| 7 | + | import copy | |
| 8 | + | ||
| 9 | + | ''' | |
| 10 | + | _ _ _ _ _ _ _ _ | |
| 11 | + | | | ___| |_ ___ | | | | __ _ ___| | __ (_) |_| | | |
| 12 | + | | | / _ \ __/ __| | |_| |/ _` |/ __| |/ / | | __| | | |
| 13 | + | | |__| __/ |_\__ \ | _ | (_| | (__| < | | |_|_| | |
| 14 | + | |_____\___|\__|___/ |_| |_|\__,_|\___|_|\_\ |_|\__(_) | |
| 15 | + | ||
| 16 | + | http://letshackit.energylabs.com.br | |
| 17 | + | ||
| 18 | + | Lucas Teske | |
| 19 | + | ''' | |
| 20 | + | ||
| 21 | + | emptypiece = [ " ", " ", " ", | |
| 22 | + | " ", " ", " ", | |
| 23 | + | " ", " ", " " ] | |
| 24 | + | ||
| 25 | + | pieces = [ | |
| 26 | + | [ "#", " ", " ", | |
| 27 | + | "#", " ", " ", | |
| 28 | + | "#", "#", "#" ], | |
| 29 | + | ||
| 30 | + | [ "#", "#", " ", | |
| 31 | + | " ", "#", " ", | |
| 32 | + | " ", "#", "#" ], | |
| 33 | + | ||
| 34 | + | [ " ", "#", "#", | |
| 35 | + | " ", "#", " ", | |
| 36 | + | "#", "#", " " ], | |
| 37 | + | ||
| 38 | + | [ " ", "#", " ", | |
| 39 | + | " ", "#", " ", | |
| 40 | + | " ", "#", " " ], | |
| 41 | + | ||
| 42 | + | [ "#", "#", "#", | |
| 43 | + | "#", "#", "#", | |
| 44 | + | "#", "#", "#" ] | |
| 45 | + | ] | |
| 46 | + | ||
| 47 | + | colors = [ | |
| 48 | + | "\033[31m", | |
| 49 | + | "\033[32m", | |
| 50 | + | "\033[33m", | |
| 51 | + | "\033[34m", | |
| 52 | + | "\033[35m", | |
| 53 | + | "\033[36m", | |
| 54 | + | "\033[37m" | |
| 55 | + | ] | |
| 56 | + | ||
| 57 | + | screen_text = ''' | |
| 58 | + | _____ _____ _____ ____ ___ ____ | |
| 59 | + | |_ _| ____|_ _| _ \|_ _/ ___| | |
| 60 | + | | | | _| | | | |_) || |\___ \ | |
| 61 | + | | | | |___ | | | _ < | | ___) | | |
| 62 | + | |_| |_____| |_| |_| \_\___|____/ | |
| 63 | + | ||
| 64 | + | TTTTTTTTTTTXTTTTTTTTTTTTTTTTTTTTTTTTX | |
| 65 | + | T T T | |
| 66 | + | T PPP T T | |
| 67 | + | T PPP T T | |
| 68 | + | T PPP T T | |
| 69 | + | T T T | |
| 70 | + | T T T | |
| 71 | + | T T T | |
| 72 | + | T T T | |
| 73 | + | T T T | |
| 74 | + | T T T | |
| 75 | + | T SCORE T T | |
| 76 | + | T 00000000 T T | |
| 77 | + | T T T | |
| 78 | + | T T T | |
| 79 | + | TTTTTTTTTTTXTTTTTTTTTTTTTTTTTTTTTTTTX | |
| 80 | + | ''' | |
| 81 | + | ctr = screen_text.split("\n") | |
| 82 | + | scr = [ [ (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) ] | |
| 83 | + | scr[0][0] = "\033[0m\033[31m" | |
| 84 | + | scr[6][0] = "\033[0m" | |
| 85 | + | ||
| 86 | + | window = sf.RenderWindow(sf.VideoMode(100, 100), "TETRIS") | |
| 87 | + | input = window.GetInput() | |
| 88 | + | acpxy = [ 0 , -2 ] | |
| 89 | + | actualpiece = (pieces[int(round(random()*(len(pieces)-1)))], colors[int(round(random()*(len(colors)-1)))]) | |
| 90 | + | lasttime = round(timec.time()*1000) | |
| 91 | + | deltatime = 0 | |
| 92 | + | ||
| 93 | + | gamecoord = [ (0,0),(0,0),(0,0),(0,0) ] | |
| 94 | + | x = 0 | |
| 95 | + | y = 0 | |
| 96 | + | xmax = 37 | |
| 97 | + | p = 0 | |
| 98 | + | ||
| 99 | + | screen_base = copy.deepcopy(scr) | |
| 100 | + | ||
| 101 | + | time = round(timec.time()*1000) | |
| 102 | + | ||
| 103 | + | nextpiece = (pieces[int(round(random()*(len(pieces)-1)))], colors[int(round(random()*(len(colors)-1)))]) | |
| 104 | + | ||
| 105 | + | starttime = round(timec.time()*1000) | |
| 106 | + | stdout.write("\033[2J\033[;H") | |
| 107 | + | ||
| 108 | + | def prnscr(screen): | |
| 109 | + | for line in screen: | |
| 110 | + | for column in line: | |
| 111 | + | stdout.write(column) | |
| 112 | + | stdout.write("\n") | |
| 113 | + | ||
| 114 | + | ||
| 115 | + | for 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 | + | ||
| 125 | + | for 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 | + | ||
| 130 | + | def 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 | + | ||
| 142 | + | def 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 | + | ||
| 150 | + | def 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 | + | ||
| 165 | + | def 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 | + | ||
| 170 | + | def 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 | + | ||
| 175 | + | def 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 | + | ||
| 198 | + | def 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 | + | ||
| 211 | + | while 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) | |
Новіше
Пізніше