-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path777.py
284 lines (250 loc) · 10.9 KB
/
777.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from os import system, name
import time
from random import randint
from sty import fg
def clear():
"""This function will be used to clear the screen"""
if name == 'nt':
# If run on Windows
system('cls')
else:
# If run on Linux
system('clear')
# Below are defined 6 lists of 6 strings which are displayed in game symbols
# Number in parenthesis for 'fg' method sets 8-bit color for string
strawberry = [fg(111) + ' ' + fg.rs,
fg(34) + ' ## ' + fg.rs,
fg(196) + ' #### ' + fg.rs,
fg(196) + ' #### ' + fg.rs,
fg(196) + ' ## ' + fg.rs,
fg(111) + ' ' + fg.rs]
banana = [fg(226) + ' ' + fg.rs,
fg(226) + ' # ' + fg.rs,
fg(226) + ' ## ' + fg.rs,
fg(220) + ' ## ' + fg.rs,
fg(220) + ' ## ' + fg.rs,
fg(226) + ' ' + fg.rs]
plum = [fg(22) + ' ' + fg.rs,
fg(22) + ' ## ' + fg.rs,
fg(19) + ' #### ' + fg.rs,
fg(19) + ' ##### ' + fg.rs,
fg(19) + ' ## ' + fg.rs,
fg(22) + ' ' + fg.rs]
raspberry = [fg(161) + ' ' + fg.rs,
fg(34) + ' # ' + fg.rs,
fg(161) + ' ### ' + fg.rs,
fg(161) + ' ##### ' + fg.rs,
fg(161) + ' ### ' + fg.rs,
fg(161) + ' ' + fg.rs]
orange = [fg(111) + ' ' + fg.rs,
fg(40) + ' # ' + fg.rs,
fg(208) + ' ### ' + fg.rs,
fg(208) + ' ##### ' + fg.rs,
fg(208) + ' ### ' + fg.rs,
fg(111) + ' ' + fg.rs]
seven = [fg(111) + ' ' + fg.rs,
fg(88) + ' #### ' + fg.rs,
fg(124) + ' # ' + fg.rs,
fg(160) + ' # ' + fg.rs,
fg(196) + ' # ' + fg.rs,
fg(111) + ' ' + fg.rs]
class Cash:
def __init__(self, credit=100, bet=2):
"""
Sets amount of player's credits at the beginning of game,
initial bet, and variants of bets that player will be able to toggle between
"""
self.credit = credit
self.bet = bet
self.bets = [2, 5, 10, 20]
def change_bet(self):
"""Moves first number of bets list at the end"""
self.bets.append(self.bets.pop(0))
self.bet = self.bets[0]
def charge(self):
"""
Checks if player has enough credit to place a bet
if not gives prompt and returns True so player will be able to lower bet
else it subtracts bet from player's credits
"""
if self.bet <= self.credit:
self.credit -= self.bet
return False
else:
machine.still()
print('\n\t Not enough credit to place a bet')
time.sleep(2)
return True
class Slots:
def __init__(self):
"""
Defining 3 slots of machine as lists from already defined variables
First slot is unique because except printable symbol it has also multiplier of bet
"""
self.slot_one = [(strawberry, 2), (banana, 10), (plum, 3), (strawberry, 2), (raspberry, 5), (plum, 3), (orange, 8), (seven, 15)]
self.slot_two = [banana, strawberry, plum, orange, strawberry, plum, raspberry, seven]
self.slot_three = [plum, strawberry, raspberry, orange, plum, strawberry, seven, banana]
def roll(self):
"""
This method is responsible for showing machine's slots in motion
"""
# First of all 3 random numbers are set
# Since every symbol of slots has 6 lines, all 3 numbers must be divisible by 6 to make full rotates
a, b, c = 1, 1, 1
while a % 6 != 0:
a = randint(70, 110)
while b % 6 != 0:
b = randint(60, 90)
while c % 6 != 0:
c = randint(70, 100)
# This for loop composes 3 lists to pass them to function that will print current state of slots
# Does this for random number of times using first random number set before
# Variable 'i' will be used to access only some parts of displayed symbols
i = 1
for _ in range(a):
clear()
if i == 6:
i = 0
# After each 6 jumps of slots by single line first symbol of each slot list will be moved at the end
self.slot_one.append(self.slot_one.pop(0))
self.slot_two.append(self.slot_two.pop(0))
self.slot_three.append(self.slot_three.pop(0))
# Here are composed lists to be printed
# First symbol may be partial and then will be taken part of forth symbol
s1 = self.slot_one[0][0][i:] + self.slot_one[1][0] + self.slot_one[2][0] + self.slot_one[3][0][0:i]
s2 = self.slot_two[0][i:] + self.slot_two[1] + self.slot_two[2] + self.slot_two[3][0:i]
s3 = self.slot_three[0][i:] + self.slot_three[1] + self.slot_three[2] + self.slot_three[3][0:i]
i += 1
layout(s1, s2, s3)
time.sleep(0.02)
# Similar as before, but now first slot will not changed - no move
i = 1
for _ in range(b):
clear()
if i == 6:
i = 0
self.slot_two.append(self.slot_two.pop(0))
self.slot_three.append(self.slot_three.pop(0))
s1 = self.slot_one[0][0] + self.slot_one[1][0] + self.slot_one[2][0]
s2 = self.slot_two[0][i:] + self.slot_two[1] + self.slot_two[2] + self.slot_two[3][0:i]
s3 = self.slot_three[0][i:] + self.slot_three[1] + self.slot_three[2] + self.slot_three[3][0:i]
i += 1
layout(s1, s2, s3)
time.sleep(0.02)
# Last sequence, with only last slot changing
i = 1
for t in range(c):
clear()
if i == 6:
i = 0
self.slot_three.append(self.slot_three.pop(0))
s1 = self.slot_one[0][0] + self.slot_one[1][0] + self.slot_one[2][0]
s2 = self.slot_two[0] + self.slot_two[1] + self.slot_two[2]
s3 = self.slot_three[0][i:] + self.slot_three[1] + self.slot_three[2] + self.slot_three[3][0:i]
i += 1
layout(s1, s2, s3)
# Equation below gives last slot slowing-down effect, can be tweaked easily
time.sleep(0.02 + t**2 / 90000)
def still(self):
"""This method is used to compose states of slots while not moving and pass to print"""
clear()
fin1 = self.slot_one[0][0] + self.slot_one[1][0] + self.slot_one[2][0]
fin2 = self.slot_two[0] + self.slot_two[1] + self.slot_two[2]
fin3 = self.slot_three[0] + self.slot_three[1] + self.slot_three[2]
layout(fin1, fin2, fin3)
print('\n\t [ENTER] Play [B] Change bet [Q] Quit ')
def adding_credits(self, prize):
"""
Adds credits to player's account, 1 credit for prize times
Also print the whole layout
:param prize: result of win_amount function, may be 0 if there is no match in slots
"""
for n in range(prize):
clear()
money.credit += 1
fin1 = self.slot_one[0][0] + self.slot_one[1][0] + self.slot_one[2][0]
fin2 = self.slot_two[0] + self.slot_two[1] + self.slot_two[2]
fin3 = self.slot_three[0] + self.slot_three[1] + self.slot_three[2]
layout(fin1, fin2, fin3)
time.sleep(0.03)
def win_amount():
"""
This function checks if there is a match in slots, 3 horizontal lines and 2 diagonal
:return: If match - placed bet times symbol's multiplier, if no match - 0
"""
if machine.slot_one[0][0] == machine.slot_two[0] == machine.slot_three[0]:
return money.bet * machine.slot_one[0][1]
elif machine.slot_one[1][0] == machine.slot_two[1] == machine.slot_three[1]:
return money.bet * machine.slot_one[1][1]
elif machine.slot_one[2][0] == machine.slot_two[2] == machine.slot_three[2]:
return money.bet * machine.slot_one[2][1]
elif machine.slot_one[0][0] == machine.slot_two[1] == machine.slot_three[2]:
return money.bet * machine.slot_one[0][1]
elif machine.slot_one[2][0] == machine.slot_two[1] == machine.slot_three[0]:
return money.bet * machine.slot_one[2][1]
else:
return 0
def layout(s1, s2, s3):
"""
Responsible for graphical representation of game
:param s1, s2, s3: Three slots prepared by one of methods from Slots class
"""
print('\n\t __________________________________________')
print('\t / /|')
print('\t /_________________________________________/ |')
print('\t| | |')
print('\t| | |')
print('\t| | |')
print('\t| STRAWBERRY x2 PLUM x3 | |')
print('\t| | |')
print('\t| RASPBERRY x5 ORANGE x8 | |')
print('\t| | |')
print('\t| BANANA x10 SEVENS x15 | |')
print('\t| | |')
print('\t| | |')
print('\t| ----------------------------------- | |')
for l, m, r in zip(s1, s2, s3):
print('\t| |', l, '|', m, '|', r, '| | |')
print('\t| ----------------------------------- | |')
print('\t| | |')
print(f'\t| BET {money.bet:2} CREDIT {money.credit:5} | |')
print('\t| | /')
print('\t|_________________________________________|/')
def welcome():
welcome = ['\n\n\t ####### ####### #######',
'\t######## ######## ########',
'\t ## ## ##',
'\t ## ## ##',
'\t ## ## ##',
'\t ## ## ##',
'\t ## ## ##',
'\n\n'
'\t#### ### # # # ##### ####',
'\t# # # # # # # # ',
'\t### ### # # # # ####',
'\t# # # # # # # #',
'\t# # # #### # # ####']
for c in welcome:
print(c)
time.sleep(0.1)
time.sleep(1)
if __name__ == '__main__':
clear()
welcome()
money = Cash()
machine = Slots()
while True:
machine.still()
again = input('\n')
if again == 'q':
break
elif again == 'b':
money.change_bet()
continue
elif again == '':
if money.charge():
continue
machine.roll()
machine.adding_credits(win_amount())
time.sleep(1)
clear()