[Tutor] images

Beck, Caroline BeckC21 at duchesne.org
Wed Jul 25 13:36:53 EDT 2018


Hi! I am a student working on a game in which I am trying to use a gif as my turtle. Currently, the turtle moves around the screen with the arrow keys, but the gif does not move with it. Below is how I have tried to tell the code that the gif is the turtle, but the code is not reading that the gif is my turtle. I was wondering if I could have some help with making the gif in my code to read as my turtle.
Thank you!

Here is my code so far:
import random
import turtle as t

screen = t.Screen()
screen.setup(1250,900)
screen.bgpic("newbackground.gif")

screen.addshape("shark.gif")
t.shape("shark.gif")
shark = t.Turtle()
shark.speed(0)
shark.penup()
shark.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('orange')
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_height() / 2
    bottom_wall = -t.window_height() / 2
    (x,y) = t.pos()
    outside = \
            x< left_wall or \
            x> right_wall or \
            y< bottom_wall or \
            y> top_wall
    return outside

def game_over():
    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=('Arial', 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

    shark_speed = 2
    shark_length = 3
    shark.shapesize(1, shark_length,1)
    shark.showturtle()
    display_score(score)
    place_leaf()

    while True:
        shark.forward(shark_speed)
        if shark.distance(leaf) < 20:
            place_leaf()
            #shark_length=t_length + 1
            #shark.shapesize(1, t_length, 1)
            shark_speed=shark_speed + 1
            score = score + 10
            display_score(score)
        if outside_window():
            game_over()
            break

def move_up():
    if shark.heading() == 0 or shark.heading() == 180:
        shark.setheading(90)

def move_down():
    if shark.heading() == 0 or shark.heading() == 180:
        shark.setheading(270)

def move_left():
    if shark.heading() == 90 or shark.heading() == 270:
        shark.setheading(180)

def move_right():
    if shark.heading() == 90 or shark.heading() == 270:
        shark.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()

From: Crystal Frommert <cfrommert at gmail.com>
Sent: Tuesday, July 24, 2018 1:11 PM
To: Beck, Caroline <BeckC21 at duchesne.org>
Subject: images

Try a gif image instead of png


More information about the Tutor mailing list