python趣味编程_python游戏趣味编程:飞机大战

0

效果预览

ec6247f36283978065f8ac14d55b85e6.gif

1

显示飞机与背景

用到的资源:字体、图片、声音。

ca10507a3521fbd5e9836bbb9088bafd.png

4eb5b569a4009b05608dfd27fe72142f.png

import pgzrun  # 导入游戏库WIDTH = 480    # 设置窗口的宽度HEIGHT = 852   # 设置窗口的高度background = Actor('background')  # 导入背景图片hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标def draw():    background.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机pgzrun.go()  # 开始执行游戏

a7154ad8da9839282317d10683c598c0.png

飞机随鼠标的移动而移动:

import pgzrun  # 导入游戏库WIDTH = 480    # 设置窗口的宽度HEIGHT = 852   # 设置窗口的高度background = Actor('background')  # 导入背景图片hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标def draw():    background.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标pgzrun.go()  # 开始执行游戏
2

背景循环滚动

import pgzrun  # 导入游戏库WIDTH = 480    # 设置窗口的宽度HEIGHT = 700   # 设置窗口的高度background1 = Actor('background')  # 导入背景图片background1.x = 480/2  # 背景1的x坐标background1.y = 852/2  # 背景1的y坐标background2 = Actor('background')  # 导入背景图片background2.x = 480/2   # 背景2的x坐标background2.y = -852/2  # 背景2的y坐标hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标def draw():  # 绘制模块,每帧重复执行    background1.draw()  # 绘制游戏背景    background2.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机def update():  # 更新模块,每帧重复操作    # 以下代码用于实现背景图片的循环滚动效果    if background1.y > 852/2 + 852:        background1.y = -852/2  # 背景1移动到背景2的正上方    if background2.y > 852/2 + 852:        background2.y = -852/2  # 背景2移动到背景1的正上方    background1.y += 1  # 背景1向下滚动    background2.y += 1  # 背景2向下滚动def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标pgzrun.go()  # 开始执行游戏

12e18a3d1319d9821176570d2e7c7f7d.gif

3

发射子弹

import pgzrun  # 导入游戏库WIDTH = 480    # 设置窗口的宽度HEIGHT = 700   # 设置窗口的高度background1 = Actor('background')  # 导入背景1图片background1.x = 480/2  # 背景1的x坐标background1.y = 852/2  # 背景1的y坐标background2 = Actor('background')  # 导入背景2图片background2.x = 480/2   # 背景2的x坐标background2.y = -852/2  # 背景2的y坐标bullet = Actor('bullet')  # 导入子弹图片bullet.x = WIDTH/2        # 子弹的x坐标bullet.y = -HEIGHT       # 子弹的y坐标,开始不可见hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标def draw():  # 绘制模块,每帧重复执行    background1.draw()  # 绘制游戏背景    background2.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机    bullet.draw() # 绘制子弹def update():  # 更新模块,每帧重复操作    # 以下代码用于实现背景图片的循环滚动效果    if background1.y > 852/2 + 852:        background1.y = -852/2  # 背景1移动到背景2的正上方    if background2.y > 852/2 + 852:        background2.y = -852/2  # 背景2移动到背景1的正上方    background1.y += 1  # 背景1向下滚动    background2.y += 1  # 背景2向下滚动    if bullet.y > -HEIGHT:        bullet.y = bullet.y - 10 # 子弹自动向上移动def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标def on_mouse_down(): # 当鼠标键按下时    bullet.x = hero.x   # 把子弹位置设为玩家飞机的正上方    bullet.y = hero.y - 70pgzrun.go()  # 开始执行游戏

第12~13行:子弹开始不可见

第42~43行,按动鼠标,子弹出现在飞机上方

第35行:子弹向上移动,直到消失

b81c3905db2ea3fb6c5a5628e37632db.gif

4

敌机的显示和下落

