Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Karasuro

Game within a game?? Screen locking help.

Question

So, I'm going to try and make a rogue-like dungeon crawler but I seem to have an issue with screen locking.
 

Basically, when you press an arrow key, the event handler runs the function below, through an if statement, and if true moves the player in that direction. sH and sW are values from gpu.getResolution(). However, if it is false then it should just skip the if statement entirely. Or am I wrong?

local function checkPlayerPos(dir)
  if (dir == "up") and (player.y == 1) then
    return false
  else
    return true
  end
  
  if (dir == "down") and (player.y == sH) then
    return false
  else
    return true
  end
  
  if (dir == "left") and (player.x == 1) then
    return false
  else
    return true
  end
  
  if (dir == "right") and (player.x == sW) then
    return false
  else
    return true
  end
end

Entire Program:

--[[GameTest by Karasuro]]--



-- Requirements

local comp = require "component";

local event = require "event";

local gpu = comp.gpu;

local term = require "term";

local sW, sH = gpu.getResolution();



local player = {x=sW/2,y=sH/2,w=1,h=1,col={bg=0xAAAAAA,fg=0x00AA00}};

local running = false;



-- Render Screen --

local function render()

  gpu.setBackground(0x000000);

  gpu.setForeground(0xFFFFFF);

  term.clear();

 

  -- Render Player

  gpu.setBackground(player.col.bg);

  gpu.setForeground(player.col.fg);

  gpu.fill(player.x,player.y,player.w,player.h," ");

 

  gpu.setBackground(0x000000);

  gpu.setForeground(0xFFFFFF);

end



local function checkPlayerPos(dir)

  if (dir == "up") and (player.y == 1) then

    return false

  else

    return true

  end

 

  if (dir == "down") and (player.y == sH) then

    return false

  else

    return true

  end

 

  if (dir == "left") and (player.x == 1) then

    return false

  else

    return true

  end

 

  if (dir == "right") and (player.x == sW) then

    return false

  else

    return true

  end

end



--Event Handler

local function wait()

  --Wait 1 cycle

end



local myEventHandlers = setmetatable({}, {__index = function() return wait end});



function myEventHandlers.key_up(adress, char, code, playerName)

  if (code == 0xC8) then

    if (checkPlayerPos("up")) then

      player.y = player.y - 1;

      render();

    end

  end

 

  if (code == 0xD0) then

    if (checkPlayerPos("down")) then

      player.y = player.y + 1;

      render();

    end

  end

 

  if (code == 0xCB) then

    if (checkPlayerPos("left")) then

      player.x = player.x - 1;

      render();

    end

  end

 

  if (code == 0xCD) then

    if (checkPlayerPos("right")) then

      player.x = player.x + 1;

      render();

    end

  end

end



local function handleEvent(eventID, ...)

  if (eventID) then

    myEventHandlers[eventID](...);

  end

end



if (running == false) then

  running = true;

  render();

end



while running do

  handleEvent(event.pull(0.001));

end

Edited by SpiritedDusty
Put code in spoilers
Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Your problem is that the function checkPlayerPos always returns in the first if statement.

--check the given condition
if (dir == "up") and (player.y == 1) then
 --if it is true: execute this part
 return false
else
 --if it isn't: execute this part
 return true
end
--The code below is never executed because both parts return.

You'd have to modify your if statements so that they don't always return but instead continue with the next if statement.

If you don't want to think about it; read this spoiler:

Remove the else part of all ifs except the last one.

Link to post
Share on other sites
  • 0

I changed it so it won't use Elses at all, instead it runs through each IF then just returns TRUE.

 

local function checkPlayerPos(dir)

  if (dir == "up") and (player.y == 1) then
    return false
  end

  if (dir == "down") and (player.y == sH) then
    return false
  end

  if (dir == "left") and (player.x == 1) then
    return false
  end

  if (dir == "right") and (player.x == sW) then
    return false
  end

  return true

end
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.