当前位置:首页 >> 脚本专栏

PyQt5实现类似别踩白块游戏

本文实例为大家分享了PyQt5实现类似别踩白块游戏的具体代码,供大家参考,具体内容如下

#引入可能用到的库
from PyQt5.QtWidgets import (QWidget, QApplication,QPushButton,QMessageBox,QLabel,QDesktopWidget,QMainWindow)
from PyQt5.QtCore import Qt,QRect,QSize,QPoint,QTimer
from PyQt5.QtGui import QPainter, QFont, QColor, QPen,QIcon,QBrush
import sys
import random
 
class mainForm(QMainWindow):
 def __init__(self):
 
  super().__init__()
  self.initUI()
  self.show()
 
 def initUI(self):
 
  self.setWindowTitle("Step Game")
  self.setGeometry(0,0,520,360)
  self.setWindowIcon(QIcon(R"Doraemon.ico"))
  self.center()
 
  self.myBoard=board()
  self.myBoard.resize(500,400)
  self.myBoard.move(10,10)
  self.myBoard.setParent(self)
 
 def center(self):
 
  frame=self.frameGeometry()
  availableCenter=QDesktopWidget().availableGeometry().center()
  frame.moveCenter(availableCenter)
  self.move(frame.topLeft())
 
 
class board(QWidget):
 
 def __init__(self):
  super().__init__()
  self.initUI()
  self.initPath()
  self.isStarted=False
 def initUI(self):
 
  _font=QFont("Roman times",16,QFont.Bold)
 
  self.tipLabel=QLabel("加油!",self)
  self.tipLabel.setFont(_font)
  self.tipLabel.move(300,310)
 
  self.timingLabel=QLabel("0S",self)
  self.timingLabel.move(200,310)
  self.timingLabel.setFont(_font)
 
  self.startButton=QPushButton("开始",self)
  self.startButton.setFont(_font)
  self.startButton.move(20,310)
  self.startButton.clicked.connect(self.start)
 
  self.costTimer=QTimer()
  self.costTimer.timeout.connect(self.tick)
 
  self.myPainter = QPainter()
  #让这个整个控件获得焦点
  self.setFocusPolicy(Qt.StrongFocus)
  #为了不让按钮获得焦点
  self.startButton.setFocusPolicy(Qt.NoFocus)
 
 def initPath(self):
 
  self.queue=[[0,0,0],[1,2,3]]
 
  for i in range(100):#从123中随机选取2个数字
   _num=random.randint(1,3)
   _oneTowThree=[1,2,3]
   _oneTowThree.remove(_num)
   self.queue.append(_oneTowThree)
 
  for i in range(5):#地图的最后有空的方块
   self.queue.append([])
 
 def start(self):
  
  self.isStarted=True
  self.currentX=1
  self.timeCost=0
  self.currentY=random.randint(1,3)
  self.previouY=self.currentY
  self.costTimer.start(1000)
  self.repaint()
 
 def tick(self):
 
  self.timeCost+=1
  self.timingLabel.setText(str(self.timeCost)+"S")
 
 def paintEvent(self, e):
  #点击开始之前,currenX变量还没有初始化,下面的代码会引用这个变量,导致出错
  if self.isStarted==False:return
  #开始画图
  self.myPainter.begin(self)
  #画背景
  self.drawBackGround()
  
  interval=2#方块之间的间隔的二分之一
  #画场景
  for i in range(5):
   _item = self.queue[i + self.currentX-1]
   for _num in _item:
    #确定每个方块的位置和大小
    _startPoint = QPoint(i *(100+interval), (_num -1)* (100+interval) )
    _size = QSize((100-2*interval), (100-2*interval))
    #设置画笔和画刷
    self.myPainter.setPen(QColor.fromRgb(255, 255, 255))
    self.myPainter.setBrush(QColor.fromRgb(205,211,159))
    #画方块们
    self.myPainter.drawRect(QRect(_startPoint, _size))
  #画currentMe
  #设置画笔和画刷
  _myPen = QPen(Qt.white)
  self.myPainter.setPen(_myPen) # pen用来画边框
  self.myPainter.setBrush(QColor.fromRgb(105, 139 ,105))
  #确定每个方块的位置和大小
  _startPoint = QPoint(100+interval, (100+interval) *(self.currentY-1))
  _size = QSize((100-2*interval), (100-2*interval))
  #画方块
  self.myPainter.drawRect(QRect(_startPoint, _size))
  #结束画图
  self.myPainter.end()
 
 def drawBackGround(self):
 
  _myPen = QPen(Qt.white)
 
  self.myPainter.setPen(_myPen) # pen用来画边框
  self.myPainter.setBrush(QColor.fromRgb(255,239,124))
 
  _tempSize = self.size()
  _newSize = QSize(_tempSize.width(), _tempSize.height()-100)
  _startPoint = QPoint(0, 0)
 
  self.myPainter.drawRect(QRect(_startPoint, _newSize))
 
 
 def keyPressEvent(self, kEvent):
 
  if self.isStarted==False:return
 
  _k=kEvent.key()
  #每次只能走相邻的格子
  if _k==Qt.Key_Up:
   if self.currentY==1:
    _nextY=1
   else:
    _nextY=self.currentY-1
  elif _k==Qt.Key_Down:
   if self.currentY == 3:
    _nextY = 3
   else:
    _nextY = self.currentY +1
  elif _k==Qt.Key_Left:
   return#未定义事件
  elif _k==Qt.Key_Right:
   _nextY = self.currentY
  else:
   return
 
  if _nextY not in self.queue[self.currentX+1]:
   QMessageBox.warning(self,"Step Game","掉坑里了,用时+3S",QMessageBox.Ok)
   self.timeCost+=3
  else:
   self.currentX += 1
   self.previouY=self.currentY
   self.currentY=_nextY
 
   self.sceneForward()#场景往前走一步
 
 def sceneForward(self):
 
  if self.currentX==101:
   self.costTimer.stop()
   _reply=QMessageBox.question(self,'恭喜你闯关成功',"总耗时<b>秒"+str(self.timeCost)+"<br>Restart or quit",
          QMessageBox.No|QMessageBox.Yes,
          QMessageBox.Yes)
   if _reply==QMessageBox.Yes:
    self.start()
   else:
    self.parent().close()
 
  self.repaint()
 
if __name__ == '__main__':
 app = QApplication(sys.argv)
 ex = mainForm()
 sys.exit(app.exec_())

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。