My Simon Says Game For the PI in python - Raspberry Pi Forums


hey everyone, i'm super new programming , pi , wondering if experts @ code , see improve on. works how want i'm sure did bunch of stuff inefficiently , i'm looking @ ways better.

it's simon says game uses 5 push buttons have led's built inside of them. i'm using 10 gpio pins mc23017 chip because plan on using gpio pins on pi other stuff. starts off flashing 1 light , if user hits correct button associated light give them 2 lights... , on until reach end. if hit wrong light lose , creates new random sequence of lights , game starts over. anyways here's code. looking!

code: select all

#!/usr/bin/python  import time import datetime import threading import random import libraries.adafruit_gpio.mcp230xx mcp #mcp23017 library extending gpio pins  #config setup  mcp = mcp.mcp23017(busnum = 1, address = 0x20)  #pins whitelight = 0 whitebutton = 1 redbutton = 8 redlight = 9 yellowbutton = 10 yellowlight = 11 greenlight = 12 greenbutton = 13 bluebutton = 14 bluelight = 15  mcp.setup(redbutton, gpio.in) mcp.setup(redlight, gpio.out) mcp.setup(yellowbutton, gpio.in) mcp.setup(yellowlight, gpio.out) mcp.setup(greenbutton, gpio.in) mcp.setup(greenlight, gpio.out) mcp.setup(bluebutton, gpio.in) mcp.setup(bluelight, gpio.out) mcp.setup(whitebutton, gpio.in) mcp.setup(whitelight, gpio.out)  #length of game in lights length = 5  #game start  buttons = [whitebutton, redbutton, yellowbutton, greenbutton, bluebutton] lights = [whitelight, redlight, yellowlight, greenlight, bluelight]  #setup pullup buttons in buttons:     mcp.pullup(i, 1)  #globals currentlight = 0 showsequence = true solution = []  #create random solution def createsolution():     global solution     global showsequence     global currentlight     solution = []     currentlight = 0     in range(0, length):         solution.append(random.choice(buttons))     print(solution)     time.sleep(1)     showsequence = true      def flashlight(light, length):     mcp.output(light, gpio.high)     time.sleep(length)     mcp.output(light, gpio.low)  def incorrecthit():     #flash pins     time.sleep(0.5)     x in range(0,3):         in lights:             mcp.output(i, gpio.high)                      time.sleep(.25)                  in lights:             mcp.output(i, gpio.low)          time.sleep(.25)      #create new solution     createsolution()           def flashsequence():     while(true):         if(showsequence):             in range(0, currentlight + 1):                 flashlight(lights[buttons.index(solution[i])], 1)                 time.sleep(1)             time.sleep(3)         time.sleep(0.10)  createsolution()    #general loop of simon says threading.thread(target=flashsequence).start()  currentstep = 0  while (true):     #flash current light     in buttons:         if mcp.input(i) == false:             print(i)             showsequence = false             #turn on light             flashlight(lights[buttons.index(i)], .50)              #check if hit correct button             if(i ==  solution[currentstep]):                 print("hit: " , , " wanted: " , solution[currentstep])                 #correct button                 if(currentstep == currentlight):                     #finished current step                     currentstep = 0                     currentlight += 1                     time.sleep(1)                     showsequence = true                 else:                     currentstep += 1                  #check victory                 if(currentlight == 5):                     print("victory!")                     showsequence = false                     break                                  print("cl: " , currentlight)                 print("cs: " , currentstep)             else:                 print("incorrect button hit")                 incorrecthit()                              time.sleep(0.15) #debounce                  time.sleep(0.10)

there funny things going on in code...

think need go , think algorithm program, writing psuedo code might help

code: select all

while gameover false     add new colour sequence.     display light sequence     tries in length of sequence          user button press          if button incorrect                mark incorrect                break         else                mark correct     if user correct         give user point         continue     else          gameover = true     


raspberrypi



Comments