O'Reilly Forums: Retro Text-mode Game Challenge - O'Reilly Forums
Retro Text-mode Game Challenge Try the challenge for a chance to win a free eBook!
#1
Posted 17 August 2010 - 10:40 PM
I've got a C# coding challenge for anyone who's game... game being the operative word. I'm a sucker for old-school, retro, text-mode games. So the challenge this week is to build a text-mode game in C#. I'll judge them based on game play, fun, technical coolness, and general awesomeness, with extra points for retro nostalgia. The winner will receive five O'Reilly eBooks of his or her choice. Plus, I'll choose runners-up who will get a free O'Reilly eBook.
I chose this challenge because I wanted to try to level the playing field a bit. I know I've got readers of all experience levels, from clever novices to hard-core developers. But I'm willing to bet that very few of you have built text-mode games before. So to start you out, have a look at my latest blog post about building text-mode games in C#. I wrote it as a tutorial-style post, with examples and advice that should give you everything you need to build your game. I also included source for complete game that I wrote. It's called WordFinder, and if you like it, please feel free to steal liberally from it.
Understanding C#: Use System.Console to build text-mode games
I'll be posting new topics throughout the week. Today's topic is all about text-mode games, and anything else that has to do with quick and dirty retro-style gaming. If you run into any roadblocks with your own game, this is the place to post.
So good luck with the challenge, and I hope to hear plenty of questions!
Andrew
If you're looking for ideas on what to build, if you've done Lab #1 from Head First C# then try adapting it to turn it into a text-mode game. That would definitely be a good learning experiment! If you really want some great cred, try a text-mode version of the dungeon game in Lab #2, or Space Invaders in Lab #3[/size]
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#2
Posted 18 August 2010 - 01:28 PM
I sure hope I can find the time this week to make an entry!
- BluJai
www.blujai.com
#3
Posted 18 August 2010 - 01:42 PM
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#5
Posted 19 August 2010 - 07:27 AM
Oops, sorry -- did I forget to mention that? I'd like to close it at the end of the Inner Circle event, so after next Wednesday. That should be enough time for a quick and dirty game, or at least for a proof of concept to demonstrate sufficient awesomeness!
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#6
Posted 20 August 2010 - 06:53 AM
The game I'm working on will need to determine whether when the cursor position is set to a location on the screen to draw, if there is already a character there - ala collision detection.
I'm hoping that there might be a method in the System.Console namespace which will allow me to check this (so I can say something like
if(Console.SetCursorPosition(x,y).hasCharacter)
{
player.Collide = true;
}
pseudo code at best)
yet I cannot seem to find it.
Does anything like this exist? If not, would best options be to check all active objects (since they all have an x/y position defined in their respective classes) to see if there is anything at that location PRIOR to allowing the user controlled player object to move? Doing it this way would determine whether the next draw of the player object would need to be the "exploded" one.
This might not be a very intelligible question, and if not, I apologize in advance.
#7
Posted 20 August 2010 - 08:16 AM
But I recommend treating the console as display-only -- you can render your game to it, but don't use it to actually store any information. If you're a Head First C# reader, this would follow the same pattern that you used in the Space Invaders game or the Beehive Simulator. (You can think of this as a model-view-controller pattern: the console represents the view, a Game class represents the controller, and the classes with the various in-game elements like invaders or a ship represent the model.)
So, yes, I think your alternative plan to check all active objects for a collision is the right one. Again, I think the method from the Invaders project will work well. You should be able to come up with a simple LINQ statement to find any objects that collide with a certain coordinate. If your visual elements are larger than one character, consider using a property of type System.Drawing.Rectangle. You can call its Contains() method to see if it contains a point (which works really well from inside a LINQ query).
Another alternative, which I think is not as elegant a solution but should work just fine, is to keep track of all of your characters using a Dictionary<System.Drawing.Point, char>. Every time you update a character's position, you update the Dictionary. Then you can use Dictionary.Contains() to see what character it contains.
Does that help?
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#8
Posted 20 August 2010 - 09:51 AM
Absolutely! Thanks alot!
#9
Posted 24 August 2010 - 05:31 AM
Maybe I'm missing something...
The Visual Studio IDE isn't allowing me to reference the System.Drawing namespace. It keeps indicating that Drawing is not in the System namespace, which I know it is. I'm manually working around things as best I can quickly (to try and get my entry in before the deadline), but I'd really love to be able to use the Rectangle.Intersect method -- and Points for that matter.
I'm using Visual Studio 2010 on a 64-bit machine targeting .NET 4 Client Profile and the project's output type is Console Application.
What is the piece I'm overlooking?
- BluJai
www.blujai.com
#10
Posted 24 August 2010 - 07:26 AM
The Visual Studio IDE isn't allowing me to reference the System.Drawing namespace. It keeps indicating that Drawing is not in the System namespace, which I know it is. I'm manually working around things as best I can quickly (to try and get my entry in before the deadline), but I'd really love to be able to use the Rectangle.Intersect method -- and Points for that matter.
You're getting this error, right?
The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?)
By default, Console Application projects don't have a reference to System.Drawing. Luckily, it's easy to add. Just right-click on the project in the Solution Explorer and choose "Add Reference..." and add a reference to System.Drawing.
You can read more about how this works in my .NET assembly and namespace tutorial post.
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#11
Posted 24 August 2010 - 12:30 PM
That's great information! I wish the email me notification of replies functionality had worked on the board, because that would've saved me some time. I ended up recreating the wheel a bit instead (making C[onsole]Point and C[onsole]Rectangle classes and writing up the code I needed). I'll probably go back to this project later on and swap that out -- now that I have a reference to System.Drawing.
I was familiar with adding references to other DLLs, but I wasn't aware (until your enlightening post) that System namespace items weren't automatically available to using statements. Learning is what this Inner Circle is all about, right?
Anyway... I have a proof-of-concept up-and-running now... The full VS2010 solution is attached.
I present TxtPong, an alpha-build console-based homage of what (to me) is the ultimate retro classic, PONG!
Controls are simple --- up and down keys on keyboard. Close application using the close button or CTRL-C to exit.
- BluJai
Attached File(s)
-
TxtPong.zip (65.52K)
Number of downloads: 246
www.blujai.com
#12
Posted 24 August 2010 - 07:34 PM
Controls are simple --- up and down keys on keyboard. Close application using the close button or CTRL-C to exit.
That is awesome!
I am seriously impressed with this game. Very, very cool. I'm still going through the source, but it looks really good. I really like the way you handled drawing with the Chixel class. That was really clever.
I noticed that the player's paddle can scroll off the top and the bottom. You can probably fix this really easily by overriding the SpeedY property in the Player class and changing it from an automatic property to one with a backing field. Then you can change the speed to 0 in the set accessor if the paddle hits either the top or the bottom.
Seriously, really good job with this.
Reminder to everyone else: the contest closes tomorrow. (If you're getting close, we can extend it!
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#13
Posted 25 August 2010 - 06:25 AM
That's a great idea. That particular issue was one that I definitely want to address. However, I first want to extend the GameController class to implement an idea for an "active" game area. That can block off the top bar of the display, which would obviously be implemented in the PongController sub-class so as to keep the GameController class more generic.
I would also like to implement a Sprite "representation" engine... basically feed in a text "image" and it would create the appropriate Chixel array to use as the Sprite representation.
Anyway, thanks for the opportunity to participate. This exercise got my mind juiced-up for some console-based fun! I've got more ideas that I can't wait to get to work on!
- BluJai
www.blujai.com
#14
Posted 26 August 2010 - 02:08 AM

The game uses SQL Server (SQL Express) as the shared storage mechanism. Each element on the screen is a row in a single table in the database. Each element row consists of a color, X and Y location and the character to be displayed, along with some other information for possible multiplayer capability.

This is a testament to the speed of SQL Server, even in the Express version. The game uses threading and each element on the screen is its own thread which calls Sleep. The system keeps score in the Console.Title property and it does contain a cheat, by pressing the letter "A" (for A-bomb) all of the enemy ships disappear. The ship can move up and down and left and right and pressing the space bar generates a multicolored missle as it shoots from left to right. If it intercepts an enemy ship (a red #) the enemy ship disappears.
The game is not fully featured at this point but that's part of what the students enhance when they start enhancing the game. The students start with a shell, the calls to the database are already in place and some other features but they have to add the navigation of the ship and the missle functions.
To get the application running just unzip the zipped file and open the solution/project in either Visual Studio or C# Express. There's a .sql file inside the project that you can execute against a database that you'll need to create called "Game". The connectionstring is at the bottom of the only .cs file in the project but it might work without altering it. The connectionstring uses .\SQLExpress and integrated security by default.
Good luck and let me know what you think.
Todd Meister
Attached File(s)
-
ChaseGame.zip (52.39K)
Number of downloads: 137
#15
Posted 26 August 2010 - 12:29 PM
Everyone, please congratulate BluJai on his truly awesome entry, TxtPong! You can download the source for this game from his forum post. I absolutely encourage you to have a look, because he did some clever stuff in there.
(BluJai, one of the great folks from O'Reilly will contact you shortly.)
If you're still working on an entry, don't stop! We've given out the grand prize, but I'm keeping the contest open, and I'm still looking for runner-up winners for worthy projects. So keep 'em coming!
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
#16
Posted 30 January 2011 - 01:03 PM
I have uploaded the source code for the game to an online source control repository based on mercurial (hg) which you can get at http://mercurial.selenic.com/. Once this is installed you can click the "clone link" in the page below to get a console command that you can run to download the files. You will need some flavor of Visual Studio 2010 to run it.
https://walbalooshi.kilnhg.com/Repo/PuzzleM...zleMeMaze/Files
You can also browse the files in the using the link above. Also, the following link is a great resource for learning to use hg:
hginit.com



















