import turtle as t
import random as rd
t.bgcolor('lightgreen')
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()
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) = 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')
leaf.color('yellow')
t.penup()
t.hideturtle()
t.write('GAME OVER!',align='center' , font=('Aerial',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.hideturtle()
leaf.setx(rd.randint(-200,200))
leaf.sety(rd.randint(-200,200))
leaf.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(leaf)<20:
place_leaf()
caterpillar_length = caterpillar_length + 1
caterpillar.shapesize(1,caterpillar_length,1)
caterpillar_speed = caterpillar_speed + 1
score = 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)
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()
# Coded with ���� by Mr. Unity Buddy
Hungry Caterpillar Game in Python.zip饥饿的毛毛虫游戏Python源码
需积分: 0 5 浏览量
更新于2023-03-17
收藏 1KB ZIP 举报
饥饿的毛毛虫游戏在Python免费源代码
介绍
这 饥饿的毛毛虫游戏在 程序设计语言。该项目包含演示实际游戏玩法的基本功能。此应用程序包含各种形状和彩色背景。该项目将使正在学习IT相关课程的学生受益。这 饥饿的毛毛虫游戏 提供初学者可以轻松理解的简单代码。这 饥饿的毛毛虫游戏在蟒蛇 提供有关如何编程的基本示例 python 编程。
这 饥饿的毛毛虫游戏在Python免费源代码 可以免费下载,只需阅读下面的内容以获取更多信息。此应用程序适用于 仅教育目的.
饥饿的毛毛虫游戏在Python免费源代码 基本信息
使用的语言: pytho
使用的编码工具: 内置 Python IDLE
类型: 桌面应用程序
使用的数据库: 没有
关于饥饿的毛毛虫游戏
这 饥饿的毛毛虫游戏 仅使用构建 p 程序设计语言。此应用程序是一种用户友好的系统,可以轻松满足您的需求。该应用程序提供了一个可供玩家玩的简单功能。玩家可以使用键盘绑定(左箭头键向左移动,右箭头键向右移动,向上箭头键向上移动,向下箭头键向下移动)玩游戏。游戏玩法非常简单,玩家必须用绿叶喂饥饿的毛毛虫。绿色在不同的位置生成,你必须让它获得分数。

xiaoshun007~
- 粉丝: 4122
- 资源: 3116