-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPong.cs
103 lines (91 loc) · 3.56 KB
/
Pong.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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using pong.objects;
using static pong.objects.InputHandler;
using static pong.objects.Paddle;
namespace pong;
public class Pong : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private InputHandler _inputHandler;
private Texture2D _texture2D;
private Paddle LeftPaddle;
private Paddle RightPaddle;
private GameBall Ball;
private GameCounter Counter;
private Song newDestinationsSong;
public Pong()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
_inputHandler = new InputHandler();
_graphics.IsFullScreen = true;
_graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
_graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
_graphics.ApplyChanges();
}
protected override void Initialize()
{
base.Initialize();
CreatePaddles();
Counter = new GameCounter(Content.Load<SpriteFont>("Arial"));
newDestinationsSong = Content.Load<Song>("songs/LOOP_Your Mind Posessed!");
MediaPlayer.IsRepeating = true;
MediaPlayer.Volume = 0.5f; // 50% volume
MediaPlayer.Play(newDestinationsSong);
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_texture2D = new Texture2D(GraphicsDevice, 1, 1);
_texture2D.SetData(new[] { Color.White });
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
InputUpdate(deltaTime);
Ball.Move(RightPaddle, LeftPaddle, Counter, deltaTime);
base.Update(gameTime);
}
private void InputUpdate(float deltaTime)
{
UpdatePaddle(LeftPaddle, _inputHandler.GetLeftPaddleDirection, _inputHandler.IsLeftLaunchPressed, deltaTime);
UpdatePaddle(RightPaddle, _inputHandler.GetRightPaddleDirection, _inputHandler.IsRightLaunchPressed, deltaTime);
}
private void UpdatePaddle(Paddle paddle, Func<Direction> getDirection, Func<bool> isLaunchPressed, float deltaTime)
{
Direction direction = getDirection();
paddle.Move(direction, deltaTime);
if (isLaunchPressed())
Ball.Launch(paddle.PaddleDirection);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
LeftPaddle.Draw(_spriteBatch, _texture2D);
RightPaddle.Draw(_spriteBatch, _texture2D);
Ball.Draw(_spriteBatch, _texture2D);
Counter.Draw(_spriteBatch, GraphicsDevice.Viewport);
_spriteBatch.End();
base.Draw(gameTime);
}
public void CreatePaddles()
{
LeftPaddle = new Paddle(Paddle.Player.Left, Color.Green, GraphicsDevice.Viewport);
RightPaddle = new Paddle(Paddle.Player.Right, Color.Green, GraphicsDevice.Viewport);
Ball = new GameBall(GraphicsDevice.Viewport, SelectRandomPlayer(), LeftPaddle, RightPaddle, Content);
}
public Player SelectRandomPlayer()
{
var randomPlayer = new Random().Next(0, 2);
return randomPlayer == 1 ? Player.Left : Player.Right;
}
}