用户:
星惠awa查看:0 回复:1 评论:0 创建时间:2022-01-09T10:01:30
【作品展示】

【作品介绍】
以后还会持续更新!开源代码!注意侵犯作者利益!
欢迎提建议!收到立即更新!
谢谢大家支持!
【作品源代码】
import random
import turtle
xujiaming = 0
turtle.bgcolor('yellow')
caterpillar = turtle.Turtle()
caterpillar.shape('喵')
caterpillar.color('red')
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()
caterpillar2 = turtle.Turtle()
caterpillar2.shape('喵')
caterpillar2.color('blue')
caterpillar2.speed(0)
caterpillar2.penup()
caterpillar2.hideturtle()
leaf = turtle.Turtle()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
turtle.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color('green')
leaf.speed(0)
leaf.penup()
leaf.hideturtle()
game_started = False
text_turtle = turtle.Turtle()
turtle.hideturtle()
text_turtle.write('Press space to start', align='center',
font=('arial', 50, 'bold'))
text_turtle.hideturtle()
score1_turtle = turtle.Turtle()
score1_turtle.hideturtle()
score1_turtle.speed(0)
score2_turtle = turtle.Turtle()
score2_turtle.hideturtle()
score2_turtle.speed(0)
def outside_window(caterpillar):
left_wall = -turtle.window_width() / 2
right_wall = turtle.window_width() / 2
top_wall = turtle.window_height() / 2
bottom_wall = -turtle.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(score1, score2):
caterpillar.color('yellow')
caterpillar2.color('yellow')
leaf.color('yellow')
turtle.penup()
turtle.hideturtle()
if score1 > score2:
winner = 'player2'
elif score1 < score2:
winner = 'player1'
elif score1 == score2:
turtle.write('GAME OVER!' + 'it ends in a draw!',
align='center', font=('Arial', 50, 'normal'))
turtle.write('GAME OVER!' + winner + 'WIN!',
align='center', font=('Arial', 50, 'normal'))
def display_score1(current_score):
score1_turtle.clear()
score1_turtle.penup()
x = (turtle.window_width() / 2) - 50
y = (turtle.window_height() / 2) - 50
score1_turtle.setpos(x, y)
score1_turtle.write(str(current_score), align='right',
font=('Arial', 40, 'bold'))
def display_score2(current_score):
score2_turtle.clear()
score2_turtle.penup()
x = (-turtle.window_width() / 2) + 50
y = (turtle.window_height() / 2) - 50
score2_turtle.setpos(x, y)
score2_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
text_turtle.clear()
score1 = 0
score2 = 0
caterpillar_speed = 2
caterpillar_length = 3
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar.showturtle()
caterpillar2.shapesize(1, caterpillar_length, 1)
caterpillar2.setheading(180)
caterpillar2.showturtle()
place_leaf()
while True:
caterpillar.forward(caterpillar_speed)
caterpillar2.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
score1 = score1 + 10
display_score1(score1)
if caterpillar2.distance(leaf) < 20:
place_leaf()
caterpillar_length = caterpillar_length + 1
caterpillar2.shapesize(1, caterpillar_length, 1)
caterpillar_speed = caterpillar_speed + 1
score2 = score2 + 10
display_score2(score2)
if outside_window(caterpillar) or outside_window(caterpillar2):
game_over(score1, score2)
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)
def caterpillar2_move_up():
if caterpillar2.heading() == 0 or caterpillar2.heading() == 180:
caterpillar2.setheading(90)
def caterpillar2_move_down():
if caterpillar2.heading() == 0 or caterpillar2.heading() == 180:
caterpillar2.setheading(270)
def caterpillar2_move_left():
if caterpillar2.heading() == 90 or caterpillar2.heading() == 270:
caterpillar2.setheading(180)
def caterpillar2_move_right():
if caterpillar2.heading() == 90 or caterpillar2.heading() == 270:
caterpillar2.setheading(0)
turtle.listen()
turtle.onkey(start_game, 'space')
turtle.onkey(move_up, 'Up')
turtle.onkey(move_right, 'Right')
turtle.onkey(move_down, 'Down')
turtle.onkey(move_left, 'Left')
turtle.listen()
turtle.onkey(caterpillar2_move_up, 'w')
turtle.onkey(caterpillar2_move_right, 'd')
turtle.onkey(caterpillar2_move_down, 's')
turtle.onkey(caterpillar2_move_left, 'a')
turtle.listen()
turtle.mainloop()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn