#libraries
import machine
import time
time.sleep(0.1)
#defining components
button2 = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_DOWN)
button1 = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_DOWN)
led8 = machine.Pin(18, machine.Pin.OUT)
led7 = machine.Pin(19, machine.Pin.OUT)
led6 = machine.Pin(20, machine.Pin.OUT)
led5 = machine.Pin(21, machine.Pin.OUT)
led4 = machine.Pin(22, machine.Pin.OUT)
led3 = machine.Pin(26, machine.Pin.OUT)
led2 = machine.Pin(27, machine.Pin.OUT)
led1 = machine.Pin(28, machine.Pin.OUT)
# global variables
debounce_time_ms = 100 # debounce time
debounce_timer1 = machine.Timer() # timer 1
debounce_timer2 = machine.Timer() #timer 2
# are we debouncing or not
debouncing_button1 = False
debouncing_button2 = False
#gpio interrupter handler for button1
def gpio_handler1(pin):
global debouncing_button1
global button_state
if not debouncing_button1:
#print(f"interrupt triggered on pin {pin}")
debouncing_button1 = True # start debouncing
button_state[0] = True
# start the debounce timer
debounce_timer1.init(period = debounce_time_ms,
mode = machine.Timer.ONE_SHOT,
callback = lambda t: end_debounce1()
)
#end debounce func
def end_debounce1():
global debouncing_button1
debouncing_button1 = False # clear the debouncing flag
# configure gpio pin 5 for falling edge interrupts
button1.irq(trigger = machine.Pin.IRQ_FALLING, handler = gpio_handler1)
#gpio interrupter handler for button2
def gpio_handler2(pin):
global debouncing_button2
global button_state
if not debouncing_button2:
# print(f"interrupt triggered on pin {pin}")
debouncing_button2 = True # start debouncing
button_state[1]=True
# start the debounce timer
debounce_timer2.init(period = debounce_time_ms,
mode = machine.Timer.ONE_SHOT,
callback = lambda t: end_debounce2()
)
#end debounce func
def end_debounce2():
global debouncing_button2
debouncing_button2 = False # clear the debouncing flag
# configure gpio pin 5 for falling edge interrupts
button2.irq(trigger = machine.Pin.IRQ_FALLING, handler = gpio_handler2)
#led array for player1
def led_ctrl(a):
a = f"{a:08b}"
led1.value(a[0]=="1")
led2.value(a[1]=="1")
led3.value(a[2]=="1")
led4.value(a[3]=="1")
led5.value(a[4]=="1")
led6.value(a[5]=="1")
led7.value(a[6]=="1")
led8.value(a[7]=="1")
def led_ctrl_p1(p1):
led_ctrl(p1)
def led_ctrl_p2(p2):
led_ctrl(p2)
#state machine
game_state = 0
led_state = [0,0]
button_state = [False,False]
score = [0,0]
main_state = 0
states = {"idle":0,"p1_pressed":1,"p2_pressed":2,"p1n2_pressed":3}
def game_state_mach():
global game_state, led_state, button_state, score
if game_state == states["idle"]:
led_state = [0,0]
if button_state[0]:
game_state = states["p1_pressed"]
led_ctrl_p2(led_state[0])
button_state[0] = False
print("player 1 shooted")
if button_state[1]:
game_state = states["p2_pressed"]
led_ctrl_p2(led_state[1])
button_state[1] = False
print("player 2 shooted")
elif game_state == states["p1_pressed"]:
#moving led for P1
if led_state[0] == 0:
led_state[0] = 1
elif led_state[0] == 128: #checks if p1 wins
score[0] += 1
game_state = states["idle"]
print("p1 wins the round, gets 1 point")
print(f"P1:{score[0]} | P2:{score[1]}")
else:
led_state[0] = led_state[0] << 1 #moving led
#checks double press for p1
if button_state[0]:
score[1]+=2
game_state = states["idle"]
button_state[0] = False
print("p1 double pressed > p2 wins the round, gets 2 point")
print(f"P1:{score[0]} | P2:{score[1]}")
#checks if p1 and p2 press together
if button_state[1]:
game_state = states["p1n2_pressed"]
button_state[1] = False
elif game_state == states["p2_pressed"]:
#moving led for P2
if led_state[1] == 0:
led_state[1] = 128
elif led_state[1] == 1: #checks if p2 wins
score[1] += 1
game_state = states["idle"]
print("p2 wins the round, gets 1 point")
print(f"P1:{score[0]} | P2:{score[1]}")
else:
led_state[1] = led_state[1] >> 1 #moving led
#checks double press for p2
if button_state[1]:
score[0]+=2
game_state = states["idle"]
button_state[1] = False
print("p1 double pressed > p2 wins the round, gets 2 point")
print(f"P1:{score[0]} | P2:{score[1]}")
#checks if p1 and p2 press together
if button_state[0]:
game_state = states["p1n2_pressed"]
button_state[0] = False
elif game_state == states["p1n2_pressed"]:
#moving led for p1
if led_state[0] != 0:
led_state[0] = led_state[0] << 1
else:
led_state[0] = 1
#moving led for p2
if led_state[1] != 0:
led_state[1] = led_state[1] >> 1
else:
led_state[1] = 128
#checks double press for p1
if button_state[0]:
score[1] += 2
game_state = states["idle"]
button_state[0] = False
print("p1 double pressed > p2 wins the round, gets 2 point")
print(f"P1:{score[0]} | P2:{score[1]}")
#checks double press for p2
elif button_state[1]:
score[0] += 2
game_state = states["idle"]
button_state[1] = False
print("p1 double pressed > p2 wins the round, gets 2 point")
print(f"P1:{score[0]} | P2:{score[1]}")
# Checks for clashing shots
if led_state [0] == led_state[1] or (led_state[0] << 1) == led_state[1] or (led_state[0] >> 1) == led_state[1]:
print("CLASHING SHOTS - DRAW")
print(f"P1:{score[0]} | P2:{score[1]}")
game_state = states["idle"]
#checking win situations
if score[0] >= 5:
print("Player 1 Wins")
game_state = states["idle"]
main_state = 0
score = [0,0]
print("Restarting...")
elif score[1] >= 5:
print("Player 2 Wins")
game_state = states["idle"]
main_state = 0
score = [0,0]
print("Restarting...")
led_ctrl_p1(led_state[0])
led_ctrl_p2(led_state[1])
while 1:
# Checking states
if main_state == 0: # Menu State
print("Enter: ")
print("1 for starting the game")
main_state = int(input())
if main_state == 1:
game_state_mach()
time.sleep(0.25)
else:
# If input is not in range
print("Input not in range --> quit()")