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/<strong>The</strong> tetrominoes are drawn<strong>The</strong> shapes move on a square by square basis (not pixel bypixel)Mathematically a board is a simple list of numbers<strong>The</strong> following example is a modified version of the tetris game,available with PyQt4 installation files.#!/usr/bin/python# tetris.pyimport wximport randomclass Tetris(wx.Frame):def __init__(self, parent, id, title):wx.Frame.__init__(self, parent, id, title, size=(180, 380))self.statusbar = self.CreateStatusBar()self.statusbar.SetStatusText('0')self.board = Board(self)self.board.SetFocus()self.board.start()self.Centre()self.Show(True)class Board(wx.Panel):BoardWidth = 10BoardHeight = 22Speed = 300ID_TIMER = 1def __init__(self, parent):wx.Panel.__init__(self, parent)self.timer = wx.Timer(self, Board.ID_TIMER)self.isWaitingAfterLine = Falseself.curPiece = Shape()self.nextPiece = Shape()self.curX = 0self.curY = 0self.numLinesRemoved = 0self.board = []self.isStarted = Falseself.isPaused = Falseself.Bind(wx.EVT_PAINT, self.OnPaint)self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)self.Bind(wx.EVT_TIMER, self.OnTimer, id=Board.ID_TIMER)self.clearBoard()def shapeAt(self, x, y):2 de 12 27/04/2008 1:10

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

Saved successfully!

Ooh no, something went wrong!