-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMap.py
More file actions
677 lines (538 loc) · 22.4 KB
/
GameMap.py
File metadata and controls
677 lines (538 loc) · 22.4 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
import random
from PyQt5.QtGui import QColor, QPen, QFont, QFontMetrics
from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout)
from PyQt5.QtCore import pyqtSignal, QTimer ,QSize
from Box import Box
from Items import Items
from PyQt5.QtCore import Qt
DEFAULT_WIDTH = 800
DEFAULT_HEIGHT = 600
EDGE_SPACE = 4
class GameMap(QWidget):
"""
游戏地图类,负责管理所有方块、道具、玩家、倒计时、提示等核心逻辑。
"""
requestHintBox = pyqtSignal() # 请求提示信号
def __init__(self, row, col, size, game_type, parent=None):
"""
初始化地图对象。
:param row: 行数
:param col: 列数
:param size: 窗口大小
:param game_type: 游戏模式
:param parent: 父级QWidget
"""
super().__init__(parent)
self.hintBoxTimer = QTimer(self)
self.countdownTimer = QTimer(self)
self.x_ratio = None
self.y_ratio = None
self.boxRow = row
self.boxCol = col
self.size = size
self.gameType = game_type
self.boxWidth = None
self.boxHeight = None
self.leftSpaceWidth = None
self.leftSpaceHeight = None
self.endOfGame = False
self.gamePause = False
self.seconds = 300
self.hintSeconds = 10
self.hintStatus = False
self.player1 = None
self.player2 = None
self.selectChecker = None
self.boxMap = [[Box for _ in range(col)] for _ in range(row)]
self.items = []
self.typeItemNum = [None for _ in range(3)]
self.hintBox = None
self.color = [QColor for _ in range(4)]
self.map_init()
def random_color(self):
"""为不同类型方块生成随机颜色"""
for i in range(4):
green = random.randint(0, 255)
red = random.randint(0, 255)
blue = random.randint(0, 255)
self.color[i] = QColor(red, green, blue)
def map_init(self):
"""初始化地图、方块、道具、颜色和倒计时"""
self.x_ratio = self.size.width() / float(DEFAULT_WIDTH)
self.y_ratio = self.size.height() / float(DEFAULT_HEIGHT)
sizeWidth = self.size.width()
sizeHeight = self.size.height()
self.boxWidth = int(DEFAULT_WIDTH * self.x_ratio / (self.boxCol + EDGE_SPACE))
self.boxHeight = int(DEFAULT_HEIGHT * self.y_ratio / (self.boxRow + EDGE_SPACE))
self.leftSpaceWidth = int((sizeWidth - self.boxWidth * self.boxCol) / 2)
self.leftSpaceHeight = int((sizeHeight - self.boxHeight * self.boxRow) / 2)
self.endOfGame = False
for i in range(self.boxRow):
for j in range(self.boxCol):
leftTopX = self.leftSpaceWidth + j * self.boxWidth
leftTopY = self.leftSpaceHeight + i * self.boxHeight
tmpBox = Box(self.boxWidth,self.boxHeight,leftTopX,leftTopY,0)
tmpBox.setBoxSelected(False)
tmpBox.setBoxRemoved(False)
tmpBox.setBoxEdgeColor()
tmpBox.setBoxRowCol(i,j)
tmpBox.setHintStatus(False)
tmpBox.updateBox()
self.boxMap[i][j] = tmpBox
self.random_color()
for i in range(self.boxRow):
for j in range(self.boxCol):
rand = (i + j + random.randint(0,114514)) % 4
self.boxMap[i][j].setBoxType(rand)
self.boxMap[i][j].updateBox()
if rand != 0:
self.boxMap[i][j].setBoxColor(self.color[rand])
if self.gameType == 1:
self.initItems(5,3,1)
self.initCountdown()
''' 测试特殊地图排布
self.setAllType0()
self.getBoxMap(1, 1).setBoxType(1)
self.boxMap[1][1].setBoxColor(self.color[1])
self.getBoxMap(2, 2).setBoxType(1)
self.boxMap[2][2].setBoxColor(self.color[1])
self.getBoxMap(1, 2).setBoxType(2)
self.boxMap[1][2].setBoxColor(self.color[2])
self.getBoxMap(2, 1).setBoxType(2)
self.boxMap[2][1].setBoxColor(self.color[2])
'''
def getRandomNumbers(self,numbers,num):
numbers.clear()
for i in range(num):
numbers.append(i)
random.shuffle(numbers)
return numbers
def initItems(self,type0num,type1num,type2num):
self.typeItemNum[0] = type0num
self.typeItemNum[1] = type1num
self.typeItemNum[2] = type2num
totalNum = type0num + type1num + type2num
rowNum = []
colNum = []
rowNum = self.getRandomNumbers(rowNum,self.boxRow)
colNum = self.getRandomNumbers(colNum,self.boxCol)
for i in range(type0num):
# ...existing code...
row = rowNum[i]
col = colNum[i]
self.boxMap[row][col].setBoxType(4)
# ...existing code...
tmpItem = Items(0,row,col,self.boxMap[row][col].getLeftTopX(),self.boxMap[row][col].getLeftTopY(),self.boxWidth,self.boxHeight,self)
self.items.append(tmpItem)
# ...existing code...
for i in range(type0num, type0num+type1num):
row = rowNum[i]
col = colNum[i]
self.boxMap[row][col].setBoxType(4)
tmpItem = Items(1, row, col, self.boxMap[row][col].getLeftTopX(), self.boxMap[row][col].getLeftTopY(),
self.boxWidth, self.boxHeight,self)
self.items.append(tmpItem)
for i in range(type0num+type1num, type0num+type1num+type2num):
row = rowNum[i]
col = colNum[i]
self.boxMap[row][col].setBoxType(4)
tmpItem = Items(2, row, col, self.boxMap[row][col].getLeftTopX(), self.boxMap[row][col].getLeftTopY(),
self.boxWidth, self.boxHeight,self)
self.items.append(tmpItem)
def getItem(self, row, col):
"""获取指定位置的道具对象"""
if len(self.items) == 0:
return None
for i in range(len(self.items)):
if self.items[i] is None:
continue
elif (self.items[i].getItemRow() == row) and (self.items[i].getItemCol() == col):
return self.items[i]
return None
def drawMap(self, painter):
"""绘制整个地图,包括方块和道具"""
for i in range(self.boxRow):
for j in range(self.boxCol):
if (self.boxMap[i][j].getBoxType() == 0) or (self.boxMap[i][j].paintRemoved):
self.boxMap[i][j].setBoxRemoved(True)
continue
elif self.boxMap[i][j].getBoxType() ==4:
tmpItem = self.getItem(i,j)
if tmpItem is not None:
tmpItem.drawItem(painter)
else:
if self.hintStatus:
formerHintNum = 0
if not self.hintBox:
self.setHintBoxes()
continue
for k in range(len(self.hintBox)):
if not self.boxMap[self.hintBox[k][0]][self.hintBox[k][1]].isBoxRemoved() :
formerHintNum += 1
if formerHintNum < 2:
self.setHintBoxes()
painter.setPen(QPen(self.boxMap[i][j].getBoxEdgeColor(), self.boxMap[i][j].getEdgeLen()))
painter.setBrush(self.boxMap[i][j].getBoxColor())
self.boxMap[i][j].drawBox(painter)
def isBoxijExist(self, i, j):
"""判断指定位置是否为有效方块"""
if i < 0 or j < 0 or i >= self.boxRow or j >= self.boxCol:
return False
return self.boxMap[i][j].isBoxExist()
def isBoxOrItemijExist(self, i, j):
"""判断指定位置是否为有效方块或道具"""
if i < 0 or j < 0 or i >= self.boxRow or j >= self.boxCol:
return False
return self.boxMap[i][j].isBoxOrItemExist()
def isEndOfGame(self):
"""判断游戏是否结束"""
return self.endOfGame
def setEndOfGame(self, status):
"""设置游戏结束状态"""
self.endOfGame = status
def setGamePause(self, status):
"""设置游戏暂停状态"""
self.gamePause = status
def getGamePause(self):
"""获取游戏暂停状态"""
return self.gamePause
def getGameType(self):
"""获取游戏模式"""
return self.gameType
def setGameType(self, gameType):
"""设置游戏模式"""
self.gameType = gameType
def getPlayerWidth(self):
"""获取玩家宽度(相对方块)"""
return self.boxWidth * 0.8
def getPlayerHeight(self):
"""获取玩家高度(相对方块)"""
return self.boxHeight * 0.8
def getBoxWidth(self):
"""获取方块宽度"""
return self.boxWidth
def getBoxHeight(self):
"""获取方块高度"""
return self.boxHeight
def setBoxWidth(self, width):
"""设置方块宽度"""
self.boxWidth = width
def setBoxHeight(self, height):
"""设置方块高度"""
self.boxHeight = height
def getPathWidth(self):
"""获取地图左侧空白宽度"""
return self.leftSpaceWidth
def getPathHeight(self):
"""获取地图上方空白高度"""
return self.leftSpaceHeight
def setPathWidth(self, width):
"""设置地图左侧空白宽度"""
self.leftSpaceWidth = width
def setPathHeight(self, height):
"""设置地图上方空白高度"""
self.leftSpaceHeight = height
def getScreenWidth(self):
"""获取屏幕宽度"""
return self.size.width()
def getScreenHeight(self):
"""获取屏幕高度"""
return self.size.height()
def getBoxRow(self):
"""获取方块行数"""
return self.boxRow
def getBoxCol(self):
"""获取方块列数"""
return self.boxCol
def setBoxRow(self, row):
"""设置方块行数"""
self.boxRow = row
def setBoxCol(self, col):
"""设置方块列数"""
self.boxCol = col
def secondsPlus1(self):
"""倒计时加30秒"""
self.seconds+=30
def getSeconds(self):
"""获取剩余秒数"""
return self.seconds
def setSeconds(self, seconds):
"""设置剩余秒数"""
self.seconds = seconds
def getHintStatus(self):
"""获取提示状态"""
return self.hintStatus
def setHintStatus(self, status):
"""设置提示状态"""
self.hintStatus = status
def setPlayer1(self, player):
"""设置玩家1对象"""
self.player1 = player
def setPlayer2(self, player):
"""设置玩家2对象"""
self.player2 = player
def getPlayer1(self):
"""获取玩家1对象"""
return self.player1
def getPlayer2(self):
"""获取玩家2对象"""
return self.player2
def getBoxMap(self, row, col):
"""获取指定位置的方块对象"""
return self.boxMap[row][col]
def setBoxMap(self, row, col, box):
"""设置指定位置的方块对象"""
self.boxMap[row][col] = box
def getMap(self):
"""获取整个方块地图二维数组"""
return self.boxMap
def setSize(self, size):
"""设置地图窗口大小"""
self.size = size
def isBoxOrItemExist(self, i, j):
"""判断指定位置是否为有效方块或道具(冗余)"""
if i < 0 or j < 0 or j >= self.boxCol or i >= self.boxRow :
return False
return self.boxMap[i][j].isBoxOrItemExist()
def isBoxExist(self, i, j):
"""判断指定位置是否为有效方块(冗余)"""
if i < 0 or j < 0 or j >= self.boxCol or i >= self.boxRow :
return False
return self.boxMap[i][j].isBoxOrExist()
def initCountdown(self):
"""初始化倒计时和提示计时器"""
self.seconds = 300
self.countdownTimer = QTimer(self)
self.countdownTimer.timeout.connect(self.updateCountdown)
self.countdownTimer.start(1000)
self.hintBoxTimer = QTimer(self)
self.requestHintBox.connect(self.setHintTimer)
self.hintBoxTimer.timeout.connect(self.updateHintCountdown)
def updateCountdown(self):
"""倒计时每秒减少,归零则游戏结束"""
self.seconds -= 1
if self.seconds <= 0:
self.countdownTimer.stop()
self.endOfGame = True
def stopCountdown(self):
"""停止倒计时"""
self.countdownTimer.stop()
def startCountdown(self):
"""启动倒计时"""
self.countdownTimer.start(1000)
def setHintTimer(self):
"""启动提示计时器"""
self.hintSeconds = 10
self.hintBoxTimer.start(1000)
self.hintStatus = True
def updateHintCountdown(self):
"""提示倒计时每秒减少,归零则关闭提示"""
self.hintSeconds -= 1
if self.hintSeconds <= 0:
self.finishHint()
self.hintSeconds = 10
def finishHint(self):
"""提示时间结束,关闭提示"""
self.hintStatus = False
self.hintBoxTimer.stop()
self.deleteHintBox()
def deleteHintBox(self):
"""清除所有提示方块的提示状态"""
if not self.hintBox:
return
elif len(self.hintBox) != 0:
for i in range(len(self.hintBox)):
self.boxMap[self.hintBox[i][0]][self.hintBox[i][1]].setHintStatus(False)
self.hintBox.clear()
def setHintBoxes(self):
"""设置当前可消除的提示方块"""
if not self.hintBox: #是none
self.hintBox = []
elif len(self.hintBox) != 0:
for i in range(len(self.hintBox)):
if self.hintBox[i]:
self.boxMap[self.hintBox[i][0]][self.hintBox[i][1]].setHintStatus(False)
self.hintBox.clear()
from SelectChecker import SelectChecker
checker = SelectChecker()
self.hintBox = checker.getHintBoxes(self,self.player1)
if not self.hintBox:
return False
elif len(self.hintBox) < 2:
return False
self.boxMap[self.hintBox[0][0]][self.hintBox[0][1]].setHintStatus(True)
self.boxMap[self.hintBox[1][0]][self.hintBox[1][1]].setHintStatus(True)
return True
def getHintSeconds(self):
"""获取提示剩余秒数"""
return self.hintSeconds
def setHintSeconds(self, seconds):
"""设置提示剩余秒数"""
self.hintSeconds = seconds
def stopHintTimer(self):
"""停止提示计时器"""
self.hintBoxTimer.stop()
def paintCountdown(self, painter):
"""绘制倒计时文本"""
font = QFont()
font.setPointSize(12)
painter.setFont(font)
painter.setPen(Qt.black)
text = str(self.seconds)+"s"
metrics = QFontMetrics(font)
textWidth = metrics.horizontalAdvance(text)
textHeight = metrics.height()
x = (DEFAULT_WIDTH - textWidth) / 2
y = textHeight
painter.drawText(x, y, text)
#地图重排(洗牌)
def shuffleMap(self):
"""地图洗牌,随机交换方块和道具"""
#print("before clear select")
#print(self.player1.getCurrentSelected())
tmpSelect1 = self.player1.getCurrentSelected()
if tmpSelect1 is not None:
for i in range(len(tmpSelect1)):
tmpBox = self.boxMap[tmpSelect1[i][0]][tmpSelect1[i][1]]
tmpBox.setBoxSelected(False)
tmpBox.updateBox()
#print("clear select")
self.player1.clearCurrentSelected()
if self.gameType == 2:
tmpSelect2 = self.player2.getCurrentSelected()
if tmpSelect2 is not None:
for i in range(len(tmpSelect2)):
tmpBox = self.boxMap[tmpSelect2[i][0]][tmpSelect2[i][1]]
tmpBox.setBoxSelected(False)
tmpBox.updateBox()
self.player2.clearCurrentSelected()
self.player1.finishTextPrint()
if self.player2 is not None:
self.player2.finishTextPrint()
for i in range(self.boxRow):
for j in range(self.boxCol):
if self.boxMap[i][j].isRemoved == True:
self.boxMap[i][j].paintRemoved = True
rowNum = []
colNum = []
rowNum = self.getRandomNumbers(rowNum, self.boxRow)
colNum = self.getRandomNumbers(colNum, self.boxCol)
randomSpot = []
for i in range(self.boxRow * self.boxCol):
randomSpot.append((-1,-1))
tmpIndex = 0
for i in range(self.boxRow):
for j in range(self.boxCol):
randomSpot[tmpIndex] = (rowNum[i], colNum[j])
tmpIndex += 1
selectNum = []
selectNum = self.getRandomNumbers(selectNum, self.boxRow * self.boxCol)
for i in range(0, len(selectNum)-1,2):
self.swapSpot(randomSpot[i], randomSpot[i+1])
if self.hintStatus:
for i in range(len(self.hintBox)):
self.boxMap[self.hintBox[i][0]][self.hintBox[i][1]].setHintStatus(False)
self.hintBox.clear()
def swapSpot(self, spot1, spot2):
"""交换两个位置的方块或道具"""
x1 = spot1[0]
y1 = spot1[1]
x2 = spot2[0]
y2 = spot2[1]
if not self.isBoxOrItemijExist(x1,y1) and not self.isBoxOrItemijExist(x2,y2):
return
elif self.boxMap[x1][y1].getBoxType() != 4 and self.boxMap[x2][y2].getBoxType() != 4:
self.swapBox(x1, y1, x2, y2)
elif self.boxMap[x1][y1].getBoxType() != 4 and self.boxMap[x2][y2].getBoxType() == 4:
self.swapBoxItem(x1,y1,x2,y2)
#print("swap box item")
elif self.boxMap[x1][y1].getBoxType() == 4 and self.boxMap[x2][y2].getBoxType() != 4:
self.swapBoxItem(x2,y2,x1,y1)
#print("swap box item")
elif self.boxMap[x1][y1].getBoxType() == 4 and self.boxMap[x2][y2].getBoxType() == 4:
self.swapItem(x1,y1,x2,y2)
#print("swap item")
def swapBox(self, x1, y1, x2, y2):
"""交换两个普通方块的位置"""
# 交换引用
box1 = self.boxMap[x1][y1]
box2 = self.boxMap[x2][y2]
self.boxMap[x1][y1] = box2
self.boxMap[x2][y2] = box1
# 获取交换后新的坐标
topX2 = self.boxMap[x1][y1].getLeftTopX()
topY2 = self.boxMap[x1][y1].getLeftTopY()
topX1 = self.boxMap[x2][y2].getLeftTopX()
topY1 = self.boxMap[x2][y2].getLeftTopY()
# 更新坐标和行列信息
self.boxMap[x1][y1].setXandY(topX1, topY1)
self.boxMap[x1][y1].setBoxRowCol(x1, y1)
self.boxMap[x2][y2].setXandY(topX2, topY2)
self.boxMap[x2][y2].setBoxRowCol(x2, y2)
def swapBoxItem(self, x1, y1, x2, y2):
"""交换普通方块和道具方块的位置"""
# 交换引用
box1 = self.boxMap[x1][y1]
box2 = self.boxMap[x2][y2]
self.boxMap[x1][y1] = box2
self.boxMap[x2][y2] = box1
# 获取交换后新坐标
topX2 = self.boxMap[x1][y1].getLeftTopX()
topY2 = self.boxMap[x1][y1].getLeftTopY()
topX1 = self.boxMap[x2][y2].getLeftTopX()
topY1 = self.boxMap[x2][y2].getLeftTopY()
# 更新新位置的坐标和行列信息
self.boxMap[x1][y1].setXandY(topX1, topY1)
self.boxMap[x1][y1].setBoxRowCol(x1, y1)
self.boxMap[x2][y2].setXandY(topX2, topY2)
self.boxMap[x2][y2].setBoxRowCol(x2, y2)
# 道具坐标/行列同步
tmpItem = self.getItem(x2, y2)
if tmpItem is not None:
tmpItem.setItemXY(topX1, topY1)
tmpItem.setItemRowCol(x1, y1)
def swapItem(self, x1, y1, x2, y2):
"""交换两个道具方块的位置"""
# 交换 boxMap 中的引用
box1 = self.boxMap[x1][y1]
box2 = self.boxMap[x2][y2]
self.boxMap[x1][y1] = box2
self.boxMap[x2][y2] = box1
# 获取交换后新的坐标
topX2 = self.boxMap[x1][y1].getLeftTopX()
topY2 = self.boxMap[x1][y1].getLeftTopY()
topX1 = self.boxMap[x2][y2].getLeftTopX()
topY1 = self.boxMap[x2][y2].getLeftTopY()
# 更新 boxMap 中 box 的 (x, y) 和 (row, col)
self.boxMap[x1][y1].setXandY(topX1, topY1)
self.boxMap[x1][y1].setBoxRowCol(x1, y1)
self.boxMap[x2][y2].setXandY(topX2, topY2)
self.boxMap[x2][y2].setBoxRowCol(x2, y2)
# 取出对应道具对象并同步数据
item1 = self.getItem(x1, y1)
item2 = self.getItem(x2, y2)
if item1 is not None:
item1.setItemXY(topX1, topY1)
item1.setItemRowCol(x1, y1)
if item2 is not None:
item2.setItemXY(topX2, topY2)
item2.setItemRowCol(x2, y2)
def twoPlayerConflictDeal(self):
"""处理双人模式下两个玩家同时选中同一方块的冲突"""
if self.gameType == 1:
return
else:
self.player1.conflictDeal()
self.player2.conflictDeal()
def setAllType1(self):
"""将所有方块设置为类型1(测试用)"""
for i in range(self.boxRow):
for j in range(self.boxCol):
self.boxMap[i][j].setBoxRemoved(False)
self.boxMap[i][j].setBoxType(1)
def setAllType0(self):
"""将所有方块设置为空(类型0,测试用)"""
for i in range(self.boxRow):
for j in range(self.boxCol):
self.boxMap[i][j].setBoxRemoved(False)
self.boxMap[i][j].setBoxType(0)