-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
122 lines (91 loc) · 4.59 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
import tkinter as tk
from tkinter import font
class GameScreen():
def __init__(self, master, image, roi, inventory_item=None, help_text=None):
self.master = master
self.roi = roi
self.image = tk.PhotoImage(file=image)
self.inventory_item = inventory_item
self.help_text = help_text
def on_click(self, event):
if (self.roi[0] <= event.x <= self.roi[2]
and self.roi[1] <= event.y <= self.roi[3]):
if self.inventory_item:
self.master.add_inventory_item(self.inventory_item)
self.master.show_next_screen()
class Game(tk.Toplevel):
def __init__(self):
super().__init__()
self.inventory_slots = []
self.inventory_slots_in_use = []
self.current_screen_number = 0
self.success_font = font.Font(family="ubuntu", size=50, weight=font.BOLD)
self.title("Point and Click")
self.geometry("800x640")
self.resizable(False, False)
self.key_image = tk.PhotoImage(file="./assets/key.png")
self.question_mark_image = tk.PhotoImage(file="./assets/questionmark.png")
self.screen = tk.Canvas(self, bg="white", width=500, height=800)
self.right_frame = tk.Frame(self, width=300, height=800)
self.right_frame.pack_propagate(0)
self.help_var = tk.StringVar(self.right_frame)
self.help_var.set("Try Clicking Something")
self.help_box = tk.Label(self.right_frame, textvar=self.help_var, background="black", foreground="white", padx=10, pady=20)
self.help_box.pack(side=tk.TOP, fill=tk.X, padx=10, pady=10)
inventory_title = tk.Label(self.right_frame, text="Inventory:", background="grey", foreground="white")
inventory_space = tk.Frame(self.right_frame, background="lightgrey", width=300, height=320)
inventory_space.pack_propagate(0)
inventory_space.pack(side=tk.BOTTOM)
inventory_title.pack(side=tk.BOTTOM, fill=tk.X)
inventory_slot_1 = tk.Button(inventory_space, image=self.question_mark_image, width=50, height=50)
inventory_slot_2 = tk.Button(inventory_space, image=self.question_mark_image, width=50, height=50)
inventory_slot_3 = tk.Button(inventory_space, image=self.question_mark_image, width=50, height=50)
inventory_slot_1.pack(pady=(40,20), padx=20)
inventory_slot_2.pack(pady=20, padx=20)
inventory_slot_3.pack(pady=(20,0), padx=20)
self.inventory_slots.append(inventory_slot_1)
self.inventory_slots.append(inventory_slot_2)
self.inventory_slots.append(inventory_slot_3)
self.right_frame.pack(side=tk.RIGHT)
self.screen.pack(side=tk.LEFT)
self.screen.bind("<Button-1>", self.handle_click)
def handle_click(self, event):
self.active_screen.on_click(event)
def set_game_screens(self, game_screens):
self.game_screens = game_screens
def display_screen(self, game_screen_number):
self.active_screen = self.game_screens[game_screen_number]
self.screen.delete("all")
self.screen.create_image((250,400), image=self.active_screen.image)
self.help_var.set(self.active_screen.help_text)
def show_next_screen(self):
self.current_screen_number += 1;
if self.current_screen_number < len(self.game_screens):
self.display_screen(self.current_screen_number)
else:
self.screen.delete("all")
self.screen.configure(bg="black")
self.screen.create_text((250,300), text="You Win!", font=self.success_font, fill="white")
def add_inventory_item(self, item_name):
next_available_inventory_slot = len(self.inventory_slots_in_use)
if next_available_inventory_slot < len(self.inventory_slots):
next_slot = self.inventory_slots[next_available_inventory_slot]
if item_name == "key":
next_slot.configure(image=self.key_image)
self.inventory_slots_in_use.append(item_name)
def play(self):
if not self.game_screens:
print("No screens added!")
else:
self.display_screen(0)
def main():
game = Game()
scene1 = GameScreen(game, "./assets/scene1.png", (378,135,427,217), "key", "You Need To Leave but the Door is Locked!")
scene2 = GameScreen(game, "./assets/scene2.png", (117,54,329,412), None, "You Got the Key!")
scene3 = GameScreen(game, "./assets/scene3.png", (117,54,329,412), None, "The Door is Open!")
all_screens = [scene1, scene2, scene3]
game.set_game_screens(all_screens)
game.play()
game.mainloop()
if __name__ == "__main__":
main()