Python基础篇:用Python简简单单写个星空大战,可不能用来摸鱼啊~_简单的python编程作品,太空大战

	- [14.闯关成功把子弹删除](#14_215)
+ [五:总结](#_244)

一.游戏画面

在这里插入图片描述

二.游戏结束画面

在这里插入图片描述

三.游戏素材

在这里插入图片描述

在这里插入图片描述

四.游戏代码

星空飞碟大战.py
由于配音需要混音器,这里用到了pygame的混音器,

1.导入模块
from sprites import \*
import pygame.mixer

2.动态星空背景函数
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()                # 播放射击声

7.播放背景音乐与生成声效对象
# 播放背景音乐与生成声效对象
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')

8.新建屏幕
width,height = 480,640
screen = Screen()               # 新建屏幕
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]

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

10.哭脸
cry = Sprite(shape='cry.png',visible=False,pos=(0,100))

11.玩家
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数量
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!



### 一、Python所有方向的学习路线



Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。



![](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)



### 二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。



![](https://img-blog.csdnimg.cn/img_convert/8c4513c1a906b72cbf93031e6781512b.png)



### 三、全套PDF电子书



书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

![](https://img-blog.csdnimg.cn/img_convert/46506ae54be168b93cf63939786134ca.png)



### 四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。



![](https://img-blog.csdnimg.cn/afc935d834c5452090670f48eda180e0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA56iL5bqP5aqb56eD56eD,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)



### 五、实战案例



光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。



![](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)



### 六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。



![](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)  

![](https://img-blog.csdnimg.cn/img_convert/d2d978bb523c810abca3abe69e09bc1a.png)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值