Video Game Programming Level One – Platformer

Download Report

Transcript Video Game Programming Level One – Platformer

VIDEO GAME PROGRAMMING
INSTRUCTOR:
Video Game
Programming
<Your Name Here>
Level One – Platformer
VIDEO GAME PROGRAMMING
PART 1 –Platformer Basics
Objective:
Unpack the platform engine and perform
a test build.
• Step 1 – Platformer Setup
• Step 2 – Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – Third Party Game Engines
• Some game developers create their
games using in house game engines,
which are simply a framework and
series of tools from which you can
create a game. For instance, the
ProjectFUN Editor is a game engine.
• They also put these pieces of software
on the market for other developers to
use.
VIDEO GAME PROGRAMMING
CONCEPT – Third Party Game Engines
Continued ...
• In this project, you will be getting the
Platformer Engine, which is something
written in the ProjectFUN Editor to
ease the creation of platformer-style
games.
• The Platformer Engine has all the
base code that you need to create a
simple platforming game.
VIDEO GAME PROGRAMMING
STEP 1: Platformer Setup
• Open Platform Engine.fun
• Change the window title to
Platformer (Your Name)
• Save the project as
Platformer_(Your Name).fun.
VIDEO GAME PROGRAMMING
STEP 2: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– The project should build without any errors.
VIDEO GAME PROGRAMMING
PART 2 – Initial Asset Setup
Objective:
Import the actors and animation sets
using the Import Wizard. Look at some
options to optimize the animations.
• Step 1 – The Import Wizard
• Step 2 – Reviewing Engine Data
• Step 3 – Save and Test
VIDEO GAME PROGRAMMING
STEP 1: The Import Wizard
• The Import Wizard is located in the
Resources menu.
• Open the Import Wizard and use it
to import the PLYR Actor from
platformer11.fun.
VIDEO GAME PROGRAMMING
CONCEPT – Horizontal Flipping
• Consider the situation where you want a
character to walk both left and right;
normally you would need to make two
separate animations for this.
• The ProjectFUN Editor automates this
process with Horizontal Flipping. H-Flip flips
the animation when the movement
direction changes signs (e.g. From positive
to negative).
• NOTE: H-Flip assumes that the character is
initially right-facing.
VIDEO GAME PROGRAMMING
STEP 2: Reviewing Engine Data
• There is already code and some Local
Data entered into the Platformer Engine.
• Take a look at the following code:
– PlyrOnWhatSM: Determines if the owning
Sprite is on a platform or in the air, and reacts
accordingly.
– My Functions: These are some useful prewritten functions that ease the creation of your
game, as well as providing detailed error
messages when something goes wrong.
VIDEO GAME PROGRAMMING
STEP 2: Reviewing Engine Data
CONTINUED
– GravityFactor is a global value used
for gravity.
– PlayerLD is Local data to store values
such as the player’s jumping power and
speed.
VIDEO GAME PROGRAMMING
STEP 3: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– The project should build without any errors.
VIDEO GAME PROGRAMMING
PART 3 – Levels
Objective:
Add the levels to be in the game.
• Step
• Step
• Step
• Step
1
2
3
4
–
–
–
–
Add the levels
Bypass to Level_1
Level_1’s Map
Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – Adding Levels
• Sometimes it is good to add all of the
levels that are fundamentally different
at the same time.
• When levels are added later, a copy
of another level can be made so
information doesn’t need to be set up
repeatedly.
VIDEO GAME PROGRAMMING
STEP 1: Add the Levels
• Add a level called TitleScreen.
• Add a level called MainMenu.
• Add a level called Level_1.
VIDEO GAME PROGRAMMING
STEP 2: Bypass to Level_1
• When you’re creating a game that has
a lot of levels, it is hard to debug a
problem on Level 20, say, when you
have to keep playing through Levels
1-19 over and over.
• It’s a good idea to put in a “bypass”
to Level 20, so you can go straight to
the problem.
• That’s exactly what we’re doing here.
VIDEO GAME PROGRAMMING
STEP 2: Bypass to Level_1 Continued...
• Add the following code to the game’s
OnStart to skip to Level_1:
myGame->LevelName(“Level_1”);
VIDEO GAME PROGRAMMING
STEP 3: Level_1’s Map
• Add a map to Level_1.
• The collision data for the platforms
should be added to this map.
• Each platform will need collision data
pointing up on the top.
• The map should have collision data
added around the edges to keep the
player in the world; the left and right
sides of the map should have a Collision
ID of 1
VIDEO GAME PROGRAMMING
STEP 4: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– When the game is run Level_1 should come up
with the platforms map in the background.
VIDEO GAME PROGRAMMING
PART 4 – The Player
Objective:
Put the player into the game.
• Step 1 – Add the Player Sprite
• Step 2 – Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – State Machines
• In different game projects, a need arises for
controlled collections of different behaviour.
• Each “state” represents a different type of
behaviour, such as an AI-controlled guard might
patrol or chase away the player. Each behaviour,
patrolling and chasing, is a different state.
• Between two States is an “edge”, which essentially
controls if the current behaviour changes to a
different state. For instance, if the guard sees the
player, he will change from patrolling to chasing.
This change, is represented as an Edge.
• In the ProjectFUN Editor, these States and Edges
comprise what is known as a State Machine.
VIDEO GAME PROGRAMMING
CONCEPT – State Machines Continued...
• Each State Machine has a “Starting State”, which
is the default behaviour to start in. An example
with our guard would be that he starts out in the
game patrolling, and then can change states from
there.
• Additionally, every State Machine has a “Current
State”, which is the currently active behaviour.
This could be patrolling or chasing, respectively.
• Finally, each State has both an OnStart behaviour
and an Update behaviour. The OnStart is called
when the State is first activated (such as when
changing states) and the Update is called every
frame while the State is active.
VIDEO GAME PROGRAMMING
STEP 1: Add the Player Sprite
• Add the sprite “Player” to Level_1
• Give the player sprite the
PlyrOnWhatSM state machine.
• Give the player sprite the PlayerLD
local data object and set its Display
List to 1.
• Set the player’s collision to precise
and check collision with sprites and
the map.
VIDEO GAME PROGRAMMING
STEP 2: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– When the game is run the player should be
able to jump around the screen.
VIDEO GAME PROGRAMMING
PART 5 – The Key
Objective:
Put the Key pickup into the game.
• Step
• Step
• Step
• Step
• Step
1
2
3
4
5
–
–
–
–
–
Import the Key Actor
Add the Key Sprite
Update the PlayerLD
PlayerUpdateFN
Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – Pickups in Games
• In many games there is the need to add a puzzle
element to the gameplay, which can be
represented as a “pickup”.
• Pickups in games represent a goal that the
player must achieve, while fulfilling the puzzle
requirements. An example of pickups would be
the colored keys in DOOM or the golden rings in
Sonic.
• In the Platformer, the Key represents the goal
that the player must achieve to open the door to
the next level. Ideally, it will be difficult to get,
but not too difficult.
VIDEO GAME PROGRAMMING
STEP 1: Import the Key Actor
• Open the Import Wizard and import
the KEY actor from
platformer11.fun.
VIDEO GAME PROGRAMMING
STEP 2: Add the Key Sprite
• Create a new sprite and name it Key.
• Set the Key to only check collision
with other sprites.
• Set the Actor for the Key
VIDEO GAME PROGRAMMING
STEP 3: Update the PlayerLD
• Open the PlayerLD local data and add
a boolean variable named HaveKey.
• This value will be used to keep track
of whether or not the player holds the
key, so set the initial value to false.
VIDEO GAME PROGRAMMING
STEP 4: PlayerUpdateFN
Use:
This function will detect when the player
picks up the key and will change the
HaveKey flag in PlayerLD.
• Create an Object Function named
PlayerUpdateFN and enter the following
code for the function body.
• Then add this behavior to the Player sprite
VIDEO GAME PROGRAMMING
PlayerUpdateFN Code
Sprite* key = This->CollisionWithSpritePtr( "Key" );
if ( key )
{
PlayerLD *PlayerData = GetPlayerLD( This );
PlayerData->HaveKey = true;
key->DeleteFlag( true );
}
VIDEO GAME PROGRAMMING
STEP 5: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– When the game is run the player should be
able to pick up the key.
VIDEO GAME PROGRAMMING
PART 6 – The Gate
Objective:
Put the Gate into the game.
• Step
• Step
• Step
• Step
1
2
3
4
–
–
–
–
Import the Gate Actor
Add the Gate Sprite
Modify the PlayerUpdateFN
Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – Sprites as Map Objects
• Another necessity in many games are pieces of the
level that react to player input, such as a pressure
pad to a door or mine in the sand.
• In the ProjectFUN Editor, you can accomplish this
by letting Sprites masquerade as parts of the
level.
• For instance, in the Platformer, the Gate has
specific behaviour if it is opened, but it does not
move. It acts as a door to the next level, and not
as a separate moving entity. This is precisely the
concept that we’re trying to get at. The Gate
Sprite acts as if it were a piece of the level, and
not necessarily a Sprite.
VIDEO GAME PROGRAMMING
STEP 1: Import the Gate Actor
• Open the Import Wizard and import
the GATE actor from
platformer11.fun.
VIDEO GAME PROGRAMMING
STEP 2: Add the Gate Sprite
• Create a new sprite and name it
Gate.
• Set the Gate to only check collision
with other sprites.
• Set the Actor for the Gate
VIDEO GAME PROGRAMMING
STEP 3: Modify PlayerUpdateFN
• Update PlayerUpdateFN with the
following bold face code changes.
VIDEO GAME PROGRAMMING
STEP 3: Modify PlayerUpdateFN
Sprite* key = This->CollisionWithSpritePtr( "Key" );
if ( key )
{
PlayerLD *PlayerData = GetPlayerLD( This );
PlayerData->HaveKey = true;
key->DeleteFlag( true );
}
SpritePTR pGate( "Gate" );
pGate->Animation( GATE_OPENED);
if ( This->CollisionWithSprite( "Gate" ) &&
GetPlayerLD( This )->HaveKey )
myGame->NextLevel();
VIDEO GAME PROGRAMMING
STEP 4: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– When the player picks up the key the gate
should open.
VIDEO GAME PROGRAMMING
PART 7 – Level 2
Objective:
Create Level_2 by making a copy of
Level_1.
• Step 1 – Copy Level_1
• Step 2 – Modify the New Level
• Step 3 – Save and Test
VIDEO GAME PROGRAMMING
STEP 1: Copy Level_1
• Insert a copy of Level_1 and name it
Level_2.
VIDEO GAME PROGRAMMING
STEP 2: Modify the New Level
• Create a map in an art program or
use the provided level art.
• Use this map for Level_2’s map
• Add the collision data to the map.
• Change the Key’s position.
• Change the Gate’s position.
VIDEO GAME PROGRAMMING
STEP 3: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– Level_2 should display with the new map. Test
and tweak this map to better gameplay.
VIDEO GAME PROGRAMMING
PART 8 – The Enemy Guard
Objective:
Add in a guard and reuse the code for
the behavior.
• Step
• Step
• Step
• Step
• Step
1
2
3
4
5
–
–
–
–
–
Import the Actor
Add the GuardLD
Create EnemyOnWhatSM
Add the Guard Sprite
Save and Test
VIDEO GAME PROGRAMMING
STEP 1: Import the Actor
• Import the ENEMY actor from
platformer11.fun
VIDEO GAME PROGRAMMING
STEP 2: Add the GuardLD
• Add Local Data GuardLD.
• Add a boolean value Jumping with
initial value false.
• Add a float value JumpStrength with
initial value 10.5.
• Add an integer value delay with initial
value 100.
VIDEO GAME PROGRAMMING
STEP 3: Create EnemyOnWhatSM
• Import the EnemyOnWhateSM from
the platformer11.fun.
VIDEO GAME PROGRAMMING
STEP 4: Add the Guard Sprite
• Add an Enemy sprite.
• Give it the ENEMY actor and add the
GuardLD.
• Use EnemyOnWhatSM for the
behavior.
• Position the guard on the map away
from the start position of the player.
VIDEO GAME PROGRAMMING
STEP 5: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– Level_2 should display and the guard should
appear in the level.
VIDEO GAME PROGRAMMING
PART 8 – AI for the Enemy Guard
Objective:
Give the guard an update function that
will make the guard appear to be
somewhat intelligence, such as
aggressive chasing behavior.
• Step 1 – EnemyUpdateFN
• Step 2 – Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – Artificial Intelligence (AI)
• In almost every game, there is some form of
artificial intelligence (AI). This can span from
the rudimentary to the incredibly complex.
• As with our previous example, we could simply
have a guard that patrols and chases, or we
could have a perfect Chess AI.
• There are many ways to create in-game AI,
and paradigm “simplest is best” is usually the
most successful path to follow.
• Some of the methods are as follows: State
Machines, Neural Networks, Fuzzy Sensors,
and more.
VIDEO GAME PROGRAMMING
CONCEPT – Artificial Intelligence (AI)
CONTINUED
• In games, it is easy in some cases to make
incredibly stupid or incredibly brilliant AI, such
as with the perfect Chess AI example.
• The most difficult AI to achieve is the perfectly
balanced AI, one that acts intelligently, but not
in-humanly intelligent.
• In the Platformer, the AI isn’t terribly bright,
but it’s simple enough to work well for this
type of game. Other games, however, most
definitely need something more complex.
VIDEO GAME PROGRAMMING
STEP 1: EnemyUpdateFN
• Add the Object Function
EnemyUpdateFN.
• Enter the following code into the
function body.
• Then add this function to the Enemy
sprite’s behavior.
VIDEO GAME PROGRAMMING
EnemyUpdateFN
if ( This->DirectionY() == 0 )
{
GuardLD* guardData = GetGuardLD(This);
if ( guardData->delay <= 0 )
{
SpritePTR pPlayer("Player");
This->VectorDirection( pPlayer->WorldPositionX() This->WorldPositionX(), 0 );
}
}
guardData->delay = RandInt(30) + 15;
if ( guardData->delay > 0 )
guardData->delay = guardData->delay - 1;
VIDEO GAME PROGRAMMING
STEP 2: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– The guard should now go after the player.
VIDEO GAME PROGRAMMING
PART 9 –Making the Enemy Jump
Objective:
Modify the EnemyUpdateFN with code
that will make the enemy jump for the
player.
• Step
• Step
• Step
• Step
• Step
1
2
3
4
5
–
–
–
–
–
Modify EnemyUpdateFN
Add GuardJump function
Modify EnemyOnWhatSM
Add Collision with the Enemy
Save and Test
VIDEO GAME PROGRAMMING
STEP 1: Modify EnemyUpdateFN
• Modify EnemyUpdateFN by adding
the following lines of code to the
bottom of the function body.
VIDEO GAME PROGRAMMING
EnemyUpdateFN (update)
SpritePTR pPlayer("Player");
bool RightUnder =
( pPlayer->WorldPositionY() < This->WorldPositionY() ) &&
( abs( pPlayer->WorldPositionX() – This>WorldPositionX())
< 10 );
bool RealClose = This->Distance( pPlayer ) < 80;
if ( RightUnder || RealClose )
{
guardData->Jumping = true;
}
} // This is the closing bracket that is already there
VIDEO GAME PROGRAMMING
STEP 2: Add GuardJump function
• Insert a copy of the My Function
PlayerJump and name it
GuardJump.
• Modify the code to reflect the
following bold face changes.
VIDEO GAME PROGRAMMING
GuardJump (update)
GuardLD *guardld = GetGuardLD( This );
if ( guardld->Jumping )
{
float vx = SpriteVectorX(This);
// Horizontal vector component
float vy = -guardld->JumpStrength; // Vertical vector component
SpriteVectorX(This, vx);
SpriteVectorY(This, vy);
guardld->Jumping = false;
return true;
}
else
return false;
VIDEO GAME PROGRAMMING
STEP 3: Modify EnemyOnWhatSM
• Now modify the platform to air edge
which contains return false;
• Replace the existing code with the
following:
GuardJump(This);
VIDEO GAME PROGRAMMING
STEP 4: Add Collision with the Enemy
• We must now edit the
PlayerUpdateFN to allow the
player to collide with the enemy.
• Add the following code to the
bottom of the funtion:
if ( This->CollisionWithSprite( "Enemy" ) )
myGame->RestartLevel();
VIDEO GAME PROGRAMMING
STEP 5: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– The guard should jump after the player now.
VIDEO GAME PROGRAMMING
PART 10 – Smooth Transitions Between
Levels
Objective:
Add in transitional effects to the levels to
make the level switches look better.
• Step 1 – Fade in the Levels
• Step 2 – Save and Test
VIDEO GAME PROGRAMMING
STEP 1: Fade in the Levels
• Adjust the Level properties in each of
the levels to have them fade in and
out when switching.
• Adjust fade durations and tweak.
VIDEO GAME PROGRAMMING
STEP 2: Save and Test
• Save the Project:
– Click on Project and Save.
• Run the Project:
– Click on the Build/Run button in the menu
bar.
• Results:
– The Levels should fade in and out when
switching.
VIDEO GAME PROGRAMMING
Two Steps Forward, One Step Back
• By this time, you should have fully
completed the Platformer Game. There are
a few things that you can do with this,
though your primary focus should be
developing ideas for you own game.
• The easiest additions are:
– Adding new levels
– Programming in a weapon for the Player
character