-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.py
More file actions
executable file
·171 lines (155 loc) · 5.99 KB
/
car.py
File metadata and controls
executable file
·171 lines (155 loc) · 5.99 KB
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
#coding=utf-8
import random
import math
import time
import console
class Game(object):
def __init__(self):
self.running = True
self.money = 1000
self.parts = {'engine' : 1,
'tyres' : 1,
'body' : 1}
self.prices = {'engine' : 500,
'tyres' : 200,
'body' : 400}
print('\n'*console.HEIGHT)
print(self.car(' '))
def menu(self, menuType):
game.displayMoney()
if menuType == 'main':
print('1. Upgrade')
print('2. Race')
print('3. Exit')
valid = False
while not valid:
choice = str(input('Choose an option: '))
if choice == '1':
print('\n'*console.HEIGHT)
self.menu('upgrade')
valid = True
elif choice == '2':
print('\n'*console.HEIGHT)
self.race()
valid = True
elif choice == '3':
print('\n'*console.HEIGHT)
self.running = False
valid = True
elif menuType == 'upgrade':
print('1. Engine - Level ' + str(self.parts['engine']+1) + ' - £' + str(self.prices['engine']))
print('2. Tyres - Level ' + str(self.parts['tyres']+1) + ' - £' + str(self.prices['tyres']))
print('3. Body - Level ' + str(self.parts['body']+1) + ' - £' + str(self.prices['body']))
print('4. Go Back')
valid = False
while not valid:
choice = str(input('Choose an option: '))
if choice == '1':
print('\n'*console.HEIGHT)
self.upgrade('engine')
valid = True
elif choice == '2':
print('\n'*console.HEIGHT)
self.upgrade('tyres')
valid = True
elif choice == '3':
print('\n'*console.HEIGHT)
self.upgrade('body')
valid = True
elif choice == '4':
print('\n'*console.HEIGHT)
valid = True
def upgrade(self, part):
if self.pay(self.prices[part]):
self.parts[part] += 1
print('You bought the ' + part + ' upgrade for £' + str(self.prices[part]) + '.')
print('Your ' + part + ' is now upgraded to level ' + str(self.parts[part]))
self.prices[part] += self.prices[part] / 10
else:
print('You cannot afford this upgrade')
def displayMoney(self):
print('You have £' + str(self.money))
def pay(self, amount):
if self.money >= amount:
self.money -= amount
return True
else:
return False
def race(self):
# Calculating Winner
opponentParts = {}
for part in self.parts:
opponentParts[part] = self.parts[part] + float(random.randint(0 - self.parts[part]*10, self.parts[part]*9))/100
opponentLevel = 0;
for part in opponentParts:
opponentLevel += opponentParts[part]
playerLevel = 0
for part in self.parts:
playerLevel += self.parts[part]
# Prize Money
prizeMoney = int(abs(round((playerLevel * random.randint(-playerLevel*10, playerLevel*10))/50)*50)+50)
population = [i for i in range(50, 105)]
population2 = [i for i in range(50, 75)]
population += population2
entryCost = prizeMoney*random.choice(population)/100
print('The entry cost for this race is £' + str(entryCost))
print('The prize for this race is £' + str(prizeMoney))
cont = input('Do you want to enter this race? (Y/n) ')
if cont == 'Y' or cont == 'y' or cont == '':
# Taking Entry Cost
if not self.pay(entryCost):
print('\n'*console.HEIGHT)
print('You cannot afford the entry cost.')
else:
self.draw(playerLevel, opponentLevel)
print('\n'*console.HEIGHT)
if opponentLevel > playerLevel:
# Opponent Wins
print('You Lost!!')
elif opponentLevel == playerLevel:
# Tie
print('Tie!!')
print('You got your £' + str(entryCost) + ' entry cost back')
self.money += entryCost
else:
# Player Wins
print('You Won £' + str(prizeMoney) + '!!')
self.money += prizeMoney
else:
print('\n'*console.HEIGHT)
print('You quit the race.')
def car(self, num, dist=0):
if dist == 0 or 100-dist <= 8:
spaceAfter = ''
else:
spaceAfter = (100-dist-8)*' ' + '|'
return '{0} +--+ {1}\n{0}/ {2} \-o{1}\n{0}-O----O-{1}'.format(dist*' ', spaceAfter, num)
def draw(self, player, opp):
winnerSpeed = random.randint(5, 7)
loserSpeed = random.randint(2, 4)
if player > opp:
speed = [winnerSpeed, loserSpeed]
winnerCar = 0
else:
speed = [loserSpeed, winnerSpeed]
winnerCar = 1
winner = False
n = 1
while not winner:
display = ''
display += '\n'
display += 'Player: 1\n'
display += 'Opponent: 2\n'
display += (console.HEIGHT/2-7)*('\n' + 100*' ' + '|') + '\n'
display += self.car('1', n*speed[0]) + '\n'
display += 100*' ' + '|\n'
display += self.car('2', n*speed[1]) + '\n'
display += (console.HEIGHT/2-5)*(100*' ' + '|' + '\n')
if n*speed[winnerCar] >= 100:
winner = True
print(display)
n += 1
time.sleep(0.1)
game = Game()
while game.running:
game.menu('main')