1

I'm working on a project where I have to make a program that fulfils several requirements. I chose the game Simon.

I think I have, for the most part, completed it, however, there is one part I haven't been able to get working right, and that's making it so that it loops through the entire pattern each time. Right now, the user just has to click the most recent color, and it will think it is correct. Say the pattern is red, yellow, blue, green, then the color yellow appears again. All the user has to do is press yellow and it thinks it's correct. I want the user to have to press red, yellow, blue, green, then yellow, in order for it to be correct.

How can I modify my code to do this? Here's my code:

pattern = []
userpattern = []
score = 1
global run_once
run_once = 0

def randomcolor():
  
  print(score)
  for i in range(10):

      padchoice = random.randint(0, 3)

      if(padchoice == 0):
        pattern.append("green")
        break
      elif(padchoice == 1):
        pattern.append("red")
        break
        ...

  print("Pattern: " + str(pattern))
  print(userpattern)


fps = pg.time.Clock()

while(end == False):
  fps.tick(60)

  if run_once == 0:
      randomcolor()
      run_once = 1

  for event in pg.event.get():
      #If Quit Button Pressed
      if event.type == pg.QUIT:
          end = True

      elif event.type == pg.MOUSEBUTTONDOWN:
        pos_x = pg.mouse.get_pos()[0]
        pos_y = pg.mouse.get_pos()[1]

        #Mouse Position
        print(str(pos_x) + ", " + str(pos_y))
        
        if(pos_x > 5 and pos_x < 150):
          if(pos_y > 5 and pos_y < 150):
            green("on")
            userpattern.append("green")
            print(userpattern)
        
        ...
      
  if(userpattern != pattern[:len(userpattern)] and len(userpattern) != 0):
    print("Wrong!")
    userpattern.clear()
    pattern.clear()
    score = 0
    run_once = 0
  
  for i in range(len(pattern)):
    if(pattern[i] == "green"):
      if(pos_x > 5 and pos_x < 150):
          if(pos_y > 5 and pos_y < 150):
            green("on")
            userpattern.append("green")
            print(userpattern)    

  
  if(userpattern == pattern):
    print("Correct")
    score += 1
    print(score)
    run_once = 0

if(score == 11):
  wintext = font.render("Win!"), 0, (255, 255, 255)
  screen.blit(wintext, (screen.get_width()/2, screen.get_height()/2))
  pg.display.flip()

P.S.- I know the code isn't the greatest, I'm sure, but I really just want to get this to work.

Edit: I think the problem lays somewhere around in these lines of code, where I'm trying to get it to work:

      if(userpattern != pattern[:len(userpattern)] and len(userpattern) != 0):
    print("Wrong!")
    userpattern.clear()
    pattern.clear()
    score = 0
    run_once = 0
  
  for i in range(len(pattern)):
    if(pattern[i] == "green"):
      if(pos_x > 5 and pos_x < 150):
          if(pos_y > 5 and pos_y < 150):
            green("on")
            userpattern.append("green")
            print(userpattern)

if(userpattern == pattern): #and new == False
   print("Correct")
   score += 1
   print(score)
   run_once = 0

If anyone needs any more information, or could help, please, let me know! Thanks in advance!

Fox GAMING_NTF
  • 153
  • 2
  • 14
  • 1
    That is a lot of code. Do you think you could narrow it down, so it easier to understand where the problem is exactly? – go2nirvana Dec 22 '20 at 12:49
  • Yes, I'll try. Thanks. – Fox GAMING_NTF Dec 22 '20 at 12:50
  • Ok, I've removed some commented code. The part that I'm trying to get working is right above the part of the code that is commented with, "Render Out Text". I'm trying to figure out how to properly loop through the entire pattern, and try to check it with userpattern, or something like that. I'm currently only trying to do this with green. – Fox GAMING_NTF Dec 22 '20 at 12:53
  • @go2nirvana I've also tried adding a section that is just the code that tries to loop through all the pattern, at the bottom. I hope this helps. Please let me know if you need any more clarification. Thanks! – Fox GAMING_NTF Dec 22 '20 at 12:58
  • @go2nirvana Any ideas? I don't know why I can't figure it out. – Fox GAMING_NTF Dec 22 '20 at 13:09

1 Answers1

1

You can fix your code with ease. Allow the user to make multiple entries. Compare the input (userpattern) with pattern only if the lists have the same length or the part of the pattern that has already been entered is different:

while(end == False):
    # [...]

    for event in pg.event.get():
         if event.type == pg.MOUSEBUTTONDOWN:
         
           # add input to `userpattern`
           # [...]

    if len(userpattern) == len(pattern) or userpattern != pattern[:len(userpattern)]:

        win = userpattern == pattern

        if not win:
            # [...]
        else:
            # [...]

Relevant changes to the code:

while(end == False):
  fpsmax.tick(60)

  # [...]

  #USER INPUT
  for event in pg.event.get():
      # [...]

  #Render Out Text
  patterntext = font.render(str(pattern), 0, (255, 255, 255))
  userpatterntext = font.render(str(userpattern), 0, (255, 255, 255))
  screen.blit(patterntext, (screen.get_width()/2, screen.get_height()/2))
  screen.blit(userpatterntext, (screen.get_width()/2, (screen.get_height()/2) - 50 ))
  pg.display.flip()

  if len(userpattern) == len(pattern) or userpattern != pattern[:len(userpattern)]:
    
    win = userpattern == pattern

    if not win:
      print("Wrong!")
      userpattern.clear()
      pattern.clear()
      score = 0
      run_once = 0
    else:
      print("Correct")
      userpattern.clear()
      new = True
      score += 1
      print(score)
      run_once = 0

      #OUTPUT
      if(score == 10):
        wintext = font.render("You Win!"), 0, (255, 255, 255)
        screen.blit(wintext, (screen.get_width()/2, screen.get_height()/2))
        pg.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • This almost works, except it seems to only count it wrong once the user enters in a pattern the same length as the actual pattern. Other than that, this works perfectly, thanks so much! – Fox GAMING_NTF Dec 22 '20 at 13:29
  • 1
    @FoxGAMING_NTF I see. I've changed the answer: `if len(userpattern) == len(pattern) or userpattern != pattern[:len(userpattern)]:` – Rabbid76 Dec 22 '20 at 13:33
  • Ok, thanks. Sorry, just one final thing I was wondering if you could help me with: It appears as if, sometimes, when the user clicks a color, the square won't light up. Do you have any idea what might be causing this, or how to fix it? – Fox GAMING_NTF Dec 22 '20 at 13:43
  • @FoxGAMING_NTF See [How to wait some time in pygame?](https://stackoverflow.com/questions/18839039/how-to-wait-some-time-in-pygame/64701602#64701602). – Rabbid76 Dec 22 '20 at 14:38