python海龟模块制作的星空飞碟大战_python精灵模块

下面的Sprite类继承自海龟模块的Turtle类,所以本程序属于海龟画图,以下是代码:

星空飞碟大战_python精灵模块例子

 

"""
   星空飞碟大战.py
   本程序需要sprites模块支持,它是继承自Turtle模块的。
   安装方法: pip install sprites
   由于配音的需要,  本程序需要pygame的混音器支持,安装方法为在命令提示符里输入:pip install pygame --user
   
"""
from sprites import *
import pygame.mixer

def star_move():
  """动态星空背景函数"""
  for star in stars:
    star.move(0,-20)
    if star.ycor() < -height//2:
      star.ht()
      x = random.randint(-width//2,width//2)
      y = random.randint(10+height//2,height*2)
      star.goto(x,y)
      star.st()

def spawn_enemy():
  """不定时产生敌机函数"""
  if random.randint(1,10)==1 and len(screen._turtles)<30:
    x = random.randint(-200,200)
    y = random.randint(100,300)
    enemy = Sprite(shape='res/ufo.png',visible=False,pos=(x,y))
    enemy._rotatemode = 1
    enemy.tag = 'enemy'
    enemy.scale(0.5)
    enemy.setheading(random.randint(1,360))
    enemy.show()

def enemymove():
  """ufo移动"""
  for e in enemys:
    e.fd(3)    
    # 设定一定的机率让ufo朝向player
    if random.randint(10,100) == 10 and \
       abs(e.xcor())<200:
      e.heading(player)     
      
def bulletmove():
  """子弹的移动"""  
  for b in bullets:
     b.move(0,10)
     if b.collide_edge():b.remove()
     
def player_shoot():
  """玩家射击函数"""
  if player.alive == False : return 
  if m1.down and framecounter % 5 == 0 :
     b = Sprite(shape='circle',visible=False)
     b.scale(0.5)
     b.color('yellow')
     b.tag = 'bullet'            # 设定标签(用于分组)
     b.goto(player.pos())        # 移到player坐标
     b.show()                    # 显示子弹
     shoot.play()                # 播放射击声

# 播放背景音乐与生成声效对象
pygame.mixer.init()
pygame.mixer.music.load('audio/FrozenJam.ogg')
pygame.mixer.music.play(-1,0)
explosion = pygame.mixer.Sound('audio/expl3.wav')
shoot = pygame.mixer.Sound('audio/pew.wav')

width,height = 480,640
screen = Screen()               # 新建屏幕
screen.tracer(0,0)              # 关闭自动刷新和设置绘画延时为0毫秒
screen.bgcolor('black')         # 屏幕背景色为黑
screen.setup(width,height)
screen.title("星空飞碟大战by李兴球")

screen.addshape('res/fighter.png')
screen.addshape('res/ufo.png')
frames = ['res/explosion0.png','res/explosion1.png']
[screen.addshape(frame) for frame in frames]

# 星星,用来做向下滚动背景,星星的移动也可以通过移动图章实现
# 这样可以有更多的星星。如果用克隆的话有数量限制,根据计算机配置不同而不同。
star = Turtle(shape='circle')
star.color('white')
star.shapesize(0.1,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)

# 玩家
player = Sprite(shape='res/fighter.png',pos=(0,-200))
player.scale(0.65)
player.alive = True             # 表示player是活的

m1 = Mouse()                    # 鼠标左键检测实例
clock = Clock()                 # 实钟对象,用来控制fps
framecounter = 0
counter = 0                     # 统计击中的ufo数量 
while 1:
  framecounter += 1             # 帧计数器
  spawn_enemy()                 # 不定时产生敌机UFO
  player_shoot()                # 单击鼠标左键,射击子弹
     
  bullets = [b for b in screen.turtles() if b.tag=='bullet'] # 子弹组
  enemys = [e for e in screen.turtles() if e.tag=='enemy']   # 敌机组
  enemymove()
  
  # 如果用了屏幕的tracer(0,0)命令的话,移动之后立即更新重画,
  # 否则bbox没有得到实时的信息返回不正确的结果,导致碰撞检测不正确,从而不会反弹(会粘滞在边缘)
  screen.update()         
  for e in enemys:               # 对每架敌机进行碰撞检测
    if e.collide(player):            
        explode(e.position(),frames)
        e.remove()
        explode(player.pos(),frames)
        player.remove()
        player.alive = False
        explosion.play()         # 爆炸声
        break
    # 敌机是否碰到任意一颗子弹
    for b in bullets:
      if e.collide(b):           # 如果ufo碰到子弹        
        explode(e.position(),frames)
        e.remove()
        b.remove()
        explosion.play()         # 爆炸声
        counter +=1              # 进行统计
        break        
    e.bounce_on_edge()
    
  bulletmove()                   # 子弹的移动
     
  player.goto(mousepos())        # 移到鼠标的坐标
 
  star_move()                    # 滚动背景    
  screen.title('星空飞碟大战,当前击毙:' +  str(counter) + " 架UFO")
  clock.tick(60)

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李兴球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值