Home > front end >  Skipping through all elifs to the end and activating the last statements
Skipping through all elifs to the end and activating the last statements

Time:11-10

I am making a very creative tetris clone for a project with a friend and we have custom sprites for every shape (including the rotations) and we made a wall of else if statements for the rotations. It's supposed to work like this: It calls a random shape out of list of the main 7 shapes and every time the user presses "a" it changes the left facing gif of that shape using the giant el if wall. However, we ran unto an error where instead of executing the elif statements, it just skips to the last line and changes to the J shape no matter what shape was called in the random statement. Any help would be awesome, thank you! (We are still learning btw) also the rotate function is also supposed to happen when the user presses the "d" key but its supposed to change to the other direction.

   
import turtle as trtl
import random as rand
wn = trtl.Screen()
 
#configurations
tetris = trtl.Turtle()
drawer = trtl.Turtle()
background = trtl.Turtle()
 
starty = int(-400)
 
square = ("sqaure_block.gif")
 
s_block_normal = ("S_block_norm.gif")
s_block_standing = ("S_block_right.gif")
 
invert_s_block_normal = ("Z_block_norm.gif")
invert_s_block_standing = ("Z_block_right.gif")
 
l_block_normal = ("L_block_norm.gif")
l_block_180 = ("L_block_180.gif")
l_block_right = ("L_block_right.gif")
l_block_left = ("L_block_left.gif")
 
line_normal = ("line_block_norm.gif")
line_standing = ("line_block_standing.gif")
 
t_block_normal = ("T_block_norm.gif")
t_block_180 = ("T_block_180.gif")
t_block_right = ("T_BLOCK_RIGHT.gif")
t_block_left = ("T_block_left.gif")
 
j_block_normal = ("J_block_norm.gif")
j_block_180 = ("J_block_180.gif")
j_block_right = ("J_block_right.gif")
j_block_left = ("J_block_left.gif")
 
wn.addshape (square)
 
wn.addshape(s_block_normal)
wn.addshape(s_block_standing)
 
wn.addshape(invert_s_block_normal)
wn.addshape(invert_s_block_standing)
 
wn.addshape(l_block_normal)
wn.addshape(l_block_180)
wn.addshape(l_block_right)
wn.addshape(l_block_left)
 
wn.addshape(line_normal)
wn.addshape(line_standing)
 
wn.addshape(t_block_normal)
wn.addshape(t_block_180)
wn.addshape(t_block_right)
wn.addshape(t_block_left)
 
wn.addshape(j_block_normal)
wn.addshape(j_block_180)
wn.addshape(j_block_right)
wn.addshape(j_block_left)
 
Tshape = [square, s_block_normal, invert_s_block_normal, l_block_normal, line_normal, t_block_normal, j_block_normal]
 
squarecor = [-50, -25, -100, -50]
wn.bgcolor("lightblue")
background.speed("fastest")
background.hideturtle()
background.pu()
background.goto(-400,-200)
background.fillcolor("blue")
background.pencolor("blue")
background.begin_fill()
background.pd()
background.goto(400,-200)
background.goto(400,-350)
background.goto(-400,-350)
background.goto(-400,-200)
background.end_fill()
#sprite direction change
 
def sprite_right():
    global tetris
    tetris.hideturtle()
    if (tetris.shape(s_block_normal)):
        tetris.shape(s_block_standing)
    elif (tetris.shape(invert_s_block_normal)):
        tetris.shape(invert_s_block_standing)
    elif (tetris.shape(line_normal)):
        tetris.shape(line_standing)
    elif (tetris.shape(l_block_normal)):
        tetris.shape(l_block_right)
    elif (tetris.shape(t_block_normal)):
        tetris.shape(t_block_right)
    elif (tetris.shape(j_block_normal)):
        tetris.shape(j_block_right)
    elif (tetris.shape(l_block_right)):
        tetris.shape(l_block_180)
    elif (tetris.shape(t_block_right)):
        tetris.shape(t_block_180)
    elif (tetris.shape(j_block_right)):
        tetris.shape(j_block_180)
    elif (tetris.shape(s_block_standing)):
        tetris.shape(s_block_normal)
    elif (tetris.shape(invert_s_block_standing)):
        tetris.shape(invert_s_block_normal)
    elif (tetris.shape(line_standing)):
        tetris.shape(line_normal)
    elif (tetris.shape(l_block_180)):
        tetris.shape(l_block_left)
    elif (tetris.shape(t_block_180)):
        tetris.shape(t_block_left)
    elif (tetris.shape(j_block_180)):
        tetris.shape(j_block_left)
    elif (tetris.shape(l_block_left)):
        tetris.shape(l_block_normal)
    elif (tetris.shape(t_block_left)):
        tetris.shape(t_block_normal)
    elif (tetris.shape(j_block_left)):
        tetris.shape(j_block_normal)
    tetris.showturtle()
 
