欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5實(shí)現(xiàn)類(lèi)似別踩白塊游戲

 更新時(shí)間:2019年01月24日 14:56:38   作者:York1996  
這篇文章主要為大家詳細(xì)介紹了PyQt5實(shí)現(xiàn)類(lèi)似別踩白塊游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了PyQt5實(shí)現(xiàn)類(lèi)似別踩白塊游戲的具體代碼,供大家參考,具體內(nèi)容如下

#引入可能用到的庫(kù)
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("開(kāi)始",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()
  #讓這個(gè)整個(gè)控件獲得焦點(diǎn)
  self.setFocusPolicy(Qt.StrongFocus)
  #為了不讓按鈕獲得焦點(diǎn)
  self.startButton.setFocusPolicy(Qt.NoFocus)
 
 def initPath(self):
 
  self.queue=[[0,0,0],[1,2,3]]
 
  for i in range(100):#從123中隨機(jī)選取2個(gè)數(shù)字
   _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):
  #點(diǎn)擊開(kāi)始之前,currenX變量還沒(méi)有初始化,下面的代碼會(huì)引用這個(gè)變量,導(dǎo)致出錯(cuò)
  if self.isStarted==False:return
  #開(kāi)始畫(huà)圖
  self.myPainter.begin(self)
  #畫(huà)背景
  self.drawBackGround()
  
  interval=2#方塊之間的間隔的二分之一
  #畫(huà)場(chǎng)景
  for i in range(5):
   _item = self.queue[i + self.currentX-1]
   for _num in _item:
    #確定每個(gè)方塊的位置和大小
    _startPoint = QPoint(i *(100+interval), (_num -1)* (100+interval) )
    _size = QSize((100-2*interval), (100-2*interval))
    #設(shè)置畫(huà)筆和畫(huà)刷
    self.myPainter.setPen(QColor.fromRgb(255, 255, 255))
    self.myPainter.setBrush(QColor.fromRgb(205,211,159))
    #畫(huà)方塊們
    self.myPainter.drawRect(QRect(_startPoint, _size))
  #畫(huà)currentMe
  #設(shè)置畫(huà)筆和畫(huà)刷
  _myPen = QPen(Qt.white)
  self.myPainter.setPen(_myPen) # pen用來(lái)畫(huà)邊框
  self.myPainter.setBrush(QColor.fromRgb(105, 139 ,105))
  #確定每個(gè)方塊的位置和大小
  _startPoint = QPoint(100+interval, (100+interval) *(self.currentY-1))
  _size = QSize((100-2*interval), (100-2*interval))
  #畫(huà)方塊
  self.myPainter.drawRect(QRect(_startPoint, _size))
  #結(jié)束畫(huà)圖
  self.myPainter.end()
 
 def drawBackGround(self):
 
  _myPen = QPen(Qt.white)
 
  self.myPainter.setPen(_myPen) # pen用來(lái)畫(huà)邊框
  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","掉坑里了,用時(shí)+3S",QMessageBox.Ok)
   self.timeCost+=3
  else:
   self.currentX += 1
   self.previouY=self.currentY
   self.currentY=_nextY
 
   self.sceneForward()#場(chǎng)景往前走一步
 
 def sceneForward(self):
 
  if self.currentX==101:
   self.costTimer.stop()
   _reply=QMessageBox.question(self,'恭喜你闖關(guān)成功',"總耗時(shí)<b>秒"+str(self.timeCost)+"<br>Restart or quit?<br><b>Press Yes to restart or press no to quit this game.",
          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_())

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論