paint-brush
How To Make Your Own Game in Pythonby@theavidcoder
981 reads
981 reads

How To Make Your Own Game in Python

by Divya KelaskarJune 12th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How To Make Your Own Game in Python: Stone Paper Scissor Game in python. The game will be a Human vs Computer game. Place a infinite while loop (You don’t have to run program again and again.) State the rules of the game. Assign numbers to the user’s choice and assign numbers to computer's choice. Write a replay question and code the condition to break the infinite loop. If you want to play again? (y/n)" ans = input() if ans is 'n' or 'N' : break
featured image - How To Make Your Own Game in Python
Divya Kelaskar HackerNoon profile picture

Hi there !

Today I will share some tips to make a simple stone paper scissor game in python. The game will be a Human vs Computer game.

  • Import the random module by the following:
import random
Why?"
So that computer will generate its own choice.

💡 Place a infinite while loop (You don’t have to run program again and again.)

while True:
  • State the rules of the game.
# Rules of the game   
    print("""Enter your choice : 
    a. Press '1' to select 'Stone'. 
    b. Press '2' to select 'Paper'.
    c. Press '3' to select 'Scissor'.\n""")
  • Take the input from the user according to the rules mention above.
user_choice = int(input("Enter YOUR choice: "))
  • Code the condition when user gives input out of the range.
while user_choice > 3 or user_choice < 1:
    user_choice = int(input("Enter valid input: "))
  • Assign numbers to the user’s choice.
if user_choice == 1: 
        choice_name = 'Stone'
    elif user_choice == 2: 
        choice_name = 'Paper'
    else: 
        choice_name = 'Scissor'
  • Let the computer select its choice and then assign numbers to the computer’s choice.
computer_choice = random.randint(1, 3)  
# Assigning numbers to the computer's choices
    if computer_choice == 1: 
        computer_choicehoice_name = 'Stone'
    elif computer_choice == 2: 
        computer_choicehoice_name = 'Paper'
    else: 
        computer_choicehoice_name = 'Scissor'
  • Write the main logic of the game.
if((user_choice == 1 and computer_choice == 2) or
      (user_choice == 2 and computer_choice ==1 )): 
        print("Paper wins !!! \n", end = "") 
        result = "Paper"
    elif((user_choice == 1 and computer_choice == 3) or
        (user_choice == 3 and computer_choice == 1)): 
        print("Stone wins !!! \n", end = "") 
        result = "Stone"
    else: 
        print("Scissor wins !!!\n", end = "") 
        result = "Scissor"
  • Declare the result.
if result == choice_name: 
        print("\nYOU WIN !!!\n") 
    else: 
        print("\nCOMPUTER WINS !!!\n")
  • Ask a replay question and also code the condition to break the infinite while loop.
print("Do you want to play again? (y/n)") 
    ans = input() 
    if ans == 'n' or ans == 'N': 
        break

🎉 That’s it! Isn’t it easy? Now you have your own game to play!

You can also refer my GitHub repo.

How was the article ? Feedbacks are warmly welcomed.

Previously published at https://medium.com/@theavidcoder/make-your-own-game-in-python-ec0b83f08d35