PyQt5實(shí)現(xiàn)類(lèi)似別踩白塊游戲
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
- pycharm配置pyqt5-tools開(kāi)發(fā)環(huán)境的方法步驟
- pycharm+PyQt5+python最新開(kāi)發(fā)環(huán)境配置(踩坑)
- PyQt5實(shí)現(xiàn)五子棋游戲(人機(jī)對(duì)弈)
- python+pyqt5實(shí)現(xiàn)24點(diǎn)小游戲
- Python魔法方法詳解
- Python函數(shù)中不定長(zhǎng)參數(shù)的寫(xiě)法
- Python判斷變量名是否合法的方法示例
- Python中常用的內(nèi)置方法
- PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例
相關(guān)文章
python 3.6 +pyMysql 操作mysql數(shù)據(jù)庫(kù)(實(shí)例講解)
下面小編就為大家分享一篇python 3.6 +pyMysql 操作mysql數(shù)據(jù)庫(kù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12Python編程對(duì)列表中字典元素進(jìn)行排序的方法詳解
這篇文章主要介紹了Python編程對(duì)列表中字典元素進(jìn)行排序的方法,涉及Python針對(duì)列表及字典元素的遍歷、讀取、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05使用C語(yǔ)言擴(kuò)展Python程序的簡(jiǎn)單入門(mén)指引
這篇文章主要介紹了使用C語(yǔ)言擴(kuò)展Python程序的簡(jiǎn)單入門(mén)指引,來(lái)自于IBM官網(wǎng)網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04Python爬蟲(chóng)圖片懶加載技術(shù) selenium和PhantomJS解析
這篇文章主要介紹了Python爬蟲(chóng)圖片懶加載技術(shù) selenium和PhantomJS解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Django rstful登陸認(rèn)證并檢查session是否過(guò)期代碼實(shí)例
這篇文章主要介紹了Django rstful登陸認(rèn)證并檢查session是否過(guò)期代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼
這篇文章主要介紹了Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例
今天小編就為大家分享一篇python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12