俄罗斯方块

俄罗斯方块,第1张

概述俄罗斯方块

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#!/usr/bin/python# tetris.pyimport wximport randomclass Tetris(wx.Frame):    def __init__(self,parent,ID,Title):        wx.Frame.__init__(self,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 = 10    BoardHeight = 22    Speed = 300    ID_TIMER = 1    def __init__(self,parent):        wx.Panel.__init__(self,parent)        self.timer = wx.Timer(self,Board.ID_TIMER)        self.isWaitingAfterline = False        self.curPIEce = Shape()        self.nextPIEce = Shape()        self.curX = 0        self.curY = 0        self.numlinesRemoved = 0        self.board = []        self.isstarted = False        self.isPaused = False        self.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):        return self.board[(y * Board.BoarDWIDth) + x]    def setShapeAt(self,y,shape):        self.board[(y * Board.BoarDWIDth) + x] = shape    def squareWIDth(self):        return self.GetClIEntSize().GetWIDth() / Board.BoarDWIDth    def squareHeight(self):        return self.GetClIEntSize().GetHeight() / Board.BoardHeight    def start(self):        if self.isPaused:            return        self.isstarted = True        self.isWaitingAfterline = False        self.numlinesRemoved = 0        self.clearBoard()        self.newPIEce()        self.timer.Start(Board.Speed)    def pause(self):        if not self.isstarted:            return        self.isPaused = not self.isPaused        statusbar = self.GetParent().statusbar        if self.isPaused:            self.timer.Stop()            statusbar.SetStatusText('paused')        else:            self.timer.Start(Board.Speed)            statusbar.SetStatusText(str(self.numlinesRemoved))        self.Refresh()    def clearBoard(self):        for i in range(Board.BoardHeight * Board.BoarDWIDth):            self.board.append(Tetrominoes.NoShape)    def OnPaint(self,event):        dc = wx.PaintDC(self)                size = self.GetClIEntSize()        boardtop = size.GetHeight() - Board.BoardHeight * self.squareHeight()                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,0 + j * self.squareWIDth(),boardtop + i * self.squareHeight(),shape)        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())    def OnKeyDown(self,event):        if not self.isstarted or self.curPIEce.shape() == Tetrominoes.NoShape:            event.Skip()            return        keycode = event.GetKeyCode()        if keycode == ord('P') or keycode == ord('p'):            self.pause()            return        if self.isPaused:            return        elif keycode == wx.WXK_left:            self.tryMove(self.curPIEce,self.curX - 1,self.curY)        elif keycode == wx.WXK_RIGHT:            self.tryMove(self.curPIEce,self.curX + 1,self.curY)        elif keycode == wx.WXK_DOWN:            self.tryMove(self.curPIEce.rotatedRight(),self.curX,self.curY)        elif keycode == wx.WXK_UP:            self.tryMove(self.curPIEce.rotatedleft(),self.curY)        elif keycode == wx.WXK_SPACE:            self.dropDown()        elif keycode == ord('D') or keycode == ord('d'):            self.onelineDown()        else:            event.Skip()    def OnTimer(self,event):        if event.GetID() == Board.ID_TIMER:            if self.isWaitingAfterline:                self.isWaitingAfterline = False                self.newPIEce()            else:                self.onelineDown()        else:            event.Skip()    def dropDown(self):        newY = self.curY        while newY > 0:            if not self.tryMove(self.curPIEce,newY - 1):                break            newY -= 1        self.pIEceDropped()    def onelineDown(self):        if not self.tryMove(self.curPIEce,self.curY - 1):            self.pIEceDropped()    def pIEceDropped(self):        for i in range(4):            x = self.curX + self.curPIEce.x(i)            y = self.curY - self.curPIEce.y(i)            self.setShapeAt(x,self.curPIEce.shape())        self.removeFulllines()        if not self.isWaitingAfterline:            self.newPIEce()    def removeFulllines(self):        numFulllines = 0        statusbar = self.GetParent().statusbar        rowsToRemove = []        for i in range(Board.BoardHeight):            n = 0            for j in range(Board.BoarDWIDth):                if not self.shapeAt(j,i) == Tetrominoes.NoShape:                    n = n + 1            if n == 10:                rowsToRemove.append(i)        rowsToRemove.reverse()        for m in rowsToRemove:            for k in range(m,Board.BoardHeight):                for l in range(Board.BoarDWIDth):                        self.setShapeAt(l,k,self.shapeAt(l,k + 1))            numFulllines = numFulllines + len(rowsToRemove)            if numFulllines > 0:                self.numlinesRemoved = self.numlinesRemoved + numFulllines                statusbar.SetStatusText(str(self.numlinesRemoved))                 self.isWaitingAfterline = True                self.curPIEce.setShape(Tetrominoes.NoShape)                self.Refresh()    def newPIEce(self):        self.curPIEce = self.nextPIEce        statusbar = self.GetParent().statusbar        self.nextPIEce.setRandomShape()        self.curX = Board.BoarDWIDth / 2 + 1        self.curY = Board.BoardHeight - 1 + self.curPIEce.minY()        if not self.tryMove(self.curPIEce,self.curY):            self.curPIEce.setShape(Tetrominoes.NoShape)            self.timer.Stop()            self.isstarted = False            statusbar.SetStatusText('Game over')    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 False            if self.shapeAt(x,y) != Tetrominoes.NoShape:                return False        self.curPIEce = newPIEce        self.curX = newX        self.curY = newY        self.Refresh()        return True    def drawSquare(self,dc,shape):        colors = ['#000000','#CC6666','#66CC66','#6666CC','#CCCC66','#CC66CC','#66CCCC','#DAAA00']        light = ['#000000','#F89FAB','#79FC79','#7979FC','#FCFC79','#FC79FC','#79FCFC','#FCC600']        dark = ['#000000','#803C3B','#3B803B','#3B3B80','#80803B','#803B80','#3B8080','#806200']        pen = wx.Pen(light[shape])        pen.SetCap(wx.CAP_PROJECTING)        dc.SetPen(pen)        dc.Drawline(x,y + self.squareHeight() - 1,y)        dc.Drawline(x,x + self.squareWIDth() - 1,y)        darkpen = wx.Pen(dark[shape])        darkpen.SetCap(wx.CAP_PROJECTING)        dc.SetPen(darkpen)        dc.Drawline(x + 1,y + self.squareHeight() - 1)        dc.Drawline(x + self.squareWIDth() - 1,y + 1)        dc.SetPen(wx.transparent_PEN)        dc.SetBrush(wx.Brush(colors[shape]))        dc.DrawRectangle(x + 1,y + 1,self.squareWIDth() - 2,self.squareHeight() - 2)class Tetrominoes(object):    NoShape = 0    ZShape = 1    SShape = 2    lineshape = 3    TShape = 4    SquareShape = 5    LShape = 6    MirroredLShape = 7class Shape(object):    coordstable = (        ((0,0),(0,0)),((0,-1),(-1,1)),(1,1),2)),((-1,((1,1))    )    def __init__(self):        self.coords = [[0,0] for i in range(4)]        self.pIEceShape = Tetrominoes.NoShape        self.setShape(Tetrominoes.NoShape)    def shape(self):        return self.pIEceShape    def setShape(self,shape):        table = Shape.coordstable[shape]        for i in range(4):            for j in range(2):                self.coords[i][j] = table[i][j]        self.pIEceShape = shape    def setRandomShape(self):        self.setShape(random.randint(1,7))    def x(self,index):        return self.coords[index][0]    def y(self,index):        return self.coords[index][1]    def setX(self,index,x):        self.coords[index][0] = x    def setY(self,y):        self.coords[index][1] = y    def minX(self):        m = self.coords[0][0]        for i in range(4):            m = min(m,self.coords[i][0])        return m    def maxX(self):        m = self.coords[0][0]        for i in range(4):            m = max(m,self.coords[i][0])        return m    def minY(self):        m = self.coords[0][1]        for i in range(4):            m = min(m,self.coords[i][1])        return m    def maxY(self):        m = self.coords[0][1]        for i in range(4):            m = max(m,self.coords[i][1])        return m    def rotatedleft(self):        if self.pIEceShape == Tetrominoes.SquareShape:            return self        result = Shape()        result.pIEceShape = self.pIEceShape        for i in range(4):            result.setX(i,self.y(i))            result.setY(i,-self.x(i))        return result    def rotatedRight(self):        if self.pIEceShape == Tetrominoes.SquareShape:            return self        result = Shape()        result.pIEceShape = self.pIEceShape        for 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()

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的俄罗斯方块全部内容,希望文章能够帮你解决俄罗斯方块所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存