Course Content

Exercise 6: Game Development: Snake Water Gun

Today's task for you guys will be to develop your first-ever Python game i.e., "Snake Water Gun."

Most of you must already be familiar with the game. Still, I will provide you with a brief description.

This is a two-player game where each player chooses one object.  As we know, there are three objects, snake, water, and gun. So, the result will be 

  • Snake vs. Water: Snake drinks the water hence wins.
  • Water vs. Gun: The gun will drown in water, hence a point for water
  • Gun vs. Snake: Gun will kill the snake and win.
  • In situations where both players choose the same object, the result will be a draw.

Now moving on to instructions:

  • You have to use a random choice function that we studied in tutorial #38, to select between, snake, water, and gun.
  • You do not have to use a print statement in case of the above function.
  • Then you have to give input from your side.
  • After getting ten consecutive inputs, the computer will show the result based on each iteration.
  • You have to use loops(while loop is preferred).

Code file as described in the video

# Snake water gun
# Create a snake water gun game in Python! Search Snake water gun game in google if you need help on rules and how to play the game!

Comments(10)

coder4web 1 year, 4 months ago
import random

def gameprocess(a,b):
    if (a == 's' and b == 'w') or (a == 'w' and b == 'g') or( a == 'g' and b == 's'):
        return 'c'
    if (a == 'w' and b == 's') or (a == 'g' and b == 'w') or (a == 's' and b == 'g'):
        return 'u'
    elif a == b:
        return 0


def snakewatergun():
    choices = range(10)
    resultDic = []
    gameChoice = {
        's': 'Snake',
        'w': 'Water',
        'g': 'Gun'
    }
    computerScore = 0
    userScore = 0
    for x in choices:
        computerChoice = random.choice(("s", "w", "g"))
        wFlag = False
        while wFlag == False:
            userChoice = input("Select S for snake,W for water and G for gun:").lower()
            if userChoice == "s" or userChoice == "w" or userChoice == 'g' :
                wFlag = True
                result = gameprocess(computerChoice,userChoice)
                if result == 'u':
                    userScore += 1
                    resultDic.append({'Computer Choice': gameChoice[computerChoice], 'User Choice': gameChoice[userChoice], 'Result': 'User Won'})
                elif result == 'c':
                    computerScore += 1
                    resultDic.append({'Computer Choice': gameChoice[computerChoice], 'User Choice': gameChoice[userChoice], 'Result': 'Computer Won'})
                else:
                    resultDic.append({'Computer Choice': gameChoice[computerChoice], 'User Choice': gameChoice[userChoice], 'Result': 'It Tie'})
        if x == choices[-1]:
            print("Game Over")

    if computerScore > userScore:
        print("You Lose game, Final Score is Computer: {s1}, Your: {s2}".format(s1 = computerScore,s2 =userScore))
    elif userScore > computerScore:
        print("You Won game, Final Score is Computer: {s1}, Your: {s2}".format(s1 = computerScore,s2 =userScore))
    elif userScore == computerScore:
        print("It Tie, Final Score is Computer: {s1}, Your: {s2}".format(s1 = computerScore,s2 =userScore))

    for x in resultDic:
        print(x)


snakewatergun()
harry 1 year, 4 months ago
nice!
oyanhr 9 months ago
#I made this vai. Please check and give feedback.
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = "\033[32m"
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    ResetAll = "\033[0m"
list1 = []
mypoint = int(0)
oppoint = int(0)
inpname1 = str(input("Enter your name to start playing: "))
inpname = f"{bcolors.BOLD}{inpname1}{bcolors.ResetAll}"
print(f"Welcome,", inpname, "to Rock-Paper-Scissors Game by OHR.")
print(f"{bcolors.HEADER}Choose one:{bcolors.ResetAll}\n{bcolors.BOLD}R{bcolors.ResetAll} for Rock \n  {bcolors.BOLD}P{bcolors.ResetAll} for Paper\n    {bcolors.BOLD}S{bcolors.ResetAll} for Scissors\n ")
print(f"10 Turns left", {bcolors.ResetAll})
while len(list1)<10:
    inp1 = str(input(f"R/P/S: {bcolors.ResetAll}"))
    list1.append(inp1)
    list2 = ["Rock", "Paper", "Scissors", "Rock", "Scissors", "Paper"]
    import random
    cho = random.choice(list2)
    if cho == "Rock" and inp1=="R":
        print(f"You chose {bcolors.BOLD}Rock{bcolors.ResetAll}\nYour opponent also chose {bcolors.BOLD}Rock{bcolors.ResetAll}. It was a draw!")
        new1 = f"{bcolors.HEADER}Score:{bcolors.ResetAll}\n{inpname}:{bcolors.BOLD}{mypoint}{bcolors.ResetAll}\nOpponent:{oppoint}"
        print(new1)
    elif cho == "Rock" and inp1=="P":
        mypoint = mypoint + 1
        print("You chose Paper\nYour opponent chose Rock.")
        new = f"{bcolors.OKGREEN}You won the point.{bcolors.ResetAll}\nScore:\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print (new)
    elif cho == "Rock" and inp1 == "S":
        print("You chose Scissors\nYour opponent Chose Rock.")
        oppoint = oppoint +1
        new2 = f"{bcolors.FAIL}You lost the point.{bcolors.ResetAll}\nScore:\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print(new2)
    elif cho == "Paper" and inp1=="P":
        print("Both chose Paper.")
        new1 = f"{bcolors.BOLD}Score:{bcolors.ResetAll}\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print(new1)
    elif cho == "Paper" and inp1=="S":
        mypoint = mypoint + 1
        print("You chose Scissors\n And your opponent chose Paper.")
        new = f"{bcolors.OKGREEN}You got the point.{bcolors.ResetAll}\nScore:\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print (new)
    elif cho == "Paper" and inp1 == "R":
        print("You chose Rock\n And your opponent Chose Paper. You lose.")
        oppoint = oppoint +1
        new2 = f"{bcolors.FAIL}You lost the point.{bcolors.ResetAll}\nScore:\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print(new2)
    elif cho == "Scissors" and inp1=="S":
        print("Both chose Scissors.")
        print("No points")
        new1 = f"{bcolors.BOLD}Score:{bcolors.ResetAll}\n{inpname}:{mypoint}\nOpponent:{oppoint}."
        print(new1)
    elif cho == "Scissors" and inp1=="R":
        mypoint = mypoint + 1
        print(f"You chose Rock\n And your opponent chose Scissors")
        new = f"{bcolors.OKGREEN}You got the point.{bcolors.ResetAll}\n{bcolors.BOLD}Score:{bcolors.ResetAll}\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print(new)
    elif cho == "Scissors" and inp1 == "P":
        print("You chose Paper\n And your opponent Chose Scissors. You lose.")
        oppoint = oppoint +1
        new2 = f"{bcolors.FAIL}You lost the point.{bcolors.ResetAll}\n{bcolors.BOLD}Score:{bcolors.ResetAll}\n{inpname}:{mypoint}\nOpponent:{oppoint}"
        print(new2)
    else:
        print("You chose invalid option.")
        del list1[-1]
    print(10 - len(list1), f"Turns left.{bcolors.ResetAll}")
    print("--------------------")
    import time
    time.sleep(1)
else:
    new3 = f"{inpname}:{mypoint}\nOpponent:{oppoint}"
    print(f"10 turns finished.\n{bcolors.BOLD}Final Result:{bcolors.ResetAll}\n", new3)
    if mypoint>oppoint:
        print(f"{bcolors.OKGREEN}You won the match! Thanks for playing. Hope you enjoyed.")
    elif mypoint == oppoint:
        print("Both have same points. Match drawn. Well played.")
    else:
        print(f"{bcolors.FAIL}Opponent won the match. Thanks for playing. Hope you enjoyed.")
oyanhr 9 months ago
https://codeshare.io/G61dwN
hack01001 1 month ago
Nice code
tahim123 8 months ago
import random
c=0
u1=0
for i in range(0,10):
   
    print("Enter that u want s/w/g")
    u=input()
    lst = ['s','w','g']
    comp=random.choice(lst)
    print(comp)
    
    if ((u=='s' and comp=='g')or(u=='g' and comp=='w')or(u=='w' and comp=='s')):
        print ("computer won the game")
        c=c+1
    elif((u=='g' and  comp=='s')or(u=='s' and comp=='w')or(u=='w' and comp=='g')):
        print ("user won the game")
        u1+=1
    else:
        print("game tie")

print("computer win:", c)
print("user win:",u1)
Mr.X 6 months, 3 weeks ago
This game is awesom!  
Mr.X 6 months, 3 weeks ago
This game is awesom!    
Mr.X 6 months, 3 weeks ago
This game is awesome!     
plas9a 3 months, 2 weeks ago
https://www.dropbox.com/sh/s8zca80sfzm4j6g/AAB8i_2VWQbZdDCeOBkofRD9a/temp.txt?dl=0
bohot e kamal ka game he ..real feel paoge vai..plz dekhna

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here