def sprite_left():
    tetris.hideturtle()
    if (tetris.shape(s_block_normal)):
        tetris.shape(s_block_standing)
    elif (tetris.shape(invert_s_block_normal)):
        tetris.shape(invert_s_block_standing)
    elif (tetris.shape(line_normal)):
        tetris.shape(line_standing)
    elif (tetris.shape(l_block_normal)):
        tetris.shape(l_block_left)
    elif (tetris.shape(t_block_normal)):
        tetris.shape(t_block_left)
    elif (tetris.shape(j_block_normal)):
        tetris.shape(j_block_left)
    elif (tetris.shape(l_block_left)):
        tetris.shape(l_block_180)
    elif (tetris.shape(t_block_left)):
        tetris.shape(t_block_180)
    elif (tetris.shape(j_block_left)):
        tetris.shape(j_block_180)
    elif (tetris.shape(s_block_standing)):
        tetris.shape(s_block_normal)
    elif (tetris.shape(invert_s_block_standing)):
        tetris.shape(invert_s_block_normal)
    elif (tetris.shape(line_standing)):
        tetris.shape(line_normal)
    elif (tetris.shape(l_block_180)):
        tetris.shape(l_block_right)
    elif (tetris.shape(t_block_180)):
        tetris.shape(t_block_right)
    elif (tetris.shape(j_block_180)):
        tetris.shape(j_block_right)
    elif (tetris.shape(l_block_right)):
        tetris.shape(l_block_normal)
    elif (tetris.shape(t_block_right)):
        tetris.shape(t_block_normal)
    elif (tetris.shape(j_block_right)):
        tetris.shape(j_block_normal)
    tetris.showturtle()
'''
def (fast_down):
    global tetris
    tetris.setheading(270)
    tetris.forward(10)
'''
 
 
 
#turn right
def turn_right():
    tetris.speed("fastest")
    tetris.setheading(0)
    tetris.speed(1)
    tetris.forward(10)
    tetris.speed("fastest")
    tetris.setheading(270)
#turn left
def turn_left():
    tetris.speed("fastest")
    tetris.setheading(180)
    tetris.speed(1)
    tetris.forward(10)
    tetris.speed("fastest")
    tetris.setheading(270)
 
#down
 
#tetris container/backround
drawer.pensize(5)
drawer.speed("fastest")
drawer.penup()
drawer.goto(150, 200)
drawer.pendown()
 
drawer.setheading(270)
drawer.forward(400)
drawer.setheading(180)
drawer.forward(300)
drawer.setheading(90)
drawer.forward(400)
drawer.hideturtle()
 
#game WIP!!!!!!!!!!
y = 0
space = int(1)
tracer = True
def spawn_T():
    new_T=rand.choice(Tshape)
    tetris.shape(t_block_180)
    tetris.pu()
    tetris.goto(0, 200)
    tetris.setheading(270)
    tetris.forward(10)
    '''
    if ((abs(space - starty)) < 200):
        tetris.forward(2)
    else:
    '''
    tetris.stamp()
    '''
    space = space =  1
    '''
    tetris.hideturtle()
    tetris.goto(0,200)
    tetris.showturtle()
   
 
 
if (y == 0):
    spawn_T()
 
#changeing the directions of the sprites
 
#events
 
wn.onkeypress(turn_right, "Right")
wn.onkeypress(turn_left, "Left")
wn.onkeypress(sprite_right, "a")
wn.onkeypress(sprite_left, "d")
'''
wn.onkeypress(fast_down, "s")
'''
wn.listen()
 
wn.mainloop()

Here is the google drive file https://drive.google.com/drive/folders/1Q_zXEqm4aFHQ4RV-ZvEXCK2Wi7SHMxrW?usp=share_link

CodePudding user response:

I think the indentation is wrong here. Maybe try putting the last else-statement one tab further?

CodePudding user response:

Please use elif instead of the else: if formulation to get away from the bewildering indentation - here's how it should look:

def sprite_right():
    global tetris
    tetris.hideturtle()
    if (tetris.shape(s_block_normal)):
        tetris.shape(s_block_standing)
    elif (tetris.shape(invert_s_block_normal)):
        tetris.shape(invert_s_block_standing)
    elif (tetris.shape(line_normal)):
        tetris.shape(line_standing)
    elif (tetris.shape(l_block_normal)):
        tetris.shape(l_block_right)
    elif (tetris.shape(t_block_normal)):
        tetris.shape(t_block_right)
    elif (tetris.shape(j_block_normal)):
        tetris.shape(j_block_right)
    elif (tetris.shape(l_block_right)):
        tetris.shape(l_block_180)
    elif (tetris.shape(t_block_right)):
        tetris.shape(t_block_180)
    elif (tetris.shape(j_block_right)):
        tetris.shape(j_block_180)
    elif (tetris.shape(s_block_standing)):
        tetris.shape(s_block_normal)
    elif (tetris.shape(invert_s_block_standing)):
        tetris.shape(invert_s_block_normal)
    elif (tetris.shape(line_standing)):
        tetris.shape(line_normal)
    elif (tetris.shape(l_block_180)):
        tetris.shape(l_block_left)
    elif (tetris.shape(t_block_180)):
        tetris.shape(t_block_left)
    elif (tetris.shape(j_block_180)):
        tetris.shape(j_block_left)
    elif (tetris.shape(l_block_left)):
        tetris.shape(l_block_normal)
    elif (tetris.shape(t_block_left)):
        tetris.shape(t_block_normal)
    elif (tetris.shape(j_block_left)):
        tetris.shape(j_block_normal)
                                                                            
    tetris.showturtle()
 
def sprite_left():
    tetris.hideturtle()
    if (tetris.shape(s_block_normal)):
        tetris.shape(s_block_standing)
    elif (tetris.shape(invert_s_block_normal)):
        tetris.shape(invert_s_block_standing)
    elif (tetris.shape(line_normal)):
        tetris.shape(line_standing)
    elif (tetris.shape(l_block_normal)):
        tetris.shape(l_block_left)
    elif (tetris.shape(t_block_normal)):
        tetris.shape(t_block_left)
    elif (tetris.shape(j_block_normal)):
        tetris.shape(j_block_left)
    elif (tetris.shape(l_block_left)):
        tetris.shape(l_block_180)
    elif (tetris.shape(t_block_left)):
        tetris.shape(t_block_180)
    elif (tetris.shape(j_block_left)):
        tetris.shape(j_block_180)
    elif (tetris.shape(s_block_standing)):
        tetris.shape(s_block_normal)
    elif (tetris.shape(invert_s_block_standing)):
        tetris.shape(invert_s_block_normal)
    elif (tetris.shape(line_standing)):
        tetris.shape(line_normal)
    elif (tetris.shape(l_block_180)):
        tetris.shape(l_block_right)
    elif (tetris.shape(t_block_180)):
        tetris.shape(t_block_right)
    elif (tetris.shape(j_block_180)):
        tetris.shape(j_block_right)
    elif (tetris.shape(l_block_right)):
        tetris.shape(l_block_normal)
    elif (tetris.shape(t_block_right)):
        tetris.shape(t_block_normal)
    elif (tetris.shape(j_block_right)):
        tetris.shape(j_block_normal)
                                                                            
    tetris.showturtle()

That said, because this is all the code you've provided, it's not possible to run it and examine why it's not working. Can you please provide an example of an implementation of these functions?

CodePudding user response:

According to the documentation, shape() returns the current shape with no parameters, and sets it with one parameter. Therefore in:

if tetris.shape(s_block_normal):

shape(name) returns None, which is always False. So, if you change to:

if tetris.shape() == s_block_normal:

It should function as required.

Also, you can get rid of the if ladder completely by, for instance, something like:

next_left_shape = {s_block_normal:        s_block_standing,
                   invert_s_block_normal: invert_s_block_standing,
                   ... }
tetris.shape(next_left_shape[tetris.shape()])
  • Related