Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cervantes' Snake
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cervantes




PostPosted: Thu Jun 02, 2005 9:46 pm   Post subject: Cervantes' Snake

Since it seems everyone has been making snake games lately, I decided to post mine. I made the basis for it a while ago, when I was just learning about classes. I so desperately wanted to make my snake into a class, so I moved heaven and earth to do so. Today, when I decided to clean this up and posted, I took out the snake class (because it just made things harder. There's no need for it, especially since there's only one snake Wink). Thus, there's probably a few redundancies in the code.

Anyways, to the point. The main feature about this program(me) is that the control of the snake is, if I do say so myself, very good. What I've done is, instead of delaying in the main loop, I go through a second loop (inside the main loop - it's nested) that only exists after a specified period of time has passed. This is essentially, a delay. However, it also serves to collect keys that are pressed into a buffer, a flexible array of strings. Each time through the main loop, the first element of this flexible array is applied to the direction (assuming there is at least one element) and everything is shifted along one spot.
Essentially, what all this means, is that the program(me) not only registers your input while delaying, it stores a buffer of all your inputs, so no key is ever missed. (Actually, it will miss some keys if you hold 3 or more keys at once. This is not a limitation, but a help. Holding three keys at once could very likely kill you!) This means that you can make really tight turns. Say you are moving right, just tap up-left (it's more of a roll of the hand then moving the fingers) and you will make as tight a 180° turn as possible (without killing yourself, that is).

So, without further ado:



Snake.zip
 Description:
Source code + 9 Level Files

Download
 Filename:  Snake.zip
 Filesize:  3.81 KB
 Downloaded:  412 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Thu Jun 02, 2005 10:39 pm   Post subject: (No subject)

Very nice, its a toss up between zylums and yours Smile.

The 'buffer' thing is pretty interesting, adds some challenge to the movement too. The levels are something that is missing all too often - it reminds me of the 'Nibbles' game that I played with Windows 3.1.

Good game, and fairly challenging too. Only thing I suggest improving is adding 'lives', reseting, and possibly a high score list (you already have the level files).
I got to level 3 on my second try.
Bacchus




PostPosted: Fri Jun 03, 2005 5:40 am   Post subject: (No subject)

It's one of the better ones I've seen, but I still prefer Zylum's to your. When I tried to make really tight turns, say I was moving up, I would press right and then down. It would move the snake right then down then right again and then down again. This is probably caused by the buffer because even though I'm just pressing the keys once, it goes through the loop and gets the key again before I even have time to lift a finger. lol. Very good though.

P.S. The movement also seemed a bit delayed.
Cervantes




PostPosted: Fri Jun 03, 2005 3:06 pm   Post subject: (No subject)

Bacchus wrote:

When I tried to make really tight turns, say I was moving up, I would press right and then down. It would move the snake right then down then right again and then down again.

In a standard program, you would not move on a diagonal because there is only one stored direction at any given time. If you hold several buttons, the direction is specified by the structure of the code.
code:

if up arrow then
..
elsif down arrow then
..
end if

If this were the case, holding both the up arrow and the down arrow at the same time would ensure the snake travels up.
code:

if up arrow then
..
end if
if down arrow then
..
end if

This would ensure the snake is travelling down.
So, if you hold the arrows for longer than the delay (75 milliseconds) in a normal game only one direction will register. This allows you to move on a diagonal (Up, left, up, left, etc.), but this will only happen if you hold the keys for longer than 75 milliseconds. pressing the up arrow and then the left arrow all under 75 milliseconds will, in most programs, result in only one turn: up. In this, however, both inputs will be stored and executed.

So basically, the buffer does not prevent results of holding keys for too long. If you do that, you will go on a diagonal (which is actually better than most snake games in which you would go in a certain direction specified by the order of the if .. elsif .. end if structure, or possibly the if .. end if ; if .. end if structure). The whole point of the buffer is to allow rapid input to be registered.

Bacchus wrote:

P.S. The movement also seemed a bit delayed.

Movement? Or turning? Either way, I don't experience it. Personally, I find that I can, 80% of the time, turn at the right spot to get in line with the apple, much better with other snake games that I've played.
Neo




PostPosted: Fri Jun 03, 2005 7:36 pm   Post subject: (No subject)

Level 9 is a bit unfair dont you think? The player has less than 1 second to move either left or right or else they are steered into a wall... Anyways very nice game.
Cervantes




PostPosted: Fri Jun 03, 2005 7:47 pm   Post subject: (No subject)

Neo wrote:
Level 9 is a bit unfair dont you think?

Could be; I haven't got there. Laughing
The levels were incredibly rushed.
RaPsCaLLioN




PostPosted: Fri Jun 03, 2005 9:28 pm   Post subject: (No subject)

Occasionally when I make fast turns the snake will veer off in another direction. Ex. I'm moving down - I'll hit Left-Up and the snake will follow through and turn right.
Cervantes




PostPosted: Sat Jun 04, 2005 6:52 am   Post subject: (No subject)

RaPsCaLLioN wrote:
Occasionally when I make fast turns the snake will veer off in another direction. Ex. I'm moving down - I'll hit Left-Up and the snake will follow through and turn right.

You turned right? And all you pressed were up and left? Oh my, that's a big problem! Laughing
I understand you, travelling down, then turning left-up-left: you would be beginning the diagonal path, because you've held the arrows down too long. But turning right is a different story altogether. Can you verify this?
Sponsor
Sponsor
Sponsor
sponsor
RaPsCaLLioN




PostPosted: Sun Jun 05, 2005 1:52 pm   Post subject: (No subject)

Well Cervantes nothing is wrong with your program. I figured out I had some keyboard problems. My up arrow was sending right signals (I realized this when I was playing JK:JA and I kept spinning in circles). I've replaced keyboard and ur prog works great!
MysticVegeta




PostPosted: Mon Jun 06, 2005 6:29 pm   Post subject: (No subject)

I had one problem though, I was in level 3 when the food spawned in side the wall!!!!!!!!!!!!!!!!!! Ofcourse i got killed Mad
Cervantes




PostPosted: Mon Jun 06, 2005 8:46 pm   Post subject: (No subject)

MysticVegeta wrote:
I had one problem though, I was in level 3 when the food spawned in side the wall!!!!!!!!!!!!!!!!!! Ofcourse i got killed Mad

Oh? I don't know how that's possible. Thinking Did it spawn in the outer wall, or one of the middle walls (ie. the ones that are not present in level 1). But still, I don't see how that makes a difference.
I just made a text file of a map that was all wall except for one space, and the apple always spawned there..
Do you have any other information that might help solve this problem?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: