It looks like you're new here. If you want to get involved, click one of these buttons!
Back to Codea! The title says it all, the 2D Minecraft Project I'm doing is going to require WAAY more drawing than the simple thing I have so far, so I need to find a way to draw things faster so that the framerate is not affected. Here's the code:
-- 2D Minecraft
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
version = 0.1
saveProjectInfo("Author","Enro Corp")
saveProjectInfo("Description", "My quest to create a fun 2D Minecraft begins...")
saveProjectData("Version",version)
worldCreate:init()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0)
-- This sets the line thickness
strokeWidth(5);rectMode(CENTER);
noStroke();fill(255)
font("ArialMT");fontSize(10)
text("Version "..readProjectData("Version"),WIDTH/2,HEIGHT - 50)
-- Do your drawing here
worldCreate:draw()
for r,row in ipairs(level) do
for c,cell in ipairs(row) do
if r == 10 then
local grass,img
grass = mesh()
img = readImage("Documents:blockGrass")
grass.texture = img
grass:addRect(-16 + c*32,-16 + r*32,32,32)
grass:draw()
else
local dirt,img
dirt = mesh()
img = readImage("Documents:blockDirt")
dirt.texture = img
dirt:addRect(-16 + c*32,-16 + r*32,32,32)
dirt:draw()
end
end
end
end
WORLD CREATE CLASS
worldCreate = class()
function worldCreate:init()
-- you can accept and set parameters here
height = 102
width = 10
level = {}
index = {}
for i = 1,width do
level[i] = {}
for j = 1,height do
level[i][j] = math.random(6)
end
end
for i = 1,width do
index[i] = {}
for j = 1,height do
index[i][j] = math.random(6)
end
end
end
function worldCreate:draw()
-- Codea does not automatically call this method
for r,row in ipairs(index) do
for c,cell in ipairs(row) do
text(r.."/"..c, WIDTH - width*40 + c*35,HEIGHT - height*40 + r*35)
end
end
end
Comments
Sounds like the first thing you should aim for is to draw all your similar blocks (e.g. dirt) as one mesh. So you have a structure like:
Then you can take this even further and pack all your dirt, grass, stone and other textures into one image. Then you can use one mesh to render all your blocks. This would be the fastest solution.
Edit: Also I would suggest constructing your meshes once, outside your draw function. Then modifying them as needed on interaction.
Have a look at this previous discussion which compares meshes and sprites for an example of what @Simeon is suggesting
http://twolivesleft.com/Codea/Talk/discussion/1378/sprites-vs-meshes/p1
Like this?
(Didn't test the code, away from Codea atm)
okay so I'm using Codea again and I feel like I'm really close to figuring it out. But I havn't. Here's what I think I'm supposed to be doing:
So the "0,0,1,1" gives me the entire tileset, whenever I change some of the digits (for example: "0, .480, .32, .32" I get the image zoomed up and stuff which is what I'm assuming you guys told me to do. make different submeshes that all use a super image filled with all the minecraft tiles I'll need.
So my problem is that I have no idea what to put in the "setRectTex()" to make the mesh be on the appropriate image. Why doesn't it go by pixel coordinates? I don't think it does because I've put in "0 [for the left edge], 480 [for the bottom edge] and 32,32 [for the size that the tile is]" and I just got some weird tv static image but with random colors from my tileset O.O
Please help me better understand so that I can put in the proper coordinates
edited
Was writing this while you posted the above. You are pretty much there!
Couple of changes
The important bit for selecting a subsection of your image is the line
idx is the identifier for the rectangle you have just added to the mesh, the next two values 0,0 are the bottom left location of the portion of the image you want to add and the next two are the size of the portion of the image. These values are in proportion to the full image.
For example
Will display the left half of the image while
Will display the top right hand quarter of the image.
These will be scaled and stretched to fit the size used in addRect on the previous line.
Okay so setRectTex() takes values from 0 to 1, where 0 is the first pixel on your axis, and 1 is the last.
Say your Minecraft tileset image is 500x500 pixels. It's divided into a grid 100 individual tiles, each tile is 50x50 pixels. Your dirt tile is the first tile in the image, the one in the upper left corner.
You can refer to it like this:
What is this saying? It's saying set the span of texture to use on the rect to go from (0,0) in your texture coordinate space, to (50/500, 50/500) — or (0.1, 0.1). Because 50 pixels across in a 500-pixel-wide texture is 10% (0.1) of the texture.
If you wanted to refer to the second tile in your tileset texture, you could do it like this:
It's starting to look like a pattern. We can now write a generic function to index any of our 100 tiles in this hypothetical Minecraft tileset image:
okay so with you guys' help I've changed my code a bit to do what you showed me.
I have it set to the grass and I just have to change the "3/16" to "2/16" because the dirt texture is the second image out of the 16 images in the first row.
How exactly do I get my code to make dirt and grass rectangles all across the Width of the screen?
btw thanks you guys I appreciate it and hopefully once I'm started off I'll be able to do some of this on my own
Okay, can you guys give me an example of a touch function that works with tables?
I'm not sure I'm explaining my request properly
my v0.1 (without textures for now)
How do I go about making a touch function to "destroy" one of the rectangles if you touch that specific one? On the ENDED touch state
I have written code for this, try =
The following is untested as I don't have my iPad with me.
You need a function to read in the screen position where you have touched, something along the lines of:
Then inside your second for loop (the one one iterating through the columns) check to see if the lastx and lasty values are within the rectangle being created, if they are then set world[r][c]=0 - this will make the rectangle appear as it does in the final "else" part (I assume that's what you mean by destroy)
The if statement might look something like:
Another thread of interest might be this one, which uses sprites
http://twolivesleft.com/Codea/Talk/discussion/comment/11214#Comment_11214
YEEEEESS! to @Jordan!!!!!! Whoo!
Glad to have helped!
Feel free to use it (I have no licenses or policies or what-what, I'm only 13)
:[ Daaaaarn! I need help again. I swear I looked the code over and there shouldn't be a reason as to why it doesn't work when I combined what I learned with the meshes (which render fast enough, btw! Thanks to EVERYONE here). Here's my worldCreate() class
Hey @RichGala1, I'm assuming you have functions setup(), draw(), and touched() somewhere else, right? And this would be much easier if you posted a link to your "minecraft tileset"
Yes to both those things. I'm gonna see where I'll put the tileset real quick
As @Jordan said have you instantiated your class? Something like this in your main class
Yeah don't worry I'm not tht noob anymore
I put the pic on my twitter. Here's the link:
pic.twitter.com/55gRt1rj
Everything shows up but I guess the problem is that the "rect()" function was directly connected to my table so it would easily erase with "nil" But my the mesh only needs to set up it's rectangles and texture stuff once and it's good to go.
"Nil" won't get rid of it because it may take it away the rectangle setup from the table but I don't want to get rid of the setup but that actual part of the mesh.
I suck at explaining myself > <
Changed the code a little. Other than adding new blocks at certain heights and making the framerate go up it isn't any much different.
and so you don't worry
Immediately, somethings I can see that are problematic =
1. The switch over to meshes has rendered my touched() code useless
2. You have a for loop to randomize the blocks, that's useless now too
3. You only have to call mesh.texture once
As I notice more things, I wil add them
You are welcome!
You are welcome!
Deleting the rectangle from the world table isn't enough: you've already copied all of that information across to the mesh so you need to delete it from the mesh. To do that, you need to save the identification numbers returned by the
addRect
method. Then you can usesetRect(id,...)
to effectively remove that rectangle at a later date (you cannot set it to nil, but if you set all the coordinates to0
then it has the same effect). Yourid
table is almost there, but it only saves the id of the last rectangle in the row. I can't see where else you use that, though.Here's what I think would work. You only need one mesh and then use
world
to save the ids so theinit
function looks like:The draw method only needs to call
self.blocks:draw()
. Then the touched function is:(Warning: not tested)
(I see Jordan's had pretty much the same ideas as me!)
But I posted first, so you had the same idea as ME!
But I see our code is pretty much identical, except that you made
self.world = world
andself.blocks = blocks
Possible responses:
If you choose (1), turn to page 45. If you choose (2), turn to page 63.
(Sorry, been reading those "Decide your destiny" books with my kids.)
More seriously, yes. A class should never set or modify global variables: what if you had two instances of the class? So things that the instance "owns" should be of the form
self.whatever
.Sorry to pollute this thread, but @Andrew_Stacey, I am working on a class that interacts with the touched function, modifies it, and returns it. But I have a problem... When I want to remove that class, I also need to restore the touched function, so it doesn't execute its code, how can I do that, without wiping the other instances of the class's changes to touched. My current solution is using a table, but I think that is an ugly answer to my question... Any help from you? (And do you think this is worth a new discussion?
Yes, I do. And put some code in as I have only the vaguest idea what you're doing!
Buttons, lots and lots of buttons. ;-) Imagine the possibilities, buttons, without having to put the pesky myButton:touched! It's the future!
Thanks you guys, it seems your guys could do my project in a day if you wanted to :P
But I do have another question. I'm trying to develop an id system to determine which blocks are which.
Kinda like:
I've tried a few ways with unsuccessful results, I'm sure you guys could help
Hold on, let me try something out....
blockID = "stone"
I've Always wondered how you put pieces of code in like that! Now I know! Lol, okay back to my project
I am thinking (I can't seem to get this to work, I will leave the code to @Andrew_Stacey) that you would have to use an if clause to check if the texture coordinates are the stone co ords
Nevermind. I figured it out. I'm sure you guys could do better but for now I'll use what I gots. Thanks for everything so far!
Code please?
Added
bid
(block id) to the codeThen I put tht in an if clause
pic.twitter.com/LTNZX2sJ
Pic of v0.1
I've embedded your image here:
(You can do this by typing

)thanks
Note = @RichGala1, you must provide the direct link to the image e.g. http://p.twimg.com/A1kZrQWCQAEdfRb.jpg:large
lmao if you had constantly refreshed your page you would've seen me stupidly trying to figure out how to work it, lol. I got it now ^ ^
Soooo, where are you going to go from here? layers? 3D? movement?
I need help again
This time with my character and how to detect if he should fall or not
I've tried making a way to have the game check if there's a block underneath the
self.y
of the character and if it's nil, to change theself.floor
to the top of the block that's underneath.This seems to be much easier to detect if you use sprites but, yeah...
You could try using the physics engine... But that would make EVERYTHING physics-y. (All the blocks...)
Well, @Jordan your pretty smart, so in your opinion would the game work better or make more sense of I used physics?
Just thinking about it, you could use fixed EDGEs on the top of every block, and it wouldn't mess with the gameplay. I will try and write something up for you (BUT YOU TRY AND BEAT ME)
P.S. Character? Hmmm... Any code for my prying eyes?
P.P.S. That applies to @Andrew_Stacey too
Lol, he's just a bunch of meshes put together to resemble the minecraft character for now
He's only colors. Lol, he's under the class, "Joe"
(@RichGala1, are you jailbroken? If not, I have a little surprise for you)
Still, I kinda want to see you code, so I can modify it, and you don't have to modify mine.
If I am then I don't get a surprise?
If you are, do you own minecraft pocket edition? And here is another little surprise for you...
I am jailbroken and I do own MCPE. I'm using their tiles :P (cheater)
Ooh! Code! Checking it now