import pgzrun  # 导入游戏库import random  # 导入随机库WIDTH = 480    # 设置窗口的宽度HEIGHT = 700   # 设置窗口的高度background1 = Actor('background')  # 导入背景1图片background1.x = 480/2  # 背景1的x坐标background1.y = 852/2  # 背景1的y坐标background2 = Actor('background')  # 导入背景2图片background2.x = 480/2   # 背景2的x坐标background2.y = -852/2  # 背景2的y坐标bullet = Actor('bullet')  # 导入子弹图片bullet.x = WIDTH/2        # 子弹的x坐标bullet.y = -HEIGHT       # 子弹的y坐标,开始不可见hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标enemy = Actor('enemy')  # 导入敌机图片enemy.x = WIDTH/2       # 设置敌机的x坐标enemy.y = 0             # 设置敌机的y坐标def draw():  # 绘制模块,每帧重复执行    background1.draw()  # 绘制游戏背景    background2.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机    enemy.draw()  # 绘制敌机飞机    bullet.draw()  # 绘制子弹def update():  # 更新模块,每帧重复操作    # 以下代码用于实现背景图片的循环滚动效果    if background1.y > 852/2 + 852:        background1.y = -852/2  # 背景1移动到背景2的正上方    if background2.y > 852/2 + 852:        background2.y = -852/2  # 背景2移动到背景1的正上方    background1.y += 1  # 背景1向下滚动    background2.y += 1  # 背景2向下滚动    if bullet.y > -HEIGHT:        bullet.y = bullet.y - 10 # 子弹自动向上移动    enemy.y += 3 # 敌机自动下落    if enemy.y > HEIGHT: # 敌机落到画面底部        enemy.y = 0 # 敌机从上面重新出现        enemy.x = random.randint(30, WIDTH-30)  # 敌机水平位置随机def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标def on_mouse_down(): # 当鼠标键按下时    bullet.x = hero.x   # 把子弹位置设为玩家飞机的正上方    bullet.y = hero.y - 70pgzrun.go()  # 开始执行游戏

第14~16行:敌机首次出现在屏幕上方中心位置

第46~48行,敌机向下移动,移出屏幕位置时,再次将其位置重新设置,横坐标随机

4256e3a5b0e23d263c763ccd8b635206.gif

4

击中敌机和得分显示

import pgzrun  # 导入游戏库import random  # 导入随机库WIDTH = 480    # 设置窗口的宽度HEIGHT = 700   # 设置窗口的高度TITLE = 'Python飞机大战'background1 = Actor('background')  # 导入背景1图片background1.x = 480/2  # 背景1的x坐标background1.y = 852/2  # 背景1的y坐标background2 = Actor('background')  # 导入背景2图片background2.x = 480/2   # 背景2的x坐标background2.y = -852/2  # 背景2的y坐标bullet = Actor('bullet')  # 导入子弹图片bullet.x = WIDTH/2        # 子弹的x坐标bullet.y = -HEIGHT       # 子弹的y坐标,开始不可见hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标enemy = Actor('enemy')  # 导入敌机图片enemy.x = WIDTH/2       # 设置敌机的x坐标enemy.y = 0             # 设置敌机的y坐标score = 0     # 游戏得分def draw():  # 绘制模块,每帧重复执行    background1.draw()  # 绘制游戏背景    background2.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机    enemy.draw()  # 绘制敌机飞机    bullet.draw()  # 绘制子弹    # 下面显示得分    screen.draw.text("得分: "+str(score), (200, HEIGHT-50), fontsize=30,                     fontname='s', color='black')def update():  # 更新模块,每帧重复操作    global score    # 以下代码用于实现背景图片的循环滚动效果    if background1.y > 852/2 + 852:        background1.y = -852/2  # 背景1移动到背景2的正上方    if background2.y > 852/2 + 852:        background2.y = -852/2  # 背景2移动到背景1的正上方    background1.y += 1  # 背景1向下滚动    background2.y += 1  # 背景2向下滚动    if bullet.y > -HEIGHT:        bullet.y = bullet.y - 10 # 子弹自动向上移动    enemy.y += 3 # 敌机自动下落    if enemy.y > HEIGHT: # 敌机落到画面底部        enemy.y = 0 # 敌机从上面重新出现        enemy.x = random.randint(50, WIDTH-50)  # 敌机水平位置随机    if bullet.colliderect(enemy): # 子弹与敌机发生碰撞后        enemy.y = 0 # 敌机从上面重新出现        enemy.x = random.randint(0, WIDTH)  # 敌机水平位置随机        score = score + 1 # 得分加1        bullet.y = -HEIGHT  # 隐藏子弹def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标def on_mouse_down(): # 当鼠标键按下时    bullet.x = hero.x   # 把子弹位置设为玩家飞机的正上方    bullet.y = hero.y - 70pgzrun.go()  # 开始执行游戏

第36~37行:用汉字显示分数

第56~57行:子弹和敌机相撞,得分加1

d07e3da9df5c3568653434f251fa0ce3.gif

4

多根针的发射与转动

第14行:针的旋转有个前提,就是按键按下后将needle.x = 400才开始。

562ff324837a98348080ce687c424aa1.gif

6

游戏失败的判定

import pgzrun  # 导入游戏库import random  # 导入随机库WIDTH = 480    # 设置窗口的宽度HEIGHT = 700   # 设置窗口的高度TITLE = 'Python飞机大战'background1 = Actor('background')  # 导入背景1图片background1.x = 480/2  # 背景1的x坐标background1.y = 852/2  # 背景1的y坐标background2 = Actor('background')  # 导入背景2图片background2.x = 480/2   # 背景2的x坐标background2.y = -852/2  # 背景2的y坐标bullet = Actor('bullet')  # 导入子弹图片bullet.x = WIDTH/2        # 子弹的x坐标bullet.y = -HEIGHT       # 子弹的y坐标,开始不可见hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标enemy = Actor('enemy')  # 导入敌机图片enemy.x = WIDTH/2       # 设置敌机的x坐标enemy.y = 0             # 设置敌机的y坐标score = 0     # 游戏得分isLoose = False # 游戏是否失败,初始不失败def draw():  # 绘制模块,每帧重复执行    background1.draw()  # 绘制游戏背景    background2.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机    enemy.draw()  # 绘制敌机飞机    bullet.draw()  # 绘制子弹    # 下面显示得分    screen.draw.text("得分: "+str(score), (200, HEIGHT-50), fontsize=30,                     fontname='s', color='black')    if isLoose: # 游戏失败后输出信息        screen.draw.text("游戏失败!", (50, HEIGHT/2), fontsize=90,fontname='s', color='red')def update():  # 更新模块,每帧重复操作    global score, isLoose    if isLoose:        return # 如果游戏失败,返回,不做下面的操作    # 以下代码用于实现背景图片的循环滚动效果    if background1.y > 852/2 + 852:        background1.y = -852/2  # 背景1移动到背景2的正上方    if background2.y > 852/2 + 852:        background2.y = -852/2  # 背景2移动到背景1的正上方    background1.y += 1  # 背景1向下滚动    background2.y += 1  # 背景2向下滚动    if bullet.y > -HEIGHT:        bullet.y = bullet.y - 10 # 子弹自动向上移动    enemy.y += 3 # 敌机自动下落    if enemy.y > HEIGHT: # 敌机落到画面底部        enemy.y = 0 # 敌机从上面重新出现        enemy.x = random.randint(50, WIDTH-50)  # 敌机水平位置随机    if bullet.colliderect(enemy): # 子弹与敌机发生碰撞后        enemy.y = 0 # 敌机从上面重新出现        enemy.x = random.randint(0, WIDTH)  # 敌机水平位置随机        score = score + 1 # 得分加1        bullet.y = -HEIGHT  # 隐藏子弹    if hero.colliderect(enemy): # 玩家飞机和敌机发生碰撞        isLoose = True  # 游戏失败        hero.image = 'hero_blowup' # 更换游戏玩家的图片为爆炸图片def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    if isLoose:        return  # 如果游戏失败,返回,不做下面的操作    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标def on_mouse_down(): # 当鼠标键按下时    if isLoose:        return  # 如果游戏失败,返回,不做下面的操作    bullet.x = hero.x   # 把子弹位置设为玩家飞机的正上方    bullet.y = hero.y - 70pgzrun.go()  # 开始执行游戏
增加变量isLoose记录游戏的状态,当敌机碰到飞机时,游戏失败。

