0
votes

This code is supposed to move the player to left and right using the left and right arrow keys, but the player is disappearing when I try to press the arrow key. How do I solve the problem?

Code

import turtle

wn=turtle.Screen()
wn.title("falling skies")
wn.bgcolor("pink")
wn.setup(width=800,height=600)
wn.tracer(0)

#add player
player = turtle.Turtle()
player.speed(0)
player.shape("square")
player.color("blue")
player.penup()
player.goto(0,-250)
player.direction="stop"

#functions
def go_left():
    player.direction="left"
def go_right():
    player.direction="right"

#keyboard
wn.listen()
wn.onkeypress(go_left,"Left")
wn.onkeypress(go_right,"Right")

while True:
    wn.update()
    if player.direction == "left":
        x = player.xcor()
        x -= 3
        player.setx(x)
    if player.direction == "right":
        x = player.xcor()
        x += 3
        player.setx(x)
wn.mainloop()

Traceback (most recent call last):

File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\mine.py", line 34, in player.setx(x) File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 1808, in setx self._goto(Vec2D(x, self._position[1])) File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 3158, in _goto screen._pointlist(self.currentLineItem), File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 755, in _pointlist cl = self.cv.coords(item) File "", line 1, in coords File "C:\Users\Harshitha.P\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2469, in coords self.tk.call((self._w, 'coords') + args))] _tkinter.TclError: invalid command name ".!canvas"

1
I don't believe the traceback above has anything to do with the problem. This code ends with an infinite loop so breaking out of it always generates a trace of some sort, until the infinite loop is replaced with a timer event. - cdlane

1 Answers

0
votes

An alternate interpretation to that of @patel, possibly consistent with the "Falling Skies" video turtorial on YouTube, would be to have the player move by itself until it reached one side of the window, or the other, and then stop:

from turtle import Screen, Turtle

TURTLE_SIZE = 20

# functions
def go_left():
    player.direction = 'left'

def go_right():
    player.direction = 'right'

screen = Screen()
screen.setup(width=800, height=600)
screen.title("Falling Skies")
screen.bgcolor('pink')
screen.tracer(0)

# Add player
player = Turtle()
player.shape('square')
player.speed('fastest')
player.color('blue')
player.penup()
player.sety(-250)
player.direction = 'stop'

# Keyboard
screen.onkeypress(go_left, 'Left')
screen.onkeypress(go_right, 'Right')
screen.listen()

while True:
    x = player.xcor()

    if player.direction == 'left':
        if x > TURTLE_SIZE - 400:
            x -= 3
            player.setx(x)
        else:
            player.direction = 'stop'
    elif player.direction == 'right':
        if x < 400 - TURTLE_SIZE:
            x += 3
            player.setx(x)
        else:
            player.direction = 'stop'

    screen.update()

screen.mainloop()  # never reached