Pages

Showing posts with label Game development. Show all posts
Showing posts with label Game development. Show all posts

Friday, November 7, 2014

Java Game Development - Part 2: Developing multi user game



Extending the game as multi-user application


When extending this game as multi-user game, another player baby fish is added to the game. In addition to escaping from enemy fish and meeting friendly fish, the baby fish players should compete with each other to go home sooner than the other while getting maximum number of points.

Design

Client - server architecture

Client server architecture is used instead of peer-to-peer approach to due to its simplicity and ease of development. 

In a client-server architecture all the game players (baby fishes), or "clients", are connected to a central machine, the Fish game server. 

The server is responsible for important decisions such as creating game friend/ enemy fish collection, managing state and broadcasting this information (x, y coordinates of players and non-players) to the individual clients. 

As a result, the server becomes a key bottleneck for both bandwidth and computations.
However, this approach will consume more network bandwidth. 



Concurrent game playing using multi-threading


Multi- threading approach is used to enable multiple users to play the game concurrently. A separate thread will represent each client.

Network Access using socket communication


TCP/ IP Socket communication (low level network communication) is used for two-way communication between server and clients. Remote Method Invocation (RMI) approach is not used here, as it will incur additional processing overhead.
Using encapsulation to support multiple network protocols

FishServer class and FishClient interface do not include any TCP/ IP networking specific programming. This generic design will support different network protocols with out changing the core game logic. 

Class Diagram


 Sequence Diagram


Implementation

Threading in Java

Synchronized keyword

Since game server is accepting different threads that request or send messages, which access same resources (E.g., objects, variables), for preventing thread interference and memory consistency errors.

Runnable interface

Runnable interface is used to implement what each player client thread is supposed to do once executed.

Networking in Java

Serializable Interface

We need to send the game information such as game scores and player/ non-player x, y coordinates across network.

To achieve the above, state of the objects is transmitted across network by converting objects to byte arrays.

Classes are serialized before sending through network and de-serialized once received using Serializable interface.

Socket and ServerSocket


ServerSocket object is created to listen for incoming game player client connections.
The accept method is used to wait for incoming connections. 

The accept method returns an instance of a Socket class, which represents the connection to the new client.

ObjectOutputStream/ ObjectInputStream methods (writeObject, readObject) are used to get object streams to reading from and writing to the new client. 

UI









 



Java Game Development - Part 1: Developing a single user game using strategy design pattern

Introduction


The Game

The lost fish is a simple educational game for children to learn about behaviors and characteristics of different sea creatures such as fishes and plants in a fun and challenging way.

E.g., Dolphin is an innocent, friendly sea animal, whereas shark is harmful

Also, it is intended to make the children aware of common characteristics of the sea creatures, which belongs to a particular category.

E.g., harmful animals will scream furiously and look angry

In this game, player is a baby fish that has lost in the sea. Baby fish has to find its way home passing different barriers, while escaping from harmful animals.

Game Rules


Assume, before starting the game, baby fish will be given some knowledge on the sea creatures by its mother fish. But, mother fish will not be able to tell about “all” the sea creatures.

If the baby fish collides with a harmful creature, the baby fish will get weak. (Loose energy)

If the baby fish identifies and meet friendly fish it will gain energy. If energy level is below zero, baby fish will die and the game is over.

Baby fish will get bonus points if it reach/find home soon.

Win the Game


Baby fish has to reach home with maximum energy level with maximum bonus points, to win the game.

Design

This game is designed with the intention of improving further with a variety of sea animals with different appearances and behaviors. Then, game will keep the players interested and also this will lead to a good learning resource as well.

Design Decisions

OOP concepts such as encapsulation, inheritance and polymorphism are used to improve the reusability, maintainability and the extendibility of the game.

 Strategy Design Pattern

Strategy design pattern is used to effectively extend the game with new sea creatures with diverse behaviors, to keep the player entertained. Also, different behaviors can be dynamically invoked using this approach.

An example scenario is given below:

Assume we have to add two new sea animals (E.g., whale and jelly fish) with different/ existing sound behaviors to the game, with out doing major changes to the core game design and avoiding duplicate code. If we use the traditional inheritance approach, where all the sea animal behaviors (E.g., sound/ scream) inherit from parent class SeaAnimal, then the code will be duplicated for similar behaviors. But, the given approach will solve that problem using interface implementations for similar behaviors.

The current design supports the above scenario in two different ways.
•    Inheritance: New sea creatures can be created by extending “SeaAnimal” abstract class
•    Polymorphism: Novel sound behaviors can be added or existing sound behaviors can be reused using “Sound” interface.

Using Constants

Constant values are used wherever applicable to improve reusability and maintainability.

Class Diagram


Sequence diagrams






 UI Design


Here's the video: https://www.youtube.com/watch?v=ipv_6yYAUw4

References

[1] http://obviam.net/index.php/design-in-game-entities-object-composition-strategies-part-1/
-->



Sunday, September 23, 2012

Convert clockwise angles to anti-clockwise angles in Construct2

By default, angles in Construct2 facing right and increments clockwise.

Angles in Construct2

To change this behavior, to anti-clockwise do the following:
 
Anti-clockwise angle = - ( Clockwise angle)

To display clockwise angle as anti-clockwise angle use the following calculation,
Anti-clockwise angle = 360 degrees - clockwise angle

Ex: Clockwise angle 45 degrees = Anticlockwise angle 315 degrees and equals to -45