e7f083e657c9ac9c32563ec5c1724f76.gif

7

增加音效

import pgzrun  # 导入游戏库import random  # 导入随机库WIDTH = 480    # 设置窗口的宽度HEIGHT = 700   # 设置窗口的高度TITLE = 'Python飞机大战'background1 = Actor('background')  # 导入背景1图片background1.x = 480/2  # 背景1的x坐标background1.y = 852/2  # 背景1的y坐标background2 = Actor('background')  # 导入背景2图片background2.x = 480/2   # 背景2的x坐标background2.y = -852/2  # 背景2的y坐标bullet = Actor('bullet')  # 导入子弹图片bullet.x = WIDTH/2        # 子弹的x坐标bullet.y = -HEIGHT       # 子弹的y坐标,开始不可见hero = Actor('hero')  # 导入玩家飞机图片hero.x = WIDTH/2      # 设置玩家飞机的x坐标hero.y = HEIGHT*2/3   # 设置玩家飞机的y坐标enemy = Actor('enemy')  # 导入敌机图片enemy.x = WIDTH/2       # 设置敌机的x坐标enemy.y = 0             # 设置敌机的y坐标score = 0     # 游戏得分isLoose = False # 游戏是否失败,初始不失败sounds. game_music.play(-1)  # 循环播放背景音乐def draw():  # 绘制模块,每帧重复执行    background1.draw()  # 绘制游戏背景    background2.draw()  # 绘制游戏背景    hero.draw()  # 绘制玩家飞机    enemy.draw()  # 绘制敌机飞机    bullet.draw()  # 绘制子弹    # 下面显示得分    screen.draw.text("得分: "+str(score), (200, HEIGHT-50), fontsize=30,                     fontname='s', color='black')    if isLoose:  # 游戏失败后输出信息        screen.draw.text("游戏失败!", (50, HEIGHT/2), fontsize=90,fontname='s', color='red')def update():  # 更新模块,每帧重复操作    global score, isLoose    if isLoose:        return # 如果游戏失败,返回,不做下面的操作    # 以下代码用于实现背景图片的循环滚动效果    if background1.y > 852/2 + 852:        background1.y = -852/2  # 背景1移动到背景2的正上方    if background2.y > 852/2 + 852:        background2.y = -852/2  # 背景2移动到背景1的正上方    background1.y += 1  # 背景1向下滚动    background2.y += 1  # 背景2向下滚动    if bullet.y > -HEIGHT:        bullet.y = bullet.y - 10 # 子弹自动向上移动    enemy.y += 3 # 敌机自动下落    if enemy.y > HEIGHT: # 敌机落到画面底部        enemy.y = 0 # 敌机从上面重新出现        enemy.x = random.randint(50, WIDTH-50)  # 敌机水平位置随机    if bullet.colliderect(enemy): # 子弹与敌机发生碰撞后        sounds.got_enemy.play()  # 播放击中敌机音效        enemy.y = 0  # 敌机从上面重新出现        enemy.x = random.randint(0, WIDTH)  # 敌机水平位置随机        score = score + 1 # 得分加1        bullet.y = -HEIGHT  # 隐藏子弹    if hero.colliderect(enemy): # 玩家飞机和敌机发生碰撞        sounds.explode.play()  # 播放玩家飞机爆炸音效        isLoose = True  # 游戏失败        hero.image = 'hero_blowup' # 更换游戏玩家的图片为爆炸图片def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时执行    if isLoose:        return  # 如果游戏失败,返回,不做下面的操作    hero.x = pos[0]  # 玩家飞机的x坐标设为鼠标的x坐标    hero.y = pos[1]  # 玩家飞机的y坐标设为鼠标的y坐标def on_mouse_down(): # 当鼠标键按下时    if isLoose:        return  # 如果游戏失败,返回,不做下面的操作    bullet.x = hero.x   # 把子弹位置设为玩家飞机的正上方    bullet.y = hero.y - 70    sounds.gun.play() # 播放发射子弹音效pgzrun.go()  # 开始执行游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值