利用Python写个简易版星空大战游戏

作者:五包辣条! 时间:2023-08-26 14:07:42 

前言

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

利用Python写个简易版星空大战游戏

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

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

一.游戏画面

利用Python写个简易版星空大战游戏

二.游戏结束画面

利用Python写个简易版星空大战游戏

三.游戏素材

利用Python写个简易版星空大战游戏

利用Python写个简易版星空大战游戏

四.游戏代码

星空飞碟大战.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数量
bullets = Group('bullet')       # * 组
enemys = Group('enemy')         # ufo敌人组

12.飞碟移动与 * 移动

while counter<100:
 framecounter += 1             # 帧计数器
 spawn_enemy()                 # 不定时产生敌机UFO
 player_shoot()                # 单击鼠标左键,射击 *
 enemymove()                   # 飞碟们的移动
 bulletmove()                  # * 的移动
 if player.alive:
   player.goto(mousepos())
 else:
   cry.show()                  # 显示哭脸,表示失败
 star_move()                   # 星空滚动背景    

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)

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)

六、总结

看到这里的都是妥妥的铁粉无疑了,既然是铁粉那就给你们一次亲密接触机会,底下是我的个人ming片找到我的可是有大把源码,学习路线啥的,多的我就不透露,看大家自己的积极性了啊~

其次呢再次重申辣条我真的不是想让你们摸鱼的,虽然说是个很简单的东西,但是对于初学者,或者基础不扎实的弟弟妹妹们而言估计也很难实现出来的,所以最后大家加油吧~

来源:https://blog.csdn.net/AI19970205/article/details/123821548

标签:Python,星空大战,游戏
0
投稿

猜你喜欢

  • PHP垃圾回收机制超详细介绍

    2023-11-21 23:11:44
  • CentOS 7 安装python3.7.1的方法及注意事项

    2023-03-10 21:23:53
  • 在SQL Server2000中恢复Master数据库

    2008-01-05 14:05:00
  • Tensorflow 使用pb文件保存(恢复)模型计算图和参数实例详解

    2023-12-13 05:42:06
  • Django中更新多个对象数据与删除对象的方法

    2021-08-13 13:31:46
  • python tornado微信开发入门代码

    2023-11-01 01:04:59
  • 解决python3 整数数组转bytes的效率问题

    2023-08-09 19:39:41
  • python中opencv实现图片文本倾斜校正

    2023-08-27 11:07:03
  • Python Flask框架模板操作实例分析

    2022-09-21 02:56:23
  • 基于DATAFRAME中元素的读取与修改方法

    2021-11-18 03:59:09
  • python利用插值法对折线进行平滑曲线处理

    2023-10-15 14:48:08
  • python opencv实现图像配准与比较

    2023-03-01 15:30:24
  • Python做图像处理及视频音频文件分离和合成功能

    2022-07-31 14:58:05
  • Python urlopen()参数代码示例解析

    2021-05-22 18:26:14
  • PHP基础知识详细讲解

    2023-06-03 15:41:45
  • python多线程并发实例及其优化

    2021-04-03 12:28:11
  • python 获取剪切板内容的两种方法

    2021-09-26 03:22:34
  • 利用Opencv实现图片的油画特效实例

    2022-01-26 14:31:59
  • Python计算三角函数之asin()方法的使用

    2023-08-04 22:31:46
  • PHP+MYSQL不恶补十句话

    2009-12-02 10:09:00
  • asp之家 网络编程 m.aspxhome.com