当前位置: 移动技术网 > IT编程>脚本编程>Python > python如何写个俄罗斯方块

python如何写个俄罗斯方块

2020年11月06日  | 移动技术网IT编程  | 我要评论
俄罗斯方块是俄罗斯人发明的一款休闲类的小游戏,这款小游戏可以说是很多人童年的主打电子游戏了,本文我们使用 python 来实现这款小游戏。游戏的基本规则是:移动、旋转和摆放游戏自动输出的各种方块,使之

俄罗斯方块是俄罗斯人发明的一款休闲类的小游戏,这款小游戏可以说是很多人童年的主打电子游戏了,本文我们使用 python 来实现这款小游戏。

游戏的基本规则是:移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。

实现

我们实现俄罗斯方块,主要用到的是 pyqt5 库,安装使用 pip install pyqt5 即可,游戏的组成比较简单,主要包括:主界面、各种方块和计分板,下面我们来看一下具体实现。

首先,我们来画一个主界面,主要实现代码如下:

class mainboard(qframe):
 msg = pyqtsignal(str)
 boardwidth = 10
 boardheight = 20
 speed = 300

 def __init__(self, parent):
  super().__init__(parent)
  self.initboard()

 def initboard(self):
  self.timer = qbasictimer()
  self.iswaitingafterline = false
  self.curx = 0
  self.cury = 0
  self.numlinesremoved = 0
  self.board = []
  self.setfocuspolicy(qt.strongfocus)
  self.isstarted = false
  self.ispaused = false
  self.clearboard()

看一下效果:

分数的显示就是利用上面 msg 的 emit() 方法实现的。

我们接着画各种方块,方块的形状主要包括:t、z、l、i、o 等,主要实现代码如下:

class shapeform(object):
 noshape = 0
 zshape = 1
 sshape = 2
 lineshape = 3
 tshape = 4
 squareshape = 5
 lshape = 6
 mirroredlshape = 7

class shape(object):
 coordstable = (
  ((0, 0),  (0, 0),  (0, 0),  (0, 0)),
  ((0, -1), (0, 0),  (-1, 0), (-1, 1)),
  ((0, -1), (0, 0),  (1, 0),  (1, 1)),
  ((0, -1), (0, 0),  (0, 1),  (0, 2)),
  ((-1, 0), (0, 0),  (1, 0),  (0, 1)),
  ((0, 0),  (1, 0),  (0, 1),  (1, 1)),
  ((-1, -1), (0, -1), (0, 0),  (0, 1)),
  ((1, -1), (0, -1), (0, 0),  (0, 1))
 )

 def __init__(self):
  self.coords = [[0,0] for i in range(4)]
  self.pieceshape = shapeform.noshape
  self.setshape(shapeform.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 timerevent(self, event):
	if event.timerid() == self.timer.timerid():
		if self.iswaitingafterline:
			self.iswaitingafterline = false
			self.newpiece()
		else:
			self.onelinedown()
	else:
		super(mainboard, self).timerevent(event)

在方块下落的过程中,我们需要通过键盘来控制方块的形状以及左右移动,因此,我们需要一个按键事件来控制它,主要实现代码如下:

def keypressevent(self, event):
	if not self.isstarted or self.curpiece.shape() == shapeform.noshape:
		super(mainboard, self).keypressevent(event)
		return
	key = event.key()
	if key == qt.key_p:
		self.pause()
		return
	if self.ispaused:
		return
	elif key == qt.key_left:
		self.trymove(self.curpiece, self.curx - 1, self.cury)
	elif key == qt.key_right:
		self.trymove(self.curpiece, self.curx + 1, self.cury)
	elif key == qt.key_down:
		self.trymove(self.curpiece.rotateright(), self.curx, self.cury)
	elif key == qt.key_up:
		self.trymove(self.curpiece.rotateleft(), self.curx, self.cury)
	elif key == qt.key_space:
		self.dropdown()
	elif key == qt.key_d:
		self.onelinedown()
	else:
		super(mainboard, self).keypressevent(event)

当方块落到底部后,需要来检测是否有构成一条直线的,因此我们需要有一个方法来找到所有能消除的行并且消除它们,主要实现代码如下:

def removefulllines(self):
	numfulllines = 0
	rowstoremove = []
	for i in range(mainboard.boardheight):
		n = 0
		for j in range(mainboard.boardwidth):
			if not self.shapeat(j, i) == shapeform.noshape:
				n = n + 1
		if n == 10:
			rowstoremove.append(i)
	rowstoremove.reverse()
	for m in rowstoremove:
		for k in range(m, mainboard.boardheight):
			for l in range(mainboard.boardwidth):
					self.setshapeat(l, k, self.shapeat(l, k + 1))
	numfulllines = numfulllines + len(rowstoremove)
	if numfulllines > 0:
		self.numlinesremoved = self.numlinesremoved + numfulllines
		self.msg.emit(str(self.numlinesremoved))
		self.iswaitingafterline = true
		self.curpiece.setshape(shapeform.noshape)
		self.update()

我们来看一下最终实现效果:

是不是有内味了。

总结

本文我们使用 pyqt5 库写了一个俄罗斯方块小游戏,如果你对 pyqt5 库感兴趣的话,可以尝试使用一下。

示例代码:py-tetris

以上就是python写个俄罗斯方块的详细内容,更多关于python 俄罗斯方块的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网