learn-python/contrib/mini-projects/Rock_Paper_Scissors_Game.md

85 wiersze
3.2 KiB
Markdown
Czysty Zwykły widok Historia

2024-05-15 13:29:12 +00:00
# Rock Paper Scissors Game
This is a simple implementation of the classic rock-paper-scissors game in Python.
2024-05-15 14:24:12 +00:00
## Code Explanation:
2024-05-15 13:29:12 +00:00
2024-05-15 14:24:12 +00:00
In this section, we import the required libraries (`tkinter` for GUI and `random` for generating computer choices) and define two functions:
- `determine_winner(user_choice, computer_choice)`:
- This function determines the winner of the game based on the choices made by the user and the computer.
- It returns a tuple containing the result of the game and the computer's choice.
- `play_game()`:
- This function handles the gameplay logic.
- It gets the user's choice from the radio buttons, generates a random choice for the computer, determines the winner using the `determine_winner()` function, and updates the result and computer pick labels accordingly.
### Imports and Function Definitions:
2024-05-15 13:23:35 +00:00
```python
import tkinter as tk
import random
def determine_winner(user_choice, computer_choice):
"""Determine the winner of the game."""
if user_choice == computer_choice:
return "It's a tie!", computer_choice
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
return "You win!", computer_choice
else:
return "Computer wins!", computer_choice
def play_game():
"""Play the game and display the result."""
user_choice = user_var.get()
computer_choice = random.choice(["rock", "paper", "scissors"])
result, computer_pick = determine_winner(user_choice, computer_choice)
result_label.config(text=result)
computer_label.config(text=f"Computer picked: {computer_pick}")
2024-05-15 14:24:12 +00:00
```
### GUI Setup:
```python
2024-05-15 13:23:35 +00:00
# Create main window
root = tk.Tk()
root.title("Rock Paper Scissors")
# User choice options
user_var = tk.StringVar()
user_var.set("rock") # Default choice
choices = ["rock", "paper", "scissors"]
for choice in choices:
rb = tk.Radiobutton(root, text=choice, variable=user_var, value=choice)
rb.pack()
2024-05-15 14:24:12 +00:00
```
- Here, we create the main window for the game using `tkinter.Tk()`. We set the title to "Rock Paper Scissors".
2024-05-15 14:27:34 +00:00
- We define a `StringVar` to store the user's choice and set the default choice to "rock".
2024-05-15 14:24:12 +00:00
- We create radio buttons for the user to choose from ("rock", "paper", "scissors") and pack them into the main window.
```
```
### Play Button and Result Labels:
```python
2024-05-15 13:23:35 +00:00
# Play button
play_button = tk.Button(root, text="Play", command=play_game)
play_button.pack()
# Result label
result_label = tk.Label(root, text="", font=("Helvetica", 16))
result_label.pack()
# Computer pick label
computer_label = tk.Label(root, text="", font=("Helvetica", 12))
computer_label.pack()
2024-05-15 14:24:12 +00:00
```
2024-05-15 14:27:34 +00:00
- We create a "Play" button that triggers the `play_game()` function when clicked, using `tkinter.Button`.
- We create two labels to display the result of the game (`result_label`) and the computer's choice (`computer_label`). Both labels initially display no text and are packed into the main window.
2024-05-15 14:24:12 +00:00
```
```
2024-05-15 13:23:35 +00:00
2024-05-15 14:24:12 +00:00
### Mainloop:
```python
2024-05-15 13:23:35 +00:00
root.mainloop()
2024-05-15 14:24:12 +00:00
```
2024-05-15 14:27:34 +00:00
- Finally, we start the Tkinter event loop using `root.mainloop()`, which keeps the GUI window open and responsive until the user closes it.
2024-05-15 14:24:12 +00:00
-