-
Notifications
You must be signed in to change notification settings - Fork 0
/
game4move.py
303 lines (262 loc) · 7.58 KB
/
game4move.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
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
#level4
import pygame
from PygameGame import PygameGame
from Gear import Gear
from Pipe import Pipe
from tile import Tile
def isin(pos):
(mx,my)=pos
mx -= 300
my -= 150
k = (mx//65)*4 + (my//65) + 1
return k
class Game4move(PygameGame):
def init(self):
super().init()
self.bgColor = (255,255,255)
self.count = 50
self.sec = 0
self.min = 0
self.timetext = "TIME : 0:00"
self.moveup = False
self.mute = -1
global curk,k
curk,k = 16,16
global pg,pp
pg = [2,7,10,11,14]
pp = [1,3,4,5,9,13]
Gear.init()
Tile.init()
Pipe.init()
self.win = False
self.gears = pygame.sprite.Group()
self.pipes = pygame.sprite.Group()
self.gears.add(Gear(330-65,180+65,-1,0))
self.gears.add(Gear(330+65*4,180,17,0))
self.tiles = pygame.sprite.Group()
for i in range(4):
for j in range(4):
x=330+65*j
y=180+65*i
num=j*4+i+1
if num != curk: self.tiles.add(Tile(x,y,num))
if num in pg:
self.gears.add(Gear(x,y,num,0))
if num in pp:
if num == 13:
self.pipes.add(Pipe(x,y,num,4))
elif num == 5:
self.pipes.add(Pipe(x,y,num,5))
else:
self.pipes.add(Pipe(x,y,num,1))
self.exit = 20
self.exittype = 1
self.timer = 15
def mousePressed(self,x,y):
if x>=720 and x<=770 and y>=520 and y<=570: self.mute *= (-1)
if x>=30 and x<=90 and y>=520 and y<=560:
self.playing = False
global curk
pos=(x,y)
k=isin(pos)
if k in range(1,17) and not self.win and not self.moveup:
if abs(curk-k)==1 or abs(curk-k)==4:
self.count -= 1
effect = pygame.mixer.Sound('sound/buttonPress.wav')
effect.play()
global pg,pp
j=(curk-1)//4
i=(curk-1)%4
x=330+65*j
y=180+65*i
if k in pp:
pp.remove(k)
pp.append(curk)
for pipe in self.pipes:
if pipe.num == k:
pstyle = pipe.style
self.pipes.remove(pipe)
self.pipes.add(Pipe(x,y,curk,pstyle))
elif k in pg:
pg.remove(k)
pg.append(curk)
for gear in self.gears:
if gear.num == k:
self.gears.remove(gear)
self.gears.add(Gear(x,y,curk,0))
for tile in self.tiles:
if tile.num == k:
self.tiles.remove(tile)
self.tiles.add(Tile(x,y,curk))
curk=k
def keypressed(self,mode,mod):
pass
def timerFired(self,dt):
self.timer -= 2
if self.timer <= 0: self.timer = 15
self.gears.update(self.width, self.height)
for pipe in self.pipes:
pipe.steam = False
for gear in self.gears:
gear.rotate = 0
gear.angleSpeed = 0
x,y,direction = 4,4,"right"
self.checksteam(x,y,direction)
cog = self.checkgear()
gearsound = pygame.mixer.Sound('sound/gear2.wav')
if cog:
gearsound.play()
start = None
for gear in self.gears:
if gear.num == -1:
gear.rotate = 1
gear.angleSpeed = 5
start = gear
self.checkconnect(start,1)
else:
gearsound.stop()
if not self.win: self.checkwin()
def checkgear(self):
for pipe in self.pipes:
if pipe.num == 3:
if pipe.style == 1 or pipe.style == 4:
return pipe.steam
return False
def checksteam(self,x,y,direction):
if x<=4 and y<=4 and x>0 and y>0:
number = (y-1)*4+x
for pipe in self.pipes:
if pipe.num == number:
connected = False
if pipe.style == 1:
if direction == "left":
connected = True
y += 1
if direction == "right":
connected = True
y -= 1
if pipe.style == 4:
if direction == "left":
connected = True
x += 1
direction = "up"
if direction == "down":
connected = True
y -= 1
direction = "right"
if pipe.style == 5:
if direction == "up":
connected = True
y += 1
direction = "left"
if direction == "right":
connected = True
x -= 1
direction = "down"
if connected:
self.exit = number
self.exittype = pipe.style
pipe.steam = True
self.checksteam(x,y,direction)
def checkwin(self):
for gear in self.gears:
if gear.num==17 and gear.rotate != 0:
self.win = True
effect = pygame.mixer.Sound('sound/puzzleDone.wav')
effect.play()
def checkconnect(self,start,rotation):
if start.num == -1:
for gear in self.gears:
if gear.num==2:
gear.rotate = -1
gear.angleSpeed = -5
self.checkconnect(gear,-1)
else:
length=len(self.gears)
b=False
for gear in self.gears:
if gear.num>0:
n1,n2=start.num,gear.num
row1,row2 = (n1-1)//4, (n2-1)//4
if (abs(n1-n2)==1) and (row1==row2) or abs(n1-n2)==4:
if gear.rotate==0:
rotation *= (-1)
gear.rotate = rotation
gear.angleSpeed = gear.rotate * 5
self.checkconnect(gear,rotation)
def redrawAll(self,screen):
color=(150,80,0)
x,y=300,150
global curk
pygame.draw.rect(screen, (0,0,0), pygame.Rect(250,100,400,400))
image = pygame.image.load("image/board1.png").convert_alpha()
image = pygame.transform.scale(image, (800,600))
screen.blit(image, (0,0))
image = pygame.image.load("image/steam3.png").convert_alpha()
image = pygame.transform.scale(image, (33,66))
screen.blit(image, (266+65*4+33,341))
image = pygame.image.load("image/steam4-2.png").convert_alpha()
image = pygame.transform.scale(image, (65,60))
screen.blit(image, (266+33-65,341-65+5))
image = pygame.image.load("image/steam2.png").convert_alpha()
image = pygame.transform.scale(image, (22,55))
screen.blit(image, (266+33-65+20,341-115))
self.tiles.draw(screen)
self.gears.draw(screen)
self.pipes.draw(screen)
pygame.draw.rect(screen, (0,0,0), pygame.Rect(294,144,6,260))
pygame.draw.rect(screen, (0,0,0), pygame.Rect(554,144,6,260))
pygame.draw.rect(screen, (0,0,0), pygame.Rect(294,144,260,6))
pygame.draw.rect(screen, (0,0,0), pygame.Rect(294,399,260,6))
font = pygame.font.SysFont("arial", 35)
counttext = "MOVES : " + str(self.count)
text = font.render(counttext, True, (255,0,0))
screen.blit(text,(15,200))
if self.count <= 0:
if not self.moveup:
effect = pygame.mixer.Sound('sound/Failure.wav')
effect.play()
self.moveup = True
font = pygame.font.SysFont("arial", 80)
text = font.render("Fail !!!", True, (255,0,0))
screen.blit(text,(275,480))
if not self.win and not self.moveup: self.sec += 7
if self.sec >= 6000:
self.sec = 0
self.min += 1
if (self.sec//100) < 10:
self.timetext = "TIME : " + str(self.min) + ":0" + str(self.sec//100)
else:
self.timetext = "TIME : " + str(self.min) + ":" + str(self.sec//100)
font = pygame.font.SysFont("arial", 35)
text = font.render(self.timetext, True, (255,255,255))
screen.blit(text,(15,260))
if self.exit!=3:
j=(self.exit-1)//4
i=(self.exit-1)%4
x=330+65*j
y=180+65*i
if self.exittype == 1 or self.exittype == 4:
x -= 75
y -= 6
pygame.draw.ellipse(screen,(255,255,255),pygame.Rect(x+2.5*self.timer,y+0.5*self.timer,36-2*self.timer,18-self.timer))
if self.exittype == 2 or self.exittype == 5:
x -= 4
y -= 80
pygame.draw.ellipse(screen,(255,255,255),pygame.Rect(x+0.5*self.timer,y+2.5*self.timer,18-self.timer,36-2*self.timer))
font = pygame.font.SysFont("arial", 40)
text = font.render("Back", True, (255,255,255))
screen.blit(text,(30,520))
if self.mute == 1:
image = pygame.image.load("image/mute.png").convert_alpha()
image = pygame.transform.scale(image, (50,50))
screen.blit(image, (720,520))
else:
image = pygame.image.load("image/audio.png").convert_alpha()
image = pygame.transform.scale(image, (50,50))
screen.blit(image, (720,520))
if self.win:
font = pygame.font.SysFont("chalkduster", 70)
text = font.render("Level Passed!", True, (255,255,255))
screen.blit(text,(175,472))
#Game4move(800,600).run()