IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Lua + 开源引擎制作的飞机大战游戏 -> 正文阅读

[游戏开发]Lua + 开源引擎制作的飞机大战游戏

写的还是很多不足,尽量保持每天敲码保持手感,
这是一个用lua写的基础游戏,逻辑也很简单,
用来水文章(不是)

-- plane1
Window = UsingModule("Window")
Interactivity = UsingModule("Interactivity")
Graphic = UsingModule("Graphic")
Time = UsingModule("Time")

-- 创建游戏窗口
Window.CreateWindow("plane war", {
    x = Window.WINDOW_POSITION_DEFAULT,
    y = Window.WINDOW_POSITION_DEFAULT,
    w = 550,
    h = 700
}, {})
Window.UpdateWindow()
Window.SetWindowMode(Window.WINDOW_MODE_WINDOWED)
Window.UpdateWindow()
-- 字体信息
font_num = Graphic.LoadFontFromFile("./Inkfree.ttf", 125)
font_score = Graphic.LoadFontFromFile("./Inkfree.ttf", 30)
font_score:SetStyle({Graphic.FONT_STYLE_ITALIC, Graphic.FONT_STYLE_UNDERLINE})
-- 导入图片信息
Image_background =
    Graphic.LoadImageFromFile(".//图片//images//background.png")
Image_me1 = Graphic.LoadImageFromFile(".//图片//images//me1.png")
Image_enemy1 = Graphic.LoadImageFromFile(".//图片//images//enemy1.png")
Image_enemy2 = Graphic.LoadImageFromFile(".//图片//images//enemy2.png")
Image_enemy3 = Graphic.LoadImageFromFile(".//图片//images//enemy3_n1.png")
Image_enemy4 = Graphic.LoadImageFromFile(".//图片//images//enemy3_n2.png")
Image_bullet = Graphic.LoadImageFromFile(".//图片//images//bullet2.png")
Image_bullet1 = Graphic.LoadImageFromFile(".//图片//images//bullet1.png")
Image_enemy1down1 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy1_down1.png")
Image_enemy1down2 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy1_down2.png")
Image_enemy1down3 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy1_down3.png")
Image_enemy2down1 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy2_down1.png")
Image_enemy2down2 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy2_down2.png")
Image_enemy2down3 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy2_down3.png")
Image_enemy3down1 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy3_down1.png")
Image_enemy3down2 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy3_down2.png")
Image_enemy3down3 = Graphic.LoadImageFromFile(
                        ".//图片//images//enemy3_down3.png")
-- 数值
speed = 15
speed1 = 5
bulletnumber = 15
enemy1number = 10
enemy2number = 5
enemy3number = 2
blood = 10 -- 血量
boom = 0 -- 爆炸标志
count = 0 -- 得分
rect = {judge = true, x = 300, y = 550}
boomrect = {x = 0, y = 0, judge = false}
bullet_list = {
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}
}
enemy1_list = {
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false}
}
BoomEnemy1_list = {
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false}
}
enemy2_list = {
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false}
}
BoomEnemy2_list = {
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false}
}
enemy3_list = {x = 0, y = 0, judge = false}
BoomEnemy3_list = {x = 0, y = 0, judge = false}
enenmy_bullet = {
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false},
    {x = 0, y = 0, judge = false}, {x = 0, y = 0, judge = false}
}
_bnStartTime = Time.GetInitTime() -- 开机到现在的毫秒数
_bnEndTime = Time.GetInitTime()
_nStartTime = Time.GetInitTime()
_nEndTime = Time.GetInitTime()
_nStartTime1 = Time.GetInitTime()
_nEndTime1 = Time.GetInitTime()
_nStartTime2 = Time.GetInitTime()
_nEndTime2 = Time.GetInitTime()
-- 子弹的构建
function FireBullet()
    for i = 1, 15 do
        if bullet_list[i].judge == false then
            bullet_list[i].judge = true
            bullet_list[i].x = rect.x + 15
            bullet_list[i].y = rect.y - 10
            break
        end
    end
    for j = 1, 6 do
        if enenmy_bullet[j].judge == false then
            enenmy_bullet[j].judge = true
            enenmy_bullet[j].x = enemy3_list.x + 80
            enenmy_bullet[j].y = enemy3_list.y + 150
            break
        end
    end
