The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## Shine's LUA Snake Game

This is a discussion on Shine's LUA Snake Game within the Developer's Dungeon forums, part of the PSP Development Forum category; Hi Guys, I asked this on ps2dev.org LUA section as well, but I don't know who goes there now. I'm ...

 Tweet
1. ## Shine's LUA Snake Game

Hi Guys,
I asked this on ps2dev.org LUA section as well, but I don't know who goes there now.
I'm trying to understand, and recode the way Shine's Snake game works.
I have a rough idea how to follow it, but have some questions.

How is it determined where the last segment of the snake is (the tail segment that is removed)?

Also, how is the length of the snake kept track of?

Cheers, Art.

2. Here's the source:
Code:
```--[[
artworks by Gundrosen
coding by Shine
]]

snake = createImage(480, 272)

-- define tile positions
tailR = { x = 0, y = 0 }
tailT = { x = 1, y = 0 }
tailL = { x = 2, y = 0 }
tailB = { x = 3, y = 0 }

headR = { x = 0, y = 1 }
headT = { x = 1, y = 1 }
headL = { x = 2, y = 1 }
headB = { x = 3, y = 1 }

bodyLT = { x = 0, y = 2 }
bodyLB = { x = 1, y = 2 }
bodyRT = { x = 2, y = 2 }
bodyRB = { x = 3, y = 2 }
bodyLR = { x = 4, y = 2 }
bodyTB = { x = 5, y = 2 }

apple = { x = 0, y = 3 }
fly = { x = 1, y = 3 }

-- define globals
tails = {}
bodies = {}

RIGHT = 0
TOP = 1
LEFT = 2
BOTTOM = 3
shouldExit = false

tails[RIGHT] = tailR
tails[TOP] = tailT
tails[LEFT] = tailL
tails[BOTTOM] = tailB

bodies[RIGHT] = bodyLR
bodies[TOP] = bodyTB
bodies[LEFT] = bodyLR
bodies[BOTTOM] = bodyTB

score = 0
high = 0

target = {}
targetImage = 0

dx = 0
dy = 0
cellTail = {}

function createRandomTarget()
target.x = math.random(21)
target.y = math.random(15)
if math.random(2) == 1 then
targetImage = apple
else
targetImage = fly
end
end

function clearTileInSnakeImage(x, y)
fillRect(0, 16*x, 16*y, 16, 16, snake)
end

function drawTileToOffscreen(tile, x, y)
blitAlphaImageRect(16*tile.x, 16*tile.y, 16, 16, tiles, 16*x, 16*y);
end

function drawTileToSnakeImage(tile, x, y)
blitAlphaImageRect(16*tile.x, 16*tile.y, 16, 16, tiles, 16*x, 16*y, snake);
end

function newGame()
clear(0, snake)
dx = 0
dy = 0
x = 12
y = 8
cellTail.x = x
cellTail.y = y
cellTail.direction = RIGHT
x = x+1
createRandomTarget()
drawTileToSnakeImage(tailR, cellTail.x, cellTail.y)

score = 0
end

function keyboardControl()
waitVblankStart();
dx = 0
dy = -1
dx = 0
dy = 1
dx = -1
dy = 0
dx = 1
dy = 0
shouldExit = true
-- screenshot()
end
end

function move()
-- do nothing, if no movement vector defined
if dx == 0 and dy == 0 then
return
end

-- save last head direction and position

-- set direction for new head
direction = 0
if dx == 1 then
direction = RIGHT
elseif dy == -1 then
direction = TOP
elseif dx == -1 then
direction = LEFT
elseif dy == 1 then
direction = BOTTOM
end

-- check which body to draw at the place of the old head
clearTileInSnakeImage(lastX, lastY)
if lastDirection == RIGHT and direction == TOP then
drawTileToSnakeImage(bodyLT, lastX, lastY)
elseif lastDirection == TOP and direction == LEFT then
drawTileToSnakeImage(bodyLB, lastX, lastY)
elseif lastDirection == LEFT and direction == BOTTOM then
drawTileToSnakeImage(bodyRB, lastX, lastY)
elseif lastDirection == BOTTOM and direction == RIGHT then
drawTileToSnakeImage(bodyRT, lastX, lastY)
elseif lastDirection == RIGHT and direction == BOTTOM then
drawTileToSnakeImage(bodyLB, lastX, lastY)
elseif lastDirection == TOP and direction == RIGHT then
drawTileToSnakeImage(bodyRB, lastX, lastY)
elseif lastDirection == LEFT and direction == TOP then
drawTileToSnakeImage(bodyRT, lastX, lastY)
elseif lastDirection == BOTTOM and direction == LEFT then
drawTileToSnakeImage(bodyLT, lastX, lastY)
else
drawTileToSnakeImage(bodies[direction], lastX, lastY)
end

-- check for target
createRandomTarget()
score = score + 1
else
-- remove tail
clearTileInSnakeImage(cellTail.x, cellTail.y)
cellTail = cellTail.next

-- draw new tail image
if cellTail.direction ~= cellTail.next.direction then
cellTail.direction = cellTail.next.direction
end
clearTileInSnakeImage(cellTail.x, cellTail.y)
drawTileToSnakeImage(tails[cellTail.direction], cellTail.x, cellTail.y)
end
end

function isGameOver()
gameOver = false
if lastX >= 22 then
gameOver = true
end
if lastX < 1 then
gameOver = true
end
if lastY >= 16 then
gameOver = true
end
if lastY < 1 then
gameOver = true
end
cell = cellTail
if cell.x == lastX and cell.y == lastY then
gameOver = true
break
end
cell = cell.next
end
return gameOver
end

-- init
math.randomseed(os.time())
newGame()

-- game loop
while true do
for i=0,4 do
keyboardControl()
end
move()
blitImage(0, 0, background)
blitAlphaImage(0, 0, snake)
drawTileToOffscreen(targetImage, target.x, target.y)
if score > high then
high = score
end
printDecimal(410, 81, score, 0);
printDecimal(429, 129, high, 0);
waitVblankStart()
flipScreen()

if(shouldExit) then
break
end
if isGameOver() then
waitVblankStart(50)
newGame()
end
end```

