This is an ancient game that is full of fun and is generally played between two players. Somewhat it is similar to Rock, Paper & Scissor.
Good Points
- This game can be played without the need for a second human opponent.
- The second opponent in this game will be a computer.
- This exercise is part of code with harry's Python Tutorial for Beginners
- This code is completely written by me.
How does this game work?
The game is developed for single-player only with easy & hard mode.
- Easy mode allows you to give a total of 10 chances to score better.
- Hard mode allows you to give a total of 5 chances to score better.
And the Rules are-
The game keeps running until you don't reach the last value of your selected mode(Easy or Hard).
1. Water will win from the gun, the gun will win from snake, the snake will
win from water.
2. Out of user or computer whoever scores the highest, wins the match.
3. "Tie" is given if both of them chooses the same shape & no score is rewarded
to any player.
4. Scores are given on the basis of each chance's winner.
Final Code/Solution to Code with Harry's Project 1 - Snake, Water, Gun Game at Timestamp: 06:52:30
import random
import os
logo = '''
███ █ █ ███ █ █ ███ █ █ ███ ███ ███ ███ ███ █ █ █ █ ███ ███ █ █ ███
█ ██ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ ██ ██ █
█ █ █ █ █ █ ██ ███ █ █ █ █ █ █ ███ ██ █ █ █ █ █ █ █ █ █ █ █ █ ███
█ █ ██ ███ █ █ █ ██ ██ ███ █ █ █ █ █ █ █ █ █ ██ █ █ ███ █ █ █
███ █ █ █ █ █ █ ███ █ █ █ █ █ ███ █ █ ███ ███ █ █ ███ █ █ █ █ ███
'''
# Generated Ascii art from here, https://ascii.co.uk/text & font is Maxiwi
snake = '''
{0O}
\__/
/^/
( (
\_\_____
(_______)
(_________()Oo
'''
water='''
_____________________________________________
- ~~~ - ---- - - -
~ -- ~ ~ -- -- ``
--- --- --- / ~
---- ~ ---- ---- ~ /
----- ----- ----- / ' '~
~ .-~~-. ~ ------ / ~ ~
___________________________________________
'''
gun= '''
.-.____________________.-.
___ _.' .-----. _____________|======+
/_._/ ( | /_____________|
/ ` _ ____/
|_ .\( \\
.' `-._/__`_//
.' |""""'
/ /
/ |
| '
| \
`-._____.-'
'''
end_game= False
easy=10
hard=5
user_score=0
computer_score=0
shapes = [snake, water , gun]
def shape_choice(shape):
'''Allows to choose shape for a computer'''
return random.choice(shape)
def game_decision(usr_choose_shape,computer_choose_shape,shapes,mode):
'''Water will win from gun, gun will win from snake, snake will win from water.
Snake--Water--Gun'''
global user_score
global computer_score
if shapes[usr_choose_shape] == shapes[0] and computer_choose_shape == shapes[1]:
print(f"This chance is won by YOU :)")
user_score+=1
print(f'\t Your Score: {user_score}')
print(f"\t Computer's Score: {computer_score}")
print(f" You're left with {mode} chance(s).")
elif shapes[usr_choose_shape]==shapes[2] and computer_choose_shape == shapes[0]:
print("This chance is won by YOU :)")
user_score+=1
print(f'\t Your Score: {user_score}')
print(f"\t Computer's Score: {computer_score}")
print(f" You're left with {mode} chance(s).")
elif shapes[usr_choose_shape]==shapes[1] and computer_choose_shape == shapes[2]:
print("This chance is won by YOU :)")
user_score+=1
print(f'\t Your Score: {user_score}')
print(f"\t Computer's Score: {computer_score}")
print(f" You're left with {mode} chance(s).")
else:
print('This chance is won by COMPUTER :(')
computer_score+=1
print(f'\t Your Score: {user_score}')
print(f"\t Computer's Score: {computer_score}")
print(f" You're left with {mode} chance(s).")
os.system('cls')
print(logo)
user=input("Choose mode: 'E' for easy & 'H' for hard: ").lower()
if user == 'E':
print(f'\tYou have {easy} chances to win over your opponent.')
mode=easy
else:
print(f'\tYou have {hard} chances to win over your opponent.')
mode=hard
def game(mode):
global user_score
global computer_score
global easy
global hard
while mode!=0:
# user and computer choice ->
usr_choose_shape = int(input("\n\tType 1 for Snake, 2 for Water & 3 for Gun: "))
computer_choose_shape = shape_choice(shapes)
os.system('cls')
mode-=1
print(logo)
print(f"Your Choice: {shapes[usr_choose_shape-1]} ")
print(f"Computer Choice: {computer_choose_shape}" )
if shapes[usr_choose_shape-1] == computer_choose_shape:
print("Nobody wins. It's a TIE!")
print(f'\t Your Score: {user_score}')
print(f"\t Computer's Score: {computer_score}")
print(f" You're left with {mode} chance(s).")
else:
s=game_decision(usr_choose_shape-1,computer_choose_shape,shapes,mode )
os.system('cls')
print(logo)
if user_score > computer_score:
print(f"Your Final Score: 0{user_score}")
print(f"Computer's Final Score: 0{computer_score}")
print(f'Congratulations, you are an overall winner with score: 0{user_score}')
elif user_score == computer_score:
print(f"Your Final Score: 0{user_score}")
print(f"Computer's Final Score: 0{computer_score}")
print(f"Both share a equal score. It's a DRAW!")
else:
print(f"Your Final Score: 0{user_score}")
print(f"Computer's Final Score: 0{computer_score}")
print(f'Opponent wins with score: 0{computer_score}')
game(mode)
No comments:
Post a Comment