Welcome, guest | Sign In | My Account | Store | Cart

Just the game of Tetris, implemented in Python some years ago. Contains some fancy add-ons.

Python, 275 lines
  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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
"tetris -- a brand new game written in python by Alfe"

import sys, random, time, select, os, termios

width = 10
height = 22

blocks = [ [ (0,0), (0,1),  (0,-1),  (1,0)  ],  # T
           [ (0,0), (0,1),  (0,2),   (0,-1) ],  # I
           [ (0,0), (0,1),  (1,1),  (-1,0)  ],  # S
           [ (0,0), (0,-1), (1,-1), (-1,0)  ],  # Z
           [ (0,0), (0,1),  (1,1),   (1,0)  ],  # O
           [ (0,0), (-1,1), (-1,0),  (1,0)  ],  # L
           [ (0,0), (1,1),  (-1,0),  (1,0)  ],  # J
           ]

inverted     = '\033[7;1m'
blue         = '\033[7;34m'
normal       = '\033[0m'
clear_screen = '\033[2J'  # clear the screen
home         = '\033[H'   # goto top left corner of the screen
# (the latter two were found using 'clear | od -c')

empty = '  '
black = inverted + '  ' + normal  # two inverted spaces
blue  = blue     + '  ' + normal  # two inverted spaces
floor = '=='

left  = 'left'
right = 'right'
turn  = 'turn'
down  = 'down'
quit  = 'quit'

shaft = None

def play_tetris():  
    initialize_shaft()
    while True:  # until game is lost
        block = get_random_block()
        coordinates = (width/2-1, 1)  # in the middle at the top
        if not place_block(block, coordinates, blue):  # collision already?
            return  # game is lost!
        next_fall_time = time.time() + fall_delay()
        # ^^^ this is the time when the block will fall automatically
        #     one line down
        while True:  # until block is placed fixedly
            print_shaft()
            remove_block(block, coordinates)
            x, y = coordinates
            try:
                try:
                    command = get_command(next_fall_time)
                except Timeout:  # no command given
                    raise Fall()
                else:  # no exception, so process command:
                    if   command == left:
                        new_coordinates = (x-1, y)
                        new_block = block
                    elif command == right:
                        new_coordinates = (x+1, y)
                        new_block = block
                    elif command == turn:
                        new_coordinates = (x, y)
                        new_block = turn_block(block)
                    elif command == down:
                        raise Fall()
                    elif command == quit:
                        return
                    else:
                        raise Exception("internal error: %r" % command)
                    if place_block(new_block, new_coordinates,
                                   blue):  # command ok?
                        # execute the command:
                        block       = new_block
                        coordinates = new_coordinates
                    else:
                        place_block(block, coordinates, blue)
                        # ignore the command which could not be executed
                        # maybe beep here or something ;->
            except Fall:  
                # make the block fall automatically:
                new_coordinates = (x, y+1)
                next_fall_time = time.time() + fall_delay()
                if place_block(block, new_coordinates, blue):  # can be placed?
                    coordinates = new_coordinates
                else:
                    place_block(block, coordinates,
                                black)  # place block there again
                    break               # and bail out
        remove_full_lines()

class Timeout(Exception):  pass
class    Fall(Exception):  pass

def remove_full_lines():               
    global shaft, width, height
    def line_full(line):  
        global width
        for x in range(width):
            if line[x] == empty:
                return False
        return True
    
    def remove_line(y):   
        global shaft, width
        del shaft[y]  # cut out line
        shaft.insert(0, [ empty ] * width)  # fill up with an empty line
    
    for y in range(height):  
        if line_full(shaft[y]):
            remove_line(y)

def fall_delay():                      
    return 1.3  # cheap version; implement raising difficulty here

def turn_block(block):                 
    "return a turned copy(!) of the given block"
    result = []
    for x, y in block:
        result.append((y, -x))
    return result

def get_command(next_fall_time):       
    "if a command is entered, return it; otherwise raise the exception Timeout"
    while True:  # until a timeout occurs or a command is found:
        timeout = next_fall_time - time.time()
        if timeout > 0.0:
            (r, w, e) = select.select([ sys.stdin ], [], [], timeout)
        else:
            raise Timeout()
        if sys.stdin not in r:  # not input on stdin?
            raise Timeout()
        key = os.read(sys.stdin.fileno(), 1)
        if   key == 'j':
            return left
        elif key == 'l':
            return right
        elif key == 'k':
            return turn
        elif key == ' ':
            return down
        elif key == 'q':
            return quit
        else:  # any other key:  ignore
            pass

