Bouncing Ball project Confusion.



  • I'm trying to create a GUI example which bounces a ball around the screen for my book but when the code is run I get the error message:

    === import os, sys, io
    === import M5
    === from M5 import *
    === import time
    === circle0 = None
    === SCREEN_WIDTH = None
    === SCREEN_HEIGHT = None
    === Direction_x = None
    === Direction_y = None
    === Ball_Radius = None
    === def setup():
    ===   global circle0, SCREEN_WIDTH, SCREEN_HEIGHT, Direction_x, Direction_y, Ball_Radius
    ===   M5.begin()
    ===   Widgets.fillScreen(0x222222)
    ===   circle0 = Widgets.Circle(52, 91, 15, 0xffffff, 0xffffff)
    ===   SCREEN_WIDTH = int(320)
    ===   SCREEN_HEIGHT = int(240)
    ===   Direction_x = int(1)
    ===   Direction_y = int(1)
    ===   Ball_Radius = int(10)
    ===   circle0.setRadius(r=(int(Ball_Radius)))
    ===   circle0.setColor(color=0x6600cc, fill_c=0x6600cc)
    ===   circle0.setCursor(x=0, y=0)
    ===   circle0.setCursor(x=(SCREEN_WIDTH / 2), y=(SCREEN_HEIGHT / 2))
    === def loop():
    ===   global circle0, SCREEN_WIDTH, SCREEN_HEIGHT, Direction_x, Direction_y, Ball_Radius
    ===   circle0.setCursor(x=(int(Direction_x)), y=(int(Direction_y)))
    ===   if (int(Direction_x)) + (int(Ball_Radius)) >= (int(SCREEN_WIDTH)) or (int(Direction_x)) + (int(Ball_Radius)) <= (int(SCREEN_WIDTH)):
    ===     Direction_x = Direction_x - Direction_x
    ===   if Direction_y + Ball_Radius >= SCREEN_HEIGHT or Direction_y + Ball_Radius <= Direction_y:
    ===     Direction_y = Direction_y - Direction_y
    ===   time.sleep(1)
    === if __name__ == '__main__':
    ===   try:
    ===     setup()
    ===     while True:
    ===       loop()
    ===   except (Exception, KeyboardInterrupt) as e:
    ===     try:
    ===       from utility import print_error_msg
    ===       print_error_msg(e)
    ===     except ImportError:
    ===       print("please update to latest firmware")
    === 
    === 
    === 
    Traceback (most recent call last):
      File "<stdin>", line 35, in <module>
      File "<stdin>", line 24, in setup
    TypeError: can't convert float to int
    
    

    The UIflow code is as follows
    0_1690099117946_Screenshot 2023-07-23 at 08.58.22.png

    But I keep getting the error both not converting float to int on line 35?



  • @ajb2k3 Try putting int() in python, or "convert to int" in uiflow, around each "SCREEN_xxxxx / 2"

    So that it ends up

    circle0.setCursor(x=(int(SCREEN_WIDTH / 2)), y=(int(SCREEN_HEIGHT / 2)))



  • Thanks, but getting same error message and I went and put convert to int on everything.
    Seams like its actually ignoring the convert to int.



  • Sometime when I get unhelpful error messages like that in python, I take out the try/except around the call to the function and that often results in more helpful (uncaught/unhandled) error messages?

    Id also assign 0 to all those variables instead of None - but thats because I hate python and dont like it choosing its own types.

    Sorry, not much help.



  • @mtylerjr said in Bouncing Ball project Confusion.:

    Sometime when I get unhelpful error messages like that in python, I take out the try/except around the call to the function and that often results in more helpful (uncaught/unhandled) error messages?

    Id also assign 0 to all those variables instead of None - but thats because I hate python and dont like it choosing its own types.

    Sorry, not much help.

    If you look as the block code you can see they are all defined, its just they way that MP/UIFlow implements variables.



  • Did you try take the try/except out?

    If so, did the traceback give more helpful info?

    edit: I guess that's not do-able from uiflow. hmm

    Maybe circle.radius circle.x and circle.y are floats? still, I would expect different behavior.



  • I did and it didn’t work. The problem is that missed a convert to int on one line. I fixed it but the circle doesn’t move because numberz iz hardz



  • 0_1691720861301_f2c9ee59-3979-43c4-bf3a-b859e1ebd82e-image.png