end
-- 子弹的移动
function MoveBullet()
    for i = 1, 15 do
        if bullet_list[i].judge == true then
            bullet_list[i].y = bullet_list[i].y - 2
            if bullet_list[i].y < 0 then bullet_list[i].judge = false end
        end
    end
    for j = 1, 6 do
        if enenmy_bullet[j].judge == true then
            enenmy_bullet[j].y = enenmy_bullet[j].y + 3
            if enenmy_bullet[j].y >700 then
                enenmy_bullet[j].judge = false
            end
        end
    end
end
-- 敌机的构建
function SetEnemy()
    for i = 1, 10 do
        if enemy1_list[i].judge == false and _nEndTime - _nStartTime >= 80 then -- and _nEndTime - _nStartTime == 40
            enemy1_list[i].judge = true
            enemy1_list[i].x = math.random(0, 530)
            enemy1_list[i].y = math.random(0, 20)
            _nStartTime = _nEndTime
            break
        end
        _nEndTime = Time.GetInitTime()
    end
    for j = 1, 6 do
        if enemy2_list[j].judge == false and _nEndTime1 - _nStartTime1 >= 300 then -- and _nEndTime - _nStartTime == 40
            enemy2_list[j].judge = true
            enemy2_list[j].x = math.random(0, 530)
            enemy2_list[j].y = math.random(0, 300)
            _nStartTime1 = _nEndTime1
            break
        end
        _nEndTime1 = Time.GetInitTime()
    end
    if enemy3_list.judge == false and _nEndTime2 - _nStartTime2 >= 1000 then -- and _nEndTime - _nStartTime == 40
        enemy3_list.judge = true
        enemy3_list.x = math.random(0, 530)
        enemy3_list.y = math.random(0, 400)
        _nStartTime2 = _nEndTime2
    end
    _nEndTime2 = Time.GetInitTime()
end
-- 敌机的移动
function MoveEnemy()
    for i = 1, 10 do
        if enemy1_list[i].judge == true then
            enemy1_list[i].y = enemy1_list[i].y + 1
            if enemy1_list[i].y > 700 then
                enemy1_list[i].judge = false
            end
        end
    end
    for j = 1, 5 do
        if enemy1_list[j].judge == true then
            enemy1_list[j].x = enemy1_list[j].x + 1
            if enemy1_list[j].x > 550 then
                enemy1_list[j].judge = false
            end
        end
    end
    for j = 6, 10 do
        if enemy1_list[j].judge == true then
            enemy1_list[j].x = enemy1_list[j].x - 1
            if enemy1_list[j].x < 0 then enemy1_list[j].judge = false end
        end
    end
    for k = 1, 6 do
        if enemy2_list[k].judge == true then
            enemy2_list[k].x = enemy1_list[k].x + 1
            if enemy1_list[k].x > 700 then
                enemy1_list[k].judge = false
            end
        end
    end
    for k = 3, 6 do
        if enemy1_list[k].judge == true then
            enemy1_list[k].x = enemy1_list[k].x - 2
        end
        if enemy1_list[k].x < 0 then enemy1_list[k].judge = false end
    end
    if enemy3_list.judge == true then
        enemy3_list.y = enemy3_list.y - 0.3
        if enemy3_list.y < -300 then enemy3_list.judge = false end
    end
end
-- 销毁
function Boom()
    for i = 1, 10 do
        if math.random(0, 3) == 2 then BoomEnemy1_list[i].judge = false end
    end
    for j = 1, 6 do
        if math.random(0, 3) == 2 then BoomEnemy2_list[j].judge = false end
    end
    if math.random(0, 3) == 2 then BoomEnemy3_list.judge = false end
end
function Hit()
    for i = 1, 10 do
        if enemy1_list[i].judge == false then
            enemy1_list[i].judge = false
        end
        for j = 1, 15 do
            if bullet_list[j].judge == false then
                bullet_list[j].judge = false
            end
            if bullet_list[j].x < enemy1_list[i].x + 60 and bullet_list[j].x >
                enemy1_list[i].x and bullet_list[j].y < enemy1_list[i].y + 30 and
                bullet_list[j].y > enemy1_list[i].y then
                BoomEnemy1_list[i].judge = true
                BoomEnemy1_list[i].x = enemy1_list[i].x
                BoomEnemy1_list[i].y = enemy1_list[i].y
                bullet_list[j].judge = false
                enemy1_list[i].judge = false
                count = count + 1
            end
            if bullet_list[j].x + 65 < enemy1_list[i].x + 60 and
                bullet_list[j].x + 65 > enemy1_list[i].x and bullet_list[j].y <
                enemy1_list[i].y + 30 and bullet_list[j].y > enemy1_list[i].y then
                BoomEnemy1_list[i].judge = true
                BoomEnemy1_list[i].x = enemy1_list[i].x
                BoomEnemy1_list[i].y = enemy1_list[i].y
                bullet_list[j].judge = false
                enemy1_list[i].judge = false
                count = count + 1
            end
        end
    end
    for k = 1, 6 do
        if enemy2_list[k].judge == false then
            enemy2_list[k].judge = false
        end
        for h = 1, 15 do
            if bullet_list[h].judge == false then
                bullet_list[h].judge = false
            end
            if bullet_list[h].x < enemy2_list[k].x + 100 and bullet_list[h].x >
                enemy2_list[k].x and bullet_list[h].y < enemy2_list[k].y + 30 and
                bullet_list[h].y > enemy2_list[k].y then
                BoomEnemy2_list[k].judge = true
                BoomEnemy2_list[k].x = enemy2_list[k].x
                BoomEnemy2_list[k].y = enemy2_list[k].y
                bullet_list[h].judge = false
                enemy2_list[k].judge = false
                count = count + 2
            end
            if bullet_list[h].x + 65 < enemy2_list[k].x + 100 and
                bullet_list[h].x + 65 > enemy2_list[k].x and bullet_list[h].y <
                enemy2_list[k].y + 30 and bullet_list[h].y > enemy2_list[k].y then
                BoomEnemy2_list[k].judge = true
                BoomEnemy2_list[k].x = enemy2_list[k].x
                BoomEnemy2_list[k].y = enemy2_list[k].y
                bullet_list[h].judge = false
                enemy2_list[k].judge = false
                count = count + 2
            end
        end
    end
end
if enemy3_list.judge == false then enemy3_list.judge = false end
for m = 1, 15 do
    if bullet_list[m].judge == false then bullet_list[m].judge = false end
    if bullet_list[m].x < enemy3_list.x + 60 and bullet_list[m].x >
        enemy3_list.x and bullet_list[m].y < enemy3_list.y + 30 and
        bullet_list[m].y > enemy3_list.y then
        BoomEnemy3_list.judge = true
        BoomEnemy3_list.x = enemy3_list.x
        BoomEnemy3_list.y = enemy3_list.y
        bullet_list[m].judge = false
        enemy3_list.judge = false
        count = count + 10
    end
    if bullet_list[m].x + 65 < enemy3_list.x + 60 and bullet_list[m].x + 65 >
        enemy3_list.x and bullet_list[m].y < enemy3_list.y + 30 and
        bullet_list[m].y > enemy3_list.y then
        BoomEnemy3_list.judge = true
        BoomEnemy3_list.x = enemy3_list.x
        BoomEnemy3_list.y = enemy3_list.y
        bullet_list[m].judge = false
        enemy3_list.judge = false
        count = count + 10
    end
