This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mrpoopy345's profile picture

Game Development

Started by mrpoopy345, 12 November 2013 - 04:10 PM
mrpoopy345 #1
Posted 12 November 2013 - 05:10 PM
Hello pros! Another noobish question!
I am currently making a game, and I was wondering how a can make a player that moves around. All I want is him moving around and being shown on the screen. I do not care about collision and stuff like that just yet.
If you need further detail, feel free to tell me :D/>!
Agoldfish #2
Posted 12 November 2013 - 06:03 PM
I think it has something to do with events. More specifically, the key event. Say to move the player left you press "d" the code would be

local event, param1 = os.pullEvent ("char") --#limit os.pullEvent to the char event
   if param1 == "d" then --#if the returned value was 'd'
	   print"You pressed d!"
That was from the Wiki article.
So, when the d key is pressed, redraw the game board, with the character in the correct place.
Edited on 12 November 2013 - 05:04 PM
tchoutchawn #3
Posted 13 November 2013 - 12:32 AM
I think it has something to do with events. More specifically, the key event. Say to move the player left you press "d" the code would be

local event, param1 = os.pullEvent ("char")--#limit os.pullEvent to the char event
   if param1 == "d" then --#if the returned value was 'd'
	   print"You pressed d!"
That was from the Wiki article.
So, when the d key is pressed, redraw the game board, with the character in the correct place.

This will work, but another approach with possibility to use the arrows and controls keys (ctrl, alt, shift, …) could also be considered:


local event, key = os.pullEvent( "key" )	 --The event itself, returns the event type and the key code
if ( keys.getname( key ) == "a" ) then	   --If key is A
  doAction()
  ...
elseif ( keys.getname( key )  == "leftCtrl" ) then   --If the left control key is pressed
  doAnotherAction()
  ...
end

The main difference between the two approaches is that the "char" event will only be triggered on chars presses (letters, digits), while the "key" event will be triggered on any keyboard key press. The only thing you need to consider when using the "key" event is that the event will return the key code instead of the char itself, so if you want to convert them to something readable, you can use the key.getName() method.
Lyqyd #4
Posted 13 November 2013 - 12:38 AM
Or, you know, save the function call and just do a table lookup:


local event = {os.pullEvent()}
if event[1] == "key" then
  if event[2] == keys.leftCtrl then
    --# left control was pressed
  end
end
theoriginalbit #5
Posted 13 November 2013 - 01:00 AM
The main difference between the two approaches is that the "char" event will only be triggered on chars presses (letters, digits), while the "key" event will be triggered on any keyboard key press.
the char event fires when any key is pressed that has a graphical representation, not just letters and digits. Also just as a little extra, the key event fires for all keys except the escape key, the fn key, and the enter on the number pad also returns as just the normal enter.

The only thing you need to consider when using the "key" event is that the event will return the key code instead of the char itself, so if you want to convert them to something readable, you can use the key.getName() method.
Well in that case you would just wait for the char event…

local event = {os.pullEvent()}
if event[1] == "key" then
  --# check which was pressed and do whatever appropriate with it
elseif event[1] == "char" then
  --# this is the readable version of the previous "key" event when appropriate
end
TheOddByte #6
Posted 13 November 2013 - 01:43 PM
Okay.. You've gotten lots of help but a little more couldn't hurt, first of all you should select the players starting position

--# Getting the terminal width and height
local w, h = term.getSize()

--# The players data in a table
local player = {
	x = math.ceil(w/2),
	y = math.ceil(h/2),
	art = "x",
},
This will make the player start in the middle of the screen, Now let's move onto actually drawing the player


--# This function clears the screen
local function clear()
	term.clear()
	term.setCursorPos(1,1)
end


--# This function sets the cursorpos and then draws text there
local function drawAt(x, y, text)
	term.setCursorPos(x,y)
	term.write(text)
end

while true do
	clear()
	drawAt(player.x, player.y, player.art)
	-- Events here
end



Now I'll show you a simple example of how you can use events

local evt, p1 = os.pullEvent()  --# Waiting for an event to be pulled
	if evt == "key" then			 --# Checking if the event was a key event

		if p1 == keys.up then	  --# Checking which key was pressed and if it was the up key
			player.y = player.y - 1 --# Changing the players y position

		elseif p1 == keys.down then
			player.y = player.y + 1

		elseif p1 == keys.left then
			player.x = player.x - 1

		elseif p1 == keys.right then
			player.x = player.x + 1
	   end
	end

Well this is a simple example of how you can move a player around the screen, Hope it helped ;)/>
- Hellkid98
Edited on 13 November 2013 - 12:45 PM