Difference between revisions of "Game Programming Class4"

esse quam videri
Jump to: navigation, search
(Sprite class)
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
  
 +
==Objectives==
 +
*Unified Input Controller Monogame
 +
*Mongame Sprites
 +
*Refactoring classes to common game library
 +
*Separation of Concern where should classes like
 +
==Skills==
 +
*GameComponents Monogame
 +
*SVN Commit
 +
==Demo==
 +
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/PacManSprite2Component Monogame\3.5\PacManSprite2Component] : Sprite class
 +
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/GameComponents Monogame\3.5\GameComponents] : Monogame reusable components that follow game loop
 +
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/FPS Monogame\3.5\FPS] : Game component
  
===IN Class demo for Part 1===
+
==Home Work==
 +
*Refactor interesting movement using game component and two players with separate input controls.
  
http://iam.colum.edu/XNAProgramming/IntroSimpleSpriteUpdateGravityWindows.zip
 
 
===Vibration===
 
<csharp>
 
        GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);  //left low freq motor right high freq motor
 
       
 
</csharp>
 
  
 
==in class==
 
==in class==
  
 
Extract sprite from pacman class and build new project
 
Extract sprite from pacman class and build new project
 
http://www.xnawiki.com/index.php?title=Basic_Sprite_Class
 
  
 
===Sprite class===
 
===Sprite class===
  
