Posts: 76
Threads: 14
Joined: Jan 2019
May-18-2020, 12:58 PM
(This post was last modified: May-18-2020, 12:59 PM by steve_shambles.)
thankyou deanhystad
but I am struggling to insert this and make it work
due to new or renamed variables or lists etc.
def spin_reels():
"""Pseudo spin, best I can do for now."""
spinner = ['FH', 'FD', 'FC',
'WH', 'WD', 'WC',
'JH', 'JD', 'JC', 'JS',
'QH', 'QD', 'QC', 'QS',
'KH', 'KD', 'KC', 'KS',
'AH', 'AD', 'AC', 'AS',
'BH', 'BD', 'BC']
Glo.hold_btn1.configure(state=DISABLED)
Glo.hold_btn2.configure(state=DISABLED)
Glo.hold_btn3.configure(state=DISABLED)
for i in range(3):
# Spin reel on X times, two 2X times, three 3X times
for _ in range(18):
for j in range(len(reel_labels)):
# Don't spin a held reel.
if not hold[j] and j >= i:
reel[j] = randrange(len(spinner_images))
reel_label[i].config(image=spinner_images[reel[i]])
time.sleep(0.025)
check_for_win()
rnd_hold()
save_bank() Also my original description of spinr wasn't
quite right it's more this:
# spinr represents how many symbols to display for the spin.
# the bigger the value the longer the spin will take.
18seemed about right, but can be tweeked.
To know where I am coming from you really need to look
at the full game source and play a few reels to see the context.
I have uploaded the game to Github:
https://github.com/steveshambles/Freespin-Frenzy
cheers for your time again I appreciate it.
Posts: 76
Threads: 14
Joined: Jan 2019
May-18-2020, 09:06 PM
(This post was last modified: May-18-2020, 09:06 PM by steve_shambles.)
okay I have been trying to improve the readability of the function and
working out what does what exactly. Please see copious notes:
def spin_reels():
"""Pseudo spin, Best I can do for now. loading files on the fly
uses a lot of memory, so need to fix this."""
reel_symbols = ['FH', 'FD', 'FC',
'WH', 'WD', 'WC',
'JH', 'JD', 'JC', 'JS',
'QH', 'QD', 'QC', 'QS',
'KH', 'KD', 'KC', 'KS',
'AH', 'AD', 'AC', 'AS',
'BH', 'BD', 'BC']
# Required or causes not defined errors in places.
temp_lbl_one = Label(cards_frame)
card_lbl_one = Label(cards_frame)
temp_lbl_two = Label(cards_frame)
card_lbl_two = Label(cards_frame)
temp_lbl_three = Label(cards_frame)
card_lbl_three = Label(cards_frame)
# Disable all three hold buttons.
disable_hold_btns()
# The 4 detrmines how many times we loop over spinr loop,
# in other words how long we spin reels for.
for spins in range(4):
for spinr in range(18):
# spinr represents how many randomly chosen symbols to display
# for the spin.
# the bigger the value the longer the spin will take.
# Choose a random symbol for each reel.
# This is just for show, to simulate some sort of reel
# spin effect.
rnd_symb1 = randrange(len(reel_symbols))
# Create filename to fetch corresponding graphic file.
symb1 = reel_symbols[rnd_symb1]+'.png'
rnd_symb2 = randrange(len(reel_symbols))
symb2 = reel_symbols[rnd_symb2]+'.png'
rnd_symb3 = randrange(len(reel_symbols))
symb3 = reel_symbols[rnd_symb3]+'.png'
# This block of code does the spinning, but does not
# handle the outcome of the spin, that's in the next block.
# If you comment out this block you will see no spin,
# but a random card will still be displayed at the end
# by the next block of code, so it will still work.
if spins < 1 and not Glo.btn1_is_held:
temp_lbl_one = Label(cards_frame)
temp_img_one = PhotoImage(file='cards/gfx/'+str(symb1))
temp_lbl_one.config(image=temp_img_one)
temp_lbl_one.grid(row=0, column=1, padx=2, pady=2)
temp_lbl_one.photo = temp_img_one
temp_lbl_one.update()
# When spins have finished, i.e == 1, and the reel was not
# originally held, we can now display the symbol we have
# settled on for the reel and store it in global var
# Glo.reel_one for use in other functions, like detect win.
if spins == 1 and not Glo.btn1_is_held:
card_lbl_one = Label(cards_frame)
reel_one_img = PhotoImage(file='cards/gfx/'+str(Glo.reel_one))
card_lbl_one.config(image=reel_one_img)
card_lbl_one.grid(row=0, column=1, padx=2, pady=2)
card_lbl_one.photo = reel_one_img
card_lbl_one.update()
# Now do again for reel 2.
# Reel 2 runs a bit longer than reel 1 for effect.
if spins < 2 and not Glo.btn2_is_held:
temp_lbl_two = Label(cards_frame)
temp_img_two = PhotoImage(file='cards/gfx/'+str(symb2))
temp_lbl_two.config(image=temp_img_two)
temp_lbl_two.grid(row=0, column=2, padx=2, pady=2)
temp_lbl_two.photo = temp_img_two
temp_lbl_two.update()
if spins == 2 and not Glo.btn2_is_held:
card_lbl_two = Label(cards_frame)
reel_two_img = PhotoImage(file='cards/gfx/'+str(Glo.reel_two))
card_lbl_two.config(image=reel_two_img)
card_lbl_two.grid(row=0, column=2, padx=2, pady=2)
card_lbl_two.photo = reel_two_img
card_lbl_two.update()
# Reel 3 runs a bit longer than reel 2.
if spins < 3 and not Glo.btn3_is_held:
temp_lbl_three = Label(cards_frame)
temp_img_three = PhotoImage(file='cards/gfx/'+str(symb3))
temp_lbl_three.config(image=temp_img_three)
temp_lbl_three.grid(row=0, column=3, padx=2, pady=2)
temp_lbl_three.photo = temp_img_three
temp_lbl_three.update()
if spins == 3 and not Glo.btn3_is_held:
spins = 99 # end of spins.
card_lbl_three = Label(cards_frame)
reel_three_img = PhotoImage(file='cards/gfx/'+str(Glo.reel_three))
card_lbl_three.config(image=reel_three_img)
card_lbl_three.grid(row=0, column=3, padx=2, pady=2)
card_lbl_three.photo = reel_three_img
card_lbl_three.update()
time.sleep(0.025)
# Destroy temp vars holding labels that were used in rnd spin.
# and zero image references in hope of saving memory. It doesnt!
temp_lbl_one.destroy()
temp_img_one = ''
temp_lbl_two.destroy()
temp_img_two = ''
temp_lbl_three.destroy()
temp_img_three = ''
card_lbl_one.destroy() # perform only after spin finished to clean up.
reel_one_img = ''
symb1 = ''
card_lbl_two.destroy()
reel_two_img = ''
symb2 = ''
# card_lbl_three.destroy() #causes problems
reel_three_img = ''
symb3 = ''
# Note: when all 3 reels are held hardly any memory used
# proving its the displing of the cards that use up the memory.
check_for_win()
rnd_hold()
save_bank()
Posts: 6,798
Threads: 20
Joined: Feb 2020
Instead of deleting things you should reuse. I tried my best to follow the conventions you use in your program but I cannot force myself to not use a list when the code is SCREAMING for you to use a list. So instead of reel_one, reel_two and reel_three I use reel[].
def make_images():
card_images = {}
for card in Glo.ranks:
card_images[card] = PhotoImage(file='cards/gfx/'+card+'.png')
return card_images
def spin_reels():
"""Pseudo spin, Best I can do for now. loading files on the fly
uses a lot of memory, so need to fix this."""
disable_hold_btns()
cards = list(Glo.card_images.values())
for reel in range(len(Glo.reel)):
for _ in range(18):
# Randomly display different images
for i in range(len(Glo.reel)):
if i >= reel and not Glo.hold_btn_is_held[i]:
card = random.choice(cards)
Glo.card_labels[reel].configure(image=card)
Glo.card_labels[reel].update()
time.sleep(0.025)
# The reel is stopped. Display the final image
card = Glo.card_images[Glo.reel[reel]]
Glo.card_labels[reel].configure(image=card)
Glo.card_labels[reel].update()
check_for_win()
rnd_hold()
save_bank(reels)
# Add this to initialization code. Maybe in the same place where you make
# the hold buttons (once) and the hold button images (once)
Glo.card_labels = = [Label(cards_frame), Label(cards_frame), Label(cards_frame)]
Glo.card_images = make_images()
def get_rnd_cards()
"""Randomly select cards for slots that are not held. Can only
use a card once.
"""
# Get unused cards
cards = Glo.ranks.copy()
for i in range(len(Glo.reel)):
if Glo.hold_btn_is_held[i]:
cards.remove(Glo.reel[i])
# Choose from remaining cards
for i in range(len(Glo.reel)):
if not Glo.hold_btn_is_held[i]:
card = random.choice(cards)
Glo.reel[i] = card
cards.remove(card)
|