-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
194 lines (160 loc) · 6.76 KB
/
Copy pathgame.py
File metadata and controls
194 lines (160 loc) · 6.76 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
import tkinter as tk
import pygame
import random
from tkinter import messagebox
from PIL import Image, ImageTk
from data_2 import questions
class EduGame:
def __init__(self, root):
self.root = root
self.root.title("MulmedKuh - Python Edition")
# --- Get ukuran layar kuh ---
screen_w = self.root.winfo_screenwidth()
screen_h = self.root.winfo_screenheight()
# --- Set ukuran layar, jika lebih besar, maka ditambah, jika lebih kecil, maka di zoom ---
if screen_w >= 1600 and screen_h >= 900:
x = (screen_w - 1600) // 2
y = (screen_h - 900) // 2
self.root.geometry(f"1600x900+{x}+{y}")
else:
self.root.state("zoomed")
# --- Load background gambar kerenn ---
bg_img = Image.open("assets/bg.jpg")
bg_img = bg_img.resize((screen_w, screen_h), Image.LANCZOS)
self.bg_photo = ImageTk.PhotoImage(bg_img)
# --- Label background nya ---
self.bg_label = tk.Label(self.root, image=self.bg_photo)
self.bg_label.place(x=0, y=0, relwidth=1, relheight=1)
# --- Faktor skala font (biar adaptif layar nya) ---
scale = min(screen_w / 1600, screen_h / 900)
# --- Shuffle soal biar pertanyaan nya randomized ---
self.questions = questions.copy()
random.shuffle(self.questions)
# --- Init backsound nya ---
pygame.mixer.init()
pygame.mixer.music.load("assets/loop.mp3")
pygame.mixer.music.play(-1)
# --- Load sound effect meme nya ---
self.correct_sound = pygame.mixer.Sound("assets/correct.mp3")
self.wrong_sound = pygame.mixer.Sound("assets/wrong.mp3")
self.win_sound = pygame.mixer.Sound("assets/win.mp3")
self.score = 0
self.peak_score = 0
self.current_q = 0
# --- Judul Game EZ ---
self.title_label = tk.Label(
root,
text="Tugas Multimedia Interaktif",
font=("Arial Black", int(32 * scale), "bold"),
bg="black", fg="gold"
)
self.title_label.pack(pady=int(20 * scale))
# --- Label skor e ---
self.score_label = tk.Label(
root,
text=f"💰 Skor: {self.score}",
font=("Arial", int(20 * scale), "bold"),
bg="black", fg="white"
)
self.score_label.pack(pady=int(15 * scale))
# --- Label pertanyaan e ---
self.question_label = tk.Label(
root,
text="", wraplength=int(1200 * scale),
font=("Arial", int(22 * scale), "bold"),
bg="black", fg="cyan"
)
self.question_label.pack(pady=int(40 * scale))
# --- Frame nya tombol jawaban ---
self.button_frame = tk.Frame(root, bg="black")
self.button_frame.pack(expand=True, fill="both")
# --- Tombol jawaban nya ---
self.buttons = []
# buat 4 tombol jawaban pake perulangan
for i in range(4):
btn = tk.Button(
self.button_frame,
text="", width=int(25 * scale), height=int(2 * scale),
font=("Arial", int(18 * scale), "bold"),
bg="navy", fg="white",
activebackground="gold",
activeforeground="black",
relief="raised",
# klo di klik, panggil fungsi check_answer, buat cek jawaban nya
command=lambda idx=i: self.check_answer(idx)
)
btn.grid(row=i//2, column=i%2,
padx=int(30 * scale), pady=int(30 * scale),
sticky="nsew")
self.buttons.append(btn)
# --- Biar tombol e melar ---
for r in range(2):
self.button_frame.rowconfigure(r, weight=1)
for c in range(2):
self.button_frame.columnconfigure(c, weight=1)
# --- Show soal pertama ---
self.show_question()
def animate_question(self, text):
# buat typing animation nya pertanyaan, biar keren abizz
self.question_label.config(text="")
def type_writer(i=0):
if i < len(text):
self.question_label.config(text=text[:i+1])
self.root.after(30, type_writer, i+1)
type_writer()
# --- Fungsi nampilinss pertanyaan ---
def show_question(self):
if self.current_q < len(self.questions):
q = self.questions[self.current_q]
self.animate_question(q[0])
# Update tombol jawaban nya
for i, choice in enumerate(q[1]):
self.buttons[i].config(text=choice, state="normal",
bg="navy", fg="white")
else:
pygame.mixer.music.stop()
self.win_sound.play()
if self.score < len(self.questions):
messagebox.showinfo("🎉 Selamat!", f"Anda menang!\nTotal skor: {self.score}\nSkor Max: {len(self.questions)}")
else:
messagebox.showinfo("🎉 Selamat!", f"Anda menang Skor Tertinggi!\nTotal skor: {self.score}")
self.root.destroy()
# --- Fungsi highlight tombol ituh, buat klo bener / salah ---
def highlight_button(self, btn, color):
original = btn.cget("bg")
btn.config(bg=color)
self.root.after(400, lambda: btn.config(bg=original))
# --- buat Cek jawaban nya ya novannn ---
def check_answer(self, idx):
q = self.questions[self.current_q]
selected = q[1][idx]
if selected == q[2]:
# Kalo jawabannya bener
# Skor tambahken 1, sambil muter sound nya
self.correct_sound.play()
self.score += 1
# Update label skornya ben gak sama
self.score_label.config(text=f"💰 Skor: {self.score}")
self.highlight_button(self.buttons[idx], "green")
#index pertanyaan ditambah biar gak ngulang2
self.current_q += 1
self.root.after(800, self.show_question)
else:
# kalo jawabannya salah
# muter sound nya sambil ngurangin skor
self.wrong_sound.play()
self.score -= 1
# Update label skornya ben gak sama jugaa
self.score_label.config(text=f"💰 Skor: {self.score}")
self.highlight_button(self.buttons[idx], "red")
# lek skornya habis, gem over
if self.score < 0:
self.root.after(500, self.game_over)
# ini buat ngitung skor tertinggi
if self.peak_score < self.score:
self.peak_score = self.score
# kalo game over, tadi dipanggil jika skor habis
def game_over(self):
pygame.mixer.music.stop()
self.root.destroy()
messagebox.showerror("Game Over", f"Skor anda habis!\nSkor Tertinggi: {self.peak_score}")