/
Main.py
454 lines (333 loc) · 15.6 KB
/
Main.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
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
import sys
from tkinter import *
from random import * # for random placement of houses
# Import from our files
from TextureHandler import Textures
from Dog import Dog
from Cat import Cat
from Gui import GUI
class Map():
"""Base class for maps"""
def __init__(self,filePath):
self.mapList = []
self.ReadSplit(filePath)
self.ExitX = 100
self.ExitY = 100
def ReadSplit(self,filePath):
"""Reads the map and places into 2D array/list,
no returns, puts it directly into class"""
file = open(filePath,"r")
string = file.read()
file.close()
Pre = string.rsplit("\n")
for i in Pre:
self.mapList.append(i.split())
def DisplayMap(self,gui,h = 500, w = 500, d = 50):
for i in range(int(h / d)):
y = i * d
for j in range(int(w / d)):
x = j * d
# Add more elif for more options
if self.mapList[i][j] == "1":
gui.CreateImageRectangle(Textures.TextureDict["grass"],x,y)
elif self.mapList[i][j] == "2":
gui.CreateImageRectangle(Textures.TextureDict["path"],x,y)
elif self.mapList[i][j] == "3":
gui.CreateImageRectangle(Textures.TextureDict["tree"],x,y)
elif self.mapList[i][j] == "4":
gui.CreateImageRectangle(Textures.TextureDict["fenceH"],x,y)
elif self.mapList[i][j] == "5":
gui.CreateImageRectangle(Textures.TextureDict["fenceV"],x,y)
elif self.mapList[i][j] == "6":
gui.CreateImageRectangle(Textures.TextureDict["bush"],x,y)
elif self.mapList[i][j] == "7":
gui.CreateImageRectangle(Textures.TextureDict["floor"],x,y)
elif self.mapList[i][j] == "8":
gui.CreateImageRectangle(Textures.TextureDict["wall"],x,y)
elif self.mapList[i][j] == "9":
gui.CreateImageRectangle(Textures.TextureDict["table"],x,y)
elif self.mapList[i][j] == "10":
gui.CreateImageRectangle(Textures.TextureDict["bed"],x,y)
elif self.mapList[i][j] == "11":
gui.CreateImageRectangle(Textures.TextureDict["door"],x,y)
# used to determine where the exit is in a house
self.ExitX = x
self.ExitY = y
elif self.mapList[i][j] == "12":
gui.CreateImageRectangle(Textures.TextureDict["box"],x,y)
else:
raise ValueError("Unidentified symbol was found in MapList")
def Search(self,gui,strTarget):
"""Search Imlementation for houses, but no navigation to houses :(
strTarget - string for targeted texture"""
canvasItems = gui.canvas.find_all()
foundList = []
# Linear search through list of items on canvas
# we cannot use binary search since its not a number and there is no good criteria to sort by
# since we are searching for an object with specific picture
for i in canvasItems:
if gui.canvas.itemcget(i,"image") == Textures.TextStr(strTarget):
foundList.append(gui.canvas.coords(i))
return foundList
# Should be changed in child class or not used
def Execute():
print("BASE CLASS, shit went wrong OR u suck, @execute")
def preChange():
print("BASE CLASS, shit went wrong OR u suck, @change")
class mExterior(Map):
"""Exterior map class,
Inherits from map"""
def __init__(self, filePath):
super().__init__(filePath)
def Execute(self,gui,dMaps,house, Cselection):
gui.ClearFrame()
gui.CreateCanvas()
self.DisplayMap(gui)
house.CreateObjects(gui.canvas,5,"house","grass")
house.PlaceAllObjects(gui)
if Cselection == 1:
cat = Cat(gui,Info.name,Textures.TextureDict["cat"],self.ExitX,self.ExitY)
elif Cselection == 2:
cat = Cat(gui,Info.name,Textures.TextureDict["snowball"],self.ExitX,self.ExitY)
elif Cselection == 3:
cat = Cat(gui,Info.name,Textures.TextureDict["tom"],self.ExitX,self.ExitY)
elif Cselection == 4:
cat = Cat(gui,Info.name,Textures.TextureDict["scratchy"],self.ExitX,self.ExitY)
elif Cselection == 5:
cat = Cat(gui,Info.name,Textures.TextureDict["pink"],self.ExitX,self.ExitY)
gui.root.bind("<z>",lambda event: self.preChange(gui.canvas.coords(cat.catID),gui,dMaps,house,cat, Cselection)) # changes to inside map, <Return> is "enter" key
def preChange(self,coords,gui,dMaps,house,cat, Cselection): # move into one of the classes
"""Takes x,y coords in list,
Checks if cat is standing on house and if yes proceeds to inside,
or if the cat is on grass, ends the game"""
ground = gui.canvas.itemcget(gui.canvas.find_overlapping(coords[0],coords[1],coords[0]+50,coords[1]+50)[0],"image")
if ground == Textures.TextStr("bush"):
cat.showInventory(GameOver = True)
bool, ID = house.CheckOverlap(coords[0],coords[1],True)
self.ExitX, self.ExitY = coords
if not bool:
dMaps["inside"].Execute(gui,dMaps,house,ID, Cselection)
class mInterior(Map):
"""Interior map class,
Inherits from map"""
def __init__(self, filePath):
super().__init__(filePath)
def Execute(self,gui,dMaps,house,ID, Cselection):
gui.ClearFrame()
gui.CreateCanvas()
self.DisplayMap(gui)
house.FillHouse(gui,2,ID)
if Cselection == 1:
cat = Cat(gui,Info.name,Textures.TextureDict["cat"],self.ExitX,self.ExitY,house.List[ID].item)
elif Cselection == 2:
cat = Cat(gui,Info.name,Textures.TextureDict["snowball"],self.ExitX,self.ExitY,house.List[ID].item)
elif Cselection == 3:
cat = Cat(gui,Info.name,Textures.TextureDict["tom"],self.ExitX,self.ExitY,house.List[ID].item)
elif Cselection == 4:
cat = Cat(gui,Info.name,Textures.TextureDict["scratchy"],self.ExitX,self.ExitY,house.List[ID].item)
elif Cselection == 5:
cat = Cat(gui,Info.name,Textures.TextureDict["pink"],self.ExitX,self.ExitY,house.List[ID].item)
dog = Dog(int(Info.difficulty),gui,Textures.TextureDict["dog"],cat)
gui.root.bind("<z>",lambda event: self.preChange(cat.catID,gui,dMaps,house,dog, Cselection)) # changes to ouside map, <Return> is "enter" key
dog.movement(gui)
def preChange(self,cat,gui,dMaps,house,dog, Cselection):
"""changes into outside when on sofa only :D"""
x,y = gui.canvas.coords(cat)
if x == self.ExitX and y == self.ExitY:
dog.STOP = True # DO NOT REMOVE, UNLESS U WANT MOVING HOUSES ...
dMaps["outside"].Execute(gui,dMaps,house, Cselection)
class Obj:
# This would be a Struct if it was written in C++
def __init__(self,x,y,texture):
self.x = x
self.y = y
self.texture = texture
self.ID = None
# used for puting in boxes inside houses and putting items inside boxes :)
self.item = None
class BaseRandomObject:
"""Random object Base class"""
def __init__(self):
self.List = []
def PlaceObject(self,gui,i):
"""places created object on tkinter canvas,
requires gui"""
self.List[i].ID = gui.CreateImageRectangle(self.List[i].texture,self.List[i].x,self.List[i].y,returnOn = True)
def CreateObjects(self,canvas,amount,texture,chkImage):
"""Creates specified amount of objects"""
while len(self.List) != amount:
rInt = randint(1,100)
x,y = canvas.coords(rInt)
currImage = canvas.itemcget(rInt,"image")
# Only place on specific tile and not overlap
if currImage == Textures.TextStr(chkImage) and self.CheckOverlap(x,y):
self.List.append(Obj(x,y,Textures.TextureDict[texture]))
def CheckOverlap(self,x,y,returnID = False):
"""return true if none of current objects use the spot
if returnID is True return bool and ID where it stopped"""
ID = 0
for h in self.List:
if h.x == x and h.y == y:
if returnID:
return False, ID
else:
return False
ID += 1
if returnID:
return True, ID
else:
return True
def PlaceAllObjects(self,gui):
"""places ALL created objects on tkinter canvas,
uses PlaceObject method"""
for i in range(len(self.List)):
self.PlaceObject(gui,i)
class Item:
"""Items go inside boxes! and to cat inventory"""
def __init__(self,item,quality):
"""Enter item name, and quality int(1 being low quality, 5 being high quality) """
self.item = item
self.quality = quality
# few special methods for printing
def __repr__(self):
return ("<" + self.item + ", " + str(self.quality) + " quality>")
def __str__(self):
return ("<" + self.item + ", " + str(self.quality) + " quality>")
class Box(BaseRandomObject):
"""Boxes Inside houses!"""
def __init__(self):
super().__init__()
self.created = False
def FillBox(self,ID):
# places item into the box randomly
while self.List[ID].item == None:
rInt = randint(0,len(Info.availableItems)-1)
rInt2 = randint(1,5)
if Info.selectedItems[rInt]:
self.List[ID].item = Item(Info.availableItems[rInt],rInt2)
def CreateObjects(self, canvas, amount, texture, chkImage):
super().CreateObjects(canvas, amount, texture, chkImage)
for i in range(len(self.List)):
self.FillBox(i)
def GiveItem(self,ID,gui):
# gives item to cat and destroys box
item = self.List[ID].item
gui.canvas.delete(self.List[ID].ID)
self.List.pop(ID)
return item
class House(BaseRandomObject):
"""Houses placeable in Outside map """
def __init__(self):
super().__init__()
self.boxCreated = False
def FillHouse(self,gui,amount,ID):
if self.List[ID].item == None:
self.List[ID].item = Box()
self.List[ID].item.CreateObjects(gui.canvas,amount,"box","floor")
self.List[ID].item.PlaceAllObjects(gui)
class Info:
"""Basic info from main menu"""
name = ""
difficulty = ""
availableItems = ["Food","Toys","Mouse","Bells","Catnip","Milk","Trophy"]
selectedItems = []
def Transition(diffvar, catname, items,gui, Cselection):
for i in items:
if i.get() == 1:
Info.selectedItems.append(True)
else:
Info.selectedItems.append(False)
Info.name = catname.get()
Info.difficulty = diffvar.get()
Info.cat = Cselection.get()
# Setup for rest of the program
Textures.ReadTexture()
dMaps = {}
dMaps["outside"] = mExterior("Layouts/Outside Layout.txt")
dMaps["inside"] = mInterior("Layouts/Inside Layout.txt")
house = House()
dMaps["outside"].Execute(gui,dMaps,house, Info.cat)
def mainmenu(gui):
"""Main menu, allows to choose all the things"""
title = Label(gui.frame, text= "CAT HUNT!", fg="blue",font = "bold" )
title.pack()
gui.CreateEmptySpace(gui.frame)
#inputing the cats name
frame1 = Frame(gui.frame)
frame1.pack( )
catnametext = Label(frame1,text="Cat name?", fg = "blue").pack(side = LEFT)
catname = Entry(frame1, fg = "blue")
catname.pack(side = LEFT)
gui.CreateEmptySpace(gui.frame)
#Different types of items
framebig = Frame(gui.frame)
framebig.pack()
lookfor = Label(framebig, text= "What do you wish to look for?", fg="blue")
lookfor.pack()
# adds a chechbox for every item in availableItems
var = []
for item in Info.availableItems:
var.append(gui.CreatCheckBox(framebig,item))
#select cat
selectcat = Label(gui.frame, text="Select your cat",fg="blue", font = ("Arial",14))
selectcat.pack()
catframe = Frame(gui.frame)
catframe.pack()
Cselection = IntVar()
nyan = Radiobutton(catframe, text="Nyan cat", variable = Cselection, value = 1)
nyan.pack(side = LEFT)
nyan.select()
snowball = Radiobutton(catframe, text="Snowball II", variable = Cselection, value = 2)
snowball.pack(side = LEFT)
tom = Radiobutton(catframe, text="Tom", variable = Cselection, value = 3)
tom.pack(side = LEFT)
scratchy = Radiobutton(catframe, text="Scratchy", variable = Cselection, value = 4)
scratchy.pack(side = LEFT)
pink = Radiobutton(catframe, text="Pink Panther", variable = Cselection, value = 5)
pink.pack(side = LEFT)
#select difficutly
diffvar = IntVar()
diff = Label(gui.frame, text="Select a difficulty",fg="blue", font = ("Arial",14))
diff.pack()
frame3 = Frame(gui.frame)
frame3.pack()
easy = Radiobutton(frame3, text = "Easy", fg = "green", variable = diffvar, value = 1)
easy.pack(side = LEFT)
med = Radiobutton(frame3, text = "Medium", fg = "orange", variable = diffvar, value = 2)
med.pack(side = LEFT)
med.select() # default difficulty
hard = Radiobutton(frame3, text = "Hard", fg = "red", variable = diffvar, value = 3)
hard.pack(side = LEFT)
gui.CreateEmptySpace(gui.frame)
#Tutorial Button
tutorialbutton = Button(gui.frame, text="TUTORIAL",font = ("Arial",14,"bold"),fg = 'blue', command = lambda: tut_window(gui))
tutorialbutton.pack()
#Start Button
startbutton = Button(gui.frame, text="PLAY!",font = ("Arial",14,"bold"),fg ='purple',command = lambda: Info.Transition(diffvar,catname,var,gui, Cselection))
startbutton.pack()
gui.CreateEmptySpace(gui.frame)
def tut_window(self):
# Function to create tutorial window.
root2 = Tk()
root2.title("Tutorial")
# Text to be displayed in window.
Tutorial_text = "Welcome to CatGame tutorial. Below is the controls for the game:\n\nTo move the cat simply use the directional arrow keys.\n\nz - enter/exit house. Press this when on the house icon from outside to enter; or on the couch when inside the house to exit.\n\nx - pick up item. Inside the house there will be boxes to collect with items inside. Simply go over the box and press x. The item is then stored in your inventory.\n\nc - view the inventory. If you wish to see what is currently in your inventory simply press c.\n\nAvoid the dogs! If you come in contact with a dog this will result in death. You will also lose an item that was collected.\n\nTo finish the game go to any of the bushes and press 'z'."
# Creates the message withe the text being 'Tutorial_text'.
msg = Message(root2, text = Tutorial_text)
# Configurates the text font and background.
msg.config(bg='white',fg='blue', font=('times',16,'bold'))
# Packs the message into the window root2.
msg.pack(side=LEFT)
def main():
# Setup tkinter
root = Tk()
gui = GUI(root,"Cat Hunt")
# To main menu and beyond
mainmenu(gui)
# added to exit the program if the main window is closed.
# to close inventory window or any others if the main one is closed
root.protocol("WM_DELETE_WINDOW",lambda:sys.exit())
# Mainloop, MUST ALWAYS BE ON BOTTOM
root.mainloop()
if __name__ == "__main__":
sys.exit(main())