-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut11_Snake_water_gun.py
190 lines (154 loc) · 5.96 KB
/
tut11_Snake_water_gun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'''
Water - 1
Snake - 0
Gun - -1
------------------------------------------------------
On adding the computer's choice and my choice,
1.if the result is '1' then I will win.
2.if the result is '0' then I will loose
3.if the result is greater then '1' then I will loose
------------------------------------------------------
=> All possible outcomes and their result are as follows :
Computer Me
snake - gun - Win
water - gun - loose
gun - gun - Tie
snake - snake - Tie
water - snake - win
gun - snake - loose
snake - water - Loose
water - water - Tie
gun - water - Win
----------------------------
Modules used -
1. random - to make the game more unpredictable
2. time - for certain delays
'''
import random
game_rules = f'''
-----------____------___________-----_________-----________------
{"Welcome to Snake,Water,Gun Game Instruction module.":^60}
This game is similar to stone,paper,scissor ; here you will find
Snake, Water and Gun.
The following table shows all the possible cominations and
their results.
Computer Me Result
snake - gun - Win
water - gun - loose
gun - gun - Tie
----------------------
snake - snake - Tie
water - snake - win
gun - snake - loose
-------------------------
snake - water - Loose
water - water - Tie
gun - water - Win
------------------------
Hope you understood the game. Now Enjoy...
Thank You!!
Team
-Koi team nahi hai, maje le raha tha.
Game sahi se khelna, wo majak nahi hai...
-------------------------------------------
You will be playing with computer maharaj and let's see who wins..
All the best :)
---__---_---__--_--_--_-_--_----_--__-__-_--_--_______--__--_--
'''
#Importing modules
from random import choice
import time
#Defining Character animation code
def char_animation(msg:str):
speed = [0.001,0.0002,0.01,0]
for char in msg:
print(char,end='')
time.sleep(random.choice(speed))
#Choices that computer has
all_choices = {'Snake':0,'Water':1,"Gun":-1}
#Game intro
user_name = input("Enter Your Full Name Please : ")
wlc_msg = f"{f'Welcome {user_name} to Snake,Water,Gun Game':-^50}\n\n" \
f"Instructions :\n" \
f"{time.asctime(time.localtime())}\n" \
f"You will be given 8 chances,and at the end," \
f" one with the highest score will win.\n\n{'Wishing You All The Best':♨^55}\n" \
f"Are you ready [Press Enter]?? //OR// \n" \
f"Type --help or h for game rules\n"
char_animation(wlc_msg)
show_rules = input("Enter your choice here : ").strip().lower()
if show_rules in ['help', '--help','h']:
print(game_rules)
# Take input from user
before_msg = '\nOkay!! Now you are entering the game...\n\nEnter Among These [Snake,Water,Gun]\n' \
'or enter exit to quit\n'
char_animation(before_msg)
def map_choice(choice: str) -> object:
choice = choice.strip().lower()
if 's' in choice: return 'Snake'
elif 'w' in choice: return 'Water'
elif 'g' in choice : return "Gun"
#Defining Game_Event Loop
computer_points = 0
user_points = 0
chances = 8
def game_event_loop(chances,computer_choice,user_input):
global user_points,computer_points
# result_condition = {-2:False,-1:False,1:True,0:False, 2:False}
result = all_choices[computer_choice] - all_choices[user_input]
print(result)
# print("Out of exception ", chances, 'cmp choice -', computer_choice)
print(f"You Chose [{user_input}]")
if result == 1:
print(f"Wohoo!! You got a point... I had chosen [{computer_choice}]\n"
f"Point +1..\n\n")
user_points +=1
elif user_input == computer_choice :
print(f"Woooh!! Its a Tie, I too chose {user_input}\n\n")
else:
print(f"OOps!! Better luck next time dude...Ha ha ha..I chose {computer_choice}\n"
"Computer's Point +1..\n\n")
computer_points+=1
def game_runner():
global chances
while chances :
user_input = input(f"Enter choice,chances left {chances}: ").strip().lower()
if user_input in ['exit', 'e', 'ex']:
print("\n\nExit Requested....\nExiting from the game..")
quit()
try:
user_input = map_choice(user_input)
computer_choice = random.choice(list(all_choices.keys()))
game_event_loop(chances, computer_choice, user_input)
chances -= 1
except Exception as e:
print(e)
print("\nGot an Input Error, please type correctly.\nContinuing Game...\n")
continue
else:
time.sleep(1)
char_animation('\n\nOhoooooyiii !! All Chances are finished.....\n\n')
# winner = ( lambda cmp,usr:f'Computer\n\nKoi Baat nahi {user_name} phir se khel lo.. :)\n\n' if cmp>usr else user_name)(computer_points,user_points)
if computer_points == user_points:
winner = "It's a tie...Both have same points."
elif computer_points>user_points:
winner = f'Computer\n\nKoi Baat nahi {user_name} phir se khel lo.. :)\n\n'
else:
winner = user_name
print(f"And The Winner is ......",end=' ')
time.sleep(1)
print(winner)
print(f"Computer's score - {computer_points}\n{user_name}'s score : {user_points}\n\n")
play_again = input("Wanna Play Again ?? : ").strip().lower()
if play_again in ['y','yes']:
print("\n\nDo you want to have a series ?\nYou can play with me multiple times and the scores will"
" keep adding and the final result after each match will be calculated on the basis of "
"the sum of the scores of the previous matches.\n")
match_series = input("Want to Have a Series [y/n] ?: ")
if match_series in ['y','yes']:
globals()['user_points'] ,globals()['computer_points'] = 0,0
chances = 8
print(f'\n\n{f"Welcoming You Once Again {user_name}":=^50}')
game_runner()
if __name__ == '__main__':
game_runner()