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.
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()
