140 likes | 202 Views
Lecture 4. PSM Review. Richard Gesick. The Major Methods of an PSM Game. Main – calls Initialize() and then loops. Any class variables need to be static Initialize() - load textures, create sprites using Sprite class, initialize variables
E N D
Lecture 4 PSM Review Richard Gesick
The Major Methods of an PSM Game Main – calls Initialize() and then loops. Any class variables need to be static Initialize() - load textures, create sprites using Sprite class, initialize variables Update - handle all user input and update game state Render - handle drawing the images/graphics to the screen
NO drawing should happen in Update and No input/state changes should happen in Draw Remember to add Sprite.fcg and Sprite.vcg to the shaders folder
Content Draw your assets (using transparent backgrounds if desired) in your favorite paint program. GIMP and Paint.NET are both free. For PSM games, they should be saved as .png Create an assets folder and add your assets to it. Then add them to your project (via “Assets | Add | Add files...")
Create attributes within the class to hold the assets Texture2D bowl = new Texture2D(“/Application/Assets/bowl.png”, false); then bowlSprite= new Sprite( graphics, bowl);
publicfloat Rotation; protected Texture2D texture; publicVector3 Position; public Vector2 Center; public Vector2 Scale=Vector2.One; floatwidth,height; publicfloat Width { get{ return width * Scale.X;}} publicfloat Height { get{ return height * Scale.Y;}} publicSprite(GraphicsContextgraphics, Texture2D texture) publicvoid Render() The Sprite Class
Handle User Input continuous movement var gamePadData = GamePad.GetData (0); if ((gamePadData.Buttons & GamePadButtons.Left) != 0) { sprite.Position.X -= speed; }
var gamePadData = GamePad.GetData (0); var pressedButtons = gamePadData.ButtonsDown; if ((pressedButtons & GamePadButtons.Cross) == GamePadButtons.Cross) { wepsSelect++; wepCount = 0; } Handle User Input discrete movement
Render Draw sprites to the screen (via the Render method of the Sprite class) graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f); graphics.Clear (); bg.Render (); graphics.SwapBuffers ();
Draw strings to the screen using the HighLevel.UI private static Label label1, label2; ... //as class variables UISystem.Initialize (graphics); //in initialize Scene scene = new Scene (); label1 = new Label (); label1.X = 20; label1.Y = 10; label1.Width = 300; label1.Text = "Press …”; scene.RootWidget.AddChildLast (label1); Update the text as necessary in Update() Then in Render() UISystem.Render ();
Random numbers Random r; … r = new Random(); … = r.Next(1, 4); // values 1 to 3 = r.Next(-100, 100); //values -100 to 99 = r.Next(100) //values 0 to 99
List<T> List<> is a parameterized collection class that is useful for storing dynamic collections of items; use the Add, Remove, RemoveAt, etc. methods falling_food = new List<FoodItem>(); … falling_food.Add(fi);
List<T> Remember that when you are removing from a list, you should work through it in reverse order so that the automatic resizing doesn’t cause you to skip elements. You also cannot alter a list when you are using a foreach loop