大蟒蛇python教程共享利用Python写个简易版星空大战游戏

前言

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

利用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片找到我的可是有大把源码,学习路线啥的,多的我就不透露,看大家自己的积极性了啊~

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

以上就是利用python写个简易版星空大战游戏的详细内容,更多关于python星空大战的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!

需要了解更多python教程分享利用Python写个简易版星空大战游戏,都可以关注python教程分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/pythontutorial/1069679.html

(0)
上一篇 2022年3月30日
下一篇 2022年3月30日

精彩推荐