Game Programming Class3

esse quam videri
Revision as of 18:28, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
Jump to: navigation, search

Class 3 Input Handling

Inclass Keyboard input demo

KeyboardState keyboardState = Keyboard.GetState();

Explore Limitations of Built in Keyboard state.

Input

input from Game Controller and Keyboard

GamePad XBoxGamePadWithLabels.png

XNA has built in support for up to 4 game controllers. The default controller is a x-box controller. X-Box Controller The Controller is accessed through the GamePad Object and the GamePadState Structure reference types

 1          //Get an instance of the gamePadState Structure
 2          GamePadState gamePad1State = GamePad.GetState(PlayerIndex.One); 
 3          
 4 /*Since the Keyboard structure is Windows only we 
 5 need to use some preprocessor
 6 directives to only compile the KeyBoard 
 7 state code if the target is not XBOX360 (I
 8 should also add zune if this is one of our build targets)
 9 */
10          #if !XBOX360
11             #region KeyBoard
12             KeyboardState keyboardState = Keyboard.GetState();
13          #endif

Angle Measured in Radians

using atan2 to get the angle from the direction vector


IntroSimpleSpriteUpdateRotateWindows Project

SpriteJump Project

Input Error missing DirectX9

Error:An unhandled exception of type 'System.DllNotFoundException' occurred in SharpDX.XInput.dll Additional information: Unable to load DLL 'xinput1_3.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

  XInput library may be missing if you have a fresh install of win8.1 or 10

Solution: Install DirectX 9 Runtime http://www.microsoft.com/en-us/download/confirmation.aspx?id=8109 or http://www.microsoft.com/en-us/download/details.aspx?id=35

Unity Basics and Movement

Demo add Art and setup folders

Movement script

parameter

1 public Vector3 Direction = new Vector3(1, 0, 0);
2     public float Speed = 5;

Update

1 void Update () {
2 
3         //Move Pacman
4         this.transform.position += this.Direction * this.Speed * Time.deltaTime;
5 }

Full Script with wall bouncing

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class SimpleMove : MonoBehaviour {
 5 
 6 
 7     public Vector3 Direction = new Vector3(1, 0, 0);
 8     public float Speed = 5;
 9 
10     static Vector3 cameraBottomLeft; //calculate camera left
11     static Vector3 cameraTopRight; //calculate camera top
12     static Rect cameraRect; //rectangle for camera
13 
14 
15     // Use this for initialization
16 	void Start () {
17 
18         cameraBottomLeft = Camera.main.ScreenToWorldPoint(Vector3.zero);
19         cameraTopRight = Camera.main.ScreenToWorldPoint(new Vector3(
20             Camera.main.pixelWidth, Camera.main.pixelHeight));
21 
22         cameraRect = new Rect(
23             cameraBottomLeft.x,
24             cameraBottomLeft.y,
25             cameraTopRight.x - cameraBottomLeft.x,
26             cameraTopRight.y - cameraBottomLeft.y);
27 
28 	}
29 	
30 	// Update is called once per frame
31 	void Update () {
32 
33         //Move Pacman
34         this.transform.position += this.Direction * this.Speed * Time.deltaTime;
35 
36         if (!cameraRect.Contains(this.transform.position))
37         {
38             //keep  on screen
39             if (this.transform.position.x <= cameraRect.xMin)
40             {
41                 
42                 this.Direction.x *= -1;
43             }
44             if (this.transform.position.x >= cameraRect.xMax)
45             {
46                 
47                 this.Direction.x *= -1;
48             }
49             if (this.transform.position.y > cameraRect.yMax)
50             {
51                 
52                 this.Direction.y *= -1;
53             }
54             if (this.transform.position.y < cameraRect.yMin)
55             {
56                 
57                 this.Direction.y *= -1;
58             }
59             
60         }
61 
62 	}
63 }

and Input from keyboard

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class SimpleInput : MonoBehaviour {
 5 
 6 
 7     public Vector3 Direction;
 8     public float Speed;
 9     public Vector3 keyDirection;
10     
11     // Use this for initialization
12 	void Start () {
13 	
14 	}
15 	
16 	// Update is called once per frame
17 	void Update () {
18         //input
19         //direction.x = direction.y = 0;
20         keyDirection.x = keyDirection.y = 0;
21 
22         //Keyboard
23         if (Input.GetKey("right"))
24         {
25             keyDirection.x += 1;
26         }
27         if (Input.GetKey("left"))
28         {
29             keyDirection.x += -1;
30         }
31 
32         if (Input.GetKey("up"))
33         {
34             keyDirection.y += 1;
35         }
36         if (Input.GetKey("down"))
37         {
38             keyDirection.y += -1;
39         }
40 
41         Direction += keyDirection;
42         Direction.Normalize();
43 
44 
45         //Move Pacman
46         this.transform.position += this.Direction * this.Speed * Time.deltaTime;
47 
48 	}
49 }

Homework

Moving Game 2 Interesting movement (Mono Game and Unity)

Update your moving assigment to make the movement more interesting. Use more than 1 input method ie Keyboard, xbox controller, mouse etc.

Create a Unity Scene with the same sprite and use a script to create similar movement.

Commit the mongame project and an export of the Unity Scene as a package to SVN or Moodle.

Repo URL https://iam.colum.edu:8443/svn/GPMonogame3/trunk/SP15