help with snake game lua
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
help with snake game lua
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?
Re: help with snake game lua
Main logic must be: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?
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
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
Re: help with snake game lua
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.
Re: help with snake game lua
For the snake game will be enough to use grid as: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?
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},
}
Code: Select all
value = grid[y][x] -- note that the y is earlier than x
For the tail of snake you can use the array of positions and extra values for head position and head direction.
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
Re: help with snake game lua
ok
Last edited by uknowntgyallyonski on Thu Mar 07, 2024 12:05 am, edited 1 time in total.
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
Re: help with snake game lua
I will make the changes to it on this code
Re: help with snake game lua
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.
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
Re: help with snake game lua
what do you mean by this?
Re: help with snake game lua
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 208 times
Who is online
Users browsing this forum: No registered users and 5 guests