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/def newPiece(self):self.curPiece = self.nextPiecestatusbar = self.GetParent().statusbarself.nextPiece.setRandomShape()self.curX = Board.BoardWidth / 2 + 1self.curY = Board.BoardHeight - 1 + self.curPiece.minY()if not self.tryMove(self.curPiece, self.curX, self.curY):self.curPiece.setShape(Tetrominoes.NoShape)self.timer.Stop()self.isStarted = Falsestatusbar.SetStatusText('Game over')<strong>The</strong> newPiece() method creates randomly a new tetris piece. If thepiece cannot go into it's initial position, the game is over.<strong>The</strong> Shape class saves information about the tetris piece.self.coords = [[0,0] for i in range(4)]Upon creation we create an empty coordinates list. <strong>The</strong> list will savethe coordinates of the tetris piece. For example, these tuples (0, -1),(0, 0), (1, 0), (1, 1) represent a rotated S-shape. <strong>The</strong> followingdiagram illustrates the shape.Figure: CoordinatesWhen we draw the current falling piece, we draw it at self.curX,self.curY position. <strong>The</strong>n we look at the coordinates table and drawall the four squares.11 de 12 27/04/2008 1:10

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

Saved successfully!

Ooh no, something went wrong!