本文实例为大家分享了PyQt5实现类似别踩白块游戏的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#引入可能用到的库 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_()) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/york1996/article/details/81429585