IntroPacManComponent.zip
+
[https://github.com/dsp56001/GameProgramming/blob/master/Monogame/3.5/MonoGameLibrary/Sprite2/Sprite2.cs Monogame/3.5/MonoGameLibrary/Sprite2.cs]
 
+
[https://github.com/dsp56001/GameProgramming/blob/master/Monogame/3.5/MonoGameLibrary/Sprite2/DrawableSprite2.cs Monogame/3.5/MonoGameLibrary/DrawableSprite2.cs]
Project that refactors the pacman class into two classes
 
 
 
Sprite.cs
 
DrawableSprite.cs
 
  
 
Then the pacman class can inherit from one of these two classes.
 
Then the pacman class can inherit from one of these two classes.
  
PacMan.cs
+
[https://github.com/dsp56001/GameProgramming/blob/master/Monogame/3.5/PacManComponentFromLibrary/PacMan.cs PacMan.cs]
  
 
Due to the desire to limit the number of spritebatches, I've created two classes. The sprite class cannot draw itself without being associated with and external spritebatch.
 
Due to the desire to limit the number of spritebatches, I've created two classes. The sprite class cannot draw itself without being associated with and external spritebatch.
 
The DrawableSprite has it's own spritebatch which makes it easier to use but less efficient.
 
The DrawableSprite has it's own spritebatch which makes it easier to use but less efficient.
 
===Classes and Generics===
 
Shooting with pacman
 
 
List<Type> Safer than Arraylist and easier to manage than Array
 
  
 
==Game Components==
 
==Game Components==
Line 63: Line 58:
 
'''DrawOrder'''
 
'''DrawOrder'''
 
:Order in which the component should be drawn, relative to other components that are in the same GameComponentCollection.
 
:Order in which the component should be drawn, relative to other components that are in the same GameComponentCollection.
 +
 +
===Frame Per Second Counter===
 +
<syntaxhighlight lang="csharp" >
 +
using Microsoft.Xna.Framework;
 +
using Microsoft.Xna.Framework.Graphics;
 +
using Microsoft.Xna.Framework.Input;
 +
using System;
 +
 +
namespace GameWindowsFPS
 +
{
 +
    public class Game1 : Microsoft.Xna.Framework.Game
 +
    {
 +
        GraphicsDeviceManager graphics;
 +
        SpriteBatch spriteBatch;
 +
 +
        float CumulativeFrameTime;
 +
        int NumFrames;
 +
        int FramesPerSecond;
 +
        SpriteFont Font;
 +
 +
        public Game1()
 +
        {
 +
            graphics = new GraphicsDeviceManager(this);
 +
            Content.RootDirectory = "Content";
 +
 +
            //Set to 30 Frames a Second
 +
            //TargetElapsedTime = TimeSpan.FromTicks(333333);
 +
 +
            //Don't set Fix Time
 +
            //this.IsFixedTimeStep = false;
 +
            //graphics.SynchronizeWithVerticalRetrace = false;
 +
            //this.TargetElapsedTime = TimeSpan.FromSeconds(1 / 60f);
 +
        }
 +
 +
 +
        protected override void Initialize()
 +
        {
 +
            // TODO: Add your initialization logic here
 +
 +
            base.Initialize();
 +
        }
 +
 +
 +
        protected override void LoadContent()
 +
        {
 +
            // Create a new SpriteBatch, which can be used to draw textures.
 +
            spriteBatch = new SpriteBatch(GraphicsDevice);
 +
 +
            Font = Content.Load<SpriteFont>("SpriteFont1");
 +
        }
 +
 +
 +
        protected override void UnloadContent()
 +
        {
 +
 +
        }
 +
 +
 +
        protected override void Update(GameTime gameTime)
 +
        {
 +
            // Allows the game to exit
 +
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
 +
                this.Exit();
 +
 +
            // TODO: Add your update logic here
 +
 +
            base.Update(gameTime);
 +
        }
 +
 +
 +
        protected override void Draw(GameTime gameTime)
 +
        {
 +
            GraphicsDevice.Clear(Color.CornflowerBlue);
 +
 +
            CumulativeFrameTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000f;
 +
            NumFrames++;
 +
            if (CumulativeFrameTime >= 1f)
 +
            {
 +
                FramesPerSecond = NumFrames;
 +
                NumFrames = 0;
 +
                CumulativeFrameTime -= 1f;
 +
            }
 +
            spriteBatch.Begin();
 +
            spriteBatch.DrawString(Font, "FPS: " + FramesPerSecond.ToString(), new Vector2(0, 0), Color.Black);
 +
            spriteBatch.End();
 +
            base.Draw(gameTime);
 +
        }
 +
    }
 +
   
 +
}
 +
</syntaxhighlight>
 +
 +
TODO in class: Refactor this FPS game into a GameComponent. Does it need to Draw? Then refactor into a library project that can be reused.
  
 
==Game Service==
 
==Game Service==
Line 77: Line 165:
 
*Scoreboard
 
*Scoreboard
  
Microsoft has added the
 
*[http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamerservices.aspx GamerServices]
 
  
  
 
===InputHandler Service===
 
===InputHandler Service===
  
In a separate Library Class. We are going to start using this library for resuable components of our games.  
+
In a separate Library Class. We are going to start using this library for reusable components of our games.  
  
 
Useful info missing from the XNA input states
 
Useful info missing from the XNA input states
Line 98: Line 184:
  
 
          
 
          
<csharp>     
+
<syntaxhighlight lang="csharp">     
 
         GameConsole gameConsole;
 
         GameConsole gameConsole;
  
Line 110: Line 196:
 
         }
 
         }
 
              
 
              
</csharp>
+
</syntaxhighlight>
  
 
IntroConsole/Game1.cs
 
IntroConsole/Game1.cs
  
 
==Homework==
 
==Homework==
*Use the fps component to create a baseline log
+
*Use the fps component to create a baseline log for you project in Monogame note the number of frames you get when you set
*Convert Project 3 to use the inputHandler service
 
 
 
Bonus
 
* Create your own game service and use it in an xna project
 
 
 
==Criteria for One button Games==
 
  
Criteria
+
*Convert Monogame Interesting movement Project  to use the inputHandler service and a playerContoller class the uses good separation of concern for input
*2 pt unified code base
 
*1 pt using input
 
*2 pt is it a game
 

Latest revision as of 02:22, 27 September 2016

Objectives

  • Unified Input Controller Monogame
  • Mongame Sprites
  • Refactoring classes to common game library
  • Separation of Concern where should classes like

Skills

  • GameComponents Monogame
  • SVN Commit

Demo

Home Work

  • Refactor interesting movement using game component and two players with separate input controls.


in class

Extract sprite from pacman class and build new project

Sprite class

Monogame/3.5/MonoGameLibrary/Sprite2.cs Monogame/3.5/MonoGameLibrary/DrawableSprite2.cs

Then the pacman class can inherit from one of these two classes.

PacMan.cs

Due to the desire to limit the number of spritebatches, I've created two classes. The sprite class cannot draw itself without being associated with and external spritebatch. The DrawableSprite has it's own spritebatch which makes it easier to use but less efficient.

Game Components

About components Application Overview

Game Components are reusable and easy to add to a game.

  • GameComponent Class
  • DrawGameComponent Class

http://blogs.msdn.com/xna/archive/2006/08/31/734204.aspx

Common methods

Enabled

Indicates whether GameComponent.Update should be called when Game.Update is called.

UpdateOrder

Indicates the order in which the GameComponent should be updated relative to other GameComponent instances. Lower values are updated first.

(available if it is a DrawableGameComponent)

Visible

Indicates whether Draw should be called.

DrawOrder

Order in which the component should be drawn, relative to other components that are in the same GameComponentCollection.

Frame Per Second Counter

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace GameWindowsFPS
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        float CumulativeFrameTime;
        int NumFrames;
        int FramesPerSecond;
        SpriteFont Font;

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

            //Set to 30 Frames a Second
            //TargetElapsedTime = TimeSpan.FromTicks(333333);

            //Don't set Fix Time
            //this.IsFixedTimeStep = false;
            //graphics.SynchronizeWithVerticalRetrace = false;
            //this.TargetElapsedTime = TimeSpan.FromSeconds(1 / 60f);
        }


        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }


        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Font = Content.Load<SpriteFont>("SpriteFont1");
        }


        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            CumulativeFrameTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000f;
            NumFrames++;
            if (CumulativeFrameTime >= 1f)
            {
                FramesPerSecond = NumFrames;
                NumFrames = 0;
                CumulativeFrameTime -= 1f;
            }
            spriteBatch.Begin();
            spriteBatch.DrawString(Font, "FPS: " + FramesPerSecond.ToString(), new Vector2(0, 0), Color.Black);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
    
}

