JamesDevJim / game-zulu

First game of the coolest project
0 stars 1 forks source link

Conditional methods in classes #8

Open shuttle1987 opened 4 years ago

shuttle1987 commented 4 years ago

I see that you have a class that has methods that are conditionally included based on what is detected at runtime:

https://github.com/JamesDevJim/game-zulu/blob/9bf97d92194ff8e7d61785aa535806c37a14003a/shared/control.py#L102

I'd like to know what you are trying to achieve here as there's likely other ways to achieve the desired outcome

JamesDevJim commented 4 years ago

This was a tough cookie to figure out.

The code would not run (and return errors) if the Arduino wasn't connected, because "Arduino" is defined if the following code. This made testing VERY difficult because the button box had to be plugged in. Thats why, in the control class there is a duplicate of everything for the keyboard if not uneArduino.

Would LOVE to hear of a better way to handle this.

try:
    arduino = ArduinoMega(ArduinoMegaPort)
    # arduino = ArduinoMega('/dev/ttyACM1')    
    # arduino = ArduinoMega('/dev/ttyACM0')
    # arduino = ArduinoMega('COM7')
    time.sleep(0.2)  
    iterator = util.Iterator(arduino)
    iterator.start()
    time.sleep(0.2)  

    # Define all input pins
    PIN_ONE = arduino.get_pin('d:37:i')
    PIN_TWO = arduino.get_pin('d:36:i')
    PIN_THREE = arduino.get_pin('d:35:i')
    PIN_LEFT = arduino.get_pin('d:32:i')
    PIN_RIGHT = arduino.get_pin('d:31:i')
    PIN_UP = arduino.get_pin('d:34:i')
    PIN_DOWN = arduino.get_pin('d:33:i')
    PIN_BACK = arduino.get_pin('d:30:i')
    PIN_MOTION = arduino.get_pin('d:8:i')
    PIN_DOOR = arduino.get_pin('d:38:i')    

    #Define all lights
    PIN_LED_B1 = arduino.get_pin('d:11:p')
    PIN_LED_B2 = arduino.get_pin('d:10:p')
    PIN_LED1 = arduino.get_pin('d:3:p')
    PIN_LED2 = arduino.get_pin('d:2:p')
    PIN_LED3 = arduino.get_pin('d:5:p')
    PIN_LED4 = arduino.get_pin('d:4:p')
    PIN_LED5 = arduino.get_pin('d:13:p')
    print('Button box connection: Successful')

except:
    useArduino = False
    print('Button box connection: Unsuccessful')
shuttle1987 commented 4 years ago

I'd make some sort of button box simulator (perhaps using Tkinter) to do this if testing is needed without the physical hardware being present.