introduction to game development and xna squid invaders

18
Introduction to Game Development and XNA Squid Invaders! XNA Space Game Beginner Tutorial Mikhail Roy Dela Cruz

Upload: independent

Post on 13-May-2023

0 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to GameDevelopment and XNA Squid

Invaders!XNA Space Game Beginner Tutorial Mikhail Roy Dela Cruz

Preface..............................................................3Introduction........................................................3

Expectations........................................................3Introduction to Game Development - "Things You Should Consider"......4

Game Design.........................................................4Gameplay........................................................4

Art.............................................................5Sound...........................................................6

Game Programming....................................................6At what programming level are you willing to work?..............6

What concepts in programming should one know in order use those tools above?....8Squid Invaders!......................................................9

“Getting Started”...................................................9Parts of a class (.cs)..........................................9

XNA Program Flow...............................................10Analyze a game.................................................12

Game Development...................................................14Creating XNA Game Project

Creating a Sprite ClassCreating a Background

Creating a Ship ClassCreating a Player Ship

Setting up KeyboardAdding Bullet

Creating an Enemy Ship

Adding AI to Enemy ShopColliding Bullet

Adding livesAdding GUI

Preface

IntroductionDeveloping video games is challenging as a programming task. Although a layman can easily relate with the mechanics and objectives of a video game, understanding the technology behind it may require sufficient programming knowledge.

Consider a normal Tetris Game -We all about the blocks: The differentshapes, how those blocks gradually go down, rotating it so it fits from the pile of other blocks, and how you lose when the pile reaches at top. It’s so easy to decipher those mechanics. But when asked how we could we represent them at a computer screen, things get a little bit tight.

That is what this document is focused on, representing things on a computer screen. In general, to provide an introduction to assist people who wish to begin making their own games.

In the few chapters at the beginning,

At the end of this document, the reader will have created a simple, yet functional single player game. We will be usin The game will just be a simple space arcade shooting game consisting of squid aliens, just like the typical Space Invader, with no power-ups and other stuffs.

As the title of the document suggests, we will be using Microsoft XNA 3.1 Framework to make such space game.

ExpectationsIf you will read this material, it would definitely help if you have at least a basic familiarity of the following:

- General C# Syntax- Object-Oriented(OO) Design- Basic Algebra

Introduction to Game Development - "Things You Should Consider"

Video game development can be sub-divided into two processes: Game Design and Game Programming.

When we talk about Game Design, this is the process where the content and mechanics of the game such as gameplay, environment, storyline, and characters are given thought and made.

On the other hand, Game Programming is the actual process of implementing the game design into a computer program.

Usually, game development starts at the designing process - as it is agood practice to start planning things before going all way to programming.

Below I have compiled questions for both Game Design and Game Programming that you should ask yourself. I believe these questions are relatively important in developing a video game.

Game Design

Gameplay What are the Objectives/ Rules/Mechanics?

These are the key components that will define your game. Makesure it is defined clearly when you are making your game.

Example: The objective of the Chess game is to checkmate theKing. You can do that by using pieces to capture other pieces(Mechanic). Though there are different rules that define thedifference between each pieces.

What kind of Genre?Genre is the different categories of video games based on theirgameplay interaction rather than visual or narrative differences;

Example: Action;Role-Playing; Action Adventure; Simulation;Strategy

How about the controls?

How would you play the game? Use a Mouse and Keyboard? Or Perhapsa Game Pad?

What kind of control mapping would suit the gameplay?

Example: Counter Strike uses WSAD to control the character movement and mouse for the camera and guns.

ArtWill it be on 2D or 3D?

This is actually can be a difficult question to answer. While most people would prefer to play 3D video games over 2D ones as 3D brings more sense of realism to the gameplay experience, developing a 3D game can be a lot harder.

In 3D, you have to be familiar with lots of thing such as 3D Math(Matrices, Projection, etc), Model Mesh and Textures, lighting, and others – which is something I would not recommend for beginner programmers to tackle head-on.

Besides, style is still a big factor when deciding for 2D over 3D. Just like Mario and Pokemon.

How about the style of art?Though one’s art is an eyesore to others, it is a rule of the thumb to have a consistent style over a work. This also applies in games.

Try to consider the style of art implemented in the Mario Game. One can’t help but notice the consistent cartoonish theme throughout the game.

How do I prepare animated 2D images for game processing?There are lots of ways to handle those, but one thing that is certain: Depending on the rate of speed, animated images have frames that changes over time.

In a sprite based-game, one way to handle animated sprites is to save all of frames into a single plane and program the game in such a way - it would just display a portion.

In the example above, each frame has a size of 196x196 pixels andthe whole image can hold 6x5frames. Thus the whole size is about 1176x980;

What tools to use? Are there other resources ?There are lots of graphic editing programs existing out there that can help you make the art assets you need for your game.

In 2D - you may use paid software like Adobe Photoshop CS5, though I would prefer to use free ones such as GIMP and Paint.Net. Well sometimes, even the basic pbrush provided by the OS is enough.

In 3D – again, you may use paid softwares like Maya, 3ds MAX. Butif you just like to play around, try free ones like Blender.

On the other hand, If you don’t like to make your own art assets,you may instead download and use other works made by other people- Though make sure you have their permission.

Usually, these kinds of works are called ‘Royalty-free’ art assets. Try searching the terms “royalty-free 2d sprite” on Google.

SoundWhen to start programming sounds in the game?

Most of the time, the sounds are inserted after coding the art and gameplay – If that is not a sign that sounds can be a lesser priority.

But of course, if the game revolves around sounds, it would be adifferent matter.

What tools to use? Are there other resources ?If you plan to create your own sounds for the game, again, you can use existing tools such as Audacity, or Guitar Pro

