First game help with logic

I will be away from computer until tomorrow, but I will write you a up a quick Check function that can check matches and if it can swipe down/left etc. which should give you an idea of how to move forward :slight_smile:

Thanks m8 for helping. I think the first game you make is the most important game to understand it all.

There are 2 scenarios I can come up with but I don’t know if corona supports that.

First one:
In the grid make a variable with the name for the sprite. Make a draw function that checks the whole grid and draws all the sprites.(loop).

Second one:
Moving the display elements in the arrays but I don’t know if this is possible I don’t understand how you can move a display that is already in a element in a array. Every time i get into the gridObj directly and move the x nothing happens. And to check if there is a match I need to move the elements in the array.

I tried allot but i dont think this can work…you cant know which icon is where to match i really think i have to use the grid and shift the objects with every swipe and draw the animation.

function scene:AddMiddleIcons() &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local tmpIcons = {"icon\_1", "icon\_2", "icon\_3", "icon\_4", "icon\_5"} &nbsp; &nbsp; local speed = 500 &nbsp; &nbsp; for columnX=1,7 do &nbsp; &nbsp; local gridObj = self.topGrid[columnX][6].gridObj &nbsp; &nbsp; print(gridObj.x) &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local iconObj = display.newImage(self.sceneGroup, self.gameArtSheet , self.gameArtInfo:getFrameIndex(tmpIcons[math.random(1, 5)])) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.topGrid[columnX][6].gridObj = iconObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iconObj.anchorX, iconObj.anchorY = 0.5, 0.5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iconObj.x = gridObj.x + (gridObj.width \* .5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iconObj.y = (gridObj.y + (gridObj.height \* .5)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.topGrid[columnX][6].gridObj = iconObj &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iconObj.xScale,iconObj.yScale = 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iconObj.alpha = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transition.to(iconObj, {transition = easing.outBounce, time=speed,alpha = 1,xScale = 1,yScale = 1, onComplete=function(iObj) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.topGrid[columnX][6].iconObj = iObj &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.topGrid[columnX][6].active = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speed = speed + 100 &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local mContainer = display.newRect((display.contentWidth \* .5),(self.topBG.y + self.topBG.height), 749, 100) &nbsp;&nbsp;&nbsp;&nbsp;mContainer.anchorX, mContainer.anchorY = 0.5, 0 &nbsp;&nbsp;&nbsp;&nbsp;mContainer:setFillColor(0,0,0,0.01) mContainer.touch = function(gObj, gEvent) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local swipeLengthX = math.abs(gEvent.x - gEvent.xStart)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local swipeLengthY = math.abs(gEvent.y - gEvent.yStart)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local t = gEvent.target &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local phase = gEvent.phase &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if "began" == phase then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif "ended" == phase or "cancelled" == phase then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if gEvent.xStart \> gEvent.x and swipeLengthX \> 10 then&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("swipe left") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shift(self.topGrid,"left") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for columnX=1,#self.topGrid do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local tmpObj = self.topGrid[columnX][6].gridObj &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tmpObj.x \< 85 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpObj.x = 742 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;transition.to(tmpObj, {transition = easing.outBack, time = 200, &nbsp;x = 722, onComplete = function(iObj) end}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- table.insert(self.topGrid[columnX - 1],6,tmpObj) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- table.remove(self.topGrid[columnX - 1],6) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(tmpObj, {transition = easing.outBack, time = 200, &nbsp;x = tmpObj.x - 107, onComplete = function(iObj)end}) &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif gEvent.xStart \< gEvent.x and swipeLengthX \> 10 then&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("swipe right") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for columnX=1,#self.topGrid do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local tmpObj = self.topGrid[columnX][6].gridObj&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if tmpObj.x \> 710 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpObj.x = 60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(tmpObj, {transition = easing.outBack, time = 200, &nbsp;x = 80, onComplete = function(iObj) end}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(tmpObj, {transition = easing.outBack, time = 200, &nbsp;x = tmpObj.x + 107, onComplete = function(iObj)end}) &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif gEvent.yStart \< gEvent.y and swipeLengthY \> 10 then&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("swipe down") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for columnY=1,11 do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local tmpObj = self.topGrid[1][columnY].gridObj&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(tmpObj, {transition = easing.outBack, time = 200, &nbsp;y = tmpObj.y + 100, onComplete = function(iObj) end}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;mContainer:addEventListener("touch")&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mContainer:toFront() end &nbsp;

Pretty sure it will work as below is one of the very first games I made using corona and it was made in under a week using the same type of logic and it isn’t very polished and was more of a proof of concept for something else I was working on.

Google Play

https://play.google.com/store/apps/details?id=com.purplebean.uguessit

iTunes

https://itunes.apple.com/us/app/u-guess-it/id635813137?mt=8

It plays much better on iPad as I had no idea what was what on Google Play at the time :slight_smile:

Anyhoot, I am going to piece together some stuff for you just got really swamped next few days then I will be getting back to it :slight_smile:

I tried everything to get it into a table and move the grid but lua doesnt have allot of choice like other languages. you cant even store a 2d array simple, its very weird lol.

But what i wanted to say is what was your idea to do a check if the same icons…can you explain it…?

Hey Christopher I got a long way with your help. I am moving the grids and so on but I have a question.

When the icons enter the field with a transition to. Sometimes I am moving that column up or down but the transition to is already set to go to the y that was empty at the moment it moved but after I moved the column up a icon is shifted to it. Now the icons are on top of each other and one does not exist in the table (it’s overwritten)

How can such a problem be solved if you know what I mean ?

Without knowing more it sounds like you need an array that contains both x/y for example.

local grid = {} for x = 1, 5 do --Lets say 5 rows across grid[x] = {} --Create empty table for columns. for y = 1, 5 do --Lets say 5 columns down. grid[x][y] = {iconKey = "fred", pushed = false, center = false, iconObject = someImageObj} --fill slot with info end end

So if you add your icon ā€œimageā€ to the iconObject = of the table you will now have access to it by simply saying

grid[2][1].iconObject -> this would be the second row and the first column icon.

as you move stuff around you can simply either destroy the iconObject or swap it using that object in the correct position.

Matching would be simple too, just say something along the lines of…

if (grid[1][1].iconKey == grid[1][2].iconKey and grid[1][2].iconKey == grid[1][3].iconKey) then --its a match, continue checking more or return true/false end

Granted above is pretty simplistic but gives you the general idea.

[quote=ā€œChristopher_Bishop,post:27,topic:323818ā€]

Without knowing more it sounds like you need an array that contains both x/y for example.

local grid = {} for x = 1, 5 do --Lets say 5 rows across grid[x] = {} --Create empty table for columns. for y = 1, 5 do --Lets say 5 columns down. grid[x][y] = {iconKey = "fred", pushed = false, center = false, iconObject = someImageObj} --fill slot with info end end

So if you add your icon ā€œimageā€ to the iconObject = of the table you will now have access to it by simply saying

grid[2][1].iconObject -> this would be the second row and the first column icon.

as you move stuff around you can simply either destroy the iconObject or swap it using that object in the correct position.

Matching would be simple too, just say something along the lines of…

if (grid[1][1].iconKey == grid[1][2].iconKey and grid[1][2].iconKey == grid[1][3].iconKey) then --its a match, continue checking more or return true/false end

Granted above is pretty simplistic but gives you the general idea. [/quote]
So in essence you say do all in one 2d array instead of one array for where the matching happens and one for the icons top or down ?

Btw this is the game I want to make… I am not going to clone it but I thought its a good first game…

http://cj-pixel.com/move-your-marbles/

Just downloaded the game to check it out however it makes no sense to me a slid the middle row left and right but nothing happen lol…

But given that yes, that is how I would do it it looks like there is 7 rows and 9 columns and the ones sliding in from the top and bottom are simple transitions.to so for example build your grid layout and inside the table do something like if (y == 5) then isCenter = true end

then it is a simple if y <= 4 then it is the top row if y >5 then it is the bottom row.

if you expand this out a little bit and do a newRect at each grid location with a 0 or alpha fill 0 then when you are dropping from the top or shooting up from the bottom you simply say transition.to(newBallIMage, {y = grid[1][4].y}) etc. and so forth.

But i am still half a sleep :slight_smile:

Haha I wonder if game design logic is always so hard. I can’t wrap my head around something that looks so easy.

So this is the idea:
Make a function that generates a grid for all columns and rows. Making sure that each object has a place for a object name, x and y.

Make a function that pushes some random objects into the generated grid (updating the object Name and x and y)

Make a function that goes through the grid and draws all ? Is this a good idea or should I fill the table with display objects instead of names ?

Am I going the right direction so far?

I won’t be back at computer until tomorrow but I think I have most of the functions you are looking for on my old dev box that I could shoot up here for ya to get you a head start :slight_smile:

Wow I am curious :slight_smile: just want to finish a game from beginning to end so I can understand the logic and process much better. In tutorials you say easy and oke I can do that. But the minute you have something you can’t figure it out lol

No worries, it just takes a bit to kind of figure out how the flow works and if you are coming from C#, PHP you should be able to grasp it pretty quick you just need a basic this is how the flow works etc. and so forth… I found some code on one of my backups that will help you out just need to put it all together into one file and comment it so you can see it… 

Once you see it I am pretty sure the light will go off and you will just get it :slight_smile:

I have to finish putting up my fence (weekend project) so more than likely wont be able to put it all together until tonight or tomorrow am :slight_smile:

Hey khawtf, sorry it took me so long ended up just writing it from scratch as the old code i had was all G1.0 stuff and thought it would be easier just to write it up and had some other things going on so was doing it in between :slight_smile:

Ok couple things.

1: If you are like me, it might be better to understand the code by just removing the comments lol.

2: I did this in under 3 hours so it is kind of ugly but you will get the idea.

3: Didn’t put a whole lot of game logic in because to be honest i have no idea how that game works.

4: The swipe for the middle row is ā€œreal basicā€ and not doing any loading from left right.

5: And of course do whatever you want with it :slight_smile:

I tried to put a few different things in just so you could get a head start on how things all come together so I ended up just making main.lua just that a main entry point and then used composer so you could see how scene stuff is put together.

Screenshot is here:Ā  example.png

Link to zip file is here:Ā https://s3.amazonaws.com/mclss/Grid.zip

Enjoy and let me know if you need help with anything else :slight_smile:

Wow this is awsome!!! Let me dive in the code.
I really appreciate this !

No problem, just keep in mind some of it isĀ spaghetti code as i was distracted and sleep deprived lol

I can follow what you did very good. Really like it. I have a question about the grids. So you divided it up into 3 different grids. Isn’t that a bit harder to move a column down into the middle field ?
Then I would have to check where I swiped up or down and shift the icon that is moved up into the column and the one that comes from beneath into the middle column… Is that the best way to do this ?

Yeah, I split it up because I really didn’t understand the game and given that I didn’t understand the game I made 3 different functions so if need be you could knock it down to one function and just increase the row/column in the for x and for y :slight_smile:

So what you could do is remove the Middle and Bottom functions (move the bottom rect up to the build top function) then just increase the x/y values in the for loop and modify the drop function to check for < 6 = Top and > 6 = Bottom and 6 = Middle :slight_smile:

Make sense?

If I had a better idea of how the game should actually work i could modify it fairly quickly, it sounds kind of like candy crush with a twist of a sliding middle piece?

Hehe its rather simple…the icons drop from the top and bottom and you can move the columns up and down you have to match them in the middle…its rather simple game :slight_smile:

So basicly i have to change the first function and add to the y and remove the functions for middle and down. Change the function for swiping to move only the middle.

Right ?

having a hard time changing the function to 1. I will try it from the bottom up

Flip the showDebug at the top to true so you can see the grid lines being draw, I will have a look at it in morning as you should be able to comment out Middle, Bottom and just increase the y in the top function to for y = 1, 11 (as 11 would be the new value)Ā