Python Forum

Full Version: [Turtle]Need help with moving a square on key press
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to learn how to create sprites and move them around the screen.

The goal was to get a black square to move in 4 directions. (I copied it from a tutorial since I am learning)
The problem is, my cube isn't moving. I think it's to do with the screen not updating or my keypresses aren't registering.

import turtle
import time
pause = 0.1
#screen
Screen = turtle.Screen()
Screen.title("Block")
Screen.bgcolor("white")
Screen.setup(width=1280, height=720)
Screen.tracer(0)#turns off the screen updates

#game
Block = turtle.Turtle()
Block.speed(0)#no slow down with animation speed
Block.shape("square")#shape
Block.color("black")#color not colour
Block.penup()#stops drawing
Block.goto(0,0)#spawns in
Block.direction = "stop"#cant you read
    

#fucntions
def go_up():
    Block.direction == "up"

def go_down():
    Block.direction == "down"

def go_left():
    Block.direction == "left"

def go_right():
    Block.direction == "right"
    
def move():
    if Block.direction == "up":
        y = Block.ycor()
        Block.sety(y + 20)
    
    elif Block.direction == "down":
        y = Block.ycor()
        Block.sety(y - 20)

    elif Block.direction == "left":
        x = Block.xcor()
        Block.setx(x - 20)

    elif Block.direction == "right":
        x = Block.xcor()
        Block.setx(x + 20)

#keyboard

Screen.onkey(go_up, "w")
Screen.onkey(go_down, "s")
Screen.onkey(go_left, "a")
Screen.onkey(go_right, "d")
Screen.listen()

#gameloop
while True:
    Screen.update()
    
    move()
    
    time.sleep(pause)
        
Screen.mainloop()
Change "==" to "=":

fucntions
def go_up():
    Block.direction = "up"

def go_down():
    Block.direction = "down"

def go_left():
    Block.direction = "left"

def go_right():
    Block.direction = "right"
Screen.tracer(0)#turns off the screen updates has stopped the screen updates, i removed this line.
The while loop you created is not needed, Screen.mainloop() takes care of it.
I removed the function move and moved the block movement code directly into the key events.
Full code
import turtle

# screen
Screen = turtle.Screen()
Screen.title("Block")
Screen.bgcolor("white")
Screen.setup(width=1280, height=720)

# game
Block = turtle.Turtle()
Block.speed(0)  # no slow down with animation speed
Block.shape("square")  # shape
Block.color("black")  # color not colour
Block.penup()  # stops drawing
Block.goto(0, 0)  # spawns in


# fucntions
def go_up():
    y = Block.ycor()
    Block.sety(y + 20)


def go_down():
    y = Block.ycor()
    Block.sety(y - 20)


def go_left():
    x = Block.xcor()
    Block.setx(x - 20)


def go_right():
    x = Block.xcor()
    Block.setx(x + 20)


# keyboard
Screen.onkey(go_up, "w")
Screen.onkey(go_down, "s")
Screen.onkey(go_left, "a")
Screen.onkey(go_right, "d")
Screen.listen()

Screen.mainloop()