* sample pong is compilable

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1356 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
trass3r 2010-01-13 22:31:17 +00:00
parent 3fa986f94e
commit 879649fa08

View File

@ -7,12 +7,12 @@ import dsfml.graphics.all;
version (Tango) version (Tango)
{ {
import tango.io.Stdout; import tango.io.Stdout;
import tango.math.Math; import tango.math.Math;
} }
else else
{ {
import std.math; import std.math;
} }
void main() void main()
@ -20,114 +20,114 @@ void main()
// Defines PI // Defines PI
const float PI = 3.14159f; const float PI = 3.14159f;
// Create the window of the application // Create the window of the application
RenderWindow app = new RenderWindow(VideoMode(800, 600, 32), "SFML Pong"); RenderWindow app = new RenderWindow(VideoMode(800, 600, 32), "SFML Pong");
app.useVerticalSync(true); app.useVerticalSync(true);
Input i = app.getInput(); Input i = app.getInput();
// Load the sounds used in the game // Load the sounds used in the game
Sound BallSound = new Sound(new SoundBuffer("Data/ball.wav")); Sound BallSound = new Sound(new SoundBuffer("Data/ball.wav"));
// Load the images used in the game // Load the images used in the game
Image PaddleImage = new Image("Data/paddle.tga"); Image PaddleImage = new Image("Data/paddle.tga");
Image BallImage = new Image("Data/ball.tga"); Image BallImage = new Image("Data/ball.tga");
// Initialize the end text // Initialize the end text
String End = new String(""c); Text End = new Text(""c);
Font font = new Font("Data/cheeseburger.ttf"); Font font = new Font("Data/cheeseburger.ttf");
End.setFont(font); End.setFont(font);
End.setSize(60.f); End.setCharacterSize(60);
End.move(150.f, 200.f); End.move(150.f, 200.f);
End.setColor(Color(50, 50, 250)); End.setColor(Color(50, 50, 250));
// Create the sprites of the background, the paddles and the ball // Create the sprites of the background, the paddles and the ball
Sprite LeftPaddle = new Sprite(PaddleImage); Sprite LeftPaddle = new Sprite(PaddleImage);
Sprite RightPaddle = new Sprite(PaddleImage); Sprite RightPaddle = new Sprite(PaddleImage);
Sprite Ball = new Sprite(BallImage); Sprite Ball = new Sprite(BallImage);
LeftPaddle.move(10, (app.getView().getRect().getHeight() - LeftPaddle.getSize().y) / 2); LeftPaddle.move(10, (app.getView().getHeight() - LeftPaddle.getSize().y) / 2);
RightPaddle.move(app.getView().getRect().getWidth() - RightPaddle.getSize().x - 10, (app.getView().getRect().getHeight() - RightPaddle.getSize().y) / 2); RightPaddle.move(app.getView().getWidth() - RightPaddle.getSize().x - 10, (app.getView().getHeight() - RightPaddle.getSize().y) / 2);
Ball.move((app.getView().getRect().getWidth() - Ball.getSize().x) / 2, (app.getView().getRect().getHeight() - Ball.getSize().y) / 2); Ball.move((app.getView().getWidth() - Ball.getSize().x) / 2, (app.getView().getHeight() - Ball.getSize().y) / 2);
// Define the paddles properties // Define the paddles properties
Clock AITimer = new Clock(); Clock AITimer = new Clock();
const float AITime = 0.1f; const float AITime = 0.1f;
float LeftPaddleSpeed = 400.f; float LeftPaddleSpeed = 400.f;
float RightPaddleSpeed = 400.f; float RightPaddleSpeed = 400.f;
// Define the ball properties // Define the ball properties
float BallSpeed = 400.f; float BallSpeed = 400.f;
float BallAngle = 0.f; float BallAngle = 0.f;
do do
{ {
// Make sure the ball initial angle is not too much vertical // Make sure the ball initial angle is not too much vertical
BallAngle = Randomizer.random(0.f, 2 * PI); BallAngle = Randomizer.random(0.f, 2 * PI);
} while (abs(cos(BallAngle)) < 0.7f); } while (abs(cos(BallAngle)) < 0.7f);
bool IsPlaying = true; bool IsPlaying = true;
Event evt; Event evt;
while (app.isOpened()) while (app.isOpened())
{ {
app.clear(Color(255, 255, 255, 255)); app.clear(Color(255, 255, 255, 255));
// Handle events // Handle events
while (app.getEvent(evt)) while (app.getEvent(evt))
{ {
// Window closed or escape key pressed : exit // Window closed or escape key pressed : exit
if ((evt.Type == Event.EventType.CLOSED) || if ((evt.Type == EventType.Closed) ||
((evt.Type == Event.EventType.KEYPRESSED) && (evt.Key.Code == KeyCode.ESCAPE))) ((evt.Type == EventType.KeyPressed) && (evt.Key.Code == KeyCode.Escape)))
{ {
app.close(); app.close();
break; break;
} }
} }
if (IsPlaying) if (IsPlaying)
{ {
// Move the player's paddle // Move the player's paddle
if (i.isKeyDown(KeyCode.UP) && (LeftPaddle.getPosition().y > 5.f)) if (i.isKeyDown(KeyCode.Up) && (LeftPaddle.getPosition().y > 5.f))
LeftPaddle.move(0.f, -LeftPaddleSpeed * app.getFrameTime()); LeftPaddle.move(0.f, -LeftPaddleSpeed * app.getFrameTime());
if (i.isKeyDown(KeyCode.DOWN) && (LeftPaddle.getPosition().y < app.getView().getRect().getHeight() - LeftPaddle.getSize().y - 5.f)) if (i.isKeyDown(KeyCode.Down) && (LeftPaddle.getPosition().y < app.getView().getHeight() - LeftPaddle.getSize().y - 5.f))
LeftPaddle.move(0.f, LeftPaddleSpeed * app.getFrameTime()); LeftPaddle.move(0.f, LeftPaddleSpeed * app.getFrameTime());
// Move the computer's paddle // Move the computer's paddle
if (((RightPaddleSpeed < 0.f) && (RightPaddle.getPosition().y > 5.f)) || if (((RightPaddleSpeed < 0.f) && (RightPaddle.getPosition().y > 5.f)) ||
((RightPaddleSpeed > 0.f) && (RightPaddle.getPosition().y < app.getView().getRect().getHeight() - RightPaddle.getSize().y - 5.f))) ((RightPaddleSpeed > 0.f) && (RightPaddle.getPosition().y < app.getView().getHeight() - RightPaddle.getSize().y - 5.f)))
{ {
RightPaddle.move(0.f, RightPaddleSpeed * app.getFrameTime()); RightPaddle.move(0.f, RightPaddleSpeed * app.getFrameTime());
} }
// Update the computer's paddle direction according to the ball position // Update the computer's paddle direction according to the ball position
if (AITimer.getElapsedTime() > AITime) if (AITimer.getElapsedTime() > AITime)
{ {
AITimer.reset(); AITimer.reset();
if ((RightPaddleSpeed < 0) && (Ball.getPosition().y + Ball.getSize().y > RightPaddle.getPosition().y + RightPaddle.getSize().y)) if ((RightPaddleSpeed < 0) && (Ball.getPosition().y + Ball.getSize().y > RightPaddle.getPosition().y + RightPaddle.getSize().y))
RightPaddleSpeed = -RightPaddleSpeed; RightPaddleSpeed = -RightPaddleSpeed;
if ((RightPaddleSpeed > 0) && (Ball.getPosition().y < RightPaddle.getPosition().y)) if ((RightPaddleSpeed > 0) && (Ball.getPosition().y < RightPaddle.getPosition().y))
RightPaddleSpeed = -RightPaddleSpeed; RightPaddleSpeed = -RightPaddleSpeed;
} }
// Move the ball // Move the ball
float Factor = BallSpeed * app.getFrameTime(); float Factor = BallSpeed * app.getFrameTime();
Ball.move(cos(BallAngle) * Factor, sin(BallAngle) * Factor); Ball.move(cos(BallAngle) * Factor, sin(BallAngle) * Factor);
// Check collisions between the ball and the screen // Check collisions between the ball and the screen
if (Ball.getPosition().x < 0.f) if (Ball.getPosition().x < 0.f)
{ {
IsPlaying = false; IsPlaying = false;
End.setText("You lost !\n(press escape to exit)"c); End.setString("You lost !\n(press escape to exit)"c);
} }
if (Ball.getPosition().x + Ball.getSize().x > app.getView().getRect().getWidth()) if (Ball.getPosition().x + Ball.getSize().x > app.getView().getWidth())
{ {
IsPlaying = false; IsPlaying = false;
End.setText("You won !\n(press escape to exit)"c); End.setString("You won !\n(press escape to exit)"c);
} }
if (Ball.getPosition().y < 0.f) if (Ball.getPosition().y < 0.f)
@ -137,11 +137,11 @@ void main()
Ball.setY(0.1f); Ball.setY(0.1f);
} }
if (Ball.getPosition().y + Ball.getSize().y > app.getView().getRect().getHeight()) if (Ball.getPosition().y + Ball.getSize().y > app.getView().getHeight())
{ {
BallSound.play(); BallSound.play();
BallAngle = -BallAngle; BallAngle = -BallAngle;
Ball.setY(app.getView().getRect().getHeight() - Ball.getSize().y - 0.1f); Ball.setY(app.getView().getHeight() - Ball.getSize().y - 0.1f);
} }
// Check the collisions between the ball and the paddles // Check the collisions between the ball and the paddles
// Left Paddle // Left Paddle
@ -167,17 +167,17 @@ void main()
} }
} }
// Draw the background, paddles and ball sprites // Draw the background, paddles and ball sprites
app.draw(LeftPaddle); app.draw(LeftPaddle);
app.draw(RightPaddle); app.draw(RightPaddle);
app.draw(Ball); app.draw(Ball);
// If the game is over, display the end message // If the game is over, display the end message
if (!IsPlaying) if (!IsPlaying)
app.draw(End); app.draw(End);
// Display things on screen // Display things on screen
app.display(); app.display();
} }
} }