thanks for the game
Your approach to create the “food” to catch, it’s:
-
create a table with 0 in all positions possibles
-
insert 1 in the position where the snake is.
-
create a new table with only the possibilities where the “food” can be created.
-
create a random number from 1 to the limit of the new table.
-
get the x, y from the new table at the random position.
it should work. I didn’t catch the bug, so my only solution was to remake the function.
If you don’t mind I remade the goalClass.lua, to try to correct the bug:
My approach was:
-
create a single dimension table with all possible positions.
-
remove from that table any position where the snake parts are.
-
create a random number 1, #table
-
get x, y from the same table at random position.
it should work same as yours. if it will still have the problem is not in this class, but the code that calls it. (didn’t gone there)
------------------- -- goalClass.lua -- ------------------- print("goalClass started") local goalImageSheetOptions = { --required parameters width = 128, height = 128, numFrames = 16, } local goalImageSheet = graphics.newImageSheet("snakeSegments6.png", goalImageSheetOptions ) local goalSequenceData = {name="basic", start=1, count=16} local goalClass = {} goalClass.new = function(super) local self = {} self.super = super -- super is of type appEngine self.mainGroup = display.newGroup() self.xPosition = false self.yPosition = false self.goalImage = display.newSprite(self.mainGroup, goalImageSheet, goalSequenceData) self.goalImage:setFrame(15) self.goalImage.anchorX = 1 self.goalImage.anchorY = 1 self.segmentScaling = self.super.gameBoard.gridSize/128 self.goalImage.xScale = self.segmentScaling self.goalImage.yScale = self.segmentScaling -- insert own mainGroup into super's mainGroup self.super.mainGroup:insert(self.mainGroup) self.spawnGoal = function() print("spawning Goal") -- make an empty array of all grids local possibleGrid = {} local horizontalNumberOfTiles = (self.super.gameBoard.totalWidth / self.super.gameBoard.gridSize) local verticalNumberOfTiles = (self.super.gameBoard.totalHeight / self.super.gameBoard.gridSize) -- fill array with zeroes! for y = 1, verticalNumberOfTiles do for x = 1, horizontalNumberOfTiles do possibleGrid[#possibleGrid+1] = {y,x} -- 0 means there is no snake segment on this tile! end -- for x loop end -- for y loop print("------ getting snake coordinates") print ("#possibleGrid:") print (#possibleGrid) -- for each segment in the snake, fill array position in grid with a 1! for i = 1, #self.super.snake.segmentList do local baseXposition = self.super.snake.segmentList[i].xPosition local baseYposition = self.super.snake.segmentList[i].yPosition --print("segments at: x"..baseXposition.." y"..baseYposition) -- temporaryGrid[baseYposition][baseXposition] = 1 for j=#possibleGrid, 1, -1 do local yx=possibleGrid[j] if yx[1]==baseYposition and yx[2]==baseXposition then print ("\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_") print ("remove object at: "..j) print (baseYposition, baseXposition) print ("\_\_\_\_") table.remove(possibleGrid, j) break; end end end -- for segmentList Loop -- get a random coordinatePair print ("#possibleGrid") print (#possibleGrid) local randomNumber = math.random(#possibleGrid) print (randomNumber) local coordinatePairForGoal = possibleGrid[randomNumber] print (coordinatePairForGoal[1]) print (coordinatePairForGoal[2]) self.yPosition = coordinatePairForGoal[1] self.xPosition = coordinatePairForGoal[2] self.mainGroup.x = self.xPosition \* self.super.gameBoard.gridSize self.mainGroup.y = self.yPosition \* self.super.gameBoard.gridSize end -- self.spawnGoal() return self end -- goalClass.new() -- -- return goalClass
Didn’t happen the bug the few times I tried.