Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Snake game for download
Started by thomas6 Nov 29 2019 05:31 AM

- - - - -
2 replies to this topic
snake example free game download 90s nokia
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 982 posts
  • Corona SDK

Hi all,


I made a snake game for a client last week and thought I'd share the code. Here's a WeTransfer download link:




For those who want to tweak: there is a parameter called self.gridSize in gameBoardClass that you can set to 60 or 40 or whatever, to increase the number of tiles.


There is one minor bug: sometimes the goal orb spawn inside the snake. There is code to prevent this but somewhere I made a mistake so it doens't fully work, but since this was a very small project the bug is still there.




[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 674 posts
  • Corona SDK

thanks for the game :)


Your approach to create the "food" to catch, it's:

1. create a table with 0 in all positions possibles

2. insert 1 in the position where the snake is.

3. create a new table with only the possibilities where the "food" can be created.

4. create a random number from 1 to the limit of the new table.

5. 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:

1. create a single dimension table with all  possible positions. 

2. remove from that table any position where the snake parts are.

3. create a random number 1, #table

4. 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.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.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)
		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.

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 982 posts
  • Corona SDK

Hi Carlos! You're welcome, and thanks! This is a work thing so I'll have to wait until monday to try it out but I'm happy with any improvement!


You're right in your view of my method. I still don't know why it doesn't work, but hey, if your method works then that's perfect!


Thanks and have a good weekend!



Also tagged with one or more of these keywords: snake, example, free, game, download, 90s, nokia