Тёмная тема
#include <lib_Basic.cpp> ' #include <SFML/System.hpp> ' #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> ' #include <SFML/Network.hpp> FUNCTION main(BYVAL COMMANDCOUNT AS INTEGER, BYPTR COMMAND[] AS CHAR) AS INTEGER setlocale(LC_ALL, "RUS") RANDOMIZE() 'Define some constants CONST pi AS FLOAT = 3.14159f CONST gameWidth AS INTEGER = 800 CONST gameHeight AS INTEGER = 600 DIM ballRadius AS FLOAT = 10.f DIM paddleSize(25, 100) AS sf.Vector2f DIM chekCPU AS INTEGER = 0 DIM chekUSER AS INTEGER = 0 'Create the window of the application SET window(sf.VideoMode(gameWidth, gameHeight, 32), "Pong", sf.Style.Titlebar | sf.Style.Close) AS sf.RenderWindow window.setVerticalSyncEnabled(true) 'Load the sounds used in the game DIM ballSoundBuffer AS sf.SoundBuffer IF NOT(ballSoundBuffer.loadFromFile("resources/ball.wav")) THEN RETURN EXIT_FAILURE END IF DIM ballSound(ballSoundBuffer) AS sf.Sound 'Create the left paddle DIM leftPaddle AS sf.RectangleShape leftPaddle.setSize(paddleSize - sf.Vector2f(3, 3)) leftPaddle.setOutlineThickness(3) leftPaddle.setOutlineColor(sf.Color.Black) leftPaddle.setFillColor(sf.Color(100, 100, 200)) leftPaddle.setOrigin(paddleSize / 2.f) 'Create the right paddle DIM rightPaddle AS sf.RectangleShape rightPaddle.setSize(paddleSize - sf.Vector2f(3, 3)) rightPaddle.setOutlineThickness(3) rightPaddle.setOutlineColor(sf.Color.Black) rightPaddle.setFillColor(sf.Color(200, 100, 100)) rightPaddle.setOrigin(paddleSize / 2.f) 'Create the ball DIM ball AS sf.CircleShape ball.setRadius(ballRadius - 3) ball.setOutlineThickness(3) ball.setOutlineColor(sf.Color.Black) ball.setFillColor(sf.Color.White) ball.setOrigin(ballRadius / 2, ballRadius / 2) 'Load the text font DIM font AS sf.Font IF NOT(font.loadFromFile("resources/sansation.ttf")) THEN RETURN EXIT_FAILURE END IF 'Initialize the pause message DIM pauseMessage AS sf.Text pauseMessage.setFont(font) pauseMessage.setCharacterSize(40) pauseMessage.setPosition(170.f, 150.f) pauseMessage.setFillColor(sf.Color.White) pauseMessage.setString("Welcome to pong!\nPress space to start the game") DIM chekMessage AS sf.Text chekMessage.setFont(font) chekMessage.setCharacterSize(40) chekMessage.setPosition(360.f, 0.f) chekMessage.setFillColor(sf.Color.White) 'Define the paddles properties DIM AITimer AS sf.Clock CONST AITime AS sf.Time = sf.seconds(0.1f) CONST paddleSpeed AS FLOAT = 400.f DIM rightPaddleSpeed AS FLOAT = 0.f CONST ballSpeed AS FLOAT = 400.f DIM ballAngle AS FLOAT = 0.f 'to be changed later DIM clock AS sf.Clock DIM isPlaying AS BOOLEAN = FALSE DO WHILE (window.isOpen()) 'Handle events DIM event AS sf.Event DO WHILE (window.pollEvent(event)) 'Window closed or escape key pressed: exit IF ((event.type = sf.Event.Closed) OR ((event.type = sf.Event.KeyPressed) AND (event.key.code = sf.Keyboard.Escape))) THEN window.close() EXIT DO END IF 'Space key pressed: play IF ((event.type = sf.Event.KeyPressed) AND (event.key.code = sf.Keyboard.Space)) THEN IF NOT(isPlaying) THEN '(re)start the game isPlaying = true clock.restart() 'Reset the position of the paddles and ball leftPaddle.setPosition(10 + paddleSize.x / 2, gameHeight / 2) rightPaddle.setPosition(gameWidth - 10 - paddleSize.x / 2, gameHeight / 2) ball.setPosition(gameWidth / 2, gameHeight / 2) 'Reset the ball angle DO 'Make sure the ball initial angle is not too much vertical ballAngle = (RND() % 360) * 2 * pi / 360 LOOP WHILE (ABS(COS(ballAngle)) < 0.7f) END IF END IF LOOP IF (isPlaying = TRUE) THEN DIM deltaTime AS FLOAT = clock.restart().asSeconds() 'Move the player's paddle IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Up) AND (leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) THEN leftPaddle.move(0.f, -paddleSpeed * deltaTime) END IF IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Down) AND (leftPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)) THEN leftPaddle.move(0.f, paddleSpeed * deltaTime) END IF 'Move the computer's paddle IF (((rightPaddleSpeed < 0.f) AND (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) OR ((rightPaddleSpeed > 0.f) _ AND (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f))) THEN rightPaddle.move(0.f, rightPaddleSpeed * deltaTime) END IF 'Update the computer's paddle direction according to the ball position IF (AITimer.getElapsedTime() > AITime) THEN AITimer.restart() IF (ball.getPosition().y + ballRadius > rightPaddle.getPosition().y + paddleSize.y / 2) THEN rightPaddleSpeed = paddleSpeed ELSE IF (ball.getPosition().y - ballRadius < rightPaddle.getPosition().y - paddleSize.y / 2) THEN rightPaddleSpeed = -paddleSpeed ELSE rightPaddleSpeed = 0.f END IF END IF END IF 'Move the ball DIM factor AS FLOAT = ballSpeed * deltaTime ball.move(COS(ballAngle) * factor, SIN(ballAngle) * factor) 'Check collisions between the ball and the screen IF (ball.getPosition().x - ballRadius < 0.f) THEN isPlaying = FALSE pauseMessage.setString("You lost!\nPress space to restart or\nescape to exit") chekCPU = chekCPU + 1 END IF IF (ball.getPosition().x + ballRadius > gameWidth) THEN isPlaying = FALSE pauseMessage.setString("You won!\nPress space to restart or\nescape to exit") chekUSER = chekUSER + 1 END IF IF (ball.getPosition().y - ballRadius < 0.f) THEN ballSound.play() ballAngle = -ballAngle ball.setPosition(ball.getPosition().x, ballRadius + 0.1f) END IF IF (ball.getPosition().y + ballRadius > gameHeight) THEN ballSound.play() ballAngle = -ballAngle ball.setPosition(ball.getPosition().x, gameHeight - ballRadius - 0.1f) END IF 'Check the collisions between the ball and the paddles 'Left Paddle IF (ball.getPosition().x - ballRadius < leftPaddle.getPosition().x + paddleSize.x / 2 AND _ ball.getPosition().x - ballRadius > leftPaddle.getPosition().x AND _ ball.getPosition().y + ballRadius >= leftPaddle.getPosition().y - paddleSize.y / 2 AND _ ball.getPosition().y - ballRadius <= leftPaddle.getPosition().y + paddleSize.y / 2) THEN IF (ball.getPosition().y > leftPaddle.getPosition().y) THEN ballAngle = pi - ballAngle + (RND() % 20) * pi / 180 ELSE ballAngle = pi - ballAngle - (RND() % 20) * pi / 180 END IF ballSound.play() ball.setPosition(leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y) END IF 'Right Paddle IF (ball.getPosition().x + ballRadius > rightPaddle.getPosition().x - paddleSize.x / 2 AND _ ball.getPosition().x + ballRadius < rightPaddle.getPosition().x AND _ ball.getPosition().y + ballRadius >= rightPaddle.getPosition().y - paddleSize.y / 2 AND _ ball.getPosition().y - ballRadius <= rightPaddle.getPosition().y + paddleSize.y / 2) THEN IF (ball.getPosition().y > rightPaddle.getPosition().y) THEN ballAngle = pi - ballAngle + (RND() % 20) * pi / 180 ELSE ballAngle = pi - ballAngle - (RND() % 20) * pi / 180 END IF ballSound.play() ball.setPosition(rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y) END IF chekMessage.setString(STR(chekUSER) + " : " + STR(chekCPU)) END IF 'Clear the window window.clear(sf.Color(50, 200, 50)) IF (isPlaying) THEN window.draw(chekMessage) 'Draw the paddles and the ball window.draw(leftPaddle) window.draw(rightPaddle) window.draw(ball) ELSE 'Draw the pause message window.draw(pauseMessage) END IF 'Display things on screen window.display() LOOP RETURN EXIT_SUCCESS END FUNCTION
Тема в архиве.