end
for n = 1, 6 do
    if enenmy_bullet[n].judge == false then enenmy_bullet[n].judge = false end
    if enenmy_bullet[n].x < rect.x + 60 and enenmy_bullet[n].x > rect.x and
        enenmy_bullet[n].y < rect.y + 30 and enenmy_bullet[n].y > rect.y then
        boomrect.judge = true
    end
end
function BoomTime()
    for i = 1, 10 do
        if math.random(0, 2) == 2 then BoomEnemy1_list[i].judge = false end
    end
    for j = 1, 6 do
        if math.random(0, 10) == 2 then BoomEnemy2_list[j].judge = false end
    end
    if math.random(0, 10) == 2 then BoomEnemy3_list.judge = false end
end
-- 战机移动
function Move(direction)
    if direction == "up" then -- 如果向上移动
        rect.y = rect.y - speed
    elseif direction == "down" then -- 如果向下移动
        rect.y = rect.y + speed
    elseif direction == "right" then -- 如果向右移动
        rect.x = rect.x + speed
    elseif direction == "left" then -- 如果向左移动
        rect.x = rect.x - speed
    elseif direction == "space" or _bnEndTime - _bnStartTime > 100 then
        FireBullet()
        _bnStartTime = _bnEndTime
    end
    _bnEndTime = Time.GetInitTime()
end
-- 画面呈现
function Show()
    -- 背景
    local _texture = Graphic.CreateTexture(Image_background)
    Graphic.CopyTexture(_texture, {x = 0, y = 0, w = 550, h = 750})
    -- 敌机
    -- local _nStartTime = Time.GetInitTime()
    local _width, _height = Image_enemy1:GetSize()
    local _texture4 = Graphic.CreateTexture(Image_enemy1)
    for i = 1, 10 do
        if enemy1_list[i].judge == true then
            Graphic.CopyTexture(_texture4, {
                x = enemy1_list[i].x,
                y = enemy1_list[i].y,
                w = _width,
                h = _height
            })
        end
    end
    local _widthen2, _heighten2 = Image_enemy2:GetSize()
    local _textureen2 = Graphic.CreateTexture(Image_enemy2)
    for h = 1, 6 do
        if enemy2_list[h].judge == true then
            Graphic.CopyTexture(_textureen2, {
                x = enemy2_list[h].x,
                y = enemy2_list[h].y,
                w = _widthen2,
                h = _heighten2
            })
        end
    end
    local _widthen3, _heighten3 = Image_enemy3:GetSize()
    local _textureen3 = Graphic.CreateTexture(Image_enemy3)
    if enemy3_list.judge == true then
        Graphic.CopyTexture(_textureen3, {
            x = enemy3_list.x,
            y = enemy3_list.y,
            w = _widthen3,
            h = _heighten3
        })
    end
    -- 敌机爆炸
    local _width2, _height2 = Image_enemy1down1:GetSize()
    local _texture1 = Graphic.CreateTexture(Image_enemy1down1)
    for k = 1, 10 do
        if BoomEnemy1_list[k].judge == true then
            Graphic.CopyTexture(_texture1, {
                x = BoomEnemy1_list[k].x,
                y = BoomEnemy1_list[k].y,
                w = _width2,
                h = _height2
            })
        end
    end
    local _widthbo2, _heightbo2 = Image_enemy2down1:GetSize()
    local _texturebo2 = Graphic.CreateTexture(Image_enemy2down1)
    for m = 1, 6 do
        if BoomEnemy2_list[m].judge == true then
            Graphic.CopyTexture(_texturebo2, {
                x = BoomEnemy2_list[m].x,
                y = BoomEnemy2_list[m].y,
                w = _width2,
                h = _height2
            })
        end
    end
    local _widthbo3, _heightbo3 = Image_enemy3down1:GetSize()
    local _texturebo3 = Graphic.CreateTexture(Image_enemy3down1)
    if BoomEnemy3_list.judge == true then
        Graphic.CopyTexture(_texturebo3, {
            x = enemy3_list.x,
            y = enemy3_list.y,
            w = _widthbo3,
            h = _heightbo3
        })
    end
    -- 子弹
    local _width1, _height1 = Image_bullet:GetSize()
    local _texture2 = Graphic.CreateTexture(Image_bullet)
    for j = 1, 15 do
        if bullet_list[j].judge == true then
            Graphic.CopyTexture(_texture2, {
                x = bullet_list[j].x,
                y = bullet_list[j].y,
                w = _width1 * 1.5,
                h = _height1 * 1.5
            })
            Graphic.CopyTexture(_texture2, {
                x = bullet_list[j].x + 65,
                y = bullet_list[j].y,
                w = _width1 * 1.5,
                h = _height1 * 1.5
            })
        end
    end
    local _width11, _height11 = Image_bullet1:GetSize()
    local _texture21 = Graphic.CreateTexture(Image_bullet1)
    for m = 1, 6 do
        if enenmy_bullet[m].judge == true then
            Graphic.CopyTexture(_texture21, {
                x = enenmy_bullet[m].x,
                y = enenmy_bullet[m].y,
                w = _width1 * 2,
                h = _height1 * 2
            })
        end
    end
    -- 战机
    local _width3, _height3 = Image_me1:GetSize()
    local _texture3 = Graphic.CreateTexture(Image_me1)
    Graphic.CopyTexture(_texture3,
                        {x = rect.x, y = rect.y, w = _width3, h = _height3})
    -- 玩家得分
    local _image = Graphic.CreateUTF8TextImageBlended(font_score,
                                                    "score"..count,
                                                      {
        r = 215,
        g = 115,
        b = 115,
        a = 255
    })
    local _widthfen, _heightfen = _image:GetSize()
    local _texturefen = Graphic.CreateTexture(_image)
    Graphic.CopyTexture(_texturefen,
                        {x = 0, y = 0, w = _widthfen, h = _heightfen})
