Python基础篇:用Python简简单单写个星空大战,可不能用来摸鱼啊~

简介: Python基础篇:用Python简简单单写个星空大战,可不能用来摸鱼啊~

通过辣条最近观察,大家好像对划水摸鱼是情有独钟啊。于是乎我重操旧业又写上了这么一个简单版的星空大战小游戏。

当然了辣条的初衷绝对不是让你们划水摸鱼啊,是真想你们能从中学习那么一点点东西的,总而言之希望大家在Python这条路上少踩坑吧,因为昨天还有人在跟学长说,以后增删改查一样拿高薪,我不信现在都堕落成这样子了

就像问一下懂事理的兄弟姐妹们,你们觉得有可能嘛?

目录
一.游戏画面
二.游戏结束画面
三.游戏素材
四.游戏代码
1.导入模块
2.动态星空背景函数
3.不定时产生敌机函数
4.飞碟的移动
5.子弹的移动
6.玩家射击函数
7.播放背景音乐与生成声效对象
8.新建屏幕
9.移动图章实现星星
10.哭脸
11.玩家
12.飞碟移动与子弹移动
13.敌机的碰撞检测
14.闯关成功把子弹删除
五:总结
一.游戏画面

二.游戏结束画面

三.游戏素材

四.游戏代码
星空飞碟大战.py
由于配音需要混音器,这里用到了pygame的混音器,
1
2
1.导入模块
from sprites import *
import pygame.mixer
1
2
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)

1
2
3
4
5
6
7
8
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()

1
2
3
4
5
6
7
8
9
10
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()

1
2
3
4
5
6
7
8
9
10
5.子弹的移动
def bulletmove():
"""子弹的移动"""
for b in list(bullets):

 b.move(0,10)
 if b.collide_edge():b.remove()

1
2
3
4
5
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()                # 播放射击声

1
2
3
4
5
6
7
8
9
10
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')
1
2
3
4
5
6
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]
1
2
3
4
5
6
7
8
9
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)
1
2
3
4
5
6
7
8
9
10
11
10.哭脸
cry = Sprite(shape='cry.png',visible=False,pos=(0,100))
1
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数量
bullets = Group('bullet') # 子弹组
enemys = Group('enemy') # ufo敌人组
1
2
3
4
5
6
7
8
9
10
12.飞碟移动与子弹移动
while counter<100:
framecounter += 1 # 帧计数器
spawn_enemy() # 不定时产生敌机UFO
player_shoot() # 单击鼠标左键,射击子弹
enemymove() # 飞碟们的移动
bulletmove() # 子弹的移动
if player.alive:

player.goto(mousepos())

else:

cry.show()                  # 显示哭脸,表示失败

star_move() # 星空滚动背景
1
2
3
4
5
6
7
8
9
10
11
13.敌机的碰撞检测
for e in list(enemys): # 对每架敌机进行碰撞检测
if e.collide(player,scale=0.6):

  explode(e.position(),frames)
  e.remove()
  explode(player.pos(),frames)
  player.remove()
  player.alive = False
  explosion.play()         # 爆炸声
  break

# 敌机是否碰到任意一颗子弹
for b in list(bullets):

if b.collide(e,scale=0.6): # 如果子弹碰到UFO        
  explode(e.position(),frames)
  e.remove()
  b.remove()
  explosion.play()         # 爆炸声
  counter +=1              # 进行统计         
  break            

screen.title('星空飞碟大战,当前击毙:' + str(counter) + " 架UFO")
clock.tick(60)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
14.闯关成功把子弹删除
[b.remove() for b in list(bullets)] # 闯关成功把子弹删除
for e in list(enemys): # 每架飞碟都爆炸
explode(e.position(),frames)
e.remove()
clock.tick(10)

t = Turtle(visible=False)
t.color('yellow')
t.write('成功闯关!',align='center',font=('黑体',32,'normal'))

while True:
player.goto(mousepos())
star_move() # 星空滚动背景
clock.tick(60)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
t = Turtle(visible=False)
t.color(‘yellow’)
t.write(‘成功闯关!’,align=‘center’,font=(‘黑体’,32,‘normal’))

while True:
player.goto(mousepos())
star_move() # 星空滚动背景
clock.tick(60)

五:总结
看到这里的都是妥妥的铁粉无疑了,既然是铁粉那就给你们一次亲密接触机会,底下是我的个人ming片找到我的可是有大把源码,学习路线啥的,多的我就不透露,看大家自己的积极性了啊~
其次呢再次重申辣条我真的不是想让你们摸鱼的,虽然说是个很简单的东西,但是对于初学者,或者基础不扎实的弟弟妹妹们而言估计也很难实现出来的,所以最后大家加油吧~

目录
相关文章
|
6月前
|
Python
Python 面向对象--人狗大战(教程)
Python 面向对象--人狗大战(教程)
112 0
|
6月前
|
Python
Python 面向对象--人狗大战(代码)
Python 面向对象--人狗大战(代码)
147 0
|
9月前
|
Python
【Python】五子棋 —— 摸鱼必备的小项目~
摸鱼必备小项目,你值得拥有~
126 1
【Python】五子棋 —— 摸鱼必备的小项目~
|
10月前
|
Python
Python写个“点球大战”小游戏
玩家的选择通过input输入获取,电脑的选择我们可以用random随机数模块来产生
|
10月前
|
Python
python小游戏——乒乓球大战2.0版本の双人联机对战功能的实现
python小游戏——乒乓球大战2.0版本の双人联机对战功能的实现
198 0
|
10月前
|
Python
python小游戏——乒乓球大战
python小游戏——乒乓球大战
78 0
|
11月前
|
存储 Python
摸鱼宝典(一)——贪吃蛇游戏改版:贪吃龙 · 双龙戏珠小游戏(Python)
摸鱼宝典(一)——贪吃蛇游戏改版:贪吃龙 · 双龙戏珠小游戏(Python)
115 0
|
传感器 物联网 项目管理
【毕设参考】“摸鱼”神器,用 ESP32 + HaaS Python DIY一款全功能鱼缸 控温、过滤、换水一键执行
【毕设参考】“摸鱼”神器,用 ESP32 + HaaS Python DIY一款全功能鱼缸 控温、过滤、换水一键执行
181 0
|
前端开发 测试技术
软件测试|selenium+python基础方法封装(二)
软件测试|selenium+python基础方法封装(二)
156 0
软件测试|selenium+python基础方法封装(二)
|
Web App开发 Java 测试技术
软件测试|selenium+python基础方法封装(一)
软件测试|selenium+python基础方法封装(一)
186 0
软件测试|selenium+python基础方法封装(一)