Learn to Code – Create a Game!

Contents

Learn to Code – Create a Game

How to Create the Game

Architecture First

Basic Project Setup

Can You Solve This Coding Problem?

Other Bits

 Learn to Code – Create a Game

Welcome to the latest news from TalkIT. This issue is about learning to code by creating a game application. TalkIT have published an online game tutorial using Java. The tutorial is the conclusion of our Java online course. It provides an opportunity to consolidate the skills covered in the course. Take a look at our Jave course. The first tutorial is a free trial.
Start Java course
By focusing on building the game, you learn the rules and grammar of the programming language as a by-product. Creating a game is fun. You get an interesting result.

To make learning easier the tutorial includes videos, solution downloads and lots of code snippets. This is the video that gets you started.

Be warned building the whole game can take more than 10 hours. It includes some advanced skills.

How to Create the Game

The game, Dungeon of Doom, allows players to move around a dungeon collecting points.

Dungeon Game

 

 

 

 

 

 

 

 

 

 

  1. The idea of the game:
    • Players are spawned in a darkened dungeon
    • They can only see so much of the map and the rest is dark and not visible
    • The aim is to collect a certain amount of gold laying in the map
    • Then make it to the exit before another player or without dying
    • You can pick-up items that can help you along the way such as lanterns and swords
    • It is a turn based game, meaning you have to be strategic
  2. The components of the game:
    • Multi-player
    • Chat System
    • Graphical UI
  3. The game has many parts but is easily extensible. You can fight in the game and kill opponents. You can run an AI bot on and off-line which I have partially implemented. This will allow you to test your skills but have support.
  4. The Rules of the game:
    • Each player starts off with 6 APs (Action Points) which dictates how many things they can do in one turn (this can change depending on items they hold).

To read more about the game see the online tutorial.

Architecture First

When creating any complex application it is a good idea to first think about its architecture. From experience, I can say it is best to invest time at working on the initial design. This avoids refactoring most of your code to add a new feature.

The game uses the Model View Controller (MVC) architecture.MVC

The components of the MVC are:

  • Model: manages business logic classes and data access
  • View: renders model objects with client-slide intelligence
  • Controller: Handles incoming requests then chooses a view to render

Some of the benefits of using MVC are that MVC encourages separation of presentation and application logic. The model classes hold the application logic. These are reusable and consistent.

The game has many similar items that the users can pickup. Hence it would be useful to employ inheritance for game items. Best to create an item base class. Specific items will then inherit from this but modify it to their needs. This will save re-writing code and will also help with maintenance and upgrades.

With the above in mind I have created a diagram that should help explain the structure. We want keep classes as separated and self-contained as possible. Also use inter-class communication only when it is needed.

Diagram of the MVC idea:

MVC Design

Diagram of game item inheritance:

Inherit Classes

Basic Project Setup

I am going to be using the Eclipse IDE to do this but you are welcome to use IntelliJ, Netbeans or any other Java IDE. Alternatively just use a text editor and compile everything via command line. For help on this see the first Java tutorial.

We are not going to be using a base project that is provided; instead we are going to be building everything from scratch.

Open Eclipse and create a new Java Project which contains 3 packages and layout as shown below:Game Packages

Now we need to create a few things – We are going to create two classes in dod called ClientGUI and ServerGUI. In dod.game we are adding a class called GameLogic and finally in dod.game.items we are adding classes Armour, Lantern, Sword, Health, Gold and GameItem.

All the classes above can be created by right clicking the src folder > New > Class and using the package dod, dod.game and dod.game.items respectively. Then just click Finish to create the empty classes that we can then write the code for. These are not all the classes but these will do for now. Game Classes

First we will tackle the problem of trying to have multiple clients running off one server which means we will need to use multi-threading. This is an advanced feature. For help on this see the Multi-threading Java tutorial.

For this we will need to create three more classes. The first is going to handle the flow of communication from the interfaces and link to a controller class. The controller will pass information to the two model classes that do the actual processing. The reason for two models is that the server and client are separate entities and have different processing requirements.Game Entities

To start with the ClientGUI and ServerGUI will be run via command line, this is for ease of use and testing purposes. Later on we will refactor these classes to use Java swing and create a graphical interface. This will be easier for normal users. This is just to clear up any confusion about why I have named the classes GUI but will be using a command line interface for now.

Let’s start with ClientGUI. This will send messages to ClientLogic (We will implement this after we create the server). The first thing that we need to implement is a BufferReader. This will read input from the console allowing us to take server details and then send commands to ClientLogic. To do this we need to import three libraries to start off with in ClientGUI.

To read more about the project setup see the online tutorial.

Can You Solve This Coding Problem?

What’s the output from this Java code snippet?puzzles

Can you modify it to make a more challenging puzzle?

Add you answer below the blog.

int age = 100 ;
String gender = “Female”;
if (age < 18) {
if (gender.equals("Male")) {
   System.out.println("boy");
} else {
   System.out.println("girl");
}
} else {
   if (age >= 100) {
     System.out.print("centurion ");
   }
if (gender.equals("Male")) {
   System.out.println("man");
} else {
   System.out.println("woman");
}
}

 Other Bits

TalkIT will publish game tutorials for our Python and C++ online course this spring. The Python tutorial will build the Mastermind game. To win at Mastermind requires a logical mind. The C++ tutorial will create an app that displays a 3D bunny. This app works with sophisticated graphics.bits

There are also some great new corporate courses. Learn the latest technologies in the classroom.

 

Course : Training in C++11 and C++14 Development

Course : Training in SPA Web Development in ASP.NET 5

Course : Training in ASP.NET MVC 6 Development

Course : Training in C# 6 Development

Course : What’s New in ASP.NET 5 and MVC 6

TalkIT has been very active on social media recently. We have been posting on coding, new gadgets and IT humour. Why don’t you connect with us on Twitter or FaceBook? You can follow all the latest news and let us know what you think. Here are some recent tweets.

Can meditation improve your coding? Let’s find out! http://ow.ly/XmLI6 

What to learn something new this week? Try our C# courses for beginners! http://ow.ly/X6uf4 

Check out our improved website! Learn in a fast, fun & enjoyable way http://ow.ly/WYiLj 

Why should you learn a programming language? http://ow.ly/Ws6EU 

Interested in a programming career? Read our latest blog! https://t.co/qLxw0u7nNr

@NowTalkIT  twitter_circle_color-64

 

facebook_circle_color-64   NowTalkIT

 Photos www.freedigitalphotos.net/

Thanks to Andy Olsen for the Java snippet

 Thanks to Steven Paske for creating the game and writing the tutorial

David Ringsell MCPD 2016 ©

 

 

Scroll to Top