On the other hand, you may instead use other’s work. Just make sure you have their permission.

Game Programming

At what programming level are you willing to work?

There are actually different ways to program a game – and it is amust for you to be aware of it.

One way is to develop a game using Game Engines –

These are software designed to specifically to make gamesa lot easier, although with limitations on what a developercan create. Typically, developers will code lesser sincethe Game Maker already provides necessary features such as,2D/3D graphics rendering, Animation , Scenes, Events,Sounds, Collision Detection, Physics, and sometimes even anIDE.

Usually, game engines have a primary programming languagewhich is required developers to use if they wish toimplement the said game engine. Usually, these programminglanguage can be of the following: (C, C++, C#, Java, Lua,Python)

There can be kinds of Game Engine out there, which are differentiated on their purpose:

RPG Maker XP – A game engine that focus primarily on making Role-Playing Games. Developers can code in Rubyprogramming language

Aleph One – A game engine for First Person Shooter (FPS), in C++;

Unity — An open-ended 3D game/interactive software engine for web, Windows, and Mac OS X. Upgrading to paid licenses can additionally enable support for the iPhone, Android, Nintendo Wii, Playstation 3, and the Xbox 360.

Unreal Engine — Considered one of the most popular game engines in the top market. The free edition, called UDK (a binary release of the engine), allows you to use the engine for commercial purposes under specific terms.

CryEngine 3 — CryEngine 3 is a new generation engine developed by Crytek to create the FPS game Crysis 2. The engine has been released recently, with a license model that is similar as offered by the UDK engine.

On another hand, programmers can use Framework and Libraries instead –

These Frameworks and Libraries are similar with Game Engines, but are less specific with the features they provide, Thus coding can on it can be longer but has fewer limitations on what a game developer can create.

Coding on a framework level may require developers to be familiarized with more programming concepts compared with Game Engines.

It does not include physics, collision detection and other things often found in game engines. It is a game developmentframework, but how the game works is programmed entirely by you.

Examples:Microsoft XNA Framework - is a set of tools with a managed runtime environment provided by Microsoft thatfacilitates video game development and management

Microsoft DirectX - is a collection of application programming interfaces (APIs) for handling tasks related to multimedia, especially game programming andvideo, on Microsoft platforms. Originally, the names of these APIs all began with Direct, such as Direct3D,DirectDraw, DirectMusic, DirectPlay, DirectSound, and so forth.Simple Directmedia Layer (SDL) - Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse,joystick, 3D hardware via OpenGL, and 2D video framebuffer.

OpenGL - is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics.

Whatever tool/s you may end-up choosing to develop your game, be reminded that there is a learning curve to be considered - this is true, even with the most basic and simplest game engine out there.

What concepts in programming should one know in order use those tools above?

Depending on what programming level you will develop the game andthe tools you will use, it can be different.

As pointed out earlier, Game Engine, Frameworks and Libraries usedifferent primary programming language for the implementation andCoding on a framework level may require developers to be familiarized with more programming concepts compared with Game Engines.

On a related note, since this document aims to build a space gamein Microsoft XNA Framework, I’ll be pointing out things I believe, you should be familiar in order for you create things onthe said framework:

Basic C# Syntax- Primitive Data Types (int, float, double, bool, char,

string)- Variables

- Declaration- Primitive vs Reference variables

- Operators- Arithmetic (+, -, *, / )- Increment/Decrement (++, -- )- Relational Operataors (<, <=, >, >=, ==, != )- Logical Operators (&&, ||, !)- Control Structures- Decision Control Structures (if, if-else, if-elseif,

switch)- Repetition Control Structures (for, while, dowhile,

foreach)- Arrays

- Object Oriented Programming- Classes vs Objects

- Class and Object/Instance Variables- Class and Object/Instance Method- Properties- Class vs Abstract Class vs Interface

- Concepts- Encapsulation- Inheritance

- Polymorphism/Casting

Squid Invaders!

“Getting Started”

Parts of a class (.cs)

using System; // Importsusing System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;

namespace Squid_Invaders // Project Name{ public class Ship : Sprite { int lives; // Class Variables

public bool isAlive { // Class Properties get { return lives > 0; } }

public Ship(Texture2D texture) // Constructor Method :base(texture){

}

public void Update(GameTime gameTime) { //Class Methods }

public override void Draw(SpriteBatch spriteBatch) { //Class Methods

}

}}

XNA Program Flow

using Microsoft.Xna.Framework;

namespace Squid_Invaders{ public class Game1 : Microsoft.Xna.Framework.Game {

Ship player1; // Class Variables

public Game1() // Constructor

{ graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; }

protected override void Initialize(){ //Class Methods base.Initialize(); }

protected override void LoadContent() //Class Methods { spriteBatch = new SpriteBatch(GraphicsDevice);

player1 = new Ship(Content.Load<Texture2D>("Chicken")); }

protected override void UnloadContent()//Class Methods { }

protected override void Update(GameTime gameTime) //Class Methods { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();

player1.Update(gameTime) base.Update(gameTime); }

protected override void Draw(GameTime gameTime) //Class Methods

{ GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin(); player1.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } }}

Load

Initialize

Update

Draw

Analyze a game

What are the objects you can differentiate?

Ships, Player, EnemyBulletsBackgroundScoreRound

BulletDraw()

PlayerMove()Fire()Draw()

EnemyMove()Fire()Draw()

BackgroundDraw()

SpriteDraw()

ShipsMove()Fire()Draw()

Game Development

Creating XNA Game Project

Creating a Sprite Class

Creating a Background

Creating a Ship Class

Creating a Player Ship

Setting up Keyboard

Adding Bullet

Creating an Enemy Ship

Adding AI to Enemy Shop

Colliding Bullet

Adding lives

Adding GUI