Skip to content

Instantly share code, notes, and snippets.

@ZacharyPatten
Last active November 19, 2022 16:24
Show Gist options
  • Save ZacharyPatten/4a8543e25ebd8296a0ca539ea7ba5fe2 to your computer and use it in GitHub Desktop.
Save ZacharyPatten/4a8543e25ebd8296a0ca539ea7ba5fe2 to your computer and use it in GitHub Desktop.
Script for my Solution 1 "dotnet-console-games" presentation.

Howdy, My name is Zachary Patten, but I often go by "ZP". I am from Kansas in the United States. I have been coding C# since 2010, and have been working proffesionally as a software developer since 2014. I have been a member of the C# discord since March 2019, and I am usually fairly active on the help channels. The topic of this presentation will be the dotnet-console-games repository on GitHub and last aproximately 30 minutes.

(mention script above webcam so if I'm awkwardly staring above the camera that is why) :P

  • ** show GitHub page ** (zoom in!)

Well, here is the repo. As the name sugguests, it is a collection of games implemented in C# console applications. There are currently 43 games. They are organized by "weight" so if you are a new developer and you want to try to code one of these games yourself, I would recommend you start with a lower "weight" game. Each game has it's own readme with more information if you click on the name. Each game also has a build status badge that indicates if the current code in the main branch builds, and also a "play now" button which links to a web page where you can play the game in the browser. Let's play a game in the browser now.

  • ** show off Whack A Mole game ** (zoom in!)

Note Keyboard input is supported but you do need to click in the console box on the web page.

Now this is the java noob edition of whack-a-mole so instead of moles those are java developers. Each hole has a number by it and I am just pressing the cooresponding number on my keyboard.

The web versions of the games are implemented in Blazor Webassembly and the website is hosted on GitHub Pages. If you are not familiar with GitHub Pages, it is a feature of GitHub that lets you host static web pages for free, it is a great way to static websites such as if you want to make an online resume or portfolio page for yourself.

  • ** show Visual Studio ** (zoom in)

Now let's move on to running the games locally with Visual Studio. There is some documentation on the readme of the repo if you need it, but once you have downloaded or cloned the code locally, just open the .sln or .slnf files in Visual Studio. If you are not familiar with the .slf extension, it allows you to reference another .sln file but filter it down so only a subset of the projects will show. In my case I wanted one solution that had all the games, and one solution that had everything (such as the website, unit testing, et.). Anyways if you opened it correctly in Visual Studio you should see all the games are a seperate project in th solution explorer. To runthe games, jsut right click and "set as start up project", and then click the green triangle at the top or the default hotkey F5 to run it.

  • ** show off Drive game ** (zoom in)

So this is a driving style game where your goal is to stay on the road. The road is randomly generated. You use the arrow keys to set your velocity, so left will make your car start turning left, up or down will make the car start moving straight vertical, and right will make the car start turning right.

  • ** show Visual Studio Code ** (zoom in)

Now let's move on to Visual Studio Code. If open the root directory in Visual Studio Code and then go to the "Debug and Run" tab, all the games should appear in the drop down. So running the games in Visual Studio Code is just as easy if not easier than Visual Studio. Let's check out another game.

  • ** show Tug of War game ** (zoom in)

Tug of war is just your basic button masher. Mash fast enough and you will out-pull your opponent and win the tug of war.

In Visual Studio the .sln(f) file had all the projects you could launch, but Visual Studio Code currently doesn't use those. Instead Visual Studio code uses specifically named .json files a .vscode/ directory:

  • ** show .vscode folder in Visual Studio Code ** (zoom in)
  1. .vscode/extensions.json recommends Vistual Studio Code extension dependencies
  2. .vscode/launch.json includes the configurations for debugging the examples
  3. .vscode/settings.json automatically applies settings to the workspace
  4. .vscode/tasks.json includes the commands to build the projects

I wish that more repoositories on GitHub were Visual Studio Code friendly. If you have a public GitHub repo that would be compatible with Visual Studio Code you should give it a shot.

All the games target .NET, so we can run them from the command line with the dotnet run command. Let's give that a shot.

  • ** show terminal ** (zoom in)

dotnet run ... (Pac Man)

Let's give Pac Man a look.

  • ** show PacMan game ** (zoom in)

As you can see it is a Pac Man clone... The movement and ghost AI are not intended to recreate the original AI. Two of the ghosts move randomly at different speeds and two of the ghosts follow you using shortest-path finding algorithms at different speeds.

The games target .NET 6 and are cross platform. I'll demonstrate this with Windows Subsystem for Linux (WSL). WSL is a great tool that lets you run Linux while using Windows.

  • ** show Draw game ** (zoom in)

Draw is a stupid little drawing game. A randomized image is generated on the right hand side and you need to replicate the image on the left hand side. But your writing utensile erases and write as you move it. So if you move to a location without ink it will write ink, but if you move to a location with ink it will erase the ink.

However, just because you target .NET does not mean your code is cross platform. For example, Console.Beep is only supported on windows, which is the reason the Beep Pad game only works on Windows. I've tried to test all the games on at least Windows and Linux, but if anyone notices an imcompatibility please let me know by opening an issue on the Github repository.

The purpose of the repository is not to tell people how to code. It's mainly trying to provide project ideas for beginner developers. Kinda of like "kata" console games. I think that console games are much more fun than typical projects like calculators, body mass index formulas, unit conversions (celcius to faurenheit), or todo list managers. There are a lot of console games on GitHub already, but they are scattered about in a lot of different repositories. I'm trying to make a sort of "hub" that people can share their console games, as well as provide some consistency so all the games run the same way (same .NET target, VS and VSC, web version, etc.).

Out of the 43 current games, I coded ~39 of them. I tried to keep the sorce code for each game relatively short. Most of the games I coded took less than 10 hours, but there have been some exceptions like Role Playing Game which probably took a bit over 20 hours. But there have been contributions from other developers too. Chess, Dice Game, and Duck Hunt were all contributed to the repository by other open source developers. And Console Monsters has been a collaborative effort with several developers pitching in. I hope the community around the repository continues to grow. I have also submitted the project to the dotnet foundation if anyone is curious.

The Console Monsters is still in developement, but let's give it a look.

  • ** show Console Monsters game ** (zoom in)

So Console Monsters is a pokemon clone. We have option menus (let me disable the audio so I'm not distracted). :D We have scrolling map movement with walking animations. We have dialog bubbles. We have transitions to other maps. And we have ~50 ascii monsters sprites.

So everything you just saw was entirely text. You probably saw some characters that you don't see on your keyboard. I would encourage you to look up ascii tables and unicode values on your own time, but I do want to mention one useful tool if you are on Windows. There is an application built into Windows called "Character Map". Jsut search for it in your start menu and you should see it. Once open, the "Character Map" application will help you type characters that are not on standard keyboards.

  • ** show Visual Studio ** (zoom in)

I want to jump back to the website versions of the games. I mentioned they use Blazor webassembly. Lets give that a quick look. Blazor Webassembly is one of the project types when you make a new project. I'm no expert at Blazor or webassembly, but in general it allows us to write C# code that will run on the client's machine. Essentially we can choose to use C# instead of Javascript. The code for the website is in the "Website" project. What I did was make a BlazorConsole class that re-implements the System.Console calls. The console code is copied into the "Website" project but I use a property/field called "Console" to redirect the calls to point to my BlazorConsole instead. Another imprtant thing I want to mention is that Blazor webassembly is currently single threaded. You cannot multithread. I'm under the impression that the feature is being worked on but I'm not sure the ETA. But for now I have to add Task delays to make sure the website will still visual update while the game is running. Is there a better way to have the code runnable on a website? Yes, but this seemed like the easiest way to do it currently and it is free. :)

If you have an questions or interest in engaging then feel free to reach out via discord, GitHub, or other means.

Special Thanks to the C# discord for allowing me to present the repository, especially Brainiac 5.

Free form (show off more games) and answer questions until end of presentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment