-
Notifications
You must be signed in to change notification settings - Fork 1
/
shotgun.py
245 lines (218 loc) · 6.17 KB
/
shotgun.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import random, time
game_is_active = True
class player:
def __init__(self):
self.action = 0
self.ammo = 0
self.score = 0
def set_action(self, action):
self.action = action
def set_ammo(self, amount):
self.ammo = amount
def set_score(self, amount):
self.score = amount
PLAYER = player()
COMPUTER = player()
def print_pre_game_menu():
get_input = (
input(
'1. Start game\n'
'2. Rulebook\n'
'3. Score\n'
'0. Exit\n'
'Your choice: '
)
)
try:
return int(get_input)
except:
pass
## prints ammo for player and computer
def print_ammo():
time.sleep(1)
print('-' * 15)
print(f'Your ammo: {PLAYER.ammo}\nComputer\'s ammo: {COMPUTER.ammo}')
print('-' * 15)
time.sleep(1)
## Prints winning message
def print_win():
PLAYER.set_score(PLAYER.score+1)
time.sleep(1)
print('-' * 15)
print('You won the game')
print('-' * 15)
time.sleep(1)
## Prints losing message
def print_defeat():
COMPUTER.set_score(COMPUTER.score+1)
time.sleep(1)
print('-' * 15)
print('You lost the game')
print('-' * 15)
time.sleep(1)
## Prints draw message
def print_draw():
time.sleep(1)
print('-' * 15)
print('The game is drawn')
print('-' * 15)
time.sleep(1)
"""def print_computer_action(computer_action):
if computer_action == 1:
print("Computer is shooting!")
elif computer_action == 2:
print("Computer is loading!")
elif computer_action == 3:
print("Computer is blocking!")"""
def print_msg(msg):
time.sleep(1)
print('-' * 15)
print(msg)
print('-' * 15)
time.sleep(1)
def print_msg_faster(msg2):
print('-' * 15)
print(msg2)
print('-' * 15)
## Ends loop and resets players
def end_game():
global game_is_active
game_is_active = False
PLAYER.set_action(0)
PLAYER.set_ammo(0)
COMPUTER.set_action(0)
COMPUTER.set_ammo(0)
#function for shooting, outcome based on computers action
def shoot():
if COMPUTER.action == 1:
print_msg_faster('Computer shoots')
print("Both players shot!")
PLAYER.set_ammo(PLAYER.ammo-1)
COMPUTER.set_ammo(COMPUTER.ammo-1)
elif COMPUTER.action == 2:
print_msg_faster('Computer loads')
print_win()
end_game()
elif COMPUTER.action == 3:
print_msg_faster('Computer blocks')
PLAYER.set_ammo(PLAYER.ammo-1)
elif COMPUTER.action == 4:
print_defeat()
end_game()
#function for blocking, outcome based on computers action
def block():
if COMPUTER.action == 1:
print_msg_faster('Computer shoots')
COMPUTER.set_ammo(COMPUTER.ammo-1)
elif COMPUTER.action == 2:
print_msg_faster('Computer loads')
COMPUTER.set_ammo(COMPUTER.ammo+1)
elif COMPUTER.action == 3:
print_msg_faster('Computer blocks')
print("Both players blocked!")
elif COMPUTER.action == 4:
print_defeat()
end_game()
def loading():
if COMPUTER.action == 1:
print_msg_faster('Computer shoots')
print_defeat()
end_game()
elif COMPUTER.action == 2:
print_msg_faster('Computer loads')
print("Both players loads!")
COMPUTER.set_ammo(COMPUTER.ammo+1)
PLAYER.set_ammo(PLAYER.ammo+1)
elif COMPUTER.action == 3:
print_msg_faster('Computer blocks')
PLAYER.set_ammo(PLAYER.ammo+1)
elif COMPUTER.action == 4:
print_defeat()
end_game()
## function for shotgunning
def shotgun():
if COMPUTER.action == 1:
print_msg_faster('Computer shoots')
print("Shotgun beats regular shot!")
print_win()
elif COMPUTER.action == 2:
print_msg_faster('Computer loads')
print_win()
elif COMPUTER.action == 3:
print_msg_faster('Computer blocks')
print_win()
elif COMPUTER.action == 4:
print_draw()
end_game()
## determines computer's action
def computer_logic():
if COMPUTER.ammo == 0:
if PLAYER.ammo == 0:
computer_action = 2
else:
computer_action = random.randint(2, 3)
elif COMPUTER.ammo >= 3:
computer_action = 4
else:
if PLAYER.ammo == 0:
computer_action = random.randint(1, 2)
else:
computer_action = random.randint(1, 3)
return computer_action
## prints menu, takes input and sets objects' actions
def print_menu():
menu_items = ['Shoot', 'Load', 'Block', 'Shotgun']
if PLAYER.ammo < 3:
menu_items.remove('Shotgun')
for i, v in enumerate(menu_items):
print(f'{i+1}. {v}')
print('0. Exit')
choice = input('Enter number: ')
try:
PLAYER.set_action(int(choice)) ## sets player action
COMPUTER.set_action(computer_logic()) ## sets computer action
if PLAYER.action == 1:
if PLAYER.ammo >= 1:
print_msg('Shooting..')
shoot()
print_ammo()
## Shoot function
else:
print_msg('You need 1 or more bullets to shoot')
elif PLAYER.action == 2:
print_msg('Loading..')
loading()
print_ammo()
## Load function
elif PLAYER.action == 3:
print_msg('Blocking..')
block()
print_ammo()
## Block function
elif PLAYER.action == 4 and PLAYER.ammo >= 3:
print_msg('Shotgun..')
shotgun()
print_ammo()
elif PLAYER.action == 0:
## Exits game
end_game()
else:
print_msg('Pick a number between 0-3')
except:
print_msg('Pick a number between 0-3')
def run_game():
global game_is_active
game_is_active = True
while game_is_active:
print_menu()
while True:
choice = print_pre_game_menu()
if choice == 1:
run_game()
elif choice == 2:
print(f'Your score: {PLAYER.score}\nComputer\'s score: {COMPUTER.score}')
elif choice == 0:
print_msg('Exits game..')
exit()
else:
print('Pick a number between 0-3')