13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> Tetris game in <strong>wxPython</strong>http://www.zetcode.com/wxpython/thetetrisgame/if self.pieceShape == Tetrominoes.SquareShape:return selfresult = Shape()result.pieceShape = self.pieceShapefor i in range(4):result.setX(i, self.y(i))result.setY(i, -self.x(i))return resultdef rotatedRight(self):if self.pieceShape == Tetrominoes.SquareShape:return selfresult = Shape()result.pieceShape = self.pieceShapefor i in range(4):result.setX(i, -self.y(i))result.setY(i, self.x(i))return resultapp = wx.App()Tetris(None, -1, 'Tetris')app.MainLoop()I have simplified the game a bit, so that it is easier to understand.<strong>The</strong> game starts immediately, after it is launched. We can pause thegame by pressing the p key. <strong>The</strong> space key will drop the tetris pieceimmediately to the bottom. <strong>The</strong> d key will drop the piece one linedown. (It can be used to speed up the falling a bit.) <strong>The</strong> game goesat constant speed, no acceleration is implemented. <strong>The</strong> score is thenumber of lines, that we have removed....self.curX = 0self.curY = 0self.numLinesRemoved = 0self.board = []...Before we start the game cycle, we initialize some importantvariables. <strong>The</strong> self.board variable is a list of numbers from 0 ... 7. Itrepresents the position of various shapes and remains of the shapeson the board.for i in range(Board.BoardHeight):for j in range(Board.BoardWidth):shape = self.shapeAt(j, Board.BoardHeight - i - 1)if shape != Tetrominoes.NoShape:self.drawSquare(dc,8 de 12 27/04/2008 1:10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!