-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas11.py
77 lines (61 loc) · 2.67 KB
/
canvas11.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
""" slot Machine reels demo, A work in progress.
Note: the fruit gfx are borrowed and are only used
here for experiments these wont be used in final code"""
import tkinter as tk
from PIL import Image, ImageTk
import random
symbols = {"bar": "bar.png", "cherry": "cherry.png",
"coin": "coin.png", "lemon": "lemon.png",
"plum": "plum.png", "tomatoe": "tom.png",}
root = tk.Tk()
root.title("Slot machine reels demo")
canvas_width = 340
canvas_height = 60
symbol_size = 60
num_symbols = 5
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
canvas.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# Create the symbol labels
symbols_list = list(symbols.keys())
reel = []
def get_five_rnd_symbols():
""" Randomly choose 5 symbols to place on reels for start up.
this will eventually be edited so spin_reels can call here to get
new symbols"""
for j in range(num_symbols):
symbol = random.choice(symbols_list)
symbol_image = Image.open(symbols[symbol])
symbol_image = symbol_image.resize((symbol_size, symbol_size))
symbol_photo = ImageTk.PhotoImage(symbol_image)
symbol_label = canvas.create_image(j * (symbol_size + 10), 0,
anchor=tk.NW, image=symbol_photo)
reel.append((symbol_label, symbol_photo))
canvas.update()
def spin_reels():
spin_btn.config(state="disabled") # to stop multiclicking
for j in range(num_symbols):
# Get a random symbol
symbol = random.choice(symbols_list)
symbol_image = Image.open(symbols[symbol])
symbol_image = symbol_image.resize((symbol_size, symbol_size))
symbol_photo = ImageTk.PhotoImage(symbol_image)
old_symbol_label, _ = reel[j]
new_symbol_label = canvas.create_image(j * (symbol_size + 10),
-symbol_size, anchor=tk.NW,
image=symbol_photo)
for k in range(symbol_size // 2):
canvas.move(old_symbol_label, 0, 2)
canvas.move(new_symbol_label, 0, 2)
canvas.update()
canvas.after(10)
canvas.delete(old_symbol_label)
reel[j] = (new_symbol_label, symbol_photo)
# Print out final reel combination, eventually for checking for win
symbol_image = symbols[symbol][:-4] # remove ".png"
print(symbol_image)
print('-------')
spin_btn.config(state="normal")
spin_btn = tk.Button(root, text="Spin", command=spin_reels)
spin_btn.grid(row=1, column=1)
get_five_rnd_symbols()
root.mainloop()