def place_block(block, coordinates, color):  
    "if the given block can be placed in the shaft at the given coordinates"\
    " then place it there and return True; return False otherwise and do not"\
    " place anything"
    global shaft, width, height
    block_x, block_y = coordinates
    for stone_x, stone_y in block:
        x = block_x + stone_x
        y = block_y + stone_y
        if (x < 0 or x >= width or
            y < 0 or y >= height or  # border collision?
            shaft[y][x] != empty):   # block collision?
            return False  # cannot be placed there
    # reached here?  ==> can be placed there
    # now really place it:
    for stone_x, stone_y in block:
        x = block_x + stone_x
        y = block_y + stone_y
        shaft[y][x] = color
    return True

def remove_block(block, coordinates):  
    global shaft
    block_x, block_y = coordinates
    for stone_x, stone_y in block:
        x = block_x + stone_x
        y = block_y + stone_y
        shaft[y][x] = empty

def get_random_block():                
    if random.randint(1, 10) == 1:
        return perfect_block() or random.choice(blocks)
    return random.choice(blocks)

def perfect_block():
    result = []
    for y in range(height):
        if filter(lambda b: b != empty, shaft[y]):  # found summit
            random_order = range(width)
            random.shuffle(random_order)
            for x in random_order:
                if shaft[y][x] == empty:  # found space besides summit
                    for x_ in range(width-x):  # fill to the right
                        if shaft[y][x+x_] != empty:
                            break
                        for y_ in range(height-y):
                            if shaft[y+y_][x+x_] == empty:
                                result.append((x_, y_))
                            else:
                                break
                    for x_ in range(-1, -x-1, -1):  # fill to the left
                        if shaft[y][x+x_] != empty:
                            break
                        for y_ in range(height-y):
                            if shaft[y+y_][x+x_] == empty:
                                result.append((x_, y_))
                            else:
                                break
                    # shift block in x direction to center it:
                    xmin = min(map(lambda v: v[0], result))
                    xmax = max(map(lambda v: v[0], result))
                    return map(lambda v: (v[0]-(xmax+xmin)/2, v[1]), result)
    return None

def initialize_shaft():                
    global width, height, shaft, empty
    shaft = [ None ] * height
    for y in range(height):
        shaft[y] = [ empty ] * width

def print_shaft():                     
    # cursor-goto top left corner:
    sys.stdout.write(home)
    for y in range(height):  
        if y > 3:  # does this line have a border?  (the topmost ones do not)
            sys.stdout.write(']')
        else:
            sys.stdout.write(' ')
        for x in range(width):
            sys.stdout.write(shaft[y][x])
        if y > 3:  # does this line have a border?  (the topmost ones do not)
            sys.stdout.write('[\n')
        else:
            sys.stdout.write('\n')
    
    # print bottom:
    sys.stdout.write(']' + floor * width + '[\n')

def prepare_tty():                       
    "set the terminal in char mode (return each keyboard press at once) and"\
    " switch off echoing of this input; return the original settings"
    stdin_fd = sys.stdin.fileno()  # will most likely be 0  ;->
    old_stdin_config = termios.tcgetattr(stdin_fd)
    [ iflag, oflag, cflag, lflag, ispeed, ospeed, cc ] = \
        termios.tcgetattr(stdin_fd)
    cc[termios.VTIME] = 1
    cc[termios.VMIN] = 1
    iflag = iflag & ~(termios.IGNBRK |
                      termios.BRKINT |
                      termios.PARMRK |
                      termios.ISTRIP |
                      termios.INLCR |
                      termios.IGNCR |
                      #termios.ICRNL |
                      termios.IXON)
    #  oflag = oflag & ~termios.OPOST
    cflag = cflag | termios.CS8
    lflag = lflag & ~(termios.ECHO |
                      termios.ECHONL |
                      termios.ICANON |
                      # termios.ISIG |
                      termios.IEXTEN)
    termios.tcsetattr(stdin_fd, termios.TCSANOW,
                      [ iflag, oflag, cflag, lflag, ispeed, ospeed, cc ])
    return (stdin_fd, old_stdin_config)

def cleanup_tty(original_tty_settings):  
    "restore the original terminal settings"
    stdin_fd, old_stdin_config = original_tty_settings
    termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_stdin_config)

original_tty_settings = prepare_tty()  # switch off line buffering etc.
sys.stdout.write(clear_screen)
try:  # ensure that tty will be reset in the end
    play_tetris()
finally:
    cleanup_tty(original_tty_settings)

1 comment

Grant Jenks 6 years, 9 months ago  # | flag

Classic game! I am impressed you implemented it in the console using text. That looks challenging. Did you consider using something like Tkinter or even the Turtle graphics package? I wrote a number of Free Python Games for education at http://www.grantjenks.com/docs/freegames/ using Turtle. You might check that for ideas.

Created by Alfe on Thu, 9 Jun 2016 (MIT)
Python recipes (4591)
Alfe's recipes (12)

Required Modules

  • (none specified)

Other Information and Tasks