Serpent - In progress
by David Roberts · in Torque Game Builder · 03/03/2005 (12:57 pm) · 10 replies
Hi everyone,
I have been putting together a "snake"-style game in T2D over the past couple of days as a mini-project to get to grips with working in Torque for the first time. My aim was to work out how to put together a simple game with a splash screen, GUI navigation, game loop.
This is a very basic unfinished version (barely scratches the potential of T2D) but I thought I'd share what I've done so far. Hopefully I might be able to help a few other new users - and maybe learn better scripting technique myself!
Heres a screenshot:

And you can download the game here!
Extract the files to the same folder as your t2d.exe file (should be c:\torque 2d\SDK\example\ for default install). Then to play the game, execute the RunSerpent.bat file.
Controls:
WASD / Arrow keys - Move serpent ( You cannot move backwards once in motion)
I - Increase the size of the serpent (for testing purposes!)
RETURN - Toggle Debug on/off
ESCAPE - Return to main menu
Its very easy at the moment, and not really a proper game. However, once I get my next load of uni work outta the way I'll be able to come back and hopefully finish this game off proper!
Let us know what you think of it so far guys!
- Dave
This is not a redistributable game! Full source is included and the editors are still accessible through shortcut keys! This game is meant for current T2D licensed users to learn from. If you want to send it to a friend you will be responsible for making it compliant with the T2D EULA).
---------------------------------
BUG UPDATE! -03/03/2005
---------------------------------
Typical, upon showing this to the world, I have discovered a curious little bug. Seemingly at random, the game generates more than one pellet (no, this wasn't intended at the mo!). This has the effect of multiplying the amount of pellets on screen everytime you pick one up! As you can imagine, this quickly can escalate out of control and slow things up real good. After dying - when restarting the game, the number of pellets on screen will also be more than the intended one.
I have a feeling I know what could be the cause of this randomly occuring bug, but I'm gonna have to tackle it tomorrow at the earliest (damn assignments!). All I will recommend is that you escape from the game if more than one pellet appears and exit the app. It should be reset back to normal once you load the .bat file again.
My apologies on any inconvenience caused by this.
- Dave
I have been putting together a "snake"-style game in T2D over the past couple of days as a mini-project to get to grips with working in Torque for the first time. My aim was to work out how to put together a simple game with a splash screen, GUI navigation, game loop.
This is a very basic unfinished version (barely scratches the potential of T2D) but I thought I'd share what I've done so far. Hopefully I might be able to help a few other new users - and maybe learn better scripting technique myself!
Heres a screenshot:

And you can download the game here!
Extract the files to the same folder as your t2d.exe file (should be c:\torque 2d\SDK\example\ for default install). Then to play the game, execute the RunSerpent.bat file.
Controls:
WASD / Arrow keys - Move serpent ( You cannot move backwards once in motion)
I - Increase the size of the serpent (for testing purposes!)
RETURN - Toggle Debug on/off
ESCAPE - Return to main menu
Its very easy at the moment, and not really a proper game. However, once I get my next load of uni work outta the way I'll be able to come back and hopefully finish this game off proper!
Let us know what you think of it so far guys!
- Dave
This is not a redistributable game! Full source is included and the editors are still accessible through shortcut keys! This game is meant for current T2D licensed users to learn from. If you want to send it to a friend you will be responsible for making it compliant with the T2D EULA).
---------------------------------
BUG UPDATE! -03/03/2005
---------------------------------
Typical, upon showing this to the world, I have discovered a curious little bug. Seemingly at random, the game generates more than one pellet (no, this wasn't intended at the mo!). This has the effect of multiplying the amount of pellets on screen everytime you pick one up! As you can imagine, this quickly can escalate out of control and slow things up real good. After dying - when restarting the game, the number of pellets on screen will also be more than the intended one.
I have a feeling I know what could be the cause of this randomly occuring bug, but I'm gonna have to tackle it tomorrow at the earliest (damn assignments!). All I will recommend is that you escape from the game if more than one pellet appears and exit the app. It should be reset back to normal once you load the .bat file again.
My apologies on any inconvenience caused by this.
- Dave
About the author
#2
It seems to be quite elusive as well as random. I have tested it quite intensively now and can't seem to replicate it for the time being - despite changing none of the scripts from the download given here.
If anyone has any spare time to look over my scripts, maybe they can see something stupid I'm doing which might be causing this bug? I'm thinking it may be something to do with the safeDelete commands and that game objects from the previous game session aren't being deleted in time for when you restart a new game session? (It creates new objects each time you click on "Play Game".
Right now I need to crash myself! Sleep is a callin'!
- Dave
03/03/2005 (3:20 pm)
Bug found! - See original post if you have downloaded this.It seems to be quite elusive as well as random. I have tested it quite intensively now and can't seem to replicate it for the time being - despite changing none of the scripts from the download given here.
If anyone has any spare time to look over my scripts, maybe they can see something stupid I'm doing which might be causing this bug? I'm thinking it may be something to do with the safeDelete commands and that game objects from the previous game session aren't being deleted in time for when you restart a new game session? (It creates new objects each time you click on "Play Game".
Right now I need to crash myself! Sleep is a callin'!
- Dave
#3
Well, after testing it again today on machines at uni, I can't seem to replicate the bug at all. This is quite confusing to say the least. I am gonna rewrite the food handling and collision code on the next iteration of this game so maybe that will either eliminate it or flush it out into the open!
- Dave
03/04/2005 (4:33 am)
UPDATE:Well, after testing it again today on machines at uni, I can't seem to replicate the bug at all. This is quite confusing to say the least. I am gonna rewrite the food handling and collision code on the next iteration of this game so maybe that will either eliminate it or flush it out into the open!
- Dave
#4
It was the first game I played on the Apple II followed by Hard Hat Mack and Aztec, all in a very brite monitor-green...ah memories.
I started making a pong game but it's grown into foosball, LOL.
03/04/2005 (6:14 pm)
You beat me to it! I'll be making a snakebite, yadda yadda yadda, game as well.It was the first game I played on the Apple II followed by Hard Hat Mack and Aztec, all in a very brite monitor-green...ah memories.
I started making a pong game but it's grown into foosball, LOL.
#5
I thought a remake of that would be quite appropriate for my first project in T2D. Suffice to say, Torque2D is proving to be slightly easier and faster to use! ;-)
03/05/2005 (3:03 am)
Well for me, one of the first games I ever made was a version of snake written in Pascal using the text graphics mode! I thought a remake of that would be quite appropriate for my first project in T2D. Suffice to say, Torque2D is proving to be slightly easier and faster to use! ;-)
#6
Seems like the snake dies when I'm too quick with the keys, resulting in biting itself.
03/05/2005 (10:39 pm)
David, everytime the snake dies the game crashes and I have to end task the application. I have not modified any of your scripts. Do you have a newer version?Seems like the snake dies when I'm too quick with the keys, resulting in biting itself.
#7
Using only T2D stuff, it should be very hard to crash so I'd definatley like to know what causes this.
Any chance you running this under a debug build to see what crashes it?
- Melv.
03/06/2005 (3:14 am)
@Joe: I've tried as best as I can to get this to crash under Windows without success. Are you using another platform, perhaps this is an issue I should know about.Using only T2D stuff, it should be very hard to crash so I'd definatley like to know what causes this.
Any chance you running this under a debug build to see what crashes it?
- Melv.
#8
The only bug I found was when the food sometimes continued to multiply which would eventually result in the app slowing to a crawl once too many objects were on screen. Haven't said that, I can't seem to replicate that bug now either - without changing the scripts!
If you turn too quickly the snake will collide with the third part of its body - thats down to my current collision detection which I am going to address in the next version.
As for the crashing issue I'm stumped. I haven't had in crash on any machines that I have tested it with - from a super fast monster of a PC to my antiquated five year old laptop. I have only tried it on Windows though, so maybe its a Mac or Linux thing?
When your snake dies it calls the following scripts:
which then calls:
which in turn calls:
After which point it returns to the main menu GUI awaiting the next instruction from the user.
03/06/2005 (4:07 am)
Hmm.The only bug I found was when the food sometimes continued to multiply which would eventually result in the app slowing to a crawl once too many objects were on screen. Haven't said that, I can't seem to replicate that bug now either - without changing the scripts!
If you turn too quickly the snake will collide with the third part of its body - thats down to my current collision detection which I am going to address in the next version.
As for the crashing issue I'm stumped. I haven't had in crash on any machines that I have tested it with - from a super fast monster of a PC to my antiquated five year old laptop. I have only tried it on Windows though, so maybe its a Mac or Linux thing?
When your snake dies it calls the following scripts:
// --------------------------------------------------------------------
// Handle when the player has been killed and reset the player
// --------------------------------------------------------------------
function KillPlayer()
{
// Disable keyboard input
playerMap.pop();
// Ensure that the player does not keep calling world limit callback
$player.setWorldLimit( clamp, "-400 -300 400 300");
// Ensure that the player does not keep calling collision callback
$player.setCollisionActive( false, false );
// Stop the serpents head
$player.setAtRest();
// Stop the serpents body
for(%a=$tailSize; %a >= 0; %a--)
{
$body[%a].dismount();
$body[%a].setAtRest();
}
// Shake that screen!
SceneWindow2D.startCameraShake( 50, 5);
// Set high score
if($playerScore > $highScore)
{
$highScore = $playerScore;
}
schedule( 5000, 0, "exitGame");
}which then calls:
// --------------------------------------------------------------------
// Return to main menu after death or escape is pressed.
// --------------------------------------------------------------------
function exitGame()
{
// Free up memory used in that game session
freeGameMemory();
// Set GUI.
Canvas.setContent( serpentMainGui );
}which in turn calls:
// --------------------------------------------------------------------
// Free system memory
// --------------------------------------------------------------------
function freeGameMemory()
{
// Delete the serpents head
$player.safeDelete();
// Delete the serpents body
for(%a=$tailSize; %a >= 0; %a--)
{
$body[%a].safeDelete();
}
// Delete any food
$food.safeDelete();
}After which point it returns to the main menu GUI awaiting the next instruction from the user.
#9
One thing that should happen automatically though (unless you turn if off) is that mounted objects are owned by their parent meaning that when you delete them, their mounted children are automagically deleted as well. You can turn-off this behaviour with the "[ownedByMount?]" option in the "mount()" function.
- Melv.
03/06/2005 (4:47 am)
Nothing obviously wrong there that I can see.One thing that should happen automatically though (unless you turn if off) is that mounted objects are owned by their parent meaning that when you delete them, their mounted children are automagically deleted as well. You can turn-off this behaviour with the "[ownedByMount?]" option in the "mount()" function.
- Melv.
#10
However, in my game, I have turned parent ownership off. The reason for this was that when the player died, all the mounted body parts moved into the space occupied by the head. The only way I managed to get the body parts to stop at their current positions when the player died was by making them independent of the head:
I mounted the body parts to the snake as follows:
- Dave
03/06/2005 (5:07 am)
Yeah, I thought this was the case. However, in my game, I have turned parent ownership off. The reason for this was that when the player died, all the mounted body parts moved into the space occupied by the head. The only way I managed to get the body parts to stop at their current positions when the player died was by making them independent of the head:
I mounted the body parts to the snake as follows:
//-----------------------------------------------------------------------------
// Handles increasing the size of the serpent
//-----------------------------------------------------------------------------
function playerSizeUp()
{
// Increase the size of the serpent
$tailSize += 1;
// Create new body part
$body[$tailSize] = new fxStaticSprite2D() { scenegraph = serpentSceneGraph; };
$body[$tailSize].setPosition( $body[$tailSize-1].getPosition() );
$body[$tailSize].setLayer( 2 );
$body[$tailSize].setSize( "32 32" );
$body[$tailSize].setImageMap( snakeBodyImageMap );
$body[$tailSize].tag = "tail";
// Set up collision model for tail part
$body[$tailSize].setGroup( 2 );
$body[$tailSize].setCollisionScale( "0.5 0.5" );
$body[$tailSize].setCollisionActive( false, true );
// Mount extra part of serpent to player
$body[$tailSize].mount($body[$tailSize-1], "0.0 0.0", 16, false, false, false, false);
}- Dave
Associate Joseph Euan