Click here to Skip to main content
Click here to Skip to main content

Multiplayer Snake in 3D using C# and Managed DirectX

By , 12 Aug 2004
 

Sample Image - screen.jpg

Introduction

Probably, everyone knows the game Snake. However, the Snake from this article is a multiplayer version and fully in 3D. It’s written in C# using Managed DirectX. A little knowledge of C# is necessary. And some (Managed) DirectX or other Graphics programming experience would be useful. I advise you, if you haven't already done so, to read Craig Andera's tutorial first.

You’ll need a C# compiler (Visual Studio .NET if possible). And the DirectX 9.0c SDK [Download it from Microsoft.]

For the code to run, you’ll also need a 3D card. But some adjustments in the code would enable it to run on older machines, decreasing its performance.

The Game Class

This is where initialization happens. Also, this is where user input is processed, using the EventKey class (all it does is check if a keystate is changed).

The Player Class

This class processes the input for the movement of one of the players. It holds a collection of Vectors for the trail. It has a Render method that will do the rendering. It also holds a reference to the Level class, the class that implements level functionality: a square array of Booleans so that the snakes can’t move through each other; it also has a couple of predefined levels to make the game a little less boring.

The Intro Class

Here, two Meshes are created for some sort of intro movie.

The Arena Class

This class renders the box in which the snakes move. It also renders the boxes which initially are already occupied by the Level class.

The Plane Class

This is a class I built quite some time ago. It creates a textured box. And allows choosing the sides you want to render.

Conclusion

I do not expect this code to be efficient. A lot of things could’ve been done better and faster. But it works, and when you’ve got nothing to do, this game can be quite fun.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Cr@zyIv@n
Netherlands Netherlands
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to run without 3D cardmemberNhilesh B11 Oct '07 - 18:25 
Hello Ivan,
 
Can you please let me know how to make this code run without 3D card?
I keep getting error on line number 119 of Form1.cs
 
device = new Device(0, DeviceType.Hardware, this , CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice , pres);
 
I know a minor change here should make it work. Can you please let me know.
 
Unsure | :~
 
Regards
Nhilesh Baua

AnswerRe: How to run without 3D cardmemberMember 340188522 Aug '09 - 13:56 
change
device = new Device(0, DeviceType.Hardware, this , CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice , pres);
to
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pres); // Put everything into the device
GeneralThis is very nicememberMashadow2 Aug '07 - 17:36 
Hi Ivan, this is very nice, it gave me some ideas, thanks for such an awsome example of 3d game written in c#.
 

I want to make some suggesstions, when u minimize the window while playing the game, device automaticly gets lost thus device.Present() call returns as an invalid call, maybe you can add a boolean to determine whether the device still exist before the game tries the render itself.
 
thanks +5
GeneralSuggestionmemberDFU2318 Aug '04 - 11:12 
If you press a button to make your snake go in the opposite direction you die. I think that it should just ignore such a silly command Big Grin | :-D
 
Wally Atkins
Newport News, VA, USA
http://wallyatkins.com

GeneralMy ExperiencesussPeterK_18 Aug '04 - 8:33 
It seems that PureDevice is not supported on my machine. I had to comment that out to avoid an InvalidCall exception when creating the Device instance. Can this be queried from the system and set appropriately?
 
I didn't seem to have any trouble with IOExceptions AFTER I installed the DirectX 9.0c SDK.
 
I would like to see the number of players be more easily modified. It is tough to play this to learn DirectX by myself and it didn't seem clear what changes were necessary to make this a single or triple player game.
 
Thanks.
GeneralRe: My ExperiencememberPeterK_18 Aug '04 - 9:27 
I tried a few things and the only option that worked was CreateFlags.SoftwareVertexProcessing. HardwareVertexProcessing also causes an InvalidCall exception on my machine.


GeneralRe: My ExperiencememberCr@zyIv@n18 Aug '04 - 22:06 
To prevent these InvalidCall exceptions, the possibilities of the machine can be queried using the DirectX.Manager class.
// Create a CreateFlags variable and assign the default CreateFlags.SoftwareVertexProcessing
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
 
// Get the capabilities of the device
Caps caps = Manager.GetDeviceCaps(0, DeviceType.Hardware);
 
// Is Hardware Vertex Processing Supported?
if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
 // Yes, replace the Software vp with Hardware vp
 flags = CreateFlags.HardwareVertexProcessing;
            
// Is a pure device supported?
if (caps.DeviceCaps.SupportsPureDevice)
 flags |= CreateFlags.PureDevice;
 
// Then create the device
device = new Device(0, DeviceType.Hardware, this, flags, pres);
 
To make the game a SinglePlayer game isn't hard. All you've to do is make the players array's length 1. And comment out the creation of the second player.
 
To add a third player would take more time because then you should add a third startpoint to every level.
 

GeneralRe: My ExperiencememberNejat Özsu8 Sep '04 - 16:10 
I also had this error when creating a device, but the following solved my problem
 
pres.BackBufferWidth = Screen.PrimaryScreen.Bounds.Width; //1280;
pres.BackBufferHeight = Screen.PrimaryScreen.Bounds.Height; //1024;

 
Hope this helps
 
Nejat
GeneralQuestion for the authormemberhacey28 Jul '05 - 4:45 
Thanks, that makes the sourcecode work on my machine.
 
Question for the author: I can't quite catch the reason for the following code:
 
public void SetUsed(int x , int y)
{
x = x/2;
y = y/2;
....
}
 
and than when calling this method, multiply the direction *2:
...level.SetUsed((int)old.X + 2*Xdir,(int)old.Z + 2*Ydir);
 
(2*Xdir) and (2*Ydir), why is it setup this way? Is it needed to 'round down'in the setused array and is that a good reasons to multiply parameters (just asking, I don't know).
GeneralSystem.IO.FileNotFoundExceptionmemberChris Sells14 Aug '04 - 14:23 
When I run Snake3D.exe, I get a System.IO.FileNotFoundException. Please advise.
 
Chris Sells
http://www.sellsbrothers.com/
GeneralRe: System.IO.FileNotFoundExceptionmemberCr@zyIv@n14 Aug '04 - 22:08 
With my first executable upload I forgot the texturesD'Oh! | :doh: .
Try downloading the file again.
 
Thomas
GeneralRe: System.IO.FileNotFoundExceptionmemberChris Sells15 Aug '04 - 8:05 
I downloaded the demo project again, extracted the zip to a folder, then ran bin\release\snake3d.exe and still get FileNotFoundException. What am I doing wrong?
 
Chris Sells
http://www.sellsbrothers.com/
GeneralRe: System.IO.FileNotFoundExceptionmemberCr@zyIv@n15 Aug '04 - 8:42 
To be honest: I don't have a clue.
If you've the DirectX SDK and a compiler you could try compiling the source.
The executable only loads 4 bmp's, these should be in the folder you extracted to.
If you have DirectX 9.0c (SDK or not) and the .NET framework ( SDK or not) it should work.
Maybe there are more with the same problem, I'll ask.
 
Thomas
 

 

 

GeneralRe: System.IO.FileNotFoundExceptionmemberjmw16 Aug '04 - 9:59 
One of the obstacles is called tex5.bmp -- and this isn't present in your distribution.
I've just copied one of the other files to tex5.bmp and it seems to work.

GeneralRe: System.IO.FileNotFoundExceptionmemberCr@zyIv@n16 Aug '04 - 22:07 
Thanks. I really should've noticed that.
GeneralRe: System.IO.FileNotFoundExceptionmemberFBKK11 Jul '05 - 11:55 
It stills throws the IO.FileNotFoundException ,even when I create the "tex5.bmp" file.
I haven't installed the DirectX SDK , and probably it will work after the SDK installation , but any game should run without the SDK involved in building it , if it doesn't I would like to know why as to avoid building a game that doesn't run without the SDK !
 
So if you can tell me why your game doesn't run I would appreciate it very much !
 
Florin Bejinaru
GeneralRe: System.IO.FileNotFoundExceptionmembermicr0chip26 Nov '05 - 11:35 
I've got the same problem. When I run a test program with the .NET 1.1 assemblies it works fine, only when I use the 2.0 assembly, it gives the exception. This is my code that gets the exception thrown:
 
public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.IsWindowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, presentParams);
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 13 Aug 2004
Article Copyright 2004 by Cr@zyIv@n
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid