-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guessing_Game.py
88 lines (66 loc) · 2.42 KB
/
Guessing_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
import random
import tkinter as tk
# Define the data (same as before)
data = {
"Apple": -1,
"Grape": -1,
"cucumber": -1,
"carrot": -1,
"Mango": 5,
}
# Initialize score
player_score = 0
# Function to generate a random value and points
def get_random_value_and_points():
random_value = random.choice(list(data.keys()))
points = data[random_value]
return random_value, points
# Function to start the game
def start_game():
global player_name, current_chance, result_text, player_score
# Get player name from the input field
player_name = player_name_entry.get()
# Reset chance and result
current_chance = 1
player_score = 0
result_text.set("")
# Enable the "Next Chance" button
next_chance_button.config(state="normal")
# Display initial message
result_text.set(f"Hi {player_name}, get ready for the fruit frenzy!")
# Function to handle button click
def next_chance():
global current_chance, player_score
# Check if remaining chances
if current_chance == 4:
next_chance_button.config(state="disabled")
result_text.set(f"Thanks for playing, {player_name}! You scored {player_score} points.")
return
# Generate random value and points
random_value, points = get_random_value_and_points()
# Update player score
player_score += points
# Update result text with current chance and earned points
result_text.set(f"Hi {player_name}, chance {current_chance}/3: You got {random_value}! You earned {points} points.")
current_chance += 1
# Initialize Tkinter window
window = tk.Tk()
window.title("Fruit Frezy!:)")
# Set window size
window.geometry("400x600")
# Player name entry
player_name_label = tk.Label(window, text="Enter your name:")
player_name_label.place(relx=0.5, rely=0.36, anchor="center")
player_name_entry = tk.Entry(window)
player_name_entry.place(relx=0.5, rely=0.4, anchor="center")
# Start game button
start_game_button = tk.Button(window, text="Start Game", command=start_game)
start_game_button.place(relx=0.5, rely=0.46, anchor="center")
# Result text
result_text = tk.StringVar()
result_label = tk.Label(window, textvariable=result_text)
result_label.place(relx=0.5, rely=0.5, anchor="center")
# Next chance button
next_chance_button = tk.Button(window, text="Next Chance", command=next_chance, state="disabled")
next_chance_button.place(relx=0.5, rely=0.55, anchor="center")
window.mainloop()