-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame1.cs
More file actions
141 lines (115 loc) · 3.76 KB
/
Game1.cs
File metadata and controls
141 lines (115 loc) · 3.76 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace mgContentlessx64;
public class Game1 : Game
{
Texture2D ballTexture;
Vector2 ballPosition;
float ballSpeed;
int deadZone;
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
ballPosition = new Vector2(_graphics.PreferredBackBufferWidth / 2,
_graphics.PreferredBackBufferHeight / 2);
ballSpeed = 100f;
deadZone = 4096;
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
ballTexture = Content.Load<Texture2D>("ball");
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
// The time since Update was called last.
float updatedBallSpeed = ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
var kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.Up))
{
ballPosition.Y -= updatedBallSpeed;
}
if (kstate.IsKeyDown(Keys.Down))
{
ballPosition.Y += updatedBallSpeed;
}
if (kstate.IsKeyDown(Keys.Left))
{
ballPosition.X -= updatedBallSpeed;
}
if (kstate.IsKeyDown(Keys.Right))
{
ballPosition.X += updatedBallSpeed;
}
if(Joystick.LastConnectedIndex == 0)
{
JoystickState jstate = Joystick.GetState((int) PlayerIndex.One);
if (jstate.Axes[1] < -deadZone)
{
ballPosition.Y -= updatedBallSpeed;
}
else if (jstate.Axes[1] > deadZone)
{
ballPosition.Y += updatedBallSpeed;
}
if (jstate.Axes[0] < -deadZone)
{
ballPosition.X -= updatedBallSpeed;
}
else if (jstate.Axes[0] > deadZone)
{
ballPosition.X += updatedBallSpeed;
}
}
if (ballPosition.X > _graphics.PreferredBackBufferWidth - ballTexture.Width / 2)
{
ballPosition.X = _graphics.PreferredBackBufferWidth - ballTexture.Width / 2;
}
else if (ballPosition.X < ballTexture.Width / 2)
{
ballPosition.X = ballTexture.Width / 2;
}
if (ballPosition.Y > _graphics.PreferredBackBufferHeight - ballTexture.Height / 2)
{
ballPosition.Y = _graphics.PreferredBackBufferHeight - ballTexture.Height / 2;
}
else if (ballPosition.Y < ballTexture.Height / 2)
{
ballPosition.Y = ballTexture.Height / 2;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.Draw(
ballTexture,
ballPosition,
null,
Color.White,
0f,
new Vector2(ballTexture.Width / 2, ballTexture.Height / 2),
Vector2.One,
SpriteEffects.None,
0f
);
_spriteBatch.End();
base.Draw(gameTime);
}
}