如何添加对象以杀死游戏中的毛毛虫

时间:2019-05-27 02:16:40

标签: python

我需要在代码中添加一个杀死毛毛虫的形状(袋子),请告诉我我在这方面没有经验。我还需要形状的坐标,例如leaf_shape =((0,0),(14,2),(18,6),(20,20),(6,18),(2,14))。请尽快提出建议,以便我完成工作。我确实从堆栈溢出中得到了这段代码,它运行良好,但是如果您有任何建议,请提出想法

import random
from turtle import Turtle, Screen

screen = Screen()
screen.bgcolor('green')

caterpillar = Turtle('triangle', visible=False)
caterpillar.color('blue')
caterpillar.speed('fast')
caterpillar.penup()

bag_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
screen.register_shape('bag', bag_shape)

bag= Turtle('bag', visible=False)
bag.color('orange')
bag.speed('fast')
bag.penup()

game_started = False
text_turtle = Turtle(visible=False)
text_turtle.write('you need help', align='center', font('Arial',                16, 'bold'))

score_turtle = Turtle(visible=False)
score_turtle.speed('fast')
score_turtle.penup()
x = (screen.window_width() / 2) - 50
y = (screen.window_height() / 2) - 50
score_turtle.setpos(x, y)
score_turtle.write(0, align='right', font=('Arial', 40, 'bold'))

def outside_window():
    left_wall = -screen.window_width() / 2
    right_wall = screen.window_width() / 2
    top_wall = screen.window_height() / 2
    bottom_wall = -screen.window_height() / 2

    x, y = caterpillar.pos()

    outside = \
        x < left_wall or \
        x > right_wall or \
        y < bottom_wall or \
        y > top_wall

    return outside

def game_over():
    caterpillar.color('yellow')
    bag.color('yellow')
    text_turtle.write('you bad at dis!', align='center', font=('Arial',      30, 'normal'))

def display_score(current_score):
    score_turtle.undo()
    score_turtle.write(current_score, align='right', font=('Arial', 40, 'bold'))

def place_leaf():
    bag.hideturtle()
    bag.setx(random.randint(-200, 200))
    bag.sety(random.randint(-200, 200))
    bag.showturtle()

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(bag) < 20:
            place_leaf()
            caterpillar_length += 1
            caterpillar.shapesize(1, caterpillar_length, 1)
            caterpillar_speed += 0
            score += 10
            display_score(score)

        if outside_window():
            game_over()
            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)

screen.onkey(start_game, 'space')
screen.onkey(move_up, 'Up')
screen.onkey(move_right, 'Right')
screen.onkey(move_down, 'Down')
screen.onkey(move_left, 'Left')

screen.listen()
screen.mainloop()

0 个答案:

没有答案