Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <vector>
- #include "GameObjects.h"
- using namespace sf;
- class posts
- {
- Text text;
- Font font;
- void loadFont(String)
- {
- }
- };
- class GameFunctions {
- public:
- bool isMenu = 1;
- void chekBall(GameObjects &a, GameObjects &b)//a=ball,b=wood
- {
- if (b.life && (((((a.x >= b.x) && (a.x <= b.x + 80)) && ((b.y <= a.y) && (b.y + 30 >= a.y)))) || ((((a.x + 20 >= b.x) && (a.x + 20 <= b.x + 80)) && ((b.y <= a.y) && (b.y + 30 >= a.y)))) || ((((a.x + 20 >= b.x) && (a.x + 20 <= b.x + 80)) && ((b.y <= a.y + 20) && (b.y + 30 >= a.y + 20)))) || ((((a.x >= b.x) && (a.x <= b.x + 80)) && ((b.y <= a.y + 20) && (b.y + 30 >= a.y + 20))))))
- {
- b.life = false;
- a.dx *= -1;
- a.dy *= -1;
- };
- };
- void menu(RenderWindow & window)
- {
- Image menutab1;
- menutab1.loadFromFile("images/tab1.png");
- menutab1.createMaskFromColor(Color(255,255,255));
- Texture menuTexture1, menuBackground;
- menuTexture1.loadFromImage(menutab1);
- menuBackground.loadFromFile("images/menu.jpg");
- Sprite menu1(menuTexture1), menuBg(menuBackground);
- int menuNum = 0;
- menu1.setPosition(100, 30);
- menuBg.setPosition(0, 0);
- while (isMenu)
- {
- Event event;
- menu1.setColor(Color::White);
- menuNum = 0;
- window.clear(Color(0, 0, 0));
- while (window.pollEvent(event))
- {
- if ((event.type == Event::Closed) || (event.key.code == Keyboard::Escape)) window.close();
- }
- if (IntRect(100, 30, 400, 100).contains(Mouse::getPosition(window))) { menu1.setColor(Color::Blue); menuNum = 1; }
- if (Mouse::isButtonPressed(Mouse::Left))
- {
- if (menuNum == 1)isMenu = false;
- }
- window.draw(menuBg);
- window.draw(menu1);
- window.display();
- }
- }
- };
- int main()
- {
- GameFunctions obj1;
- Clock gametime;
- sf::Clock clock1;
- bool lose = 0;
- bool replay = 0;
- String str ="board_and_tools.png";
- String *ptrstr = &str;
- Font font;//шрифт
- font.loadFromFile("1.ttf");
- Text text("", font, 100);
- Text text1("", font, 50);
- Text text2("", font, 50);
- text.setStyle(sf::Text::Bold);
- text1.setStyle(sf::Text::Bold);
- text2.setStyle(sf::Text::Bold);
- RenderWindow window(VideoMode(1024, 768), "arkanoid");
- obj1.menu(window);
- Texture bg;
- bg.loadFromFile("images/bg.png");
- Sprite bgsprite;
- bgsprite.setTexture(bg);
- bool start = false;
- std::vector <GameObjects> arrwoods;
- for (int i = 0; i <= 17; i++)
- {
- int b = 0;
- if (i > 8)b = 1;
- int a = i % 9;
- arrwoods.push_back(GameObjects(70 + (100 * a), 90 + (40 * b), 80, 30, 80, 70, *ptrstr));
- }
- GameObjects MyBoard(450, 650, 150, 20, 40, 40, *ptrstr);
- GameObjects Ball(450, 250, 21, 20, 40, 70, *ptrstr); Ball.dx = -0.2; Ball.dy = 0.2; Ball.boostx = 1;
- while (window.isOpen())
- {
- sf::Time elapsed1 = clock1.getElapsedTime();
- float sec = elapsed1.asSeconds();
- float time = gametime.getElapsedTime().asMicroseconds();
- gametime.restart();
- time = time / 850;
- Event event;
- while (window.pollEvent(event))
- {
- if ((event.type == Event::Closed) || (event.key.code == Keyboard::Escape)) window.close();
- }
- if ((Keyboard::isKeyPressed(Keyboard::R)) && (lose))
- {
- replay = true;
- std::cout << "R" << std::endl;
- }
- if ((Keyboard::isKeyPressed(Keyboard::Space)) && (sec > 0.2)) start = !start , clock1.restart();;
- //std::cout << elapsed1.asSeconds() << " sec=" << sec << std::endl;
- if ((!lose)&&(start)) {
- if (((Keyboard::isKeyPressed(Keyboard::Right)) || (Keyboard::isKeyPressed(Keyboard::D))) && (MyBoard.x < 810))
- {
- if (Ball.y >= 630)//boost speed ball for 3 sec
- {
- Ball.boostx = 1.8;
- Ball.boost = true;
- }
- MyBoard.dx = 0.3;
- MyBoard.x += MyBoard.dx*time;
- MyBoard.sprite.setPosition(MyBoard.x, MyBoard.y);
- std::cout << "->" << std::endl;
- }
- if (((Keyboard::isKeyPressed(Keyboard::Left)) || (Keyboard::isKeyPressed(Keyboard::A))) && (MyBoard.x > 61))
- {
- if (Ball.y >= 630)//boost speed ball for 3 sec
- {
- Ball.boostx = 1.8;
- Ball.boost = true;
- }
- MyBoard.dx = -0.3;
- MyBoard.x += MyBoard.dx*time;
- MyBoard.sprite.setPosition(MyBoard.x, MyBoard.y);
- std::cout << "<-" << std::endl;
- }
- }
- MyBoard.sprite.setPosition(MyBoard.x, MyBoard.y);
- //keyboard
- if ((Ball.boost) && (Ball.y <= 200))
- {
- Ball.boost = false;
- Ball.boostx = 1.0;
- }
- if ((!lose)&&(start)) {
- if (Ball.x >= 940)Ball.dx = -0.3;
- if (Ball.x <= 60)Ball.dx = 0.3;
- if (Ball.y <= 80)Ball.dy = 0.3;
- if (Ball.y >= 635)
- {
- Ball.dy = -0.3;
- if ((Ball.y >= 635) && ((MyBoard.x - 20 > (Ball.x)) || (MyBoard.x + 150) < (Ball.x)))lose = 1;
- }
- int slow = 1.2;
- Ball.x += Ball.dx*time*Ball.boostx / slow;
- Ball.y += Ball.dy*time*Ball.boosty / slow;
- }
- Ball.sprite.setPosition(Ball.x, Ball.y);
- //ball
- for (int i = 0; i <= 17; i++)
- {
- obj1.chekBall(Ball, arrwoods[i]);
- }
- window.clear();
- window.draw(bgsprite);
- if (!lose)
- {
- for (int i = 0; i <= 17; i++)
- {
- bool k1 = arrwoods[i].life;
- if (k1)window.draw(arrwoods[i].sprite);
- }
- window.draw(Ball.sprite);
- window.draw(MyBoard.sprite);
- }
- if (!start)
- {
- text1.setString("Game paused, press key 'space'");
- text1.setPosition(140, 299);
- window.draw(text1);
- }
- if (lose) {
- text2.setString("press key 'R' for new game");
- text2.setPosition(249, 499);
- window.draw(text2);
- text.setString("game over");
- text.setPosition(299, 299);
- window.draw(text);
- }
- if (replay)
- {
- for (int i = 0; i <= 17; i++)
- {
- arrwoods[i].life = 1;
- }
- MyBoard.x = 450;
- Ball.x = 450;
- Ball.y = 250;
- Ball.dx =-0.3;
- Ball.dy = 0.3;
- lose = 0;
- start = 0;
- replay = !replay;
- }
- window.display();
- }
- }
- //game obj.сpp
- #include "GameObjects.h"
- GameObjects::GameObjects(double X, double Y, double W, double H, double ix, double iy, const sf::String &f)
- {
- dx = 0; dy = 0; speed = 0, boostx = 1, boosty = 1;
- w = W; h = H;
- File = f;
- life = true;
- boost = false;
- image.loadFromFile("images/" + File);
- texture.loadFromImage(image);
- sprite.setTexture(texture);
- x = X; y = Y;
- sprite.setPosition(x, y);
- sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
- }
- GameObjects::GameObjects()
- {
- x = 0, y = 0, dx = 0, dy = 0, speed = 0, ix = 0, iy = 0, w = 0, h = 0;
- life = true;
- boost = false;
- image.loadFromFile("images/bg.png");
- texture.loadFromImage(image);
- sprite.setTexture(texture);
- sprite.setPosition(x, y);
- sprite.setPosition(x, y);
- sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
- }
- //game obj.cpp
- #include "GameObjects.h"
- GameObjects::GameObjects(double X, double Y, double W, double H, double ix, double iy, const sf::String &f)
- {
- dx = 0; dy = 0; speed = 0, boostx = 1, boosty = 1;
- w = W; h = H;
- File = f;
- life = true;
- boost = false;
- image.loadFromFile("images/" + File);
- texture.loadFromImage(image);
- sprite.setTexture(texture);
- x = X; y = Y;
- sprite.setPosition(x, y);
- sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
- }
- GameObjects::GameObjects()
- {
- x = 0, y = 0, dx = 0, dy = 0, speed = 0, ix = 0, iy = 0, w = 0, h = 0;
- life = true;
- boost = false;
- image.loadFromFile("images/bg.png");
- texture.loadFromImage(image);
- sprite.setTexture(texture);
- sprite.setPosition(x, y);
- sprite.setPosition(x, y);
- sprite.setTextureRect(sf::IntRect(ix, iy, w, h));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement