Conditionals & Lists? (solved)
Posted: Sun Nov 23, 2014 3:07 pm
There's a small explanation through comments in the code below, but I'll still try to explain it.
BTW, I'm building a rhythm game, which should hopefully explain some of the code?
Here is where I tell the player what sounds to play:
They are supposed to memorize what bsound1, bsound2, and bsound3 all sound like (which is shown in a label earlier).
I should also mention that I have $ order = [] and $ count = 0 initialized earlier in the code...
Anyways, I calls screen r, and there are 3 buttons on the screen.
If the player presses button E4 only 3 times then the code should look like this:
and this if statement
SHOULD come out false, and jump to the else beneath it and set levelnotcompleted to True. But no matter what, every time, that first if statement comes out True and levelcompleted sets itself to True.
So basically, no matter what order those textbuttons are pressed, levelcompleted is set to True and the player can advance in the game... I feel like it either has something to do with all those if statements, or with the list/array.
Because, before I even press the buttons, order already has 1, 2, and 3, indexed within it?
So, I'm really not sure why it's caused, but thanks to any help in advance.
BTW, I'm building a rhythm game, which should hopefully explain some of the code?
Code: Select all
screen r:
add "#FFBCD6"
text "Rhythm Game" xalign 0.5 yalign 0.05 color "#921F4B"
text "Level: [level]" xalign 0.5 yalign 0.3 color "#921F4B"
hbox:
yalign 0.5 xalign 0.5
textbutton "E3" action Play("sound", bsound1), SetVariable("pressed1", True)
textbutton "E4" action Play("sound", bsound2), SetVariable("pressed2", True)
textbutton "D4" action Play("sound", bsound3), SetVariable("pressed3", True)
if pressed1: # When button E3 is pressed, pressed1 is set to true
$ count += 1
$ order.append(1)
elif pressed2: # When button E4 is pressed, pressed2 is set to true
$ order.append(2)
$ count += 1
elif pressed3: # When button D4 is pressed, pressed3 is set to true
$ count += 1
$ order.append(3)
if count >= 3: # No matter what order you press them in, when to count is 3 or greater...
if (order[0] == 1) and (order[1] == 2) and (order[2] == 3): # ... It'll check to see if index in the list "order" is exactly each number
$ levelcompleted = True # But level completed is always being set to true
else:
$ levelnotcompleted = True
if levelcompleted:
text "Good job! You won the first level!" xalign 0.5 yalign 0.6
textbutton "Next Level!" action Jump("level_2")
elif levelnotcompleted:
text "Sorry! Better luck next time, you didn't win!"
textbutton "Try again?" action Jump("playagain")
Code: Select all
label start:
$ bsound1 = "E3.mp3"
$ bsound2 = "E4.mp3"
$ bsound3 = "D4.mp3"
# some irrelevant code in a label in between here...
label level_1:
"LEVEL 1"
queue sound [ bsound1, bsound2, bsound3 ]
I should also mention that I have $ order = [] and $ count = 0 initialized earlier in the code...
Anyways, I calls screen r, and there are 3 buttons on the screen.
If the player presses button E4 only 3 times then the code should look like this:
Code: Select all
$ order = [2, 2, 2]
$ count = 3
Code: Select all
if count >= 3: # No matter what order you press them in, when to count is 3 or greater...
if (order[0] == 1) and (order[1] == 2) and (order[2] == 3): # ... It'll check to see if index in the list "order" is exactly each number
$ levelcompleted = True # But level completed is always being set to true
else:
$ levelnotcompleted = True
So basically, no matter what order those textbuttons are pressed, levelcompleted is set to True and the player can advance in the game... I feel like it either has something to do with all those if statements, or with the list/array.
Because, before I even press the buttons, order already has 1, 2, and 3, indexed within it?
So, I'm really not sure why it's caused, but thanks to any help in advance.