-
Notifications
You must be signed in to change notification settings - Fork 1
/
horsegame.py
445 lines (394 loc) · 19.2 KB
/
horsegame.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# Horse Race Betting game by Paul and Ben Barber
# Based on an original game for the BBC model B, by John Barber of Ipswich Town
# March 2016
# Python3, can be run from IDLE or at the command line, developed under version 3.2 with pygame for sound
# Press Esc once or maybe more to exit the game early
# TODO
from tkinter import *
from pygame import mixer as sounds
import random
import time
import tkinter.messagebox
import tkinter.simpledialog
tk = Tk()
sounds.init()
# make the window and canvas to draw on
tk.title("Horse Game")
#tk.resizable(False, False)
#tk.attributes("-topmost", True)
tk.attributes("-fullscreen", True)
tk.config(cursor='none')
tk.update()
screen_width = tk.winfo_width() # 1280
screen_height = tk.winfo_height() # 720
print ("Screen: " + str(screen_width) + " by " + str(screen_height))
NumberofPunters = 0
keypressed = False
numberofLines = 32
numberofCols = 25 #30
row_spacing = screen_height/numberofLines #55 #75
#start_pos = screen_width - 200 # 1000
finish_pos = 200
horse_step = (screen_width - 400)/numberofCols #50
horse_wait = 0.4
myfont = ("Fixedsys", str(int(row_spacing))) #("Arial", "32")
# make empty lists
horselist = []
punterlist = []
# sounds
gunSound = sounds.Sound("gun.wav")
hoovesSound = sounds.Sound("hooves.wav")
brokeSound = sounds.Sound("broke.wav")
# make a horse class so it is easy to make many of them
class HorseSprite:
def __init__(self, canvas, number, name, colour):
self.canvas = canvas
self.images = [PhotoImage(file="legsin.gif"), PhotoImage(file="legsout.gif")]
# make the image bigger
zoom_factor = 5
self.images[0] = self.images[0].zoom(zoom_factor, zoom_factor)
self.images[1] = self.images[1].zoom(zoom_factor, zoom_factor)
# position it in the correct row
self.number = number
self.name = name
self.colour = colour
self.pos = 0 #start_pos
self.current_image = 0
self.price = 0
def prepare_to_race(self):
self.pos = finish_pos + (numberofCols - 6 + self.price/5)*horse_step #start_pos
self.current_image = 0
row = self.number*row_spacing*2
self.image = canvas.create_image(self.pos, row, image=self.images[0])
self.bib = canvas.create_rectangle(self.pos-10, row-5, self.pos+10, row+5, fill=self.colour)
def move(self):
# change the image to make the horse gallop
if self.current_image == 0:
self.current_image = 1
else:
self.current_image = 0
self.canvas.itemconfig(self.image, image=self.images[self.current_image])
# move some amount
self.canvas.move(self.image, -horse_step, 0)
self.canvas.move(self.bib, -horse_step, 0)
self.pos -= horse_step
hoovesSound.play()
# make a punter (player) class, there will be a few of those
class Punter:
def __init__(self, name):
self.name = name
self.total = 100 # how much money they have left # random.randint(0,100) #
self.pick = -1 # which horse they have picked, 0-6, -1=not made a pick
self.stake = 0 # how much they have bet on the horse+row_spacing
self.totalwinnings = 0 # running total of all winnings
self.numberofturns = 0 # count of how many turns this punter has
# keypress callback, just update some global variables
def KeyPress(event):
global keypressed, keyevent
keypressed = True
keyevent = event
# Wait for any keypress
def WaitForKeyPress(canvas):
global keypressed
wait_text = canvas.create_text(screen_width/2, screen_height-50, text="Press any key to continue...", fill="cyan", font=myfont, justify="center")
canvas.pack()
keypressed = False
while (keypressed == False):
# wait for KeyPress to be called
tk.update_idletasks()
tk.update()
time.sleep(0.01)
if (keyevent.keysym == 'Escape'):
exit(0)
canvas.delete(wait_text)
# Wait for integer number to be typed, textitem is where it will be echoed as you type
def WaitForInteger(canvas, textitem):
global keypressed, keyevent
string = ""
keypressed = False
escape_press = 0
while True:
while (keypressed == False):
# wait for KeyPress to be called
tk.update_idletasks()
tk.update()
time.sleep(0.01)
#print (keyevent.char)
if (keyevent.keysym == 'Return'):
if (len(string)>0):
return int(string)
elif (keyevent.keysym == 'BackSpace'):
if (len(string)>0):
string = string[:-1]
canvas.itemconfig(textitem, text=string)
elif (keyevent.char.isdigit()):
string = string + keyevent.char
#print (string)
canvas.itemconfig(textitem, text=string)
elif (keyevent.keysym == 'Escape'):
escape_press = escape_press + 1
if escape_press>1:
exit(0)
keypressed = False
# Wait for string to be typed, textitem is where it will be echoed as you type
def WaitForString(canvas, textitem):
global keypressed, keyevent
string = ""
keypressed = False
escape_press = 0
while True:
while (keypressed == False):
# wait for KeyPress to be called
tk.update_idletasks()
tk.update()
time.sleep(0.01)
if (keyevent.keysym == 'Return'):
return string
elif (keyevent.keysym == 'BackSpace'):
if (len(string)>0):
string = string[:-1]
canvas.itemconfig(textitem, text=string)
elif (keyevent.keysym == 'Escape'):
escape_press = escape_press + 1
else:
#print (keyevent.char)
string = string + keyevent.char
#print (string)
canvas.itemconfig(textitem, text=string)
if escape_press>1:
exit(0)
keypressed = False
def Initialize_characters_variables(canvas):
# create some horse sprites
horse = HorseSprite(canvas, 1, "Redwold", "red")
horselist.append(horse)
horse = HorseSprite(canvas, 2, "Black Jet", "black")
horselist.append(horse)
horse = HorseSprite(canvas, 3, "Yellow Dog", "yellow")
horselist.append(horse)
horse = HorseSprite(canvas, 4, "Super Blue", "blue")
horselist.append(horse)
horse = HorseSprite(canvas, 5, "Hot Magenta", "magenta")
horselist.append(horse)
horse = HorseSprite(canvas, 6, "Cyan Runner", "cyan")
horselist.append(horse)
horse = HorseSprite(canvas, 7, "White Flash", "white")
horselist.append(horse)
def DisplayCash(canvas, title="LEADER BOARD", wait=False):
Broke = 0 # Count how many punters are broke
# clear the scene
canvas.delete("all")
# draw the scene
canvas.create_rectangle(0, 0, screen_width, screen_height, fill="black")
canvas.create_text(screen_width/2, row_spacing, text="--------------------------------", fill="cyan", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*2, text="* " + title + " *", fill="magenta", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*3, text="--------------------------------", fill="cyan", font=myfont, justify="center")
pos = row_spacing*5
for punter in sorted(punterlist, key=lambda punter: punter.total, reverse=True):
message = punter.name + " has £" + str(punter.total)
canvas.create_text(screen_width/2, pos, text=message, fill="Yellow", font=myfont, justify="center")
pos = pos + row_spacing*2
if (punter.total<=0):
Broke = Broke+1
canvas.pack()
tk.update()
if (wait):
WaitForKeyPress(canvas)
return Broke
def Punters(canvas):
DisplayCash(canvas, title="HORSE RACING GAME")
question = canvas.create_text(screen_width/2, screen_height-row_spacing*4, text="How many punters?", fill="red", font=myfont, justify="center")
answer = canvas.create_text(screen_width/2, screen_height-row_spacing*2, text="", fill="orange", font=myfont, justify="center")
n = 0
while (n<1 or n>8): # minimum 1 and maximum 8 players
n = WaitForInteger(canvas, answer)
canvas.itemconfig(answer, text="")
canvas.delete(question, answer)
for i in range(1, n+1):
question = canvas.create_text(screen_width/2, screen_height-row_spacing*4, text="Punter " + str(i) + ", what is your name?", fill="red", font=myfont, justify="center")
answer = canvas.create_text(screen_width/2, screen_height-row_spacing*2, text="", fill="orange", font=myfont, justify="center")
name = WaitForString(canvas, answer)
punter = Punter(name)
punterlist.append(punter)
canvas.delete(question, answer)
DisplayCash(canvas, title="HORSE RACING GAME")
def StartingPrices(canvas):
# clear the scene
canvas.delete("all")
# draw the scene
canvas.create_rectangle(0, 0, screen_width, screen_height, fill="dark green")
canvas.create_text(screen_width/2, row_spacing, text="--------------------------------", fill="red", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*2, text="* STARTING PRICES *", fill="yellow", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*3, text="--------------------------------", fill="red", font=myfont, justify="center")
pos = row_spacing*5
i = 1
for horse in horselist:
horse.price = random.randint(1,6)*5
message = str(i) + ") " + horse.name + " " + str(horse.price) + "/1"
canvas.create_text(screen_width/2, pos, text=message, fill=horse.colour, font=myfont, justify="center")
pos = pos + row_spacing*2
i = i + 1
canvas.create_text(screen_width/2, pos, text="================================", fill="red", font=myfont, justify="center")
canvas.create_text(screen_width/2, pos+row_spacing, text="PLACE YOUR BETS", fill="yellow", font=myfont, justify="center")
canvas.create_text(screen_width/2, pos+row_spacing*2, text="================================", fill="red", font=myfont, justify="center")
canvas.pack()
tk.update()
# ask punters one by one for their bets
for punter in punterlist:
if punter.total <= 0:
sorry = canvas.create_text(screen_width/2, pos+row_spacing*4, text=punter.name + ", you are BROKE. NO BETS!!!!", fill="dark red", font=myfont, justify="center")
punter.pick = -1
punter.stake = 0
tk.update()
brokeSound.play()
time.sleep(3)
canvas.delete(sorry)
else:
punter.numberofturns = punter.numberofturns + 1
punter.pick=-1
punter.stake=-1
question1 = canvas.create_text(screen_width/2, pos+row_spacing*4, text=punter.name + ", you have £" + str(punter.total) + ". Pick a horse 1-7", fill="white", font=myfont, justify="center")
while (punter.pick<0 or punter.pick>6):
answer1 = canvas.create_text(screen_width/2, pos+row_spacing*5.2, text="", fill="orange", font=myfont, justify="center")
punter.pick = WaitForInteger(canvas, answer1) - 1
canvas.delete(answer1)
question2 = canvas.create_text(screen_width/2, pos+row_spacing*7, text="How much on " + horselist[punter.pick].name + "?", fill="white", font=myfont, justify="center")
while (punter.stake<0 or punter.stake>punter.total):
answer2 = canvas.create_text(screen_width/2, pos+row_spacing*8.2, text="", fill="orange", font=myfont, justify="center")
punter.stake = WaitForInteger(canvas, answer2)
if punter.stake > punter.total:
sorry = canvas.create_text(screen_width/2, pos+row_spacing*10, text="SORRY NO CREDIT!", fill="dark blue", font=myfont, justify="center")
tk.update()
time.sleep(1)
canvas.delete(sorry)
canvas.delete(answer2)
canvas.delete(question1, question2)
punter.total = punter.total - punter.stake
print (punter.name + " placed £" + str(punter.stake) + " on " + horselist[punter.pick].name + " and now has £" + str(punter.total))
def Race(canvas):
global keypressed, keyevent
# clear the scene
canvas.delete("all")
# draw the scene
canvas.create_rectangle(0, 0, screen_width, 7*2*row_spacing+65, fill="green")
canvas.create_line(finish_pos-horse_step, 1*2*row_spacing-30, finish_pos-horse_step, 7*2*row_spacing+65, width=5, fill="black")
canvas.create_oval(finish_pos-horse_step-8, 0, finish_pos-horse_step+8, 16, width=5, fill="", outline="gold")
canvas.create_line(finish_pos-horse_step, 16, finish_pos-horse_step, 1*2*row_spacing-20, width=5, fill="gold")
canvas.create_oval(finish_pos-horse_step-8, 7*2*row_spacing+30, finish_pos-horse_step+8, 7*2*row_spacing+30+16, width=5, fill="", outline="gold")
canvas.create_line(finish_pos-horse_step, 7*2*row_spacing+30+16, finish_pos-horse_step, 7*2*row_spacing+65, width=5, fill="gold")
canvas.create_rectangle(0, 8*2*row_spacing, screen_width, screen_height, fill="dark green")
pos = row_spacing*17
canvas.create_text(screen_width/2, pos, text="================================", fill="red", font=myfont, justify="center")
canvas.create_text(screen_width/2, pos+row_spacing, text="LET'S RACE", fill="yellow", font=myfont, justify="center")
canvas.create_text(screen_width/2, pos+row_spacing*2, text="================================", fill="red", font=myfont, justify="center")
pos = pos + row_spacing * 3
for punter in punterlist:
if punter.pick > -1:
canvas.create_text(screen_width/2, pos, text=punter.name +" bet £"+ str(punter.stake) +" on "+ horselist[punter.pick].name +" at "+ str(horselist[punter.pick].price) +"/1",
fill=horselist[punter.pick].colour, font=myfont, justify="center")
pos = pos + row_spacing * 1.2
for h in horselist:
h.prepare_to_race()
canvas.pack()
tk.update()
random.seed()
WaitForKeyPress(canvas)
gunSound.play()
time.sleep(0.5)
keypressed = False
esc_pressed = 0
# main race loop
while True:
# pick a random horse
h = random.randint(0,6)
horse = horselist[h]
# move that horse
horse.move()
#print("Horse ", horse.number, " pos ", horse.pos)
# update the screen
tk.update_idletasks()
tk.update()
# check for the winner, break from the race loop if we have one
if horse.pos < finish_pos:
break
# Check for Esc
if keypressed == True:
if (keyevent.keysym == 'Escape'):
esc_pressed = esc_pressed + 1
keypressed = False
if esc_pressed>1:
exit(0)
# wait for some time
time.sleep(horse_wait)
# announce the winner
winner_text = horse.name + " is the winner!"
canvas.create_text(screen_width/2, (h+1)*row_spacing*2, text=winner_text, fill=horse.colour, font=myfont, justify="center")
WaitForKeyPress(canvas)
# return the number of the winning horse so we can calculate punter winnings next
return h
def Results(canvas, winning_horse_index):
# clear the scene
canvas.delete("all")
# draw the scene
canvas.create_rectangle(0, 0, screen_width, screen_height, fill="dark green")
canvas.create_text(screen_width/2, row_spacing, text="--------------------------------", fill="red", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*2, text="* RESULTS *", fill="yellow", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*3, text="--------------------------------", fill="red", font=myfont, justify="center")
pos = row_spacing*5
i = 0
winning_horse = horselist[winning_horse_index]
for punter in punterlist:
#print("Horse index " + str(winning_horse_index) + " punter pick " + str(punter.pick))
if punter.pick == winning_horse_index:
winnings = punter.stake * winning_horse.price
punter.total = punter.total + winnings + punter.stake
punter.totalwinnings = punter.totalwinnings + winnings
message = punter.name + " wins £" + str(winnings) + " on " + winning_horse.name
canvas.create_text(screen_width/2, pos, text=message, fill=winning_horse.colour, font=myfont, justify="center")
pos = pos + row_spacing*2
i = i + 1
if i==0:
canvas.create_text(screen_width/2, pos, text="No winners this race...", fill=winning_horse.colour, font=myfont, justify="center")
sounds.music.load("yankee.mp3")
sounds.music.play()
WaitForKeyPress(canvas)
#sounds.music.fadeout(2000)
def Broke(canvas):
# clear the scene
canvas.delete("all")
# draw the scene
canvas.create_rectangle(0, 0, screen_width, screen_height, fill="black")
canvas.create_text(screen_width/2, row_spacing, text="--------------------------------", fill="cyan", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*2, text="* GAME OVER *", fill="magenta", font=myfont, justify="center")
canvas.create_text(screen_width/2, row_spacing*3, text="--------------------------------", fill="cyan", font=myfont, justify="center")
pos = row_spacing*5
for punter in sorted(punterlist, key=lambda punter: punter.totalwinnings, reverse=True):
bet_text = " bet" if punter.numberofturns==1 else " bets"
message = punter.name + " won a total of £" + str(punter.totalwinnings) + " in " + str(punter.numberofturns) + bet_text
canvas.create_text(screen_width/2, pos, text=message, fill="yellow", font=myfont, justify="center")
pos = pos + row_spacing*2
canvas.create_text(screen_width/2, screen_height-100, text="Press any key to play again or Esc to exit.", fill="white", font=myfont, justify="center")
sounds.music.load("yankee_slow.mp3")
sounds.music.play()
WaitForKeyPress(canvas)
# HORSE-RACE MAIN CODE
# setup a callback on any keypress for keyboard entry
tk.bind_all('<Key>', KeyPress)
# setup a canvas to draw on
canvas = Canvas(tk, width=screen_width, height=screen_height, highlightthickness=0)
# setup horses and punters
Initialize_characters_variables(canvas)
while True:
Punters(canvas)
# main game loop
while True:
numberbroke = DisplayCash(canvas, wait=True)
print(str(numberbroke)+" punters are broke out of "+str(len(punterlist)))
if numberbroke == len(punterlist): # if everyone broke, end the game by breaking out of the game loop
break
StartingPrices(canvas)
winning_horse = Race(canvas)
print("Horse index ", winning_horse, " ", horselist[winning_horse].name, " is the winner.")
Results(canvas, winning_horse)
Broke(canvas)
punterlist = []