end
bIsQuit = false
while not bIsQuit do
    Show()
    if boomrect == true then break end
    Move()
    MoveBullet()
    SetEnemy()
    MoveEnemy()
    Hit()
    BoomTime()
    Window.UpdateWindow()
    while Interactivity.UpdateEvent() do
        math.randomseed(os.time())
        -- local _nStartTime = Time.GetInitTime()
        -- 获取当前事件类型
        local _event = Interactivity.GetEventType()
        -- 如果是退出事件,就将退出标志位置为 true
        if _event == Interactivity.EVENT_QUIT then
            bIsQuit = true
            -- 如果为 ↑ 键抬起(本质为按键事件,为优化操作手感选择判定抬起事件,下同)
        elseif _event == Interactivity.EVENT_KEYUP_UP then
            Move("up")
        elseif _event == Interactivity.EVENT_KEYUP_DOWN then
            Move("down")
        elseif _event == Interactivity.EVENT_KEYUP_LEFT then
            Move("left")
        elseif _event == Interactivity.EVENT_KEYUP_RIGHT then
            Move("right")
        elseif _event == Interactivity.EVENT_KEYDOWN_SPACE then
            Move("space")
        end
        Window.UpdateWindow()
    end
        --boomrect.judge = CheckGameOver()
    -- 如果 _status 存在(即 CheckGameOver 函数返回值不为空)则进入判断
    if boomrect.judge ==true then
            Window.ShowMessageBox(Window.MSGBOX_INFO, "游戏结束", "失败!")
    end
    Window.ClearWindow()
end
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-19 08:17:57  更:2021-09-19 08:19:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 20:16:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码