Python Forum
t.onkey command isn't working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
t.onkey command isn't working
#1
i just need a bit of help because i just made a game called 'caterpillar' but when i tried running it it wouldnt work when i clicked space.

import random
import turtle as t

t.bgcolor('yellow')

caterpillar = t.Turtle()
caterpillar.shape('square')
caterpillar.color('red')
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()

leaf = t.Turtle()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
t.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color('green')
leaf.penup()
leaf.hideturtle()
leaf.speed(0)

game_started = False
text_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center', font=('Arial', 16, 'bold'))
text_turtle.hideturtle()

score_turtle = t.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)

def outside_window():
    left_wall = -t.window_width() / 2
    right_wall = t.window_width() / 2
    top_wall = t.window_width() / 2
    bottom_wall = -t.window_width() / 2
    (x, y) = caterpiller.pos()
    outside = \
            x< left_wall or \
            x> right_wall or \
            y< bottom_wall or \
            y> top_wall
    return outside

def game_over():
    caterpillar.colour('yellow')
    leaf.colour('yellow')
    t.penup()
    t.hideturtle()
    t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))

def display_score(current_score):
    score_turtle.clear()
    score_turtle.penup()
    x = (t.window_width() / 2) - 50
    y = (t.window_height() / 2) -50
    score_turtle.setpos(x, y)
    score_turtle.write(str(current_score), align='right', font=('Arail', 40, 'bold'))

def place_leaf():
    leaf.ht()
    leaf.setx(random.randint(-200, 200))
    leaf.sety(random.randint(-200, 200))
    leaf.st()

def start_game():
    global game_started
    if game_started:
        return
    game_started = True

    score = 0
    text_turtle.clear()

    caterpillar_speed = 2
    caterpillar_length = 3
    caterpillar.shapesize(1, caterpillar_length, 1)
    caterpillar.showturtle()
    display_score(score)
    place_leaf()

    while True:
        caterpillar.forward(caterpillar_speed)
        if caterpillar.distance(leaf) < 20:
            place_leaf()
            caterpillar_length = caterpillar_length + 1
            caterpillar.shapesize(1, caterpillar_length, 1)
            caterpillar_speed = caterpillar_speed + 1
            score = score + 10
        if outside_window():
            game_over()
            display_score(score)
            break

    def move_up():
        if caterpillar.heading() == 0 or caterpillar.heading() == 180:
            caterpillar.setheading(90)
    def move_down():
        if caterpillar.heading() == 0 or caterpillar.heading() == 180:
            caterpillar.setheading(270)
    def move_left():
        if caterpillar.heading() == 90 or caterpillar.heading() == 270:
            caterpillar.setheading(180)
    def move_right():
        if caterpillar.heading() == 90 or caterpillar.heading() == 270:
            caterpillar.setheading(0)

    t.onkey(start_game, 'Space')
    t.onkey(move_up, 'Up')
    t.onkey(move_right, 'Right')
    t.onkey(move_down, 'Down')
    t.onkey(move_left, 'left')
    t.listen()
    t.mainloop()
    
Reply
#2
Quote:it wouldnt work when i clicked space.
Please be more specific, and post all error messages in their entirety, unaltered.
Reply
#3
i didn't get any error messages when i clicked run
all i got was a screen saying "Click SPACE to begin.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle onkey() simonc8 12 7,285 Jun-29-2021, 09:09 AM
Last Post: simonc8
  Subprocess command working for one cmd and for cmd one not wrking PythonBeginner_2020 0 4,104 Mar-25-2020, 01:52 PM
Last Post: PythonBeginner_2020
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 6,128 Jul-19-2019, 04:53 PM
Last Post: ichabod801
  Linux command output not working adam2020 2 2,654 Mar-23-2019, 01:20 PM
Last Post: adam2020
  Why is subprocess.call command not working? zBernie 5 10,649 Nov-19-2018, 11:11 PM
Last Post: snippsat
  Command in Bash not working in python pynoob 5 4,648 Jan-05-2018, 06:28 PM
Last Post: pynoob

Forum Jump:

User Panel Messages

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