3. IIRC, shine's snake is a tile based one(but it's been a while so maybe it was free movement), basically this is how i'd go about making it:

create an array of say 30 of an struct to keep track of each body part, the struct well define x/y tile it was on, and the dir it faces, so when the head moves, you loop through the array from the end to the beginning, set each one to the one ahead of it, then when u get to the first body piece, simply move it behind the head

honestly, the struct could probably define just the tile, but if you add dir u can create images to represent the dir, but if u do just blocks or something like that then you probably don't need to track dir, just x/y tile

anywho, that's how i'd do it

4. I'd agree by now... I don't want graphics so I just need an x array, and y array, and be able to rotate
both arrays so new values are shifted in... I think...then just delete the tail from the end of the arrays.

5. Got it:
YouTube - 144 LED Display - Snake Game

Might look out of place here, but I did learn how to code it from a PSP LUA Player game!

6. Originally Posted by Art
Got it:
YouTube - 144 LED Display - Snake Game

Might look out of place here, but I did learn how to code it from a PSP LUA Player game!

haha, that is very epic art, i didn't realize this wasn't for the psp, but the concept is the same=-), excellent work

7. How about a Snake game for PSP that allows you to move at any angle
rather than just one of four angles? :)
I think it would be cool.. I'll do it if I get back into PSP programming again.

8. Originally Posted by Art
How about a Snake game for PSP that allows you to move at any angle
rather than just one of four angles? :)
I think it would be cool.. I'll do it if I get back into PSP programming again.
i, and mr[Ice] have done so(mine was an adhoc version inspired by his)=-), however if you believe you can top mr[Ice]'s, i'd love to play it=-)

9. Oh, I'm only interested if it hasn't been done before.
I'll have a look for the game.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 12:15 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY