13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>The</strong> Tetris game in <strong>wxPython</strong>http://www.zetcode.com/wxpython/thetetrisgame/0 + j * self.squareWidth(),boardTop + i * self.squareHeight(), shape)<strong>The</strong> painting of the game is divided into two steps. In the first step,we draw all the shapes, or remains of the shapes, that have beendropped to the bottom of the board. All the squares are rememberdin the self.board list variable. We access it using the shapeAt()method.if self.curPiece.shape() != Tetrominoes.NoShape:for i in range(4):x = self.curX + self.curPiece.x(i)y = self.curY - self.curPiece.y(i)self.drawSquare(dc, 0 + x * self.squareWidth(),boardTop + (Board.BoardHeight - y - 1) * self.squareHeight(),self.curPiece.shape())<strong>The</strong> next step is drawing of the actual piece, that is falling down.elif keycode == wx.WXK_LEFT:self.tryMove(self.curPiece, self.curX - 1, self.curY)In the OnKeyDown() method we check for pressed keys. If wepress the left arrow key, we try to move the piece to the left. We saytry, because the piece might not be able to move.def tryMove(self, newPiece, newX, newY):for i in range(4):x = newX + newPiece.x(i)y = newY - newPiece.y(i)if x < 0 or x >= Board.BoardWidth or y < 0 or y >= Board.BoardHeight:return Falseif self.shapeAt(x, y) != Tetrominoes.NoShape:return Falseself.curPiece = newPieceself.curX = newXself.curY = newYself.Refresh()return TrueIn the tryMove() method we try to move our shapes. If the shape isat the edge of the board or is adjacent to some other piece, wereturn false. Otherwise we place the current falling piece to a newposition and return true.9 de 12 27/04/2008 1:10

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

Saved successfully!

Ooh no, something went wrong!