Vous êtes sur la page 1sur 6

DigiPen Institute of Technology

Presents

Session Eight:

Behavior

DigiPen Institute of Technology


5001 150th Ave NE, Redmond, WA 98052
Phone: (425) 558-0299
www.digipen.edu

2005 DigiPen (USA) Corporation. No part of this work may be published without the written permission of DigiPen (USA) Corporation

Contents
1 Behavior Overview
2 Game Objects Behavior
2.1 Game Implementation: Condor Behavior
2.2 Game Implementation: Fire Behavior

3
5

Game Web Cast Project: Behavior

Behavior
1 Behavior Overview
A behavior is a set of functions, defined by the user, which describes how a game and its objects should behave
and act. You can define a behavior to the game and to some of its objects, like sprites and texts. The functions
assigned in a game object behavior (the game object is an instance of a class derived from the Game class) are
executed successively.

2 Game Objects Behavior


In StarTrooper
StarTrooper, the game behavior is divided into several objects that make the game. These objects are:
Trooper, Condor, and Fire.
The behavior of the Trooper object is a combination of several kinds of behavior:
Movement Behavior (already seen)
Shooting (already seen)
The behavior of the Condor object depends on the collision detection. If it collides with Fire, which means that
Fire hits it, it will explode and die with a consequent score increment. If it is hit by the Trooper, which means
that condor wins, Condor will die and the score will decrement. Therefore, we have:
Condor Behavior
The behavior of the Fire object is simple. It will die if it is hit by Condor, otherwise it will continue. Therefore,
we have:
Fire Behavior

2.1 Game Implementation: Condor Behavior


Create a list variables collidedSprites that contains all the sprites with which the enemy collides:
List<Sprite> collidedSprites = Game.GetCollidedSprites(this);
Test if the list is empty or not; if not empty:
if (collidedSprites != null)
o For each element of the list:
foreach (Sprite s in collidedSprites)
o Test if this element is the fire object:
if (s is Fire)
{
//The enemy object will die
RaptorGame.Die.Play();
// Playing the enemy explosion animation
AnimationIndex = 1;
//Incrementing the score by one
DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0
Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Behavior


m_Score++;
//Updating the value of the score displayed
//on the screen
RaptorGame.Score.Text = Score: + m_Score.ToString();
//Removing the enemy sprite from the game
Game.Remove(s);
break;
}
o Test if this element is the main character:
else if (s is Bat)
{
m_CollisionWithBat = true;
//The enemy object will die
RaptorGame.Die.Play();
//The Animation will stop looping
Animation.Stop();
//Decrementing the score by one
m_Score--;
//Updating the value of the score displayed
//on the screen
RaptorGame.Score.Text = Score: + m_Score.ToString();
break;
}
Testing if the animation of the Condor sprite is the explosion animation or not:
if (AnimationIndex != 1)
{
}
else
{
//Testing if it is the last frame of the explosion animation
if (Animation.PlayingLastFrame)
//delete the explosion sprite
Game.Remove(this);
}
Delete the following code in the StarTrooperSprites.cs file under public override void Update() function:
Trooper b = StarTooper.Trooper;
Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y - Position.Y);
v.Normalize();
Velocity = v;
if (v.X >= 0)
ScaleX = 1;
else
ScaleX = -1;

Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:

DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0


Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Behavior

Trooper b = StarTrooper.Trooper;
if (AnimationIndex != 1)
{
Vector2 v = new Vector2(b.Position.X - Position.X, b.Position.Y - Position.Y);
v.Normalize();
Velocity = v;
if (v.X >= 0)
ScaleX = 1;
else
ScaleX = -1;
List<Sprite> collidedSprites = Game.GetCollidedSprites(this);
if (collidedSprites != null)
{
foreach (Sprite s in collidedSprites)
{
if (s is Fire)
{
StarTrooper.Die.Play();
AnimationIndex = 1;
m_Score++;
StarTrooper.Score.Text = "Score: " + m_Score.ToString();
Game.Remove(s);
break;
}
else if(s is Trooper)
{
m_CollisionWithTrooper = true;
StarTrooper.Die.Play();
Animation.Stop();
m_Score--;
StarTrooper.Score.Text = "Score: " + m_Score.ToString();
break;
}
}
}
}
else
{
if (Animation.PlayingLastFrame)
Game.Remove(this);
}

2.2 Game Implementation: Fire Behavior


If the Condor hits the Fire it will be deleted: this is already seen with the Condor behavior.
If the Fire does not hit anything, it will delete itself if it is out of the game:
if (y < -100)
Game.Remove(this);
Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:
if (y < -100)
Game.Remove(this);

When the spacebar is triggered, a new Fire sprite is created, the Shoot sound effect is played, and the text
Shoots is incremented and displayed.
Delete the following code in the StarTrooperSprites.cs file under public override void Update() function:

DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0


Copyright 2005 DigiPen (USA) Corporation.

Game Web Cast Project: Behavior

if (Keyboard.IsTriggered(Key.Space))
{
Fire fire = (Fire)StarTrooper.Fire.Clone();
fire.Position = new PointF(Position.X, Position.Y - 35);
fire.Velocity = new Vector2(0, -4);
Game.Add(fire);
}

Then type the following code in the StarTrooperSprites.cs file under public override void Update() function:
if (Keyboard.IsTriggered(Key.Space))
{
Fire fire = (Fire)StarTrooper.Fire.Clone();
fire.Position = new PointF(Position.X, Position.Y - 35);
fire.Velocity = new Vector2(0, -4);
Game.Add(fire);
StarTrooper.Shoot.Play();
m_Shoots++;
StarTrooper.Shoots.Text = "Shoots:" + m_Shoots.ToString();
}

DigiPen Game Development C# Webcast Series - Session Eight, Version 1.0


Copyright 2005 DigiPen (USA) Corporation.

Vous aimerez peut-être aussi