help with snake game lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

help with snake game lua

Post by uknowntgyallyonski »

I am currently having issues with my code as when i run it and play my snake game, the snake goes through the food without eating it and the score does not go up, anyone willing to help that may have a solution to this?
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: help with snake game lua

Post by darkfrei »

uknowntgyallyonski wrote: Thu Feb 29, 2024 11:31 am I am currently having issues with my code as when i run it and play my snake game, the snake goes through the food without eating it and the score does not go up, anyone willing to help that may have a solution to this?
Main logic must be:

Code: Select all

x, y = getNextPosition (headPosition, headDirection)

if isWall (x, y) then
	eventSnakeDies ()
elseif isSnakeTail (x, y) then
	eventSnakeDies ()
elseif isApple (x, y) then
	eventSnakeGrows ()
else
	eventSnakeMoves ()
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

Re: help with snake game lua

Post by uknowntgyallyonski »

is it also a possible issue with my checkcollision code?
Last edited by uknowntgyallyonski on Thu Mar 07, 2024 12:14 am, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: help with snake game lua

Post by darkfrei »

uknowntgyallyonski wrote: Thu Feb 29, 2024 12:54 pm is it also a possible issue with my checkcollision code? can i send you it and you can let me know if it looks right or the full code if you need context?
For the snake game will be enough to use grid as:

Code: Select all

grid={
{1,1,1,1,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,0,0,0,1},
{1,1,1,1,1},
}
and you can check value in this grid just as:

Code: Select all

value = grid[y][x] -- note that the y is earlier than x
This system is very useful to store all not moving objects, for example nothing (0), wall (1) or apple (2).
For the tail of snake you can use the array of positions and extra values for head position and head direction.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

Re: help with snake game lua

Post by uknowntgyallyonski »

ok
Last edited by uknowntgyallyonski on Thu Mar 07, 2024 12:05 am, edited 1 time in total.
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

Re: help with snake game lua

Post by uknowntgyallyonski »

I will make the changes to it on this code
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: help with snake game lua

Post by darkfrei »

uknowntgyallyonski wrote: Thu Feb 29, 2024 1:15 pm I will make the changes to it on this code
I think that the code must be easier to calculate step as just 1 and just make it bigger in the love.draw() by scaling or multiplying by gridSize.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

Re: help with snake game lua

Post by uknowntgyallyonski »

what do you mean by this?
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: help with snake game lua

Post by darkfrei »

uknowntgyallyonski wrote: Sun Mar 03, 2024 6:57 pm
It's my old code:

Code: Select all

-- License CC0 (Creative Commons license) (c) darkfrei, 2022

UP, RIGHT, DOWN, LEFT = 1, 2, 3, 4
UpdateTime=0.200
Timer = 0
GridSize = 30
GridWidth, GridHeight = 20, 10

local directions = {
	[UP] = 	  {x= 0, y=-1},
	[RIGHT] = {x= 1, y= 0},
	[DOWN] =  {x= 0, y= 1},
	[LEFT] =  {x=-1, y= 0},
}

local function isPositionInBody(x, y)
	for i = 1, #Body-3, 2 do -- skip tail, it moves before we get in
		if x == Body[i] and y == Body[i+1] then
			return true
		end
	end
	return false
end

local function isPositionInApple(x, y)
	if x == Apple.x and y == Apple.y then
		return true
	end
	return false
end

local function newApple ()
	local ApplePlaced = false
	while not ApplePlaced do
		local x = GridSize*math.random (GridWidth)
		local y = GridSize*math.random (GridHeight)
		if not isPositionInBody(x, y) then
			Apple = {x=x, y=y}
			ApplePlaced = true
		end
	end
end

local function newGame ()
	Score = 0
	GameOver = false
	local x = GridSize*math.floor(math.random (0.25*GridWidth, 0.75*GridWidth))
	print (x)
	local y = GridSize*math.floor(math.random (0.25*GridHeight, 0.75*GridHeight))
	print (y)
	local iDirection = math.random(4)
	local d = directions[iDirection]
	Head = {
		x=x,
		y=y,
		iDirection = iDirection,
		nextDirection = iDirection,
	}
	Body = {x, y, x-GridSize*d.x, y-GridSize*d.y}
	Apples = {}
	newApple ()
end

function love.load()
	newGame ()
end

local function moveSnake (x, y, iDirection, longer)
	table.insert (Body, 1, x)
	table.insert (Body, 2, y)
	Head.x = x
	Head.y = y
	Head.iDirection = iDirection
	if not longer then
		-- remove last pair
		table.remove(Body)
		table.remove(Body)
	end
	if  x <= 0 or x > GridSize*(GridWidth) or
		y <= 0 or y > GridSize*(GridHeight) then
		GameOver = true
	end
end
 
function love.update(dt)
	Timer = Timer + dt
	if Timer < UpdateTime then return end
	Timer = Timer - UpdateTime

	local iDirection = Head.nextDirection
	local d = directions[iDirection]
	local x, y = Head.x+GridSize*d.x, Head.y+GridSize*d.y
	if isPositionInBody(x, y) then
		GameOver = true
	elseif isPositionInApple(x, y) then
		Score = Score + 1
		newApple ()
		moveSnake (x, y, iDirection, true)
	else
		moveSnake (x, y, iDirection, false)
	end
end

function drawHead () -- position, length, width and angle
	love.graphics.push()
	love.graphics.translate(Head.x, Head.y)
	love.graphics.rotate((Head.iDirection-2)*math.pi/2)
	love.graphics.polygon("fill", 
		-GridSize/3, -GridSize /3, 
		-GridSize/3,  GridSize /3, 
		 GridSize/3, 0)
	love.graphics.pop() 
end

function love.draw()
	love.graphics.setColor(0,1,0)
	love.graphics.print ('Score: '..tostring(Score), 10, 10)
	
	if GameOver then
		love.graphics.print ('Game Over: '..tostring(GameOver)..'. Press "Space" to continue', 10, 30)
	else
		love.graphics.print ('Press WASD to play', 10, 30)
		love.graphics.translate(GridSize, GridSize)
		love.graphics.setColor(0.6,0.6,0.6)
		love.graphics.setLineWidth(0.25)
		for x = GridSize, GridSize*GridWidth, GridSize do
			love.graphics.line (x, GridSize, x, GridSize*GridHeight)
		end
		for y = GridSize, GridSize*GridHeight, GridSize do
			love.graphics.line (GridSize, y, GridSize*GridWidth, y)
		end
		love.graphics.setLineWidth((GridSize/4)+0.5)
		love.graphics.setColor(1,1,1)
		love.graphics.line (Body)
		drawHead ()
		love.graphics.setColor(1,0,0)
		love.graphics.circle ('fill', Apple.x, Apple.y, GridSize/4)
	end
end

function love.keypressed(key, scancode, isrepeat)
	if false then
	elseif key == "space" then
		if GameOver then
			GameOver = false
			newGame ()
		end
	elseif key == "escape" then
		love.event.quit()
	else
		local iDirection = Head.iDirection
		if iDirection == UP or 
			iDirection == DOWN then
			local right = love.keyboard.isScancodeDown ("d")
			local left = love.keyboard.isScancodeDown ("a")
			if right and not left then
				iDirection = RIGHT
			elseif left and not right then
				iDirection = LEFT
			end
		else -- right or left
			local down = love.keyboard.isScancodeDown ("s")
			local up = love.keyboard.isScancodeDown ("w")
			if up and not down then
				iDirection = UP
			elseif down and not up then
				iDirection = DOWN
			end
		end
		Head.nextDirection = iDirection
	end
end
Attachments
snake-01.love
(1.47 KiB) Downloaded 25 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 72 guests