Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Library Includes:
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <sstream>
- //Object Classes:
- #include "Arrow.h"
- #include "InsectImage.h"
- #include "Button.h"
- #include "Waypoint.h"
- //Player Classes:
- #include "Caterpillar.h"
- #include "Ant.h"
- #include "Grasshopper.h"
- #include "Scorpion.h"
- //Window States:
- #define MAIN_MENU 1
- #define MAIN_GAME 2
- #define CHOOSE_INSECT 10
- int main() {
- sf::RenderWindow window;
- sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);
- window.create(sf::VideoMode(900, 900), "Insect World", sf::Style::Titlebar | sf::Style::Close);
- window.setPosition(centerWindow);
- window.setKeyRepeatEnabled(true);
- int windowState = MAIN_MENU;
- //Insect Species:
- enum InsectSpecies {
- none,
- caterpillar,
- grasshopper,
- ant
- };
- InsectSpecies insectID = none;
- //Load In Fonts:
- sf::Font arial;
- if (!arial.loadFromFile("arial.ttf")) {
- std::cerr << "Error Loading Font\n";
- }
- sf::Font impact;
- if (!impact.loadFromFile("impact.ttf")) {
- std::cerr << "Error Loading Impact\n";
- }
- //Default View:
- sf::View noView;
- noView.setCenter(window.getDefaultView().getCenter().x, window.getDefaultView().getCenter().y);
- /* WINDOW STATE 1: MAIN MENU */
- main_menu:
- //Welcome Label:
- sf::Text lblWelcome;
- lblWelcome.setCharacterSize(150);
- lblWelcome.setFont(impact);
- lblWelcome.setColor(sf::Color::Cyan);
- lblWelcome.setString("Insect World");
- lblWelcome.setPosition({ 60, 10 });
- //Label For btnPlay:
- sf::Text lblBtnPlay;
- lblBtnPlay.setCharacterSize(60);
- lblBtnPlay.setString("Play");
- lblBtnPlay.setFont(impact);
- lblBtnPlay.setPosition({ 400, 462 });
- lblBtnPlay.setColor(sf::Color::Black);
- //btnPlay:
- Button btnPlay({ 400, 100 });
- btnPlay.setColor(sf::Color::Green);
- btnPlay.setPos({ 250, 450 });
- //Label For btnChoose:
- sf::Text lblBtnChoose;
- lblBtnChoose.setCharacterSize(50);
- lblBtnChoose.setString("Choose Insect");
- lblBtnChoose.setFont(impact);
- lblBtnChoose.setPosition({ 300, 290 });
- lblBtnChoose.setColor(sf::Color::Black);
- //btnChoose:
- Button btnChoose({ 400, 100 });
- btnChoose.setColor(sf::Color::Magenta);
- btnChoose.setPos({ 250, 270 });
- //Main Menu Loop:
- while (window.isOpen() && windowState == MAIN_MENU) {
- sf::Event Event;
- int mousePosX = sf::Mouse::getPosition().x - 520;
- int mousePosY = sf::Mouse::getPosition().y - 89;
- //Event Loop:
- while (window.pollEvent(Event)) {
- switch (Event.type) {
- case sf::Event::Closed:
- window.close();
- case sf::Event::MouseButtonPressed:
- if (btnPlay.isMouseOver(mousePosX, mousePosY)) {
- //Change WindowState To Game Window:
- windowState = MAIN_GAME;
- }
- if (btnChoose.isMouseOver(mousePosX, mousePosY)) {
- //Change WindowState To Choose Insect Window
- windowState = CHOOSE_INSECT;
- }
- case sf::Event::MouseMoved:
- if (btnPlay.isMouseOver(mousePosX, mousePosY)) {
- btnPlay.setColor(sf::Color(16, 133, 52));
- }
- else {
- btnPlay.setColor(sf::Color::Green);
- }
- if (btnChoose.isMouseOver(mousePosX, mousePosY)) {
- btnChoose.setColor(sf::Color(148, 29, 124));
- }
- else {
- btnChoose.setColor(sf::Color::Magenta);
- }
- }
- }
- window.clear();
- window.draw(lblWelcome);
- btnPlay.drawTo(&window);
- window.draw(lblBtnPlay);
- btnChoose.drawTo(&window);
- window.draw(lblBtnChoose);
- window.setView(noView);
- window.display();
- }
- /* WINDOW STATE 2: MAIN GAME */
- //Waypoints:
- Waypoint point1({ 100, 900 }, sf::Color::Transparent);
- point1.setPos({ 300, 0 });
- //Characters:
- Caterpillar cater1(1);
- if (insectID == caterpillar || insectID == none) {
- cater1.setPos({ 100, 100 });
- }
- else {
- cater1.setPos({ 43242, 423432 });
- }
- Ant ant1;
- if (insectID == ant) {
- ant1.setPos({ 100, 100 });
- }
- else {
- ant1.setPos({ 43242, 423432 });
- }
- Grasshopper grass1;
- if (insectID == grasshopper) {
- grass1.setPos({ 100, 100 });
- }
- else {
- grass1.setPos({ 43242, 423432 });
- }
- //Gravity Vars:
- const int groundHeight = 780;
- bool isJumping = false;
- const float fallSpeed = 0.6;
- //Movment Vars:
- const float moveSpeed = 0.99;
- const float jumpSpeed = 0.5;
- bool goingRight = false;
- bool goingLeft = false;
- float x = cater1.getX();
- float y = cater1.getY();
- //Player Coords:
- sf::Text xCoord;
- xCoord.setCharacterSize(30);
- xCoord.setFont(arial);
- xCoord.setPosition({ 453434, 53455 });
- xCoord.setString("X Position: 0");
- sf::Text yCoord;
- yCoord.setCharacterSize(30);
- yCoord.setFont(arial);
- yCoord.setPosition({ -54543, 54355 });
- yCoord.setString("Y Position: 0");
- std::ostringstream ssX;
- std::ostringstream ssY;
- bool coordsShowing = false;
- //Decoration:
- sf::Texture grassTexture;
- sf::Sprite grassWallpaper;
- if (!grassTexture.loadFromFile("grassImage.png"))
- std::cerr << "Error\n";
- grassWallpaper.setTexture(grassTexture);
- grassWallpaper.setPosition({ -10, 350 });
- sf::Texture grassTexture2;
- sf::Sprite grassWallpaper2;
- if (!grassTexture2.loadFromFile("grassImage.png"))
- std::cerr << "Error\n";
- grassWallpaper2.setTexture(grassTexture2);
- grassWallpaper2.setPosition({ 3500, 350 });
- sf::Texture sunTexture;
- sf::Sprite sunWallpaper;
- if (!sunTexture.loadFromFile("sun.png"))
- std::cerr << "Error\n";
- sunWallpaper.setTexture(sunTexture);
- sunWallpaper.setPosition({ -50, -50 });
- //Scorpion Vars:
- std::vector<Scorpion*> scorpVec;
- Scorpion scorp1;
- scorpVec.push_back(&scorp1);
- Scorpion scorp2;
- scorpVec.push_back(&scorp2);
- Scorpion scorp3;
- scorpVec.push_back(&scorp3);
- Scorpion scorp4;
- scorpVec.push_back(&scorp4);
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->hide();
- }
- bool scorpShown = false;
- float scorpSpeed = 0.05f;
- //View Objects:
- sf::View followPlayer;
- followPlayer.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
- sf::Vector2f position(window.getSize().x / 2, window.getSize().y / 2);
- //Game Loop:
- while (window.isOpen() && windowState == MAIN_GAME) {
- sf::Event Event;
- //Caterpillar Movement:
- if (insectID == caterpillar || insectID == none) {
- ssX.str("");
- ssX << "X Position: " << cater1.getX();
- xCoord.setString(ssX.str());
- ssY.str("");
- ssY << "Y Position: " << cater1.getY();
- yCoord.setString(ssY.str());
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- if (goingLeft == true) {
- cater1.move({ -jumpSpeed, -jumpSpeed });
- }
- else if (goingRight == true) {
- cater1.move({ jumpSpeed, -jumpSpeed });
- }
- else {
- cater1.move({ 0, -jumpSpeed });
- }
- isJumping = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
- cater1.move({ -moveSpeed, 0 });
- cater1.setImage('l');
- goingLeft = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
- cater1.move({ moveSpeed, 0 });
- cater1.setImage('r');
- goingRight = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) {
- coordsShowing = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) {
- coordsShowing = false;
- }
- }
- //Ant Movement:
- if (insectID == ant) {
- ssX.str("");
- ssX << "X Position: " << ant1.getX();
- xCoord.setString(ssX.str());
- ssY.str("");
- ssY << "Y Position: " << ant1.getY();
- yCoord.setString(ssY.str());
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- if (goingLeft == true) {
- ant1.move({ -jumpSpeed, -jumpSpeed });
- }
- else if (goingRight == true) {
- ant1.move({ jumpSpeed, -jumpSpeed });
- }
- else {
- ant1.move({ 0, -jumpSpeed });
- }
- isJumping = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
- ant1.setImage('l');
- ant1.move({ -moveSpeed, 0 });
- goingLeft = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
- ant1.setImage('r');
- ant1.move({ moveSpeed, 0 });
- goingRight = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) {
- coordsShowing = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) {
- coordsShowing = false;
- }
- }
- //Grasshopper Movement:
- if (insectID == grasshopper) {
- ssX.str("");
- ssX << "X Position: " << grass1.getX();
- xCoord.setString(ssX.str());
- ssY.str("");
- ssY << "Y Position: " << grass1.getY();
- yCoord.setString(ssY.str());
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- if (goingLeft == true) {
- grass1.move({ -jumpSpeed, -jumpSpeed });
- }
- else if (goingRight == true) {
- grass1.move({ jumpSpeed, -jumpSpeed });
- }
- else {
- grass1.move({ 0, -jumpSpeed });
- }
- isJumping = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
- grass1.setImage('l');
- grass1.move({ -moveSpeed, 0 });
- goingLeft = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
- grass1.setImage('r');
- grass1.move({ moveSpeed, 0 });
- goingRight = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) {
- coordsShowing = true;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) {
- coordsShowing = false;
- }
- }
- //Event Loop:
- while (window.pollEvent(Event)) {
- switch (Event.type) {
- case sf::Event::Closed:
- window.close();
- case sf::Event::KeyPressed:
- if (Event.key.code == sf::Keyboard::Escape) {
- windowState = MAIN_MENU;
- window.setView(noView);
- goto main_menu;
- }
- case sf::Event::KeyReleased:
- if (Event.key.code == sf::Keyboard::W) {
- isJumping = false;
- }
- else if (Event.key.code == sf::Keyboard::A) {
- goingLeft = false;
- }
- else if (Event.key.code == sf::Keyboard::D) {
- goingRight = false;
- }
- }
- }
- //Gravity Logic:
- if (insectID == caterpillar || insectID == none) {
- if (cater1.getY() < groundHeight && isJumping == false) {
- cater1.fall(fallSpeed);
- }
- if (cater1.getX() + 10 > window.getSize().x / 2) {
- position.x = cater1.getX() + 10;
- }
- else {
- position.x = window.getSize().x / 2;
- }
- }
- if (insectID == ant) {
- if (ant1.getY() < groundHeight && isJumping == false) {
- ant1.fall(fallSpeed);
- }
- if (ant1.getX() > window.getSize().x / 2) {
- position.x = ant1.getX();
- }
- else {
- position.x = window.getSize().x / 2;
- }
- }
- if (insectID == grasshopper) {
- if (grass1.getY() < groundHeight && isJumping == false) {
- grass1.fall(fallSpeed);
- }
- if (grass1.getX() > window.getSize().x / 2) {
- position.x = grass1.getX();
- }
- else {
- position.x = window.getSize().x / 2;
- }
- }
- //Cater Coords Logic:
- if (coordsShowing == true && insectID == caterpillar) {
- xCoord.setPosition({ cater1.getX() + 100, cater1.getY() - 700 });
- yCoord.setPosition({ cater1.getX() + 100, cater1.getY() - 750 });
- }
- else if(coordsShowing == false && insectID == caterpillar){
- xCoord.setPosition({ 543545, 543543 });
- yCoord.setPosition({ 345435, 54453 });
- }
- if (coordsShowing == true && insectID == none) {
- xCoord.setPosition({ cater1.getX() + 100, cater1.getY() - 700 });
- yCoord.setPosition({ cater1.getX() + 100, cater1.getY() - 750 });
- }
- else if (coordsShowing == false && insectID == none) {
- xCoord.setPosition({ 543545, 543543 });
- yCoord.setPosition({ 345435, 54453 });
- }
- //Ant Coords Logic:
- if (coordsShowing == true && insectID == ant) {
- xCoord.setPosition({ ant1.getX() + 100, ant1.getY() - 700 });
- yCoord.setPosition({ ant1.getX() + 100, ant1.getY() - 750 });
- }
- else if (coordsShowing == false && insectID == ant) {
- xCoord.setPosition({ 543545, 543543 });
- yCoord.setPosition({ 345435, 54453 });
- }
- //Grasshopper Coords Logic:
- if (coordsShowing == true && insectID == grasshopper) {
- xCoord.setPosition({ grass1.getX() + 100, grass1.getY() - 700 });
- yCoord.setPosition({ grass1.getX() + 100, grass1.getY() - 750 });
- }
- else if (coordsShowing == false && insectID == grasshopper) {
- xCoord.setPosition({ 543545, 543543 });
- yCoord.setPosition({ 345435, 54453 });
- }
- //Caterpillar Waypoint Logic:
- if (insectID == caterpillar || insectID == none) {
- if (point1.isCollidingWith(&cater1)) {
- if (scorpShown == false) {
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->setPos({ cater1.getX() + 600 + (i * 38), cater1.getY() - 15 - (i * 38) });
- }
- scorpShown = true;
- }
- }
- if (scorpShown == true) {
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->move({ -scorpSpeed, 0 });
- }
- }
- }
- //Ant Waypoint Logic:
- if (insectID == ant) {
- if (point1.isCollidingWith(&ant1)) {
- if (scorpShown == false) {
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->setPos({ ant1.getX() + 600 + (i * 38), ant1.getY() - 15 - (i * 35) });
- }
- scorpShown = true;
- }
- }
- if (scorpShown == true) {
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->move({ -scorpSpeed, 0 });
- }
- }
- }
- //Grasshopper Waypoint Logic:
- if (insectID == grasshopper) {
- if (point1.isCollidingWith(&grass1)) {
- if (scorpShown == false) {
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->setPos({ grass1.getX() + 600 + (i * 38), grass1.getY() - 15 - (i * 35) });
- }
- scorpShown = true;
- }
- }
- if (scorpShown == true) {
- for (int i = 0; i < scorpVec.size(); i++) {
- scorpVec[i]->move({ -scorpSpeed, 0 });
- }
- }
- }
- followPlayer.setCenter(position);
- window.clear(sf::Color(0, 128, 255));
- //Draw Coords:
- window.draw(xCoord);
- window.draw(yCoord);
- //Draw Decors:
- window.draw(grassWallpaper);
- window.draw(grassWallpaper2);
- window.draw(sunWallpaper);
- //Draw Waypoints:
- point1.drawTo(&window);
- //Draw Player Models:
- cater1.drawTo(&window);
- ant1.drawTo(&window);
- grass1.drawTo(&window);
- //Scorpions:
- scorp1.drawTo(&window);
- scorp2.drawTo(&window);
- scorp3.drawTo(&window);
- scorp4.drawTo(&window);
- //Window Stuff:
- window.setView(followPlayer);
- window.display();
- }
- /* WINDOW STATE 10: CHOOSE INSECT" */
- //lblChoose Attributes:
- sf::Text lblChoose;
- lblChoose.setCharacterSize(90);
- lblChoose.setColor(sf::Color::Black);
- lblChoose.setString("Choose Insect:");
- lblChoose.setFont(impact);
- lblChoose.setPosition({ 170, 10 });
- //Arrows:
- Arrow arrowRight('r');
- arrowRight.setPos({ 650, 350 });
- //Images:
- int currentInsect = 1;
- InsectImage caterpillarImage("caterpillar.png");
- InsectImage antImage("ant.png");
- InsectImage grasshopperImage("grasshopperImage.png");
- caterpillarImage.setPos({ 310, 400 });
- antImage.erase();
- grasshopperImage.erase();
- //Buttons and Labels:
- sf::Text lblInsect;
- lblInsect.setCharacterSize(60);
- lblInsect.setFont(impact);
- lblInsect.setColor(sf::Color::Black);
- lblInsect.setString("Caterpillar:");
- lblInsect.setPosition({ -10, 440 });
- sf::Text lblSelect;
- lblSelect.setCharacterSize(50);
- lblSelect.setFont(impact);
- lblSelect.setColor(sf::Color::Black);
- lblSelect.setString("Select");
- lblSelect.setPosition({ 385, 720 });
- Button btnSelect({ 280, 100 });
- btnSelect.setColor(sf::Color::Green);
- btnSelect.setPos({ 310, 700 });
- //Main Loop:
- while (window.isOpen() && windowState == CHOOSE_INSECT) {
- sf::Event Event;
- int mousePosX = sf::Mouse::getPosition().x - 520;
- int mousePosY = sf::Mouse::getPosition().y - 89;
- //Event Loop:
- while (window.pollEvent(Event)) {
- switch (Event.type) {
- case sf::Event::Closed:
- window.close();
- case sf::Event::MouseButtonPressed:
- if (arrowRight.isMouseOver(mousePosX, mousePosY)) {
- if (currentInsect == 0) {
- caterpillarImage.setPos({ 310, 400 });
- antImage.erase();
- grasshopperImage.erase();
- lblInsect.setString("Caterpillar:");
- currentInsect++;
- }
- else if (currentInsect == 1) {
- antImage.setPos({ 320, 390 });
- caterpillarImage.erase();
- grasshopperImage.erase();
- lblInsect.setString("Ant:");
- currentInsect++;
- }
- else if (currentInsect == 2) {
- grasshopperImage.setPos({ 320, 390 });
- caterpillarImage.erase();
- antImage.erase();
- lblInsect.setString("Grasshopper:");
- currentInsect = 0;
- }
- }
- if (btnSelect.isMouseOver(mousePosX, mousePosY)) {
- if (currentInsect == 1) {
- insectID = caterpillar;
- std::cout << currentInsect;
- std::cout << "Cater\n";
- }
- else if (currentInsect == 2) {
- insectID = ant;
- std::cout << "Ant\n";
- }
- else if (currentInsect == 0) {
- insectID = grasshopper;
- std::cout << "Grasshopper\n";
- }
- windowState = MAIN_MENU;
- goto main_menu;
- }
- case sf::Event::MouseMoved:
- if (arrowRight.isMouseOver(mousePosX, mousePosY)) {
- arrowRight.darken();
- }
- else {
- arrowRight.lighten();
- }
- if (btnSelect.isMouseOver(mousePosX, mousePosY)) {
- btnSelect.setColor(sf::Color(16, 133, 52));
- }
- else {
- btnSelect.setColor(sf::Color::Green);
- }
- }
- }
- window.clear(sf::Color::Magenta);
- window.draw(lblChoose);
- caterpillarImage.drawTo(&window);
- antImage.drawTo(&window);
- grasshopperImage.drawTo(&window);
- arrowRight.drawTo(&window);
- btnSelect.drawTo(&window);
- window.draw(lblInsect);
- window.draw(lblSelect);
- window.setView(noView);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement