O'Reilly Forums: Retro Text-mode Game Challenge - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Retro Text-mode Game Challenge Try the challenge for a chance to win a free eBook!

#1 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 17 August 2010 - 10:40 PM

UPDATE: Congratulations to BluJai, our grand prize winner for his game, TxtPong! But don't stop coding just yet -- the contest is still open! I'll be choosing runner-up prize winners for particularly awesome entries.

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]
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#2 User is offline   BluJai 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 18-August 10

Posted 18 August 2010 - 01:28 PM

Oh, this is an awesome challenge. I love the items posted on Programming Praxis. Unlike most of those, this takes me down memory lane. I spent many an hour playing text-based games on an Apple IIe and, later, a DOS "laptop" (which, if memory serves, was a Compaq Portable).

I sure hope I can find the time this week to make an entry!

- BluJai
- BluJai
www.blujai.com
0

#3 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 18 August 2010 - 01:42 PM

Oh man, back in the mid-80s, my best friend had one of those Compaq luggables! I have many fond memories of building GRASP animations on it. It had this tiny little monochrome CRT monitor that left trails behind when pixels moved or disappeared.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#4 User is offline   leehilt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 18-August 10

Posted 19 August 2010 - 04:45 AM

when will this contest close?
0

#5 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 19 August 2010 - 07:27 AM

QUOTE (leehilt @ Aug 19 2010, 04:45 AM) <{POST_SNAPBACK}>
when will this contest close?


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!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#6 User is offline   leehilt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 18-August 10

Posted 20 August 2010 - 06:53 AM

Thanks for the reply! I do however have an implementation question:

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
CODE

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.



0

#7 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 20 August 2010 - 08:16 AM

Working directly with the console buffer is probably more painful than you want to do. If you really want to try, you can read this MSDN page.

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?
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#8 User is offline   leehilt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 18-August 10

Posted 20 August 2010 - 09:51 AM

QUOTE (AndrewStellman @ Aug 20 2010, 08:16 AM) <{POST_SNAPBACK}>
Does that help?


Absolutely! Thanks alot!

0

#9 User is offline   BluJai 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 18-August 10

Posted 24 August 2010 - 05:31 AM

QUOTE (AndrewStellman @ Aug 20 2010, 08:16 AM) <{POST_SNAPBACK}>
If your visual elements are larger than one character, consider using a property of type System.Drawing.Rectangle.


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
- BluJai
www.blujai.com
0

#10 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 24 August 2010 - 07:26 AM

QUOTE (BluJai @ Aug 24 2010, 05:31 AM) <{POST_SNAPBACK}>
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.


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.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#11 User is offline   BluJai 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 18-August 10

  Posted 24 August 2010 - 12:30 PM

QUOTE (AndrewStellman @ Aug 24 2010, 07:26 AM) <{POST_SNAPBACK}>
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.


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)


- BluJai
www.blujai.com
0

#12 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 24 August 2010 - 07:34 PM

QUOTE (BluJai @ Aug 24 2010, 03:30 PM) <{POST_SNAPBACK}>
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.


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! smile.gif And there are runner-up prizes.)

Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#13 User is offline   BluJai 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 18-August 10

Posted 25 August 2010 - 06:25 AM

QUOTE (AndrewStellman @ Aug 24 2010, 09:34 PM) <{POST_SNAPBACK}>
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.

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
- BluJai
www.blujai.com
0

#14 User is offline   Todd Meister 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 25-August 10

Posted 26 August 2010 - 02:08 AM

I was suprised when I read your post regarding a text based game. For the past several years I've been fortunate enough to work with high school students from a local career center. I usually work 2 days a week for 2 1/2 hours a day with 2 groups of students for a total of 8 to 10 weeks. Near the end I introduce the framework for a game and then they add on to it and the game is a console based text game.



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)


0

#15 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 803
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 26 August 2010 - 12:29 PM

We have a grand prize winner!


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!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#16 User is offline   walbaloushi 

  • Active Member
  • PipPipPipPip
  • Group: Members
  • Posts: 100
  • Joined: 25-July 09
  • Gender:Male

Posted 30 January 2011 - 01:03 PM

I know that this is super late, but I have been spending a little bit of time here and there working on this Maze Game. I actually used the XNA framework as a base to create a simple game engine and then built this game on top of it. At the moment it is very simple, but I am about to start a new job, so I decided that I might as well post it before I get overwhelmed with work. There are a lot of things that need to be added, so please be patient with this very rough start.

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users