mp-flipper icon indicating copy to clipboard operation
mp-flipper copied to clipboard

Can't get program to run!!

Open ThePipersScourge opened this issue 1 year ago • 4 comments

I wrote a simple program in python this morning and I can't seem to get it to run on the flipper.. I know it probably has something to do with my code, but I'm not sure what. Does anyone have a second to look over the code? Maybe point me in the right direction? Heads up, I'm pretty new to github and programming all round.

rock_paper_scissors_flipper_mp_code.txt

ThePipersScourge avatar Oct 13 '24 14:10 ThePipersScourge

I don't have a Flipper at hand to test it, but I think I've spotted some minor issues:

  • If you want to access variables from the global scope within a function, you need to declare them as global inside the function definition (as you did in get_user_input).
  • To handle use input, you need to use the @f0.on_input decorator. The decorated function will be invoked automatically upon the press of a button.

ofabel avatar Oct 13 '24 15:10 ofabel

Awesome. I'll make some changes and it try again. Thank you!

ThePipersScourge avatar Oct 13 '24 15:10 ThePipersScourge

No luck. It sends me back to the MP welcome page when I select the program I wrote. I was wondering; could it be because I'm using a .py file from VSCode?

ThePipersScourge avatar Oct 13 '24 15:10 ThePipersScourge

VSCode and the .py file is fine. This version should work - but didn't test it - just be aware, that the print statement doesn't print to the display, use the CLI instead.

import flipperzero as f0
import time
import random


user_input = None
computer_pick = None
exit_game = False

user_wins = 0
computer_wins = 0

options = ["rock", "paper", "scissors"]


@f0.on_input
def get_user_input(button, type):
    global user_input
    global exit_game

    if button == f0.INPUT_BUTTON_BACK and type == f0.INPUT_TYPE_LONG:
        exit_game = True

    elif button == f0.INPUT_BUTTON_LEFT and type == f0.INPUT_TYPE_SHORT:
        user_input = "rock"

    elif button == f0.INPUT_BUTTON_RIGHT and type == f0.INPUT_TYPE_SHORT:
        user_input = "paper"

    elif button == f0.INPUT_BUTTON_DOWN and type == f0.INPUT_TYPE_SHORT:
        user_input = "scissors"


def get_cpu_input():
    global computer_pick

    random_number = random.randint(0, 2)
    computer_pick = options[random_number]

    print("Computer picked", computer_pick + ".")


def compare(user_input, computer_pick):
    global user_wins
    global computer_wins

    if user_input == "rock" and computer_pick == "scissors":
        print("You won!")
        user_wins += 1

    elif user_input == "paper" and computer_pick == "rock":
        print("You won!")
        user_wins += 1

    elif user_input == "scissors" and computer_pick == "paper":
        print("You won!")
        user_wins += 1

    else:
        print("You lost!")
        computer_wins += 1


def main():
    global computer_pick
    global user_input

    print("Rock(left button), Paper(right button), or scissors(down button)?")
    
    user_input = None
    
    while user_input == None:
        time.sleep_ms(25)

    print("You picked " + user_input + ".")

    get_cpu_input()
    
    print("The computer picked " + computer_pick + ".")

    compare(user_input, computer_pick)

while not exit_game:
    main()
    time.sleep_ms(25)

ofabel avatar Oct 14 '24 05:10 ofabel

Made some progress with the help of @ofabel! The program (below) runs now. It will print the first line asking rock, paper, or scissors, accept the initial input, and print the second line stating what was picked but crashes immediately after that. I gotta say, this new app is awfully cool and I'm having a blast with the learning the CLI and the (slow) progression being made! Looking forward to getting this fully running and maybe adding more to it so that it's not just text game! Any input or suggestion would be greatly appreciated!

`

import flipperzero as f0
import time
import random


user_input = None
computer_pick = None
exit_game = False

user_wins = 0
computer_wins = 0

options = ["rock", "paper", "scissors"]


@f0.on_input
def get_user_input(button, type):
    global user_input
    global exit_game

    if button == f0.INPUT_BUTTON_BACK and type == f0.INPUT_TYPE_LONG:
        exit_game = True

    elif button == f0.INPUT_BUTTON_LEFT and type == f0.INPUT_TYPE_SHORT:
        user_input = options[0]

        f0.canvas_set_text(0, 6, "You picked " + user_input + ".")
        f0.canvas_update()

    elif button == f0.INPUT_BUTTON_RIGHT and type == f0.INPUT_TYPE_SHORT:
        user_input = options[1]
    
        f0.canvas_set_text(0, 6, "You picked " + user_input + ".")
        f0.canvas_update()

    elif button == f0.INPUT_BUTTON_UP and type == f0.INPUT_TYPE_SHORT:
        user_input = options[2]

        f0.canvas_set_text(0, 6, "You picked " + user_input + ".")
        f0.canvas_update()


def get_cpu_input():
    global computer_pick

    random_number = random.randint(0, 2)
    computer_pick = options[random_number]

    f0.canvas_set_text(0, 12, "The computer picked " + computer_pick + ".")
    f0.canvas_update()


def compare(user_input, computer_pick):
    global user_wins
    global computer_wins

    if user_input == "rock" and computer_pick == "scissors":
        user_wins += 1

    elif user_input == "paper" and computer_pick == "rock":
        user_wins += 1

    elif user_input == "scissors" and computer_pick == "paper":
        user_wins += 1

    else:
        computer_wins += 1

    f0.canvas_set_text(0, 18, "Your wins:", user_wins, "Computer wins:", computer_wins)
    f0.canvas_update()


def main():
    global computer_pick
    global user_input
    global computer_wins
    global user_wins

    f0.canvas_set_text(0, 0, "Rock(<), Paper(>), scissors(^)?")
    f0.canvas_update()

    user_input = None

    while user_input == None:
        time.sleep_ms(25)

    get_cpu_input()

    compare(user_input, computer_pick)

while not exit_game:
    main()
    time.sleep_ms(25)

`

ThePipersScourge avatar Oct 14 '24 22:10 ThePipersScourge