Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <time.h>
- #include <SFML/Audio/Sound.hpp>
- #include <SFML/Audio/SoundBuffer.hpp>
- #include <iostream>
- #include <windows.h>
- #include <SFML/Window/Mouse.hpp>
- #include <sstream>
- using namespace sf;
- using namespace std;
- int main()
- {
- srand(time(0));
- SoundBuffer buffer;
- buffer.loadFromFile("censor.wav");
- Sound censor;
- censor.setBuffer(buffer);
- RenderWindow app(VideoMode(520, 450), "Arkanoid!");
- app.setFramerateLimit(60);
- Texture t1,t2,t3,t4,t5;
- t1.loadFromFile("images/block01.png");
- t2.loadFromFile("images/poopole.jpg");
- t3.loadFromFile("images/ball.png");
- t4.loadFromFile("images/paddle.png");
- Sprite sBackground(t2), sBall(t3), sPaddle(t4);
- sPaddle.setPosition(300,440);
- Sprite block[100];
- int n=0;
- for (int i=1;i<=10;i++)
- {
- for (int j=1;j<=10;j++)
- {
- block[n].setTexture(t1);
- block[n].setPosition(i*43,j*20);
- n++;
- }
- }
- float dx=6, dy=5;
- float x=300, y=300;
- int score = 0;
- Font arial;
- arial.loadFromFile("arial.ttf");
- stringstream ssScore;
- ssScore << "Score: " << score;
- Text scoreDisplayed;
- scoreDisplayed.setCharacterSize(30);
- scoreDisplayed.setPosition({5,220});
- scoreDisplayed.setFont(arial);
- //ustawiamy napis i konwertujemy z inta na stringa
- scoreDisplayed.setString(ssScore.str());
- scoreDisplayed.setFillColor(sf::Color::Blue);
- while (app.isOpen())
- {
- Event e;
- while (app.pollEvent(e))
- {
- if (e.type == Event::Closed)
- {
- Sleep(1500);
- app.close();
- }
- }
- x+=dx;
- for (int i=0;i<n;i++)
- {
- //upewniamy sie ze wspolrzedne pileczki nie beda jednoczesnie dotykac innego bloczka i ustawiamy jego rozmiar na 1/1
- //nastepnie po przejsciu kilku nastepnych ifow znow ustalamy koordynaty pileczki i automatycznie przywracamy do defaultowego rozmiaru
- if ( FloatRect(x+3,y+3,1,1).intersects(block[i].getGlobalBounds()) )
- {
- block[i].setPosition(-100,0);
- dx=-dx;
- score++;
- //upewniamy sie ze tu bedzie pusty score i wywalamy nowy
- ssScore.str("");
- ssScore << "Score: " << score;
- scoreDisplayed.setString(ssScore.str());
- }
- }
- y+=dy;
- for (int i=0;i<n;i++)
- {
- if ( FloatRect(x+3,y+3,1,1).intersects(block[i].getGlobalBounds()) )
- {
- block[i].setPosition(-100,0);
- dy=-dy;
- score++;
- ssScore.str("");
- ssScore << "Score: " << score;
- scoreDisplayed.setString(ssScore.str());
- }
- }
- //odbijanie od scian
- if (x<0 || x>520)
- {
- dx=-dx;
- }
- if (y<0 || y>450)
- {
- dy=-dy;
- }
- //pileczka podaza za myszka, pobieramy relatywna pozycje myszki
- sPaddle.setPosition(Mouse::getPosition(app).x, sPaddle.getPosition().y);
- if ( FloatRect(x,y,12,12).intersects(sPaddle.getGlobalBounds()) )
- {
- dy=-(rand()%5+2);
- censor.play();
- }
- sBall.setPosition(x,y);
- app.clear();
- app.draw(sBackground);
- app.draw(sBall);
- app.draw(sPaddle);
- app.draw(scoreDisplayed);
- for (int i=0;i<n;i++)
- {
- app.draw(block[i]);
- }
- app.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement