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
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.
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?
Entire Program:
Put code in spoilers
Link to post
Share on other sites