0

I'm building a Simon game using lego's ev3 and microPython.

My code is attached underneath. I think I have a problem with each iteration emptying my array of pressings.

Thanks

#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
                                 InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
import random

# This program requires LEGO EV3 MicroPython v2.0 or higher.
# Click "Open user guide" on the EV3 extension tab for more information.


# Create your objects here.
empty= []
ev3 = EV3Brick()
colors = []
colorsTouch = []
check =False
myellow = Motor(Port.A , positive_direction=Direction.CLOCKWISE, gears=None)
mred = Motor(Port.B , positive_direction=Direction.CLOCKWISE, gears=None)
mblue = Motor(Port.C , positive_direction=Direction.CLOCKWISE, gears=None)
mgreen = Motor(Port.D , positive_direction=Direction.CLOCKWISE, gears=None)

tyellow = TouchSensor(Port.S1)
tred = TouchSensor(Port.S2)
tblue = TouchSensor(Port.S3)
tgreen = TouchSensor(Port.S4)


# Write your program here.
ev3.speaker.beep()
while(1):

    x=random.randint(1,5)

    if(x==4):
       
        colors.append(4)
        print("y")


    elif(x==3):
        
        colors.append(3)
        print("r")


    elif(x==2):

        colors.append(2)
        print ("b")

    elif(x==1):

        colors.append(1)
        print("g")


    for i in range(len(colors)):
        print(colors[i])
        
        if  colors[i]==4:
            myellow.run_angle(200, 180, then=Stop.HOLD, wait=True)
            myellow.run_angle(200, -180, then=Stop.HOLD, wait=True)
        if  colors[i]==3:
            mred.run_angle(200, 180, then=Stop.HOLD, wait=True)
            mred.run_angle(200, -180, then=Stop.HOLD, wait=True)
        if  colors[i]==2:
            mblue.run_angle(200, 180, then=Stop.HOLD, wait=True)
            mblue.run_angle(200, -180, then=Stop.HOLD, wait=True)            
        if  colors[i]==1:
            mgreen.run_angle(200, 180, then=Stop.HOLD, wait=True)
            mgreen.run_angle(200, -180, then=Stop.HOLD, wait=True)
    check=True
    colorsTouch = []
    ev3.speaker.beep()
    while(check):
        if  tyellow.pressed():
            colorsTouch.append(4)
            
        if  tred.pressed():
            colorsTouch.append(3)
            
        if  tblue.pressed():
            colorsTouch.append(2)
            
        if  tgreen.pressed():
            colorsTouch.append(1)

        # check isn't working
        print("check:", check)
        print("colors:", len(colors))
        print("colorsTouch:", len(colorsTouch))
        print(colorsTouch)
        if(len(colorsTouch)==len(colors)):
            for i in range(len(colors)):
                if colors[i]!=colorsTouch[i]:
                    print("LOSE")
            check=False
            

(I would like after each player's turn to empty my array so it will be able to get new inputs. but unstead it seems to be that the pressings are piling up each turn)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you be more specific about your problem? How does the behavior of your code differ from what you want? Are you getting any errors? – larsks Jan 01 '23 at 14:13

0 Answers0