-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathSprite.cs
52 lines (45 loc) · 1.59 KB
/
Sprite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics; // for Texture2D
using Microsoft.Xna.Framework; // for Vector2
namespace Microsoft.Xna.Samples.GameComponents
{
class Sprite : DrawableGameComponent
{
private readonly Texture2D texture; // sprite texture
private Vector2 position; // sprite position on screen
private Vector2 speed; // speed in pixels
private readonly SpriteBatch spriteBatch;
public Sprite(Game game, Texture2D Texture, Vector2 Position, Vector2 Speed, SpriteBatch spriteBatch)
: base(game)
{
this.texture = Texture;
this.position = Position;
this.spriteBatch = spriteBatch;
this.speed = Speed;
}
public override void Update(GameTime gameTime)
{
// Keep inside the screen
// right
if(position.X + texture.Width + speed.X > Game.Window.ClientBounds.Width)
speed.X = -speed.X;
// bottom
if (position.Y + texture.Height + speed.Y > Game.Window.ClientBounds.Height)
speed.Y = -speed.Y;
// left
if (position.X + speed.X < 0)
speed.X = -speed.X;
// top
if (position.Y + speed.Y < 0)
speed.Y = -speed.Y;
// update position
position += speed;
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
}