Building my first ever game in Python - Rock Scissor Paper Game

Hi friends!

Today I want to talk to you about my new achievement in the Python learning journey. I call it an achievement because it matters much to me as I am an absolute beginner in this computer science field. 

Lately, I started watching some tutorials regarding Python learning. I am learning much from them and I will be sharing here with you guys also what I learn each day. 

The notion of 'game creation' excites me tremendously. And just recently I learnt from the same tutorials how to build my first basic game.

Games are fun to play now and then. But the idea of creating them is far more exciting (at least this is so in my mind). 

This is what I learned today.

I built my first ever game in Python. The game is called Rock Scissor Paper Game. It is a very simple and basic game. But the creation of it brought so much joy to me. Let me share the exact code with you people also.

Following is The exact code that will create this Rock Scissor Paper Game is shared below. If you want to, you can copy and paste it into your favorite IDE and start playing. 

Rock Scissor Paper Game Source Code

import random
import re

def rps_game(computer, you):
    if computer == you:
        return None
    elif computer == "r":
        if you == "p":
            return True
        elif you == "s":
            return False
    elif computer == "p":
        if you == "s":
            return True
        elif you == "r":
            return False
    elif computer == "s":
        if you == "r":
            return True
        elif you == "p":
            return False

rNUM = random.randint(1, 3)
if rNUM == 1:
    computer = "r"
elif rNUM == 2:
    computer = "p"
elif rNUM == 3:
    computer = "s"

print("Computer Turn: Rock(r), Paper(p) or Scissor(s)? ")
you = input("Your Turn: Rock(r), Paper(p) or Scissor(s)? ")

if not re.match("^[p, s, r]*$", you):
    print("Wrong input! Choose b/w (s) or (p) or (r) only.")

print("The computer chose option", computer)
print("You chose option", you)

winFunc = rps_game(computer, you)

if winFunc == None:
    print("The Game is a DRAW")
elif winFunc:
    print("Hurray! You WON the Game.")
else:
    print("Ooops! You lost the Game.")


This is how the game looks like when it is played in some / any IDE.

Rock Scissor Paper Game in PLAY Mode



Hope you learned something new from this and enjoyed your time here!

Thank you very much!

Have a nice day!

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes