def star\_move():
"""动态星空背景函数"""
for star in stars:
star.move(0,-20)
if star.ycor() < -height//2:
x = random.randint(-width//2,width//2)
y = random.randint(10+height//2,height\*2)
star.reborn(x,y,0,-20)
3.不定时产生敌机函数
def spawn\_enemy():
"""不定时产生敌机函数"""
if random.randint(1,10)==1 and len(enemys)<10:
x = random.randint(-200,200)
y = random.randint(100,300)
enemy = Sprite(shape='res/ufo.png',visible=False,pos=(x,y),tag='enemy')
enemy._rotatemode = 1
enemy.scale(0.5)
enemy.setheading(random.randint(1,360))
enemy.show()
4.飞碟的移动
def enemymove():
"""飞碟的移动"""
for e in enemys:
e.fd(3)
# 设定一定的机率让ufo朝向player
if random.randint(10,100) == 10 and \
abs(e.xcor())<200 and abs(e.ycor()<250):
e.heading(player)
e.bounce_on_edge()
5.子弹的移动
def bulletmove():
"""子弹的移动"""
for b in list(bullets):
b.move(0,10)
if b.collide_edge():b.remove()
6.玩家射击函数
def player\_shoot():
"""玩家射击函数"""
if player.alive == False : return
if m1.down() and framecounter % 5 == 0 :
b = Sprite(shape='circle',visible=False,tag='bullet')
b.scale(0.5)
b.color('yellow')
b.goto(player.pos()) # 移到player坐标
b.show() # 显示子弹
shoot.play() # 播放射击声
# 星星,用来做向下滚动背景,星星的移动也可以通过移动图章实现
# 这样可以有更多的星星。如果用克隆的话有数量限制,根据计算机配置不同而不同。
star = Sprite(shape='circle')
star.color('white')
star.scale(0.1)
stars = [star]
stars.extend([star.clone() for _ in range(20)])
for star in stars:
x = random.randint(-width//2,width//2)
y = random.randint(10+height//2,height\*2)
star.goto(x,y)