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!