【Love2d从青铜到王者】第五篇:Love2d之if语句

x33g5p2x  于2022-04-23 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(277)

系列文章目录

前言

🍇一、if语句

1️⃣.if语句

  • 我们使用前一章的代码:
  • 使用if语句,我们可以允许代码片段只在满足条件时执行。
  • 您创建一个if语句,如下所示:
if condition then
    -- code
end
  • 一个条件,或者说陈述,要么是真的,要么是假的。
  • 例如:
5 > 9
  • 这>意味着,大于。所以说法是5大于9,这是假的。
  • 在x递增的代码周围放一个if语句。
function love.update(dt)
    if 5 > 9 then
        x = x + 100 * dt
    end
end

function love.load()
    x=100
end

function love.update(dt)
    if 5>9
    then
        x=x+100*dt
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end
  • 当你运行游戏时,你会注意到我们的矩形没有移动。这是因为该陈述是错误的。如果我们将语句改为5 **<**9(5小于9),则该语句为真,if语句内的代码将执行。
  • 有了这个,我们可以,比如说x升到600,然后让它停止运动,用x **<**600。
function love.update(dt)
    if x < 600 then
        x = x + 100 * dt
    end
end

function love.load()
    x=100
end

function love.update(dt)
    --if 5>9
    if x<600
    then
        x=x+20*dt
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end

2️⃣.赋值(=)和比较(==)

  • 如果我们想检查一个值是否等于另一个值,我们需要使用两个等号**(==)**。
  • 例如:4 == 7。
  • 1个等号用于赋值,2个等号用于比较。
x = 10 --Assign 10 to x
x == 10 --Compare 10 to x
  • 我们也可以使用**>=<=**检查值是大于等于还是小于等于。
10 <= 10 --true, 10 equals to 10
15 >= 4 --true, 15 is higher than 4

3️⃣.Boolean

  • 变量也可以是true的或者false的。这种类型的变量就是我们所说的布尔型变量。
  • 让我们创建一个名为speed,值为value,并检查speed存在true的在if语句中。
function love.load()
    x = 100
    speed = true
end

function love.update(dt)
    -- Remember, 2 equal signs!
    if 
    speed == true 
    then
        x = x + 100 * dt
    end
end
  • speed存在true的,所以我们的矩形移动。但是speed == true实际上是多余的。我们正在检查是否真的speed存在真实的。简单地使用speed作为声明已经足够好了。
if speed then
    x = x + 100 * dt
end
  • 如果我们想检查speed存在false的,我们可以把一个not在它面前。
function love.load()
    x=100
    speed=true
end

function love.update(dt)
    --if 5>9
    --if x<600
    --if speed==true
    if not speed
    then
        x=x+100*dt
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end

  • 如果我们想检查一个数字是否不等于另一个数字,我们使用波浪符号**(~)**。
function love.load()
    x=100
    speed=true
end

function love.update(dt)
    --if 5>9
    --if x<600
    --if speed==true
    --if not speed
    if 4~=5 
    then
        x=x+20*dt
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end

  • 我们也可以分配true的或者false的用语句转换为变量。
  • 例如:
speed = 6 > 3
  • 如果我们检查speed是否为true,然后在If语句内将speed改为false,这并不是说我们跳出了if语句。下面的所有代码仍然会被执行。
function love.load()
    x=100
    speed=true
end

function love.update(dt)
    --if 5>9
    --if x<600
    --if speed==true
    --if not speed
    --if 4~=5 
    if speed 
    then
        speed = false
        print("This will still be executed!")
        x = x + 100 * dt
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end

但是我这里后面的语句不执行为什么。

扩展点:

function love.load()
    x=100
    speed=500
end

function love.update(dt)
    --if 5>9
    --if x<600
    --if speed==true
    --if not speed
    --if 4~=5
    --[[if speed
    then
        speed = false
        print("This will still be executed!")
        x = x + 100 * dt
    end--]]
    x=x+speed*dt;
    if x>600
    then
        speed=-speed
    else if x<0
        then
            speed=-speed
        end
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end

4️⃣.方向键Arrow keys

  • 让我们根据是否按住右箭头键来移动矩形。为此,我们使用函数。love.keyboard.isDown(WiKi)
  • 注意Down的D是大写的。这种写法我们称之为驼峰原则。我们用小写字母开始第一个单词,然后用大写字母输入后面每个单词的第一个字符。这种类型的外壳也是我们将在本教程中使用的变量。
  • 我们传递字符串**“right”**作为第一个参数来检查右箭头键是否按下。
function love.load()
    x=100
    speed=200
end

function love.update(dt)
    if love.keyboard.isDown("right")
    then
        x=x+speed*dt
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end
  • 所以现在只有当我们按住右箭头键时,我们的矩形才会移动。

  • 我们也可以使用else if告诉我们的游戏在这种情况下该做什么false。当我们不按右键时,让我们的矩形向左移动。
function love.load()
    x=100
    speed=200
end

function love.update(dt)
    if love.keyboard.isDown("right")
    then
        x=x+speed*dt
    else if love.keyboard.isDown("left")
    then
        x=x-speed*dt
    end
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,50,200,100)
end

  • 我们还可以在第一个语句为假之后,用else if语句。让我们这样做,在检查右箭头键是否按下后,如果没有,我们将检查左箭头键是否按下。试着让矩形也上下移动。
function love.load()
    x=100
    y=50
    speed=200
end

function love.update(dt)
    if love.keyboard.isDown("right")
    then
        x=x+speed*dt
    else if love.keyboard.isDown("left")
    then
        x=x-speed*dt
    else if love.keyboard.isDown("up")
        then
            y=y-speed*dt
    else if love.keyboard.isDown("down")
        then
            y=y+speed*dt
        end
    end
end
    end
end

function love.draw()
    love.graphics.rectangle("fill",x,y,200,100)
end

5️⃣.and & or与或

  • 随着and我们可以检查多个陈述是否正确。
if 5 < 9 and 14 > 7 
then
    print("Both statements are true")
end

  • 随着or,如果任何语句为真,则if语句将执行。
if 5 < 9 and 14 > 7 
then
    print("Both statements are true")
end

if 20 < 9 or 14 > 7 or 5 == 10 then
    print("One of these statements is true")
end

6️⃣.还有一点

  • 更准确地说,if语句检查语句是否不是false的或者nil。
if 5 < 9 and 14 > 7 
then
    print("Both statements are true")
end

if 20 < 9 or 14 > 7 or 5 == 10 then
    print("One of these statements is true")
end

if true then print(1) 
end --Not false or nil, executes.
if false then print(2) 
end --False, doesn't execute.
if nil then print(3) 
end --Nil, doesn't execute
if 5 then print(4) 
end --Not false or nil, executes
if "hello" then print(5) 
end --Not false or nil, executes

7️⃣.总结

  • 使用if语句,我们可以允许代码片段只在满足条件时执行。我们可以检查一个数字是否高于、低于或等于另一个数字/值。变量可以是true也可以是false。这种类型的变量就是我们所说的布尔型。我们可以使用其他告诉我们的游戏当语句为假时执行什么,或者语句再做一次检查。

🍋总结

以上就是今天要讲的内容,本文仅仅简单介绍了Love2d之if语句以及各个语法的使用,介绍了love2d的三个主要函数扩展办法,与博主的lua语言文章结合更好的理解love2d的编码,如果你是一名独立游戏开发者,或者一位对游戏开发有着深厚兴趣,但是又对于unity3d,ue4等这些对于新手而言不太友好的引擎而头疼的开发者;那么现在,你可以试试Love2D。Love2D是一款基于Lua编写的轻量级游戏框架,尽管官方称呼其为引擎,但实际上它只能称得上是一个框架,因为他并没有一套全面完整的解决方案。不过,这款框架上手及其容易,是学习游戏开发的初学者入门的一个良好选择。

相关文章