-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
158 lines (127 loc) · 4.9 KB
/
Game1.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Collections.Specialized;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Pong
{
public class Game1 : Game
{
Texture2D PlayerTexture;
Texture2D LoadTexture;
Song Song;
Vector2 PlayerPos;
float ballSpeed;
Vector2 LoadPosition;
private string Scene = "Play";
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Enemy _enemy;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = false;
_enemy = new Enemy();
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
PlayerPos = new Vector2(_graphics.PreferredBackBufferWidth / 2,
_graphics.PreferredBackBufferHeight / 2);
ballSpeed = 100f;
LoadPosition = new Vector2(_graphics.PreferredBackBufferWidth / 2,
_graphics.PreferredBackBufferHeight / 2);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_enemy.SetContent(Content.Load<Texture2D>("fish"));
// TODO: use this.Content to load your game content here
PlayerTexture = Content.Load<Texture2D>("crocim");
LoadTexture = Content.Load<Texture2D>("loading_desktop_by_brianmccumber-d41z4h6");
Song = Content.Load<Song>("underwater");
MediaPlayer.Play(Song);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
{
IsMouseVisible = true;
}
// TODO: Add your update logic here
var kstate = Keyboard.GetState();
if (kstate.IsKeyDown(Keys.W))
{
PlayerPos.Y -= ballSpeed * (float) gameTime.ElapsedGameTime.TotalSeconds;
}
if (kstate.IsKeyDown(Keys.S))
{
PlayerPos.Y += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (kstate.IsKeyDown(Keys.A))
{
PlayerPos.X -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (kstate.IsKeyDown(Keys.D))
{
PlayerPos.X += ballSpeed * (float) gameTime.ElapsedGameTime.TotalSeconds;
}
if (PlayerPos.X > _graphics.PreferredBackBufferWidth - PlayerTexture.Width / 2)
PlayerPos.X = _graphics.PreferredBackBufferWidth - PlayerTexture.Width / 2;
else if (PlayerPos.X < PlayerTexture.Width / 2)
PlayerPos.X = PlayerTexture.Width / 2;
if (PlayerPos.Y > _graphics.PreferredBackBufferHeight - PlayerTexture.Height / 2)
PlayerPos.Y = _graphics.PreferredBackBufferHeight - PlayerTexture.Height / 2;
else if (PlayerPos.Y < PlayerTexture.Height / 2)
PlayerPos.Y = PlayerTexture.Height / 2;
_enemy.Update(PlayerPos);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
if (Scene == "Play")
{
GraphicsDevice.Clear(Color.CornflowerBlue);
}
else if (Scene == "Loading1")
{
GraphicsDevice.Clear(Color.Black);
}
// TODO: Add your drawing code here
if (Scene == "Play")
{
_spriteBatch.Begin();
_spriteBatch.Draw(
PlayerTexture,
PlayerPos,
null,
Color.White,
0f,
new Vector2(PlayerTexture.Width / 2, PlayerTexture.Height / 2),
Vector2.One,
SpriteEffects.None,
0f);
_enemy.Draw(_spriteBatch);
_spriteBatch.End();
}
else if (Scene == "Loading1")
{
_spriteBatch.Begin();
_spriteBatch.Draw(
LoadTexture,
LoadPosition,
null,
Color.White,
0f,
new Vector2(LoadTexture.Width / 2, LoadTexture.Height / 2),
Vector2.One,
SpriteEffects.None,
0f);
_spriteBatch.End();
}
base.Draw(gameTime);
}
}
}