-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
138 lines (122 loc) · 5.48 KB
/
Game.py
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
"""
8-puzzle and 15-puzzle game is a puzzle game played by moving of tiles
Copyright (C) 2018 Rahul Gautham Putcha
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For more details on contact please do visit, https://rahulgputcha.com or email to rahulgautham95@gmail.com
"""
from puzzle import *
import numpy as np
import cv2
#Game GUI Classes
class GamePlay() :
def __init__(self,puzzleCode,windowName,width,height) :
self.width,self.height = width,height-50
self.puzzle_Code = puzzleCode
self.change = True
self.windowClose = False
self.blockInfo = []
self.zeroBlock = None
self.row_size = int((self.puzzle_Code+1)**0.5)
self.image = np.zeros((self.width,self.height+50,3),np.uint8)
self.game = Game(self.puzzle_Code)
self.windowName = windowName
self.lastMove = None
self.hint = -1
def mainLoop(self) :
cv2.setMouseCallback(self.windowName,self.mouseCall)
while not self.windowClose :
cv2.imshow(self.windowName,self.image)
if self.change :
self.image = np.zeros((self.width,self.height+50,3),np.uint8)
self.blockInfo = []
self.draw()
self.change = False
key_pressed=cv2.waitKey(1)
if key_pressed==27 : self.windowClose = True
elif key_pressed==ord('r') or key_pressed==ord('R') :
self.game.reset_game()
self.hint=-1
self.change = True
elif key_pressed==ord('h') or key_pressed==ord('H') :
#print(self.game.nextHint())
self.hint,score = self.game.nextHint(self.lastMove)
if self.hint != -1:
self.change = True
else:
cv2.putText(self.image,"No moves available!!!",(int(width/2)-50,height-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
def draw(self) :
for i in range(self.row_size) :
for j in range(self.row_size) :
start_point = (i*(self.width//self.row_size)+5,j*(self.height//self.row_size)+5)
end_point = ((i+1)*(self.width//self.row_size)-5,(j+1)*(self.height//self.row_size)-5)
if self.game.blocks[(j,i)].number!=0 :
color = (255,0,255)
if self.game.blocks[(j,i)].number==self.hint : color = (0,255,0)
cv2.rectangle(self.image,start_point,end_point,color,-1)
text_pos = (start_point[0]+self.width//9,start_point[1]+self.height//7)
cv2.putText(self.image,str(self.game.blocks[(j,i)].number),text_pos,cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
cv2.putText(self.image,"Esc - Main Menu ",(width-150,height-30),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
cv2.putText(self.image,"R - Reset",(20,height-30),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
cv2.putText(self.image,"H - Hint",(20,height-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
if self.game.win :
cv2.putText(self.image,"You Win!!!",(int(width/2)-50,height-30),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
self.blockInfo.append((self.game.blocks[(j,i)],self.game.blocks[(j,i)].number,start_point,end_point))
else :
self.zeroBlock = self.game.blocks[(j,i)]
def mouseCall(self,event,posx,posy,flag,param) :
if event == cv2.EVENT_LBUTTONDOWN :
block,number = self.getBlock(posx,posy)
if block is not None or number!=-1 :
if block in (self.zeroBlock.up,self.zeroBlock.down,self.zeroBlock.left,self.zeroBlock.right) :
self.game.swapBlocks(self.zeroBlock,block)
self.lastMove = self.zeroBlock
self.change = True
def getBlock(self,posx,posy) :
for i in self.blockInfo :
if i[2][0]<=posx<=i[3][0] and i[2][1]<=posy<=i[3][1] :
return (i[0],i[1])
return None,-1
#Main Program + Main Menu Creation
#Function Definition
def buttonPress(event,posx,posy,flag,param) :
global difficulty
if event == cv2.EVENT_LBUTTONDOWN :
if width/2-50<=posx<=width/2+50 and height/2-100<=posy<=height/2-50 :
difficulty=0
elif (width/2)-50<=posx<=(width/2)+50 and (height/2)+50<=posy<=(height/2)+100 :
difficulty=1
#Main Program
cv2.namedWindow("Puzzle-Game")
width,height = 450,450
mainMenuButton = []
difficulty = -1
cv2.setMouseCallback("Puzzle-Game",buttonPress)
while True :
if cv2.waitKey(1) == 27 : break
image = np.zeros((width,height,3),np.uint8)
cv2.rectangle(image,(int(width/2)-50,int(height/2)-100),(int(width/2)+50,int(height/2)-50),(255,0,255),-1)
cv2.rectangle(image,(int(width/2)-50,int(height/2)+50),(int(width/2)+50,int(height/2)+100),(255,0,255),-1)
cv2.putText(image,"8-Puzzle",(int(width/2)-40,int(height/2)-70),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
cv2.putText(image,"15-Puzzle",(int(width/2)-40,int(height/2)+75),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
cv2.putText(image,"Esc - Exit ",(int(width/2)-40,int(height)-30),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,255,255),1)
cv2.imshow("Puzzle-Game",image)
if difficulty == 0 :
game8 = GamePlay(8,"Puzzle-Game",width,height)
game8.mainLoop()
difficulty = -1
cv2.setMouseCallback("Puzzle-Game",buttonPress)
elif difficulty == 1 :
game8 = GamePlay(15,"Puzzle-Game",width,height)
game8.mainLoop()
difficulty = -1
cv2.setMouseCallback("Puzzle-Game",buttonPress)
cv2.destroyAllWindows()