Python Task 4: Codsoft Rock Paper Scissors Game GUI Application for Computing Devotees

Posted by


In this tutorial, we will be creating a Rock Paper Scissor game using the Codsoft Python programming language with a GUI-based application. This project will help you understand how to create a simple game using Python and the Tkinter library.

Before we begin, make sure you have Python installed on your computer. If not, you can download Python from the official website (https://www.python.org/downloads/).

Step 1: Install Tkinter

Tkinter is a built-in Python library for creating GUI applications. To install Tkinter, open your command prompt or terminal and enter the following command:

pip install tk

Step 2: Create a new Python file

Create a new Python file and save it as "rock_paper_scissor.py".

Step 3: Import necessary libraries

In the Python file, import the necessary libraries – tkinter and random.

from tkinter import *
import random

Step 4: Create the main window

Create a main window for the game using Tkinter.

root = Tk()
root.title("Rock Paper Scissor Game")

Step 5: Create labels and buttons

Create labels and buttons for the game options – Rock, Paper, Scissor, and the result.

label = Label(root, text="Choose an option:")
label.pack()

rock_btn = Button(root, text="Rock")
rock_btn.pack()

paper_btn = Button(root, text="Paper")
paper_btn.pack()

scissor_btn = Button(root, text="Scissor")
scissor_btn.pack()

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

Step 6: Define game logic

Define the game logic and outcome based on the user’s choice and the computer’s choice.

def game(user_choice):
    choices = ["Rock", "Paper", "Scissor"]
    computer_choice = random.choice(choices)

    if user_choice == computer_choice:
        result = "It's a tie!"
    elif (user_choice == "Rock" and computer_choice == "Scissor") or (user_choice == "Paper" and computer_choice == "Rock") or (user_choice == "Scissor" and computer_choice == "Paper"):
        result = "You win!"
    else:
        result = "Computer wins!"

    result_label.config(text="Computer chose: " + computer_choice + ". " + result)

Step 7: Bind buttons to game logic

Bind the buttons to the game logic so that when the user clicks on a button, the game logic is invoked.

rock_btn.config(command=lambda: game("Rock"))
paper_btn.config(command=lambda: game("Paper"))
scissor_btn.config(command=lambda: game("Scissor"))

Step 8: Run the main loop

Run the main loop to start the game.

root.mainloop()

That’s it! You have successfully created a Rock Paper Scissor game using Python and Tkinter. Run the Python file and start playing the game.

I hope you found this tutorial helpful. Feel free to customize and enhance the game further by adding more features and functionalities. Happy coding!