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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
int Width = 800;
int Height = 600;
sf::RenderWindow Window(sf::VideoMode(Width, Height, 32), "SFML Sample Application");
Window.setVerticalSyncEnabled(true);
sf::Clock Clock;
sf::Texture Texture1;
if (!Texture1.loadFromFile("ball.png")) return 1;
sf::Texture Texture2;
if (!Texture2.loadFromFile("paddle.png")) return 1;
sf::Sprite Sprite1;
Sprite1.setTexture(Texture1);
Sprite1.setPosition(Width / 2 - Texture1.getWidth() / 2, Height / 2 - Texture1.getHeight() / 2);
//Sprite1.setColor(sf::Color(0, 0, 0, 255));
sf::Sprite Sprite2;
Sprite2.setTexture(Texture2);
Sprite2.setPosition(Texture2.getWidth(), Height / 2 - Texture2.getHeight() / 2);
Sprite2.setColor(sf::Color(240, 0, 0, 255));
sf::Sprite Sprite3;
Sprite3.setTexture(Texture2);
Sprite3.setPosition(Width - Texture2.getWidth() * 2, (Height / 2) - Texture2.getHeight() / 2);
Sprite3.setColor(sf::Color(0, 0, 240, 255));
sf::IntRect Sprite1Rect(Sprite1.getPosition().x, Sprite1.getPosition().y, Texture1.getWidth(), Texture1.getHeight());
sf::IntRect Sprite2Rect(Sprite2.getPosition().x, Sprite2.getPosition().y, Texture2.getWidth(), Texture2.getHeight());
sf::IntRect Sprite3Rect(Sprite3.getPosition().x, Sprite3.getPosition().y, Texture2.getWidth(), Texture2.getHeight());
double speed = 5.f;
bool IsMoving1 = false;
bool IsMovingUp1 = true;
bool IsMoving2 = false;
bool IsMovingUp2 = true;
bool HitLeft = true;
int Player1wins = 0;
int Player2wins = 0;
while (Window.isOpen())
{
double ElapsedTime = Clock.getElapsedTime().asSeconds() * 10;
Clock.restart();
sf::Event Event;
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::MouseButtonPressed:
Sprite1.setPosition(sf::Mouse::getPosition(Window).x - Texture1.getWidth() / 2, sf::Mouse::getPosition(Window).y - Texture1.getHeight() / 2);
break;
case sf::Event::KeyPressed:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{IsMovingUp1 = true; IsMoving1 = true;}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{IsMovingUp1 = false; IsMoving1 = true;}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{IsMovingUp2 = true; IsMoving2 = true;}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{IsMovingUp2 = false; IsMoving2 = true;}
break;
case sf::Event::KeyReleased:
IsMoving1 = false;
IsMoving2 = false;
break;
default:
break;
}
}
std::cout << ElapsedTime << std::endl;
Sprite1Rect.left = Sprite1.getPosition().x;
Sprite1Rect.top = Sprite1.getPosition().y;
Sprite2Rect.left = Sprite2.getPosition().x;
Sprite2Rect.top = Sprite2.getPosition().y;
Sprite3Rect.left = Sprite3.getPosition().x;
Sprite3Rect.top = Sprite3.getPosition().y;
std::cout << "Sprite3Rect: " << Sprite3Rect.left << ":" << Sprite3Rect.top << std::endl;
std::cout << "Sprite3: " << Sprite3.getPosition().x << ":" << Sprite3.getPosition().y << std::endl;
std::cout << "Sprite2Rect: " << Sprite2Rect.left << ":" << Sprite2Rect.top << std::endl;
std::cout << "Sprite2: " << Sprite2.getPosition().x << ":" << Sprite2.getPosition().y << std::endl;
std::cout << "Sprite1Rect: " << Sprite1Rect.left << ":" << Sprite1Rect.top << std::endl;
std::cout << "Sprite1: " << Sprite1.getPosition().x << ":" << Sprite1.getPosition().y << std::endl;
if (Sprite1Rect.intersects(Sprite2Rect))
{
if (Sprite1Rect.left - 1.f <= Sprite2Rect.left + Texture2.getWidth() / 2)
{
std::cout << "Ball Hit Top/Bottom of Left Paddle" << std::endl;
IsMoving1 = false;
}
else
{
HitLeft = true;
std::cout << "Ball Hit Left Paddle" << std::endl;
}
}
else if (Sprite1Rect.intersects(Sprite3Rect))
{
if (Sprite1Rect.left + Texture1.getWidth() / 2 >= Sprite3Rect.left - Texture2.getWidth() / 4)
{
std::cout << "Ball Hit Top/Bottom of Right Paddle" << std::endl;
IsMoving2 = false;
}
else
{
HitLeft = false;
std::cout << "Ball Hit Right Paddle" << std::endl;
}
}
if (HitLeft == true)
{
Sprite1.move(speed * ElapsedTime, 0);
}
else
{
Sprite1.move(-1 * speed * ElapsedTime, 0);
}
if (IsMoving1 == true && IsMovingUp1 == true && Sprite2.getPosition().y >= 0)
{
Sprite2.move(0, -1 * speed * ElapsedTime);
}
else if (IsMoving1 == true && Sprite2.getPosition().y <= Height - Texture2.getHeight())
{
Sprite2.move(0, speed * ElapsedTime);
}
if (IsMoving2 == true && IsMovingUp2 == true && Sprite3.getPosition().y >= 0)
{
Sprite3.move(0, -1 * speed * ElapsedTime);
}
else if (IsMoving2 == true && Sprite3.getPosition().y <= Height - Texture2.getHeight())
{
Sprite3.move(0, speed * ElapsedTime);
}
if (Sprite1.getPosition().x <= (0.f - Texture1.getWidth()))
{
Player1wins++;
Sprite1.setPosition(Width / 2 - Texture1.getWidth() / 2, Height / 2 - Texture1.getHeight() / 2);
HitLeft = true;
}
else if (Sprite1.getPosition().x >= Width)
{
Player2wins++;
Sprite1.setPosition(Width / 2 - Texture1.getWidth() / 2, Height / 2 - Texture1.getHeight() / 2);
HitLeft = false;
}
Window.clear(sf::Color(245, 245, 245));
Window.draw(Sprite1);
Window.draw(Sprite2);
Window.draw(Sprite3);
Window.display();
}
return 0;
}
|