LUA-字符的屏幕限制

发布于 2025-01-31 08:50:29 字数 725 浏览 4 评论 0原文

我有一个问题,我的角色不断出现在屏幕上。
如何解决此问题?我知道我需要放置一个功能,以防止角色熄灭屏幕并设法在圆圈的半径上进行。

    if love.keyboard.isDown("right") then
        player.x = player.x + 4
    elseif love.keyboard.isDown("left") then
        player.x = player.x - 4
    elseif love.keyboard.isDown("up") then
        player.y = player.y - 4
    elseif love.keyboard.isDown("down") then
        player.y = player.y + 4
    end

    if AABB(player.x, player.y, player.w, player.h, target.x,    target.y, target.raduis) then
        score = score + 1
        target.x = math.random(target.raduis, love.graphics.getWidth() - target.raduis)
        target.y = math.random(target.raduis, love.graphics.getHeight()- 100)
    end

I have a problem where my character keeps going out of my screen.
How do I fix this problem? I know I need to put in a function to prevent the character from going out of the screen and managed to do it on the radius of the circle.

    if love.keyboard.isDown("right") then
        player.x = player.x + 4
    elseif love.keyboard.isDown("left") then
        player.x = player.x - 4
    elseif love.keyboard.isDown("up") then
        player.y = player.y - 4
    elseif love.keyboard.isDown("down") then
        player.y = player.y + 4
    end

    if AABB(player.x, player.y, player.w, player.h, target.x,    target.y, target.raduis) then
        score = score + 1
        target.x = math.random(target.raduis, love.graphics.getWidth() - target.raduis)
        target.y = math.random(target.raduis, love.graphics.getHeight()- 100)
    end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2025-02-07 08:50:29

请同时执行玩家和目标

我不确定“角色”是什么,所以如果您希望角色不脱离屏幕,

if player.x < 0 then
    player.x = 0
elseif player.x + player.w > love.graphics.getWidth() then
    player.x = love.graphics.getWidth() - player.w
end
if player.y < 0 then
    player.y = 0
elseif player.y + player.h > love.graphics.getHeight() then
    player.y = love.graphics.getHeight() - player.h
end

,请尝试:如果您希望目标留在屏幕上,请尝试:

if AABB(player.x, player.y, player.w, player.h, target.x,    target.y, 
target.raduis) then
    score = score + 1
    target.x = math.random(0 + target.raduis, love.graphics.getWidth() - target.raduis)
    target.y = math.random(0 + target.raduis, love.graphics.getHeight() - target.raduis)
end

希望这会有所帮助

im not sure what the "character" is, so im going to do both the player and the target

if you want the character to not go off screen, try:

if player.x < 0 then
    player.x = 0
elseif player.x + player.w > love.graphics.getWidth() then
    player.x = love.graphics.getWidth() - player.w
end
if player.y < 0 then
    player.y = 0
elseif player.y + player.h > love.graphics.getHeight() then
    player.y = love.graphics.getHeight() - player.h
end

if you want the target to stay on screen, try:

if AABB(player.x, player.y, player.w, player.h, target.x,    target.y, 
target.raduis) then
    score = score + 1
    target.x = math.random(0 + target.raduis, love.graphics.getWidth() - target.raduis)
    target.y = math.random(0 + target.raduis, love.graphics.getHeight() - target.raduis)
end

hopefully this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文