FourEights
FourEights

Reputation: 39

I'm trying to write a game in python with turtle graphics, but the movement's broken

So, this is what I have so far:

import time
import random

#Window
wn = turtle.Screen()
wn.title("Turtle Game")
wn.bgcolor("black")
wn.setup(width=600, height=600)
wn.tracer(0)

#Player's character
player = turtle.Turtle()
player.speed(0)
player.shape("square")
player.color("white")
player.penup()
player.goto(0,0)
player.direction = "stop"

#Player's movement
def go_up():
    if player.direction != "down":
        player.direction = "up"

def go_down():
    if player.direction != "up":
        player.direction = "down"

def go_left():
    if player.direction != "right":
        player.direction = "left"

def go_right():
    if player.direction != "left":
        player.direction = "right"

def move():
    if player.direction == "up":
        y = player.ycor()
        player.sety(y + 20)

    if player.direction == "down":
        y = player.ycor()
        player.sety(y - 20)

    if player.direction == "left":
        x = player.xcor()
        player.setx(x - 20)

    if player.direction == "right":
        x = player.xcor()
        player.setx(x + 20)

#Listen for keypresses
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")

At the moment, all I'm getting is a blank screen in the window. the #Window seems to work fine, but other than that, nothing else is appearing. I'm honestly not sure why this is happening, so I've turned to StackOverflow for some help.

Upvotes: 1

Views: 270

Answers (1)

Some points on your code:

  1. I had to add import turtle at the beginning, to use the turtle module.

  2. The window opened and then closed immediately. I had to add wn.exitonclick() at the end to keep the window open.

  3. The move() function is never called. I had to add a call to move() in each of go_up, go_down, go_left and go_right after the player direction is updated.

  4. You disable tracing by setting wn.tracer(0). Following the documentation, without tracing you have to refresh the window manually using turtle.update(). I added this call to the end of the move() function. Alternativaly, you could use wn.tracer(1) to automatically refresh the screen on every change.

    Admittedly the documentation is not perfectly clear on the effect of 0.

Applying these changes to your code gives me a window where I can move around the turtle using w/a/s/d.

One additional note: It is unclear to me what you are trying to do with the if statements in your go_... functions. In go_up(), you set the direction to up only if it is not currently down. This has the effect that when I moved the turtle down, I cannot move it up again until I make a side-step via go_left()/go_right(). This seems strange. Maybe you meant to only update to up if the direction is not already up? As in:

    if player.direction != "up":
        player.direction = "up"

Upvotes: 3

Related Questions