[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] learning to program tetris
- To: pygame-users@seul.org
- Subject: [pygame] learning to program tetris
- From: Jason <jason@dr01d.com>
- Date: Mon, 19 Jan 2004 18:43:26 -0600
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Mon, 19 Jan 2004 19:39:08 -0500
- Reply-to: pygame-users@seul.org
- Sender: owner-pygame-users@seul.org
- User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4
Hi everyone.
I'm new to pygame/programming and I am trying to learn.
I'm starting with a tetris clone and I have run into some problems.
Specifically, I don't know the best way to erase blocks, or how to get
the pieces to "fit" together.
I imagine that the playing board in my code needs to be an array, the
pieces are checked against the array for filled blocks and stop if they
run into one.
I've reviewed the code on the pygame site, a lot of it is helpful, I
just wish there were more examples and tutorials.
I realize that making this object oriented would be a million times
better, but I'm a newbie, and I'd like to figure out the logic of what
is going on before I jump into trying to make it object oriented.
Any help would be greatly appreciated. Has anyone got some good, easy to
follow, sourcecode for newbies (that's not already on pygame's site)?
I've attached my code (hope that's okay). It was written on a windows
machine (yuck) so there might be issues with linefeeds / carriage returns.
from Numeric import *
import pygame
from pygame.locals import *
import random
# the actual game pieces
piece1 = array([[1,1,1,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]])
piece2 = array([[1,1,1,1],[0,0,0,0],[0,0,0,0],[0,0,0,0]])
piece3 = array([[1,1,0,0],[1,1,0,0],[0,0,0,0],[0,0,0,0]])
piece4 = array([[1,1,0,0],[1,0,0,0],[1,0,0,0],[0,0,0,0]])
piece5 = array([[1,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,0,0]])
piece6 = array([[1,0,0,0],[1,1,0,0],[0,1,0,0],[0,0,0,0]])
piece7 = array([[0,1,0,0],[1,1,0,0],[1,0,0,0],[0,0,0,0]])
# a tuple of the game pieces allowing for random selection
collection = (piece1,piece2,piece3,piece4,piece5,piece6,piece7)
# initial position of each block
x_position = 5
y_position = 0
# initialize pygame, create a screen that is 208x416
pygame.init()
screen = pygame.display.set_mode((208,416))
# create a background holding page to work with
background = pygame.Surface(screen.get_size())
background = background.convert()
# load the block sprite and a blank sprite to use as an eraser
# the sprites are 16x16 in size.
block = pygame.image.load('block.png').convert()
blank = pygame.image.load('blank.png').convert() #this is just a sprite filled with the color black
# function to rotate the current piece clockwise or counterclockwise depending on keypressed
# returns the resulting array with the data of the piece rotated
def rotate(piece,direction):
# a temporary blank piece used as a holder for rotation
temp = zeros((4,4))
max = 4
if direction == "right":
for y in range(max):
for x in range(max):
temp[max-1-y][x]=piece[x][y]
else:
for y in range(max):
for x in range(max):
temp[y][max-1-x]=piece[x][y]
return temp
# a function to update the current piece on the playing board.
# takes an argument that is the array of data representing the current piece, and the x_position, y_position of the piece on the board.
def update_piece(piece,x_position,y_position):
max = 4
# erase the old piece by recreating it with the blank sprite
for y in range(max):
for x in range(max):
background.blit(blank,(x*16+(x_position*16),y*16+((y_position-.01)*16)))
# really draw the piece this time
for y in range(max):
for x in range(max):
if piece[x][y] == 1:
background.blit(block,(x*16+(x_position*16),y*16+(y_position*16)))
# flip the background to the foreground
screen.blit(background, (0, 0))
pygame.display.flip()
# create a random seed number in range of 0 - 6
seed = random.randint(0,6)
# select a random gamepiece from the tuple
activeblock = collection[seed]
# main game loop
while 1:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_DOWN:
activeblock = rotate(activeblock,"right")
update_piece(activeblock,x_position,y_position)
if event.type == KEYDOWN and event.key == K_UP:
activeblock = rotate(activeblock,"left")
update_piece(activeblock,x_position,y_position)
if event.type == KEYDOWN and event.key == K_RIGHT:
x_position = x_position + 1
update_piece(activeblock,x_position,y_position)
if event.type == KEYDOWN and event.key == K_LEFT:
x_position = x_position - 1
update_piece(activeblock,x_position,y_position)
elif event.type == QUIT: raise SystemExit
if y_position <= 22:
y_position = y_position+.01
else:
y_position = 0
seed = random.randint(0,6)
activeblock = collection[seed]
update_piece(activeblock,x_position,y_position)