-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.cs
76 lines (74 loc) · 2.32 KB
/
Paddle.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace AsteroidGame
{
class Paddle
{
public float X { get; set; } //x position of paddle on screen
public float Y { get; set; } //y position of paddle on screen
public float Width { get; set; } //width of paddle
public float Height { get; set; } //height of paddle
public float ScreenWidth { get; set; } //width of game screen
private Texture2D imgPaddle { get; set; } //cached image of the paddle
private SpriteBatch spriteBatch; //allows us to write on backbuffer when we need to draw self
// All the overloads in the public Paddle let the rest of the game be able to interact with the cariables of the Paddle
public Paddle(float x, float y, float screenWidth, SpriteBatch spriteBatch, GameContent gameContent)
{
X = x;
Y = y;
imgPaddle = gameContent.Paddle;
Width = imgPaddle.Width;
Height = imgPaddle.Height;
this.spriteBatch = spriteBatch;
ScreenWidth = screenWidth;
}
public void Draw()
{
spriteBatch.Draw(imgPaddle, new Vector2(X, Y), null, Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0);
}
public void MoveLeft()
{
X = X - 4;
if (X < 1)
{
X = 1;
}
}
public void MoveRight()
{
X = X + 4;
if ((X + Width) > ScreenWidth)
{
X = ScreenWidth - Width;
}
}
public void MoveTo(float x)
{
if (x >= 0)
{
if (x < ScreenWidth - Width)
{
X = x;
}
else
{
X = ScreenWidth - Width;
}
}
else
{
if (x < 0)
{
X = 0;
}
}
}
}
}