My attempt at a snake clone. Fail.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Purdy
Prole
Posts: 4
Joined: Sun Aug 14, 2011 2:22 am

My attempt at a snake clone. Fail.

Post by Purdy »

Hello, LÖVErs. I've been creeping around the scenes now for a few weeks, trying to get a good hold on LÖVE and Lua, as well as popping into the IRC channel to get a feel for the community. I'm sort of getting the grasp on the whole thing, or rather I thought I was making good progress haha! Well, this is what I'm working with right now.

I've been working on a snake clone, and I've been doing all of the coding without looking at any others for reference, and it worked great; till it came time to conceptualize how to add more bodies to the original snake after each dot is eaten. I've tried looking at bmzz's snake game but it seriously has me lost in way to many places to count. I just can't grasp what is going on in that source code no matter how many times I look at it. I've been reading through Programming in Lua ( Just got second edition in the mail :) ) and I'm starting to understand how the ipairs function works and stuff like that, it's just that I can't seem to grasp the idea of what I need to do to get this to work.

I've gotten the movement, spawning of the dots, collision detection (Thanks to the HardonCollider), scoring, etc. Really, all I need to finish the first phase of this game is to figure out how to make the trailing bodies. Then I can work on making a menu and other variables, perhaps obstacles and stuff, but I'll cross that bridge when I get to it :)

Below is my example right now. I've included many comments so who ever looks at it has a good idea of where I'm going with this whole thing, or at least what I'm thinking or aiming for.

Thanks in advanced, you guys and the wiki are very helpful!

EDIT - Upload didn't work at first.
Attachments
game.love
Re-uploaded...
(2.92 KiB) Downloaded 186 times
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: My attempt at a snake clone. Fail.

Post by Rad3k »

I think it would be much easier to do with a grid of sorts. E.g. represent your snake as an array of dots on this grid, and each time the snake moves, add one dot to its head in the direction of movement, and remove the last dot of the tail. If the snake eats something, you just don't remove the dot from its tail. To draw the snake, you just draw every dot in this array.That would probably be the simplest way of doing it, and I'm pretty confident that it was just like this in most of the snake games.

Besides, why are you using such a cryptic way of representing directions (I know, clock dial numbers)? You can just use strings like "left" of "down".
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: My attempt at a snake clone. Fail.

Post by miko »

Purdy wrote:Really, all I need to finish the first phase of this game is to figure out how to make the trailing bodies. Then I can work on making a menu and other variables, perhaps obstacles and stuff, but I'll cross that bridge when I get to it :)
Yes, you seem to have the concept mixed up. From the following code:

Code: Select all

      for i,v in ipairs(snake.l) do
        v.tx = snake.x
        v.ty = snake.y
      end
I assume that for every single body segment you change its position to be equal the snake's head position, which is wrong, because all the segments gets to the same position.

What you need to do is to:
- append the head of the snake as a body element (with it's x,y position) to the body table (snake.l)
- if the snake.l length is greater than the current snake lenght, remove the last element
- once the element is added to the table, you dont mess with it (don't change its position - it is "static"), you can only remove it from the table
- when drawing the snake, draw all its body segments at their x,y positions

So basicaly the body of the snake (a list of elements) is a FIFO table, with its length increasing over time.

EDIT: and please name your attached file file like "snake-purdy.love" or so, because "game.love" is so generic...
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Purdy
Prole
Posts: 4
Joined: Sun Aug 14, 2011 2:22 am

Re: My attempt at a snake clone. Fail.

Post by Purdy »

Rad3k wrote:I think it would be much easier to do with a grid of sorts. E.g. represent your snake as an array of dots on this grid, and each time the snake moves, add one dot to its head in the direction of movement, and remove the last dot of the tail. If the snake eats something, you just don't remove the dot from its tail. To draw the snake, you just draw every dot in this array.That would probably be the simplest way of doing it, and I'm pretty confident that it was just like this in most of the snake games.
Hmm... That seems like a pretty legit way of doing things, I will definitely look into doing it like this. I'm not 100% sure how to do this, but I'm gonna think it over and hopefully come up with something. The more I think of it, the more it makes sense!
Rad3k wrote:Besides, why are you using such a cryptic way of representing directions (I know, clock dial numbers)? You can just use strings like "left" of "down".
Yeah, I know; I just figured it would be less typing, ya know?

And thanks for the quick reply!

On a side note: I wanna get some other people's take on this, so please keep the great ideas coming!
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: My attempt at a snake clone. Fail.

Post by GijsB »

this is my nice looking snake game thingy, i still need to ad blocks that you can eat but snake works good :3
maybe you can learn from it and get some ideas :D

Edit : SORRY PEOPLE I UPLOADED THE WRONG VERSION WICH HAS NO QUIT BUTTON, DO NOT DOWNLOAD
Edit : Oke i fixed it press 'escape' or 'q' to quit
Attachments
Snake.love
(966 Bytes) Downloaded 159 times
Last edited by GijsB on Wed Sep 14, 2011 3:54 pm, edited 2 times in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: My attempt at a snake clone. Fail.

Post by Robin »

GijsB wrote:this is my nice looking snake game thingy, i still need to ad blocks that you can eat but snake works good :3
maybe you can learn from it and get some ideas :D
That is just horrible. Everyone, do not run that. It sets itself to fullscreen, which should be punishable by death in the first place, and it has no way to quit, which means I had to break out to the shell and killall love, leaving my desktop in a messed up state.
Help us help you: attach a .love.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: My attempt at a snake clone. Fail.

Post by GijsB »

Robin wrote:
GijsB wrote:this is my nice looking snake game thingy, i still need to ad blocks that you can eat but snake works good :3
maybe you can learn from it and get some ideas :D
That is just horrible. Everyone, do not run that. It sets itself to fullscreen, which should be punishable by death in the first place, and it has no way to quit, which means I had to break out to the shell and killall love, leaving my desktop in a messed up state.
OH NO i uploaded the wrong version wich has no quit button 3:!!
User avatar
The Doctor
Prole
Posts: 3
Joined: Thu Sep 08, 2011 6:00 pm

Re: My attempt at a snake clone. Fail.

Post by The Doctor »

GijsB wrote:this is my nice looking snake game thingy, i still need to ad blocks that you can eat but snake works good :3
maybe you can learn from it and get some ideas :D

Edit : SORRY PEOPLE I UPLOADED THE WRONG VERSION WICH HAS NO QUIT BUTTON, DO NOT DOWNLOAD
Edit : Oke i fixed it press 'escape' or 'q' to quit
Woah! The circeling/gravity mode was really addicting! It feels as I've been hypnotized for 1 hour!
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: My attempt at a snake clone. Fail.

Post by GijsB »

The Doctor wrote:
GijsB wrote:this is my nice looking snake game thingy, i still need to ad blocks that you can eat but snake works good :3
maybe you can learn from it and get some ideas :D

Edit : SORRY PEOPLE I UPLOADED THE WRONG VERSION WICH HAS NO QUIT BUTTON, DO NOT DOWNLOAD
Edit : Oke i fixed it press 'escape' or 'q' to quit
Woah! The circeling/gravity mode was really addicting! It feels as I've been hypnotized for 1 hour!
thanks :D
Purdy
Prole
Posts: 4
Joined: Sun Aug 14, 2011 2:22 am

Re: My attempt at a snake clone. Fail.

Post by Purdy »

GijsB wrote:this is my nice looking snake game thingy, i still need to ad blocks that you can eat but snake works good :3
maybe you can learn from it and get some ideas :D

Edit : SORRY PEOPLE I UPLOADED THE WRONG VERSION WICH HAS NO QUIT BUTTON, DO NOT DOWNLOAD
Edit : Oke i fixed it press 'escape' or 'q' to quit
Sadly I couldn't get it to run for some reason... I dunno, it just opened up love and exited. That's really besides the point though, I've been looking at the code and I think I'm starting to get a good picture on what needs to be done. I'm gonna have to get back to you all with my progress!

Thanks for the help everyone!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests