[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] learning to program tetris



Usually in Python, we use lists instead of arrays.  I'm not really sure
what arrays are good for.  Matrix math or something, i'm guessing.

I can't recommend ChimpLineByLine highly enough for beginners.  You
should really understand that as a baseline.  I understand that you're
new to programming, and Classes and Objects may seem foreign at first,
but it will actually aid you in figuring out the logic.

I've attached the same program, with some changes that you might find
enlightening.  Please feel free to ask questions.

sjbrown

On Mon, 2004-01-19 at 19:43, Jason wrote:
> 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.
> 

> 	
-- 
---------------------------------------------------------------
  Shandy Brown                               Digital Thinkery
  shandy @ geeky.net           http://www.digitalthinkery.com
---------------------------------------------------------------
from Numeric import *
import pygame 
from pygame.locals import *
import random


# load the block sprite and a blank sprite to use as an eraser
# the sprites are 16x16 in size.
class BlockSprite(pygame.sprite.Sprite):
	def __init__(self, piece):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load('block.png').convert()
		self.rect = self.image.get_rect()

		self.piece = piece

		self.status = "FALLING"

	def update(self):
		if self.status == "FALLING":
			self.rect.top  += 1
		if self.rect.bottom >= screenDimensions[1]:
			self.status = "STUCK"
			self.piece.BlockBecameStuck()


	def shiftAcross(self, amount):
		self.rect.left += 16*amount
	def shiftDown(self, amount):
		self.rect.top += 16*amount

class Piece:
	def __init__(self, spriteGroup):
		index = random.randint(0,6)
		self.spec = collection[index]	
		self.blocks = []
		self.status = "FALLING"

		y = 0
		for blockList in self.spec:
			x = 0
			for blockBit in blockList:
				if blockBit == 1:
					b = BlockSprite( self )
					b.shiftAcross( x )
					b.shiftDown( y )
					self.blocks.append( b )
					spriteGroup.add( b )
				x += 1
			y += 1
		
	def BlockBecameStuck(self):
		self.status = "STUCK"

	def shiftAcross(self, amount):
		for block in self.blocks:
			block.shiftAcross( amount )

	def rotate(self, direction):
		newSpec = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
		max = 4
		y = 0
		for blockList in self.spec:
			x = 0
			for blockBit in blockList:
				newSpec[max-1-y][x] = blockBit
				x += 1
			y += 1

		self.spec = newSpec

# the actual game pieces
piece1 = [ [1,1,1,0],[0,1,0,0],[0,0,0,0],[0,0,0,0] ]
piece2 = [ [1,1,1,1],[0,0,0,0],[0,0,0,0],[0,0,0,0] ]
piece3 = [ [1,1,0,0],[1,1,0,0],[0,0,0,0],[0,0,0,0] ]
piece4 = [ [1,1,0,0],[1,0,0,0],[1,0,0,0],[0,0,0,0] ]
piece5 = [ [1,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,0,0] ]
piece6 = [ [1,0,0,0],[1,1,0,0],[0,1,0,0],[0,0,0,0] ]
piece7 = [ [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)

# initialize pygame, create a screen that is 208x416 
pygame.init()
screenDimensions = (208, 416)
screen = pygame.display.set_mode(screenDimensions)

# create a background holding page to work with
background = pygame.Surface(screen.get_size())
background = background.convert()

# select a random gamepiece from the tuple 
activePiece = None

allSprites = pygame.sprite.RenderUpdates()

# main game loop
while 1:
	if activePiece is None or activePiece.status == "STUCK":
		activePiece = Piece( allSprites )

	for event in pygame.event.get():
		if event.type == KEYDOWN and event.key == K_DOWN:
			activePiece.rotate("right")
		if event.type == KEYDOWN and event.key == K_UP:
			activePiece.rotate("left")
		if event.type == KEYDOWN and event.key == K_RIGHT:
			activePiece.shiftAcross( 1 )
		if event.type == KEYDOWN and event.key == K_LEFT:
			activePiece.shiftAcross( -1 )

		elif event.type == QUIT: raise SystemExit
	

	allSprites.clear( screen, background )
	allSprites.update()
	changedRects = allSprites.draw( screen )
	pygame.display.update( changedRects )