Rock Paper Scissors Game Demo Using Tkinter

Posted by

Demo Pierre Papier Ciseaux Game in Tkinter

Demo Pierre Papier Ciseaux Game in Tkinter

Python’s Tkinter library can be used to create simple and fun games, such as the classic Pierre Papier Ciseaux (rock paper scissors) game. In this demo, we will show you how to create a basic version of the game using Tkinter’s GUI capabilities.

Code:

    
      import tkinter as tk
      import random

      def play_game(user_choice):
          choices = ['Pierre', 'Papier', 'Ciseaux']
          computer_choice = random.choice(choices)

          if user_choice == computer_choice:
              result_label.config(text=f"User's choice: {user_choice}nComputer's choice: {computer_choice}nIt's a tie!")
          elif (user_choice == 'Pierre' and computer_choice == 'Ciseaux') or (user_choice == 'Papier' and computer_choice == 'Pierre') or (user_choice == 'Ciseaux' and computer_choice == 'Papier'):
              result_label.config(text=f"User's choice: {user_choice}nComputer's choice: {computer_choice}nYou win!")
          else:
              result_label.config(text=f"User's choice: {user_choice}nComputer's choice: {computer_choice}nComputer wins!")

      root = tk.Tk()
      root.title("Pierre Papier Ciseaux Game")

      choices = ['Pierre', 'Papier', 'Ciseaux']

      user_choice_label = tk.Label(root, text="Choose your move:")
      user_choice_label.pack()

      user_choice_var = tk.StringVar()
      user_choice_var.set("Pierre")
      user_choice_menu = tk.OptionMenu(root, user_choice_var, *choices)
      user_choice_menu.pack()

      play_button = tk.Button(root, text="Play", command=lambda: play_game(user_choice_var.get()))
      play_button.pack()

      result_label = tk.Label(root, text="")
      result_label.pack()

      root.mainloop()
    
  

Explanation:

We start by importing the necessary libraries: tkinter for the GUI and random for generating the computer’s choice. We then define the play_game function, which takes the user’s choice as an argument and compares it with the computer’s random choice. Based on the comparison, the function updates the result_label with the outcome of the game.

We create the main window using tk.Tk() and set its title. We define the choices for the game and create a dropdown menu for the user to select their move. We also add a “Play” button that calls the play_game function when clicked. Finally, we display the result of the game using the result_label.

Conclusion:

In this demo, we have shown how to create a simple Pierre Papier Ciseaux game using Tkinter. This example demonstrates the basic use of tkinter’s widgets and event handling to create an interactive game interface. You can further expand and customize this game by adding more features and improving the visual design.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ndoumbeseye-op8cq
6 months ago

sur la ligne de label_config qu'est ce que vous avez mis avant le score et pouvez vous nous montrer tous l'ecran