Video Game Programming Level One – Platformer
description
Transcript of Video Game Programming Level One – Platformer
VIDEO GAME PROGRAMMINGVideo Game
Programming
Level One – Platformer
INSTRUCTOR
Instructor Here
TEACHER’S ASSISTANT
TA Here
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
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
CONCEPT – Third Party Game Engines
• Third party game engines are suites of tools and code developed by one company and purchased by another to create a game.
• Some examples of third party game engines are Unreal Engine 3, Source Engine and C4 Engine.
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
FUN CONCEPT – Horizontal Flipping
• Horizontal Flipping is used with animations to reduce the number of frames needed by an animation set.
• Horizontal Flipping can be used to have “Left Walking” and “Right Walking” animations but only one set of frames.
• Look to see that Horizontal Flipping is turned on int the PLYR_WALK animation.
VIDEO GAME PROGRAMMING
STEP 2: Reviewing Engine Data
• There is already some data types set up in the Platformer Engine.
• Take a look at the following data– PlyrOnWhatSM is a State Machine that
determines whether the Sprite it is attached to is on a platform or in the air.
– My Functions here there are several prewritten functions to deal with various tasks in the platformer game.
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 3 – Levels
Objective:Add the levels to be in the game.
• Step 1 – Add the levels• Step 2 – Bypass to Level_1• Step 3 – Level_1’s Map• Step 4 – Save and Test
VIDEO GAME PROGRAMMING
STEP 2: Reviewing Engine DataCONTINUED
– 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
FUN 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 developing a game it is a waste of time to have to wait through the menus so code can be added to jump to the place we want to test.
VIDEO GAME PROGRAMMING
STEP 2: Bypass to Level_1
• 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
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
FUN CONCEPT – State Machines
• State Machines are used as a way to control the behavior of a sprite in Fun.
• State Machines are made up of different states.
• Edge cases: incoming/outgoing• Initial Actions vs. Actions
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:– 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 1 – Import the Key Actor• Step 2 – Add the Key Sprite• Step 3 – Update the PlayerLD• Step 4 – PlayerUpdateFN• Step 5 – Save and Test
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
CONCEPT – Pickups in Games
• Pickups are used for many things in games such as the rings in Sonic, or the coins in Mario Bros.
• Pickups can be used for puzzles such as the key and door scenario.
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 3: 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 CodeSprite* 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 1 – Import the Gate Actor• Step 2 – Add the Gate Sprite• Step 3 – Modify the PlayerUpdateFN• Step 4 – Save and Test
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
CONCEPT – Sprites as Map Objects
• Using sprites as static level objects makes it easy to add interactive objects to the map easily.
• These sprites are just regular sprites who’s behaviors are set up to make them seem as though they are part of the map, much like the gate in platformer.
VIDEO GAME PROGRAMMING
STEP 3: Modify PlayerUpdateFN
• Update PlayerUpdateFN with the following bold face code changes.
VIDEO GAME PROGRAMMING
STEP 3: Modify PlayerUpdateFNSprite* 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 1 – Import the Actor• Step 2 – Add the GuardLD• Step 3 – Create EnemyOnWhatSM• Step 4 – Add the Guard Sprite• Step 5 – 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
• Make a copy of PlyrOnWhatSM and name it EnemyOnWhatSM.
• Replace the code in the platform state with the following:
VIDEO GAME PROGRAMMING
EnemyOnWhatSMThis->Speed( 2 );
int y = 0;
if(This->LookAheadCollisionWithMap(DOWN, 1, 0))y = -1;
This->VectorDirection( This->DirectionX(), y );
if( This->DirectionX() )This->Animation(ENEMY_WALK);
elseThis->Animation(ENEMY_IDLE);
VIDEO GAME PROGRAMMINGSTEP 3: Create
EnemyOnWhatSMCONTINUED
• Now modify the platform to air edge which contains return PlayerJump(This);
• Replace the existing code with the following:
return false;
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.
• Step 1 – EnemyUpdateFN• Step 2 – Save and Test
VIDEO GAME PROGRAMMING
CONCEPT – Artificial Intelligence (AI)
• One of the most important parts of a game is the AI.
• In games the AI gives the illusion that characters and objects have intelligence.
• Many AI systems i.e. Neural Networks, Fuzzy Logic Systems, Genetic Algorithms, etc…
VIDEO GAME PROGRAMMINGCONCEPT – Artificial
Intelligence (AI)CONTINUED
• It is easy to make unbeatable AI’s, but hard to make balanced AI’s that still seem intelligent.
• Some well known AI examples, alien attack paths in Galaga, the guards in Metal Gear Solid, etc…
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
EnemyUpdateFNif ( 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 1 – Modify EnemyUpdateFN• Step 2 – Add GuardJump function• Step 3 – Modify GuardOnWhatSM• Step 4 – Add Collision with the Enemy• Step 5 – 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, it is already there
VIDEO GAME PROGRAMMING
CONCEPT – Customizing Open Source Game Engines
• When using an open source 3rd party game engine, it can be easier to copy one of the functions and simply modify it instead of creating the function yourself.
• Modification rules are covered in the open source aggreement.
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 componentfloat 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 – The Menus
Objective:Put the maps into the menu levels and add in behavior to start the game.
• Step 1 – Add the maps• Step 2 – Add the behaviors• Step 3 – Modify the Game’s OnStart• Step 4 – Save and Test
VIDEO GAME PROGRAMMING
STEP 1 & 2:
• Create the maps, and then add the maps to the TitleScreen level and add behaviors to navigate these menus. Any behavior for the menu is ok such as a timer, waiting for a key to be pressed, or taking input from the mouse.
VIDEO GAME PROGRAMMING
STEP 3: Modify the Game’s OnStart
• Modify the game’s OnStart code to start with the TitleScreen.
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:– The Levels should fade in and out when
switching.
VIDEO GAME PROGRAMMING
PART 11 – 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.