Python Forum
[Turtle]Need help with moving a square on key press
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Turtle]Need help with moving a square on key press
#1
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()
Reply
#2
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"
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle / turtle question DPaul 2 2,146 Oct-04-2020, 09:23 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020