用户:
leoli查看:0 回复:0 评论:0 创建时间:2023-08-12T11:10:51
【作品展示】

【作品介绍】
wasd上下左右x发射
【作品源代码】
#导入库
import turtle
import random
import time
def up():
player.sety(player.ycor()+10)
if player.ycor() >h/2:
player.goto(-w/2+100, 0)
def donw():
player.sety(player.ycor()-10)
if player.ycor() < -h/2:
player.goto(-w/2+100, 0)
def right():
player.setx(player.xcor()+10)
if player.xcor() > w/2:
player.goto(-w/2+100, 0)
def left():
player.setx(player.xcor()-10)
if player.xcor() < -w/2:
player.goto(-w/2+100, 0)
def shoot():
bullet=turtle.Turtle()
bullet.shape('Bullet.gif')
bullet.goto(player.position())
player.bullets.append(bullet)
#设置场景幕布(宽高,背景色,飞船,帧数)
s=turtle.Screen()
s.title('三体舰队')
w,h=1200,800
s.setup(w,h)
s.bgcolor('black')
s.register_shape('s0.gif')
s.register_shape('s1.gif')
s.register_shape('s2.gif')
s.tracer(0,5)#不显示作图过程,每5毫秒刷新一次
s.register_shape('humanrocket.gif')
s.register_shape('Bullet.gif')
player = turtle.Turtle()
player. shape('humanrocket.gif')
player.penup()
player.goto(-w/2+100, 0)
player.bullets=[]
s.onkey(up,'w')
s.listen()
s.onkey(donw, 's')
s.listen()
s.onkey(left, 'a')
s.listen()
s.onkey(right, 'd')
s.listen()
s.onkey(shoot, 'x')
s.listen()
#创建星空
star=turtle.Turtle()
star.hideturtle()
star.pensize(1)
#star.penup()
star.color('white')
star.speed(0)
for i in range(100):
star.penup()
x=random.randint(-w/2,w/2)
y=random.randint(-h/2,h/2)
star.goto(x,y)
star.pendown()
star.begin_fill()
star.circle(1)
star.end_fill()
#创建母舰
mother_shettles=[
turtle.Turtle(shape='s0.gif',visible=False),
turtle.Turtle(shape='s1.gif',visible=False),
turtle.Turtle(shape='s2.gif',visible=False)]
#克隆舰队
space_shuttles=[]
for i in range(60):
shuttle_num=i%3
space_shuttle=mother_shettles[shuttle_num].clone() #克隆
space_shuttle.speed=random.random()*(shuttle_num+1)
space_shuttles.append(space_shuttle)
#设置舰队的初始状态
for space_shuttle in space_shuttles:
space_shuttle.hideturtle()
space_shuttle.penup()
space_shuttle.goto(w/2+random.randint(0,w/2),random.randint(-h/2,h/2))
space_shuttle.showturtle()
#无线循环,控制舰队移动
start=time.time()
run=True
score=0
pen=turtle.Turtle()
pen.color('white')
pen.penup()
pen.goto(0,h/2-60)
pen.pendown()
while run:
pen.clear()
pen.write('积分:{}'.format(score),align='center',font=('宋体',30))
End=time.time()
timecha=End-start
for bullet in player.bullets:
if bullet.xcor() > w/2:
player.bullets.remove(bullet)
bullet.hideturtle()
continue
else:
bullet.setx(bullet.xcor()+10)
for space_shuttle in space_shuttles:
if space_shuttle.distance(bullet)<20:
score+=1
bullet.hideturtle()
player.bullets.remove(bullet)
space_shuttle.goto(w/2+random.randint(0,w/2),random.randint(-h/2,h/2))
break
for space_shuttle in space_shuttles:
if space_shuttle.distance(player)<20:
pen.clear()
pen.write('击落敌机{}架,感谢您为人类做的贡献'.format(score),align='center',font=('宋体',30))
run=False
space_shuttle.setheading(180)
space_shuttle.setx(space_shuttle.xcor()-space_shuttle.speed*(1+timecha/5))
if space_shuttle.xcor()<-w/2:
space_shuttle.hideturtle()
space_shuttle.penup()
space_shuttle.goto(w/2+random.randint(0,w/2),random.randint(-h/2,h/2))
space_shuttle.showturtle()
s.update()
turtle.done()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn