ComputerCraftFan11, on 09 May 2012 - 03:44 AM, said:
Index
This game will be bird's eye view and scrolling.
Let's start from the cursor tutorial.
So, after you finished that tutorial, your code should look like this:
Drawing The Character
Let's create something to store what direction you are looking at. This is needed for collision detectors. We also need to get the size of your terminal. Add these to your code:
Then change write(">") to write(char) so your character will face that direction
It should look like this:
Now, if you run the code, not much will change. Try to make it update the direction when you press a button. To do this, change your code to this:
If you did this correctly, your character should walk around and look where you press.
Lets try to create a object with a collision detector.
Collision Detector and objects (& scrolling!)
Since your character is limited to the size of the screen, let's try to make the maps endless.
Change
If your wondering why did we change that? It is because for scrolling, you don't want your character to move too, right? Really, right??
Now, don't delete currentX and currentY now. We need that to keep the position of the map.
To create a object, add this code right above drawCursor()
oX is the X position of the item
oY is the Y position of the item
title is what the object looks like.
Now, lets code it and explain at the same time.
Add this inside of newObject(oX, oY, title)
Now your code should look like this
Try to add this code below term.clear()
This is where the "char" becomes the most useful.
This code detects if you touch the object.
Now that we got that down, let's move the player back when you touch it.
Do this:
If you did this all correctly, your code should look like this:
Everything should work fine but theres one bug with scrolling. Try moving right/left for a long time. The game will glitch because your map is going off screen. This code will stop drawing your objects one they go off screen so the map size doesn't get too big.
That's about it for this tutorial. Check back for more />
- Drawing the Character (In this post)
- Collision Detector, Objects, and scrolling (In this post)
- Easier World Drawing (2nd Post)
- Touch Detector (3rd Post)
This game will be bird's eye view and scrolling.
Let's start from the cursor tutorial.
So, after you finished that tutorial, your code should look like this:
Drawing The Character
local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function drawCursor() term.clear() --Clears the screen term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Y write(">") end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 elseif key == 31 or key == 208 then --down currentY = currentY +1 elseif key == 203 or key == 30 then --left currentX = currentX -1 elseif key == 205 or key == 32 then --right currentX = currentX +1 end end
Let's create something to store what direction you are looking at. This is needed for collision detectors. We also need to get the size of your terminal. Add these to your code:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction
Then change write(">") to write(char) so your character will face that direction
It should look like this:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function drawCursor() term.clear() --Clears the screen term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 elseif key == 31 or key == 208 then --down currentY = currentY +1 elseif key == 203 or key == 30 then --left currentX = currentX -1 elseif key == 205 or key == 32 then --right currentX = currentX +1 end end
Now, if you run the code, not much will change. Try to make it update the direction when you press a button. To do this, change your code to this:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function drawCursor() term.clear() --Clears the screen term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 char = "^" --Change your direction to face up elseif key == 31 or key == 208 then --down currentY = currentY +1 char = "v" --Change your direction to face down elseif key == 203 or key == 30 then --left currentX = currentX -1 char = "<" --Change your direction to face left elseif key == 205 or key == 32 then --right currentX = currentX +1 char = ">" --Change your direction to face right end end
If you did this correctly, your character should walk around and look where you press.
Lets try to create a object with a collision detector.
Collision Detector and objects (& scrolling!)
Since your character is limited to the size of the screen, let's try to make the maps endless.
Change
term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Yto
term.setCursorPos(x1/2, y1/2) --Draws a cursor at the center of the screen
If your wondering why did we change that? It is because for scrolling, you don't want your character to move too
Now, don't delete currentX and currentY now. We need that to keep the position of the map.
To create a object, add this code right above drawCursor()
function newObject(oX, oY, title)This allows you to create object. We didn't add that part yet but let me explain what (oX, oY, title) means.
oX is the X position of the item
oY is the Y position of the item
title is what the object looks like.
Now, lets code it and explain at the same time.
Add this inside of newObject(oX, oY, title)
term.setCursorPos(oX - currentX, oY - currentY) --Set's the object at your position - map's position. write(title) --Draw the object end --End the function
Now your code should look like this
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function newObject(oX, oY, title) term.setCursorPos(oX-currentX,oY-currentY) write(title) end function drawCursor() term.clear() --Clears the screen term.setCursorPos(x1/2, y1/2) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 char = "^" --Change your direction to face up elseif key == 31 or key == 208 then --down currentY = currentY +1 char = "v" --Change your direction to face down elseif key == 203 or key == 30 then --left currentX = currentX -1 char = "<" --Change your direction to face left elseif key == 205 or key == 32 then --right currentX = currentX +1 char = ">" --Change your direction to face right end end
Try to add this code below term.clear()
newObject(3, 3, "X")If it works correctly, it should draw "X" on 3,3.
This is where the "char" becomes the most useful.
This code detects if you touch the object.
if oX-currentX == x1/2 and oY-currentX == y1/2 then --Will run this code if u are touching it end
Now that we got that down, let's move the player back when you touch it.
Do this:
if oX- currentX == x/2 and oY- currentY == y/2 then if char == "v" then --up currentY = currentY -1 elseif char == "^" then --down currentY = currentY +1 elseif char == ">" then --left currentX = currentX -1 elseif char == "<" then --right currentX = currentX +1 end drawCursor() --redraw end
If you did this all correctly, your code should look like this:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function newObject(oX, oY, title) term.setCursorPos(oX-currentX,oY-currentY) write(title) if oX- currentX == x/2 and oY- currentY == y/2 then if char == "v" then --up currentY = currentY -1 elseif char == "^" then --down currentY = currentY +1 elseif char == ">" then --left currentX = currentX -1 elseif char == "<" then --right currentX = currentX +1 end drawCursor() --redraw end end function drawCursor() term.clear() --Clears the screen term.setCursorPos(x1/2, y1/2) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 char = "^" --Change your direction to face up elseif key == 31 or key == 208 then --down currentY = currentY +1 char = "v" --Change your direction to face down elseif key == 203 or key == 30 then --left currentX = currentX -1 char = "<" --Change your direction to face left elseif key == 205 or key == 32 then --right currentX = currentX +1 char = ">" --Change your direction to face right end end
Everything should work fine but theres one bug with scrolling. Try moving right/left for a long time. The game will glitch because your map is going off screen. This code will stop drawing your objects one they go off screen so the map size doesn't get too big.
function newObject(oX, oY, title) if oX - currentX < x then term.setCursorPos(oX- currentX, oY- currentY) write(title) if oX- currentX == x/2 and oY- currentY == y/2 then if char == "v" then --up currentY = currentY -1 elseif char == "^" then --down currentY = currentY +1 elseif char == ">" then --left currentX = currentX -1 elseif char == "<" then --right currentX = currentX +1 end drawCursor() end end
That's about it for this tutorial. Check back for more />
Index
- Drawing the Character (In this post)
- Collision Detector, Objects, and scrolling (In this post)
- Easier World Drawing (2nd Post)
- Touch Detector (3rd Post)
This game will be bird's eye view and scrolling.
Let's start from the cursor tutorial.
So, after you finished that tutorial, your code should look like this:
Drawing The Character
local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function drawCursor() term.clear() --Clears the screen term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Y write(">") end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 elseif key == 31 or key == 208 then --down currentY = currentY +1 elseif key == 203 or key == 30 then --left currentX = currentX -1 elseif key == 205 or key == 32 then --right currentX = currentX +1 end end
Let's create something to store what direction you are looking at. This is needed for collision detectors. We also need to get the size of your terminal. Add these to your code:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction
Then change write(">") to write(char) so your character will face that direction
It should look like this:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function drawCursor() term.clear() --Clears the screen term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 elseif key == 31 or key == 208 then --down currentY = currentY +1 elseif key == 203 or key == 30 then --left currentX = currentX -1 elseif key == 205 or key == 32 then --right currentX = currentX +1 end end
Now, if you run the code, not much will change. Try to make it update the direction when you press a button. To do this, change your code to this:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function drawCursor() term.clear() --Clears the screen term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 char = "^" --Change your direction to face up elseif key == 31 or key == 208 then --down currentY = currentY +1 char = "v" --Change your direction to face down elseif key == 203 or key == 30 then --left currentX = currentX -1 char = "<" --Change your direction to face left elseif key == 205 or key == 32 then --right currentX = currentX +1 char = ">" --Change your direction to face right end end
If you did this correctly, your character should walk around and look where you press.
Lets try to create a object with a collision detector.
Collision Detector and objects (& scrolling!)
Since your character is limited to the size of the screen, let's try to make the maps endless.
Change
term.setCursorPos(currentX, currentY) --Draws a cursor at the current X/Yto
term.setCursorPos(x1/2, y1/2) --Draws a cursor at the center of the screen
If your wondering why did we change that? It is because for scrolling, you don't want your character to move too
Now, don't delete currentX and currentY now. We need that to keep the position of the map.
To create a object, add this code right above drawCursor()
function newObject(oX, oY, title)This allows you to create object. We didn't add that part yet but let me explain what (oX, oY, title) means.
oX is the X position of the item
oY is the Y position of the item
title is what the object looks like.
Now, lets code it and explain at the same time.
Add this inside of newObject(oX, oY, title)
term.setCursorPos(oX - currentX, oY - currentY) --Set's the object at your position - map's position. write(title) --Draw the object end --End the function
Now your code should look like this
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function newObject(oX, oY, title) term.setCursorPos(oX-currentX,oY-currentY) write(title) end function drawCursor() term.clear() --Clears the screen term.setCursorPos(x1/2, y1/2) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 char = "^" --Change your direction to face up elseif key == 31 or key == 208 then --down currentY = currentY +1 char = "v" --Change your direction to face down elseif key == 203 or key == 30 then --left currentX = currentX -1 char = "<" --Change your direction to face left elseif key == 205 or key == 32 then --right currentX = currentX +1 char = ">" --Change your direction to face right end end
Try to add this code below term.clear()
newObject(3, 3, "X")If it works correctly, it should draw "X" on 3,3.
This is where the "char" becomes the most useful.
This code detects if you touch the object.
if oX-currentX == x1/2 and oY-currentX == y1/2 then --Will run this code if u are touching it end
Now that we got that down, let's move the player back when you touch it.
Do this:
if oX- currentX == x/2 and oY- currentY == y/2 then if char == "v" then --up currentY = currentY -1 elseif char == "^" then --down currentY = currentY +1 elseif char == ">" then --left currentX = currentX -1 elseif char == "<" then --right currentX = currentX +1 end drawCursor() --redraw end
If you did this all correctly, your code should look like this:
local x1, y1 = term.getSize() --Returns the size of your terminal local char = ">" --Stores your character direction local currentX = 1 --Saves the current X of the cursor local currentY = 1 --Saves the current Y of the cursor function newObject(oX, oY, title) term.setCursorPos(oX-currentX,oY-currentY) write(title) if oX- currentX == x/2 and oY- currentY == y/2 then if char == "v" then --up currentY = currentY -1 elseif char == "^" then --down currentY = currentY +1 elseif char == ">" then --left currentX = currentX -1 elseif char == "<" then --right currentX = currentX +1 end drawCursor() --redraw end end function drawCursor() term.clear() --Clears the screen term.setCursorPos(x1/2, y1/2) --Draws a cursor at the current X/Y write(char) end while true do drawCursor() local e,key = os.pullEvent( "key" ) if key == 17 or key == 200 then --up currentY = currentY -1 char = "^" --Change your direction to face up elseif key == 31 or key == 208 then --down currentY = currentY +1 char = "v" --Change your direction to face down elseif key == 203 or key == 30 then --left currentX = currentX -1 char = "<" --Change your direction to face left elseif key == 205 or key == 32 then --right currentX = currentX +1 char = ">" --Change your direction to face right end end
Everything should work fine but theres one bug with scrolling. Try moving right/left for a long time. The game will glitch because your map is going off screen. This code will stop drawing your objects one they go off screen so the map size doesn't get too big.
function newObject(oX, oY, title) if oX - currentX < x then term.setCursorPos(oX- currentX, oY- currentY) write(title) if oX- currentX == x/2 and oY- currentY == y/2 then if char == "v" then --up currentY = currentY -1 elseif char == "^" then --down currentY = currentY +1 elseif char == ">" then --left currentX = currentX -1 elseif char == "<" then --right currentX = currentX +1 end drawCursor() end end
That's about it for this tutorial. Check back for more />