HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Self-playing Tetris game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
playinggametetrisself

Problem

I have an extremely simple self-playing Tetris game which I coded up and I am looking to see how it could be improved. It would also be a learning curve for me to see how those much better than I am would go about improving the code.

```
###############################################################################
## an implementation of a very basic Tetris game in Python using Pygame
###############################################################################
'''rotate --- r
pause ---- p
direction buttons for movement'''

import sys
import copy
import pygame
import random

size = width, height = 200, 400
sqrsize, pen_size = 20, 1
occupied_squares = []
top_of_screen = (0, 0)
color = {'white':(255, 255, 255)}
top_x, top_y = top_of_screen[0], top_of_screen[1]

pygame.init()
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((color['white']))
screen.blit(background, top_of_screen)
pygame.display.flip()

################################################################################
#constructors and selectors for a tetrominoe shape
################################################################################
def make_tetrominoe(block1, block2, block3, block4, name):
"""Inputsa block
function which takes a block and increments its POINT"""
point = get_point(a_block)
return (make_block(make_point(point_x(point)+delta_x,
point_y(point)+delta_y, point_color(point)),
block_width(a_block), block_height(a_block)))

###############################################################################
## game controller
###############################################################################
def tetris():
"""Sets up the whole game play and handles event handling"""
mov_delay = 150
events = {276: 'left', 275: 'right', 112: 'pause'}

while True:
move_dir = 'down' #default mo

Solution

A few general comments:

-
Python constants should be capitals, for e.g width, height etc should be capitalized in your code since they are constants (declared at the beginning).

-
The places where you have indicated "constructors and selectors" probably indicate that they aught to be in a separate class.

-
As a general thumb rule, more than one loop statement in a method is probably indicating that the method can be refactored. The method "tetris" is ripe for refactoring.

-
Same way, I would say more than 3 levels of indentation is probably a code-smell.

-
The method "tetrominoe_shape" may be refactored rather heavily. The difference between each shapes seems to be just one or two numbers. They may be factored out.

-
Try to factor out the objects for block,shape,point, controller etc, and your code will become much more tractable.

-
Also add a bit of information as to what each method does. Right now, it is rather hard to figure out what each method does.

Context

StackExchange Code Review Q#12175, answer score: 6

Revisions (0)

No revisions yet.