Skip to content

Instantly share code, notes, and snippets.

@ErrorBot1122
Last active March 5, 2024 21:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErrorBot1122/80574c5b98162a05e9a0e9a7d03a78e6 to your computer and use it in GitHub Desktop.
Save ErrorBot1122/80574c5b98162a05e9a0e9a7d03a78e6 to your computer and use it in GitHub Desktop.
Simple Python Slots Animation

A Simple Python Slot Machine Animation

A simple Slot Machine animation I made during my free time at school!

I made this as a way for me to practice animating with PYTHON in the console, for a future project I have.

I may (probobly not) add BETTING functionality to this in the future... maybe...

import random
from threading import Thread
from time import sleep
# The diffrent symbols and there corisponding chances of showing up
symbols = [ "1", "2", "3", "4", "5", "6", "7" ] # (Recomended for all the symbols to be the same langht. 1 Charcter recommended)
symbol_weights = [ 64, 32, 16, 8, 4, 2, 1 ]
slot_setep_lenght = 0.2
# PROGRAM VARS (DO NOT TOUCH)
slots = [
["1", "2", "3"],
[" ", " ", " "],
[" ", " ", " "],
["4", "5", "6"],
[" ", " ", " "],
[" ", " ", " "],
["7", "8", "9"],
]
slots_changed = True
def printSlots():
replace_last = ("\033[1A" * 11) # Weird chars that replace the last shown board
print(replace_last + f"""+---+---+---+
| {slots[0][0]} | {slots[0][1]} | {slots[0][2]} |
+-{slots[1][0]}-+-{slots[1][1]}-+-{slots[1][2]}-+
| {slots[2][0]} | {slots[2][1]} | {slots[2][2]} |
| {slots[3][0]} | {slots[3][1]} | {slots[3][2]} |
| {slots[4][0]} | {slots[4][1]} | {slots[4][2]} |
+-{slots[5][0]}-+-{slots[5][1]}-+-{slots[5][2]}-+
| {slots[6][0]} | {slots[6][1]} | {slots[6][2]} |
+---+---+---+""")
def shiftSlotColumn(col, next_item = " "):
global slots_changed
# SHift each slot down
slots[6][col] = slots[5][col]
slots[5][col] = slots[4][col]
slots[4][col] = slots[3][col]
slots[3][col] = slots[2][col]
slots[2][col] = slots[1][col]
slots[1][col] = slots[0][col]
# Replace the next item to the top of the column
slots[0][col] = next_item
slots_changed = True
def stepSlotColumn(col, animation_delay=0.4):
# SHift each slot down
for _ in range(2):
shiftSlotColumn(col, " ")
sleep(animation_delay / 3)
# Do the final shift
next_symbol = random.choices(symbols, symbol_weights)[0] # Chose the next symbol at random
shiftSlotColumn(col, next_symbol)
sleep(animation_delay / 3)
def spinSlotColumn(col):
global slots_changed
# Vary how long it takes for the spinner to stop
spin_steps = random.randint(30, 100)
# START THE SPINNN!!!
for i in range(spin_steps):
stepSlotColumn(col, slot_setep_lenght)
# Slow the spinner down to a halt
delay = slot_setep_lenght
while delay <= 2:
delay = delay * 1.5 # Add friction and slow it down every time!
stepSlotColumn(col, delay)
# "LOCK IN" the row!
slots[5][col] = "-"
slots[1][col] = "-"
slots_changed = True
def spinSlots():
## SPIN THE SLOTS ##
spinSlot0Thread = Thread(target = spinSlotColumn, args=[0])
spinSlot1Thread = Thread(target = spinSlotColumn, args=[1])
spinSlot2Thread = Thread(target = spinSlotColumn, args=[2])
# Start Spinning the slots!
spinSlot0Thread.start()
sleep(slot_setep_lenght / 3) # Offset the spins so it looks nicer
spinSlot1Thread.start()
sleep(slot_setep_lenght / 3)
spinSlot2Thread.start()
# Wait for all the slots to finish spinning
while (spinSlot0Thread.is_alive() or spinSlot1Thread.is_alive() or spinSlot2Thread.is_alive()):
sleep(0.01)
def gameLoop():
while True:
spinSlots()
sleep(3)
def renderLoop():
global slots_changed
while True:
sleep(0.05)
# Display the updated slots grafic as it spins
if slots_changed:
printSlots()
slots_changed = False # Dont keep updating it dude...
# Start the code that renders the slots
Thread(target = renderLoop).start()
# Start the main code that handles accualy spinning and awarding the slots
gameLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment