Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gameprogrammeren: Klassen en objecten in Painter

Similar presentations


Presentation on theme: "Gameprogrammeren: Klassen en objecten in Painter"— Presentation transcript:

1 Gameprogrammeren: Klassen en objecten in Painter
Arjan Egges Paul Bergervoet Wouter van Toll

2 Onderwerpen in dit voorbeeld
Variabelen Klassen en objecten Properties Static Willekeurigheid

3 Game-objecten ontwerpen
Wat doet een game-object? Invoer van de speler afhandelen Zichzelf bijwerken (update) Zichzelf tekenen (draw) Bijbehorende methoden: HandleInput, Update, Draw Daarnaast: Reset ...?

4 Klasse Cannon met methoden
public void Draw(GameTime gameTime, SpriteBatch spriteBatch) { spriteBatch.Draw(cannonBarrel, position, null, Color.White, angle, barrelOrigin, 1f, SpriteEffects.None, 0); spriteBatch.Draw(currentColor, position, null, Color.White, 0f, colorOrigin, 1f, SpriteEffects.None, 0); } public void Reset() currentColor = colorBlue; color = Color.Blue; angle = 0.0f;

5 Property voor lezen positie
Type van de property Naam van de property public Vector2 Position { get return position; } Property lezen Zo gebruiken we de property: Vector2 cannonPos = cannon.Position;

6 public Color Color { get { return color; } set if (value != Color.Red && value != Color.Green && value != Color.Blue) return; color = value; if (color == Color.Red) currentColor = colorRed; else if (color == Color.Green) currentColor = colorGreen; else if (color == Color.Blue) currentColor = colorBlue; } Property lezen Controleren of het wel mag. Property schrijven value bevat de rechterkant van de toekenning

7 GameWorld-klasse class GameWorld { Texture2D background;
Cannon cannon; public GameWorld(ContentManager Content) background = Content.Load<Texture2D>("spr_background"); cannon = new Cannon(Content); } public void HandleInput(InputHelper inputHelper) cannon.HandleInput(inputHelper); ... Game wereld bestaat uit alle game objecten

8 ... public void Update(GameTime gameTime) { } public void Draw(GameTime gameTime, SpriteBatch spriteBatch) spriteBatch.Begin(); spriteBatch.Draw(background, Vector2.Zero, Color.White); cannon.Draw(gameTime, spriteBatch); spriteBatch.End(); public Cannon Cannon get { return cannon; }

9 GraphicsDeviceManager graphics; SpriteBatch spriteBatch;
class Painter : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; InputHelper inputHelper; static GameWorld gameWorld; protected override void LoadContent() spriteBatch = new SpriteBatch(GraphicsDevice); gameWorld = new GameWorld(Content); } public static GameWorld GameWorld get { return gameWorld; } Static membervariabele Constructie van de gamewereld Static property om erbij te kunnen

10 protected override void Update(GameTime gameTime)
{ inputHelper.Update(); gameWorld.HandleInput(inputHelper); gameWorld.Update(gameTime); } protected override void Draw(GameTime gameTime) GraphicsDevice.Clear(Color.White); gameWorld.Draw(gameTime, spriteBatch);

11 Ball-klasse class Ball { Texture2D colorRed, colorGreen, colorBlue;
Texture2D currentColor; Vector2 position, velocity; Color color; bool shooting; // methoden en properties } De verfbal heeft twee toestanden

12 Ball: invoer afhandelen
“Je mag niet schieten tijdens het schieten” public void HandleInput(InputHelper inputHelper) { if (inputHelper.MouseLeftButtonPressed() && !shooting) shooting = true; velocity = (inputHelper.MousePosition - position) / 40; } Bereken de snelheid aan de hand van de muispositie

13 Ball: Update public void Update(GameTime gameTime) { if (shooting)
{ velocity.X *= 0.99f; velocity.Y += 6f; position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds; } else { Color = Painter.GameWorld.Cannon.Color; position = Painter.GameWorld.Cannon.BallPosition - Center; if (Painter.GameWorld.IsOutsideWorld(position)) Reset(); Luchtweerstand Zwaartekracht Hoe vinden we uit hoe groot de wereld is?

14 Schermdimensies (Painter-klasse)
Bewaren we in een static membervariabele: Initialisatie in LoadContent: En een (static) property om erbij te kunnen: static Point screen; screen = new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); public static Point Screen { get { return screen; } }

15 IsOutsideWorld-methode
In de GameWorld-klasse: public bool IsOutsideWorld(Vector2 position) { return position.X < 0 || position.X > Painter.Screen.X || position.Y > Painter.Screen.Y; }

16 Klassen met meerdere instanties
Klassedefinitie: In de GameWorld-klasse: class PaintCan { } PaintCan can1, can2, can3;

17 willekeurige snelheid
PaintCan-klasse Elke verfbus heeft: Sprites Positie Snelheid Kleur Doelkleur Minimumsnelheid class PaintCan { Texture2D colorRed, colorGreen, colorBlue; Texture2D currentColor; Vector2 position, velocity; Color color, targetcolor; float minVelocity; // methoden… } Gebruiken we om willekeurige snelheid te berekenen!

18 Painter-klasse uitbreiden
static Random membervariabele: Met een (static) property om erbij te kunnen: static Random random; public static Random Random { get { return random; } }

19 Random snelheid en kleur
Willekeurige snelheid in de PaintCan-klasse Het bepalen van een willekeurige kleur: return new Vector2(0.0f, (float)Painter.Random.NextDouble() / 2 + minVelocity); public Color CalculateRandomColor() { int randomval = random.Next(3); if (randomval == 0) return Color.Red; else if (randomval == 1) return Color.Green; else return Color.Blue; }

20 PaintCan-object maken
public PaintCan(ContentManager Content, float positionOffset, Color targetcol) { this.colorRed = Content.Load<Texture2D>("spr_can_red"); this.colorGreen = Content.Load<Texture2D>("spr_can_green"); this.colorBlue = Content.Load<Texture2D>("spr_can_blue"); targetcolor = targetcol; minVelocity = 30; Color = Color.Blue; position = new Vector2(positionOffset, -currentColor.Height); velocity = Vector2.Zero; }

21 class GameWorld { Texture2D background; Ball ball; PaintCan can1, can2, can3; Cannon cannon; public GameWorld(ContentManager Content) background = Content.Load<Texture2D>("spr_background"); cannon = new Cannon(Content); ball = new Ball(Content); can1 = new PaintCan(Content, 450.0f, Color.Red); can2 = new PaintCan(Content, 575.0f, Color.Green); can3 = new PaintCan(Content, 700.0f, Color.Blue); } ...

22 Game-objecten De Update-methode: Draw-methode Zie de voorbeeldcode
public void Update(GameTime gameTime) { ball.Update(gameTime); can1.Update(gameTime); can2.Update(gameTime); can3.Update(gameTime); }

23 Handige properties public Ball Ball { get { return ball; } }
public Cannon Cannon get { return cannon; }

24 PaintCan-object updaten
if (velocity.Y == 0.0f && Painter.Random.NextDouble() < 0.01) { velocity = CalculateRandomVelocity(); Color = CalculateRandomColor(); } position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds; ...

25 PaintCan-object updaten
... Vector2 distanceVector = (Painter.GameWorld.Ball.Position + Painter.GameWorld.Ball.Center) - (position + Center); if (Math.Abs(distanceVector.X) < Center.X && Math.Abs(distanceVector.Y) < Center.Y) { Color = Painter.GameWorld.Ball.Color; Painter.GameWorld.Ball.Reset(); } if (Painter.GameWorld.IsOutsideWorld(position)) Reset(); minVelocity += 0.001f;


Download ppt "Gameprogrammeren: Klassen en objecten in Painter"

Similar presentations


Ads by Google