TODO in class: Refactor this FPS game into a GameComponent. Does it need to Draw? Then refactor into a library project that can be reused.

Game Service

A game service is a game component that uses an interface to enforce a singleton pattern and make a single instance of a game component accessible to all other components. It's a nice mechanism to maintain a loose coupling between components.

A singleton is a class that only allows a single instance of the class to be created. A game service is not a true singleton as it uses a unique interface to identify and expose it at the game level.

A game service is a built in mechanism that allows a game to create and manage a single instance of a component ans allows other components access to the service.

Good candidates for a game service are.

  • InputHandler
  • GameConsole
  • Profiling Service
  • Scoreboard


InputHandler Service

In a separate Library Class. We are going to start using this library for reusable components of our games.

Useful info missing from the XNA input states

WasPressed determines is a button was press between the last update and the current update. To do this we need to store that keyboard and controller states from the previous call to update.

Console Class

Separate IntroGameLibrary Project to help Encapsulate Reusable Code.

  • IntroConsole.zip
  • IntroGameLibrary.zip

IntroGameLibrary/GameConsole.cs To add the Component to the Game we make an declare in the game class. Then we need to add the component to the Game Components Collection.


    
        GameConsole gameConsole;

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

            gameConsole = new GameConsole(this);
            this.Components.Add(gameConsole);
        }

IntroConsole/Game1.cs

Homework

  • Use the fps component to create a baseline log for you project in Monogame note the number of frames you get when you set
  • Convert Monogame Interesting movement Project to use the inputHandler service and a playerContoller class the uses good separation of concern for input