欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PyQt5实现类似别踩白块游戏

程序员文章站 2022-06-14 12:34:29
本文实例为大家分享了pyqt5实现类似别踩白块游戏的具体代码,供大家参考,具体内容如下 #引入可能用到的库 from pyqt5.qtwidgets imp...

本文实例为大家分享了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?<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_())

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