猫史档案馆


【Python作品分享】贪吃蛇

用户:OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO查看:0 回复:0 评论:0 创建时间:2021-09-18T16:58:09


【作品展示】

center_image

 

【作品介绍】

贪吃蛇

 

【作品源代码】

import random
import turtle as t
t.bgcolor('yellow')
catertpillar = t.Turtle()
catertpillar.shape('喵')
catertpillar.color('red')
catertpillar.speed(0)
catertpillar.penup()
catertpillar.hideturtle()
# 画树叶
leaf = t.Turtle()
leaf_ = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
t.register_shape('leaf', leaf_)
leaf.shape("leaf")
leaf.color('green')
leaf.penup()
leaf.hideturtle()
leaf.speed(0)

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

score_trtle = t.Turtle()
score_trtle.hideturtle()
score_trtle.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) = catertpillar.pos()
    outside = \
        x< left_wall or \
        x> right_wall or \
        y< bottom_wall or \
        y> top_wall
    return outside


def game_ovor():
    catertpillar.color('yellow')
    leaf.color('yellow')
    t.penup()
    t.hideturtle()
    t.write('GAME OVER',align='center',font=('Arial',30,'normal'))
    

def display_score(current_score):
    score_trtle.clear()
    score_trtle.penup()
    x = (t.window_width() / 2) - 50
    y = (t.window_height() / 2) - 60
    score_trtle.setpos(x,y)
    score_trtle.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
    teks_turtle.clear()

    catertpillar_speed = 2
    catertpillar_length = 3
    catertpillar.shapesize(1,catertpillar_length,1)
    catertpillar.showturtle()
    display_score(score)
    place_leaf()

    while True:
        catertpillar.forward(catertpillar_speed)
        if catertpillar.distance(leaf) < 20:
            place_leaf()
            catertpillar_length = catertpillar_length + 1
            catertpillar.shapesize(1 ,catertpillar_length,1)
            catertpillar_speed = catertpillar_speed + 1
            score = score + 10
            display_score(score)
        if outside_window():
            game_ovor()
            break

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


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


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


def move_right():
    if catertpillar.heading() == 90 or catertpillar.heading() == 270:
        catertpillar.setheading(0)


t.onkey(start_game, 'space')
t.onkey(move_up, 'w')
t.onkey(move_right, 'd')
t.onkey(move_down, 's')
t.onkey(move_left, 'a')
t.listen()
t.mainloop()

t.done()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页