2009-01-29 00:18:34 +08:00
|
|
|
module pong;
|
|
|
|
|
|
|
|
import dsfml.system.all;
|
|
|
|
import dsfml.audio.all;
|
|
|
|
import dsfml.window.all;
|
|
|
|
import dsfml.graphics.all;
|
|
|
|
|
|
|
|
version (Tango)
|
|
|
|
{
|
2010-01-14 06:31:17 +08:00
|
|
|
import tango.io.Stdout;
|
|
|
|
import tango.math.Math;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-14 06:31:17 +08:00
|
|
|
import std.math;
|
2010-03-04 10:21:18 +08:00
|
|
|
import std.perf;
|
|
|
|
import std.random;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
// Defines PI
|
|
|
|
const float PI = 3.14159f;
|
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
// Create the window of the application
|
|
|
|
RenderWindow app = new RenderWindow(VideoMode(800, 600, 32), "SFML Pong");
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:21:18 +08:00
|
|
|
app.useVerticalSync(false);
|
2010-01-14 06:31:17 +08:00
|
|
|
|
2010-08-26 04:37:00 +08:00
|
|
|
Input i = app.input;
|
2010-01-14 06:31:17 +08:00
|
|
|
|
|
|
|
// Load the sounds used in the game
|
|
|
|
Sound BallSound = new Sound(new SoundBuffer("Data/ball.wav"));
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
// Load the images used in the game
|
|
|
|
Image PaddleImage = new Image("Data/paddle.tga");
|
|
|
|
Image BallImage = new Image("Data/ball.tga");
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Initialize the end text
|
2010-01-14 06:31:17 +08:00
|
|
|
Text End = new Text(""c);
|
2009-01-29 00:18:34 +08:00
|
|
|
Font font = new Font("Data/cheeseburger.ttf");
|
2010-08-26 04:37:00 +08:00
|
|
|
End.font = font;
|
|
|
|
End.characterSize = 60;
|
2010-01-14 06:31:17 +08:00
|
|
|
End.move(150.f, 200.f);
|
2010-08-26 04:37:00 +08:00
|
|
|
End.color = Color(50, 50, 250);
|
2010-01-14 06:31:17 +08:00
|
|
|
|
2010-03-04 10:21:18 +08:00
|
|
|
Text fps = new Text(""c, font, 30);
|
|
|
|
fps.move(50.f, 50.f);
|
2010-08-26 04:37:00 +08:00
|
|
|
fps.color = Color.BLACK;
|
2010-03-04 10:21:18 +08:00
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
// Create the sprites of the background, the paddles and the ball
|
|
|
|
Sprite LeftPaddle = new Sprite(PaddleImage);
|
|
|
|
Sprite RightPaddle = new Sprite(PaddleImage);
|
|
|
|
Sprite Ball = new Sprite(BallImage);
|
|
|
|
|
2010-08-26 04:37:00 +08:00
|
|
|
LeftPaddle.move(10, (app.view.getHeight() - LeftPaddle.size.y) / 2);
|
|
|
|
RightPaddle.move(app.view.getWidth() - RightPaddle.size.x - 10, (app.view.getHeight() - RightPaddle.size.y) / 2);
|
|
|
|
Ball.move((app.view.getWidth() - Ball.size.x) / 2, (app.view.getHeight() - Ball.size.y) / 2);
|
2010-01-14 06:31:17 +08:00
|
|
|
|
|
|
|
// Define the paddles properties
|
2010-03-04 10:21:18 +08:00
|
|
|
auto AITimer = new PerformanceCounter();
|
|
|
|
const long AITime = 100; // 100 ms
|
2010-01-14 06:31:17 +08:00
|
|
|
float LeftPaddleSpeed = 400.f;
|
|
|
|
float RightPaddleSpeed = 400.f;
|
|
|
|
|
|
|
|
// Define the ball properties
|
|
|
|
float BallSpeed = 400.f;
|
|
|
|
float BallAngle = 0.f;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Make sure the ball initial angle is not too much vertical
|
2010-03-04 10:21:18 +08:00
|
|
|
BallAngle = uniform(0.f, 2 * PI);
|
2010-01-14 06:31:17 +08:00
|
|
|
} while (abs(cos(BallAngle)) < 0.7f);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
bool IsPlaying = true;
|
|
|
|
|
|
|
|
Event evt;
|
2010-03-04 10:21:18 +08:00
|
|
|
uint iFps = 0;
|
|
|
|
auto fpsClock = new PerformanceCounter();
|
2010-08-26 04:37:00 +08:00
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
while (app.isOpened())
|
|
|
|
{
|
|
|
|
app.clear(Color(255, 255, 255, 255));
|
|
|
|
|
2010-08-26 04:37:00 +08:00
|
|
|
// Handle events
|
2010-01-14 06:31:17 +08:00
|
|
|
while (app.getEvent(evt))
|
|
|
|
{
|
|
|
|
// Window closed or escape key pressed : exit
|
|
|
|
if ((evt.Type == EventType.Closed) ||
|
|
|
|
((evt.Type == EventType.KeyPressed) && (evt.Key.Code == KeyCode.Escape)))
|
|
|
|
{
|
|
|
|
app.close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPlaying)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Move the player's paddle
|
2010-08-26 04:37:00 +08:00
|
|
|
if (i.isKeyDown(KeyCode.Up) && (LeftPaddle.position.y > 5.f))
|
|
|
|
LeftPaddle.move(0.f, -LeftPaddleSpeed * app.frameTime);
|
|
|
|
if (i.isKeyDown(KeyCode.Down) && (LeftPaddle.position.y < app.view.getHeight() - LeftPaddle.size.y - 5.f))
|
|
|
|
LeftPaddle.move(0.f, LeftPaddleSpeed * app.frameTime);
|
2010-01-14 06:31:17 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
// Move the computer's paddle
|
2010-08-26 04:37:00 +08:00
|
|
|
if (((RightPaddleSpeed < 0.f) && (RightPaddle.position.y > 5.f)) ||
|
|
|
|
((RightPaddleSpeed > 0.f) && (RightPaddle.position.y < app.view.getHeight() - RightPaddle.size.y - 5.f)))
|
2010-01-14 06:31:17 +08:00
|
|
|
{
|
2010-08-26 04:37:00 +08:00
|
|
|
RightPaddle.move(0.f, RightPaddleSpeed * app.frameTime);
|
2010-01-14 06:31:17 +08:00
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
// Update the computer's paddle direction according to the ball position
|
2010-03-04 10:21:18 +08:00
|
|
|
AITimer.stop();
|
|
|
|
if (AITimer.milliseconds > AITime)
|
2010-01-14 06:31:17 +08:00
|
|
|
{
|
2010-03-04 10:21:18 +08:00
|
|
|
AITimer.start();
|
2010-08-26 04:37:00 +08:00
|
|
|
if ((RightPaddleSpeed < 0) && (Ball.position.y + Ball.size.y > RightPaddle.position.y + RightPaddle.size.y))
|
2010-01-14 06:31:17 +08:00
|
|
|
RightPaddleSpeed = -RightPaddleSpeed;
|
2010-08-26 04:37:00 +08:00
|
|
|
if ((RightPaddleSpeed > 0) && (Ball.position.y < RightPaddle.position.y))
|
2010-01-14 06:31:17 +08:00
|
|
|
RightPaddleSpeed = -RightPaddleSpeed;
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Move the ball
|
2010-08-26 04:37:00 +08:00
|
|
|
float Factor = BallSpeed * app.frameTime;
|
2010-01-14 06:31:17 +08:00
|
|
|
Ball.move(cos(BallAngle) * Factor, sin(BallAngle) * Factor);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Check collisions between the ball and the screen
|
2010-08-26 04:37:00 +08:00
|
|
|
if (Ball.position.x < 0.f)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
IsPlaying = false;
|
2010-08-26 04:37:00 +08:00
|
|
|
End.text("You lost !\n(press escape to exit)"c);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-08-26 04:37:00 +08:00
|
|
|
if (Ball.position.x + Ball.size.x > app.view.getWidth())
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
IsPlaying = false;
|
2010-08-26 04:37:00 +08:00
|
|
|
End.text("You won !\n(press escape to exit)"c);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-08-26 04:37:00 +08:00
|
|
|
if (Ball.position.y < 0.f)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
BallSound.play();
|
|
|
|
BallAngle = -BallAngle;
|
2010-08-26 04:37:00 +08:00
|
|
|
Ball.y = 0.1f;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-08-26 04:37:00 +08:00
|
|
|
if (Ball.position.y + Ball.size.y > app.view.getHeight())
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
BallSound.play();
|
|
|
|
BallAngle = -BallAngle;
|
2010-08-26 04:37:00 +08:00
|
|
|
Ball.y = app.view.getHeight() - Ball.size.y - 0.1f;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
// Check the collisions between the ball and the paddles
|
|
|
|
// Left Paddle
|
2010-08-26 04:37:00 +08:00
|
|
|
if (Ball.position.x < LeftPaddle.position.x + LeftPaddle.size.x &&
|
|
|
|
Ball.position.x > LeftPaddle.position.x + (LeftPaddle.size.x / 2.0f) &&
|
|
|
|
Ball.position.y + Ball.size.y >= LeftPaddle.position.y &&
|
|
|
|
Ball.position.y <= LeftPaddle.position.y + LeftPaddle.size.y)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
BallSound.play();
|
|
|
|
BallAngle = PI - BallAngle;
|
2010-08-26 04:37:00 +08:00
|
|
|
Ball.x = LeftPaddle.position.x + LeftPaddle.size.x + 0.1f;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Right Paddle
|
2010-08-26 04:37:00 +08:00
|
|
|
if (Ball.position.x + Ball.size.x > RightPaddle.position.x &&
|
|
|
|
Ball.position.x + Ball.size.x < RightPaddle.position.x + (RightPaddle.size.x / 2.0f) &&
|
|
|
|
Ball.position.y + Ball.size.y >= RightPaddle.position.y &&
|
|
|
|
Ball.position.y <= RightPaddle.position.y + RightPaddle.size.y)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
BallSound.play();
|
|
|
|
BallAngle = PI - BallAngle;
|
2010-08-26 04:37:00 +08:00
|
|
|
Ball.x = RightPaddle.position.x - Ball.size.x - 0.1f;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
// Draw the background, paddles and ball sprites
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
app.draw(LeftPaddle);
|
|
|
|
app.draw(RightPaddle);
|
|
|
|
app.draw(Ball);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:21:18 +08:00
|
|
|
fpsClock.stop();
|
|
|
|
if(fpsClock.seconds >= 1)
|
|
|
|
{
|
2010-08-26 04:37:00 +08:00
|
|
|
fps.text = std.string.format("%d fps", iFps);
|
2010-03-04 10:21:18 +08:00
|
|
|
iFps = 0;
|
|
|
|
fpsClock.start();
|
|
|
|
}
|
|
|
|
++iFps;
|
|
|
|
app.draw(fps);
|
2010-01-14 06:31:17 +08:00
|
|
|
// If the game is over, display the end message
|
|
|
|
if (!IsPlaying)
|
|
|
|
app.draw(End);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-14 06:31:17 +08:00
|
|
|
// Display things on screen
|
|
|
|
app.display();
|
|
|
|
}
|
2010-08-26 04:37:00 +08:00
|
|
|
}
|