Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Graphics.hpp>
- using namespace std;
- unsigned int width = 500;
- unsigned int height = 500;
- unsigned int direction = 0;
- int view_direction_adder_x;
- int view_direction_adder_y;
- int main()
- {
- int map[10][30] =
- {
- {2, 3, 2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 2, 2},
- {2, 2, 2, 2, 2, 2, 2, 3, 4, 4, 4, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2},
- {2, 2, 2, 3, 2, 3, 2, 2, 4, 4, 4, 2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2},
- {2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
- };
- sf::Vector2f view_direction(0, 0);
- sf::RenderWindow window(sf::VideoMode(width, height), "In Progress");
- window.setVerticalSyncEnabled(true);
- window.setFramerateLimit(70);
- sf::Texture sprite_sheet;
- sprite_sheet.loadFromFile("mainguy.png");
- sf::Sprite sprite;
- sprite.setTexture(sprite_sheet);
- sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
- sprite.setPosition(width/2, (height/2)); //Initial position of the sprite
- sprite.setOrigin(41 / 2, 36 / 2);
- sf::RectangleShape sky(sf::Vector2f(50, 50));
- sky.setFillColor(sf::Color::Cyan);
- sf::RectangleShape ground(sf::Vector2f(50, 50));
- ground.setFillColor(sf::Color::Green);
- sf::RectangleShape cloud(sf::Vector2f(50, 50));
- cloud.setFillColor(sf::Color::White);
- sf::RectangleShape building(sf::Vector2f(50, 50));
- building.setFillColor(sf::Color::Cyan);
- //Don't give this object any color
- sf::RectangleShape tester(sf::Vector2f(50, 50));
- sf::Time time = sf::seconds(0);
- sf::Clock clock;
- sf::Event event;
- while (window.isOpen())
- {
- view_direction_adder_x = 0;
- view_direction_adder_y = 0;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- {
- window.close();
- }
- }
- //Handle other things
- //Gather input
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- if (direction != 1)
- {
- clock.restart();
- }
- cout << "Walking right" << endl;
- direction = 1;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- if (direction != 2)
- {
- clock.restart();
- }
- cout << "Walking left" << endl;
- direction = 2;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- {
- if (direction != 3)
- {
- clock.restart();
- }
- cout << "Walking up" << endl;
- direction = 3;
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- {
- if (direction != 4)
- {
- clock.restart();
- }
- cout << "Walking down" << endl;
- direction = 4;
- }
- else
- {
- cout << "not moving" << endl;
- direction = 0;
- }
- //Execute animations
- if (direction == 1)
- {
- //sprite.move(4, 0);
- time = clock.getElapsedTime();
- if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
- {
- sprite.setTextureRect(sf::IntRect(0, 36, 41, 36));
- }
- else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
- {
- sprite.setTextureRect(sf::IntRect(41, 36, 41, 36));
- }
- else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
- {
- sprite.setTextureRect(sf::IntRect(41 * 2, 36, 41, 36));
- }
- else if (time.asSeconds() > 1.5)
- {
- clock.restart();
- }
- view_direction_adder_x += -10;
- }
- else if (direction == 2)
- {
- //sprite.move(-4, 0);
- time = clock.getElapsedTime();
- if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
- {
- sprite.setTextureRect(sf::IntRect(0, 36 * 3, 41, 36));
- }
- else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
- {
- sprite.setTextureRect(sf::IntRect(41, 36 * 3, 41, 36));
- }
- else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
- {
- sprite.setTextureRect(sf::IntRect(41 * 2, 36 * 3, 41, 36));
- }
- else if (time.asSeconds() > 1.5)
- {
- clock.restart();
- }
- view_direction_adder_x += 10;
- }
- else if (direction == 3)
- {
- //sprite.move(0, -4);
- time = clock.getElapsedTime();
- if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
- {
- sprite.setTextureRect(sf::IntRect(41, 36 * 2, 41, 36));
- }
- else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
- {
- sprite.setTextureRect(sf::IntRect(0, 36 * 2, 41, 36));
- }
- else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
- {
- sprite.setTextureRect(sf::IntRect(41 * 2, 36 * 2, 41, 36));
- }
- else if (time.asSeconds() > 1.5)
- {
- clock.restart();
- }
- view_direction_adder_y += 10;
- }
- else if (direction == 4)
- {
- //sprite.move(0, 4);
- time = clock.getElapsedTime();
- if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
- {
- sprite.setTextureRect(sf::IntRect(0, 0, 41, 36));
- }
- else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
- {
- sprite.setTextureRect(sf::IntRect(41 * 2, 0, 41, 36));
- }
- else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
- {
- sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
- }
- else if (time.asSeconds() > 1.5)
- {
- clock.restart();
- }
- view_direction_adder_y += -10;
- }
- else if (direction == 0)
- {
- sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
- }
- //Check collision/Height limit
- /*
- if (sprite.getPosition().y < 200)
- {
- sprite.setPosition(sprite.getPosition().x, 200);
- }
- */
- for (int mapY = 0; mapY < 10; mapY++)
- {
- for (int mapX = 0; mapX < 30; mapX++)
- {
- sf::FloatRect tester_boundingBox = tester.getGlobalBounds();
- if (map[mapY][mapX] == 1)
- {
- tester.setPosition(mapX * 50, mapY * 50);
- tester.move(view_direction);
- if (tester_boundingBox.intersects(sprite.getGlobalBounds()))
- cout << "Player is on ground" << endl;
- }
- else if (map[mapY][mapX] == 2)
- {
- tester.setPosition(mapX * 50, mapY * 50);
- tester.move(view_direction);
- if (tester_boundingBox.intersects(sprite.getGlobalBounds()))
- {
- cout << "Player is on sky" << endl;
- if (direction == 3)
- {
- sprite.setPosition(sprite.getPosition().x, tester.getPosition().y + 54);
- view_direction_adder_x = 0;
- view_direction_adder_y = 0;
- }
- }
- }
- else if (map[mapY][mapX] == 3)
- {
- tester.setPosition(mapX * 50, mapY * 50);
- tester.move(view_direction);
- if (tester_boundingBox.intersects(sprite.getGlobalBounds()))
- cout << "Player is on cloud" << endl;
- }
- }
- }
- view_direction.x += view_direction_adder_x;
- view_direction.y += view_direction_adder_y;
- window.clear(sf::Color::Black); //Clear
- //Here we draw everything
- for (int mapY = 0; mapY < 10; mapY++)
- {
- for (int mapX = 0; mapX < 30; mapX++)
- {
- if (map[mapY][mapX] == 1)
- {
- ground.setPosition(mapX * 50, mapY * 50);
- ground.move(view_direction);
- window.draw(ground);
- }
- else if (map[mapY][mapX] == 2)
- {
- sky.setPosition(mapX * 50, mapY * 50);
- sky.move(view_direction);
- window.draw(sky);
- }
- else if (map[mapY][mapX] == 3)
- {
- cloud.setPosition(mapX * 50, mapY * 50);
- cloud.move(view_direction);
- window.draw(cloud);
- }
- else if (map[mapY][mapX] == 3)
- {
- building.setPosition(mapX * 50, mapY * 50);
- building.move(view_direction);
- window.draw(building);
- }
- }
- }
- window.draw(sprite);
- window.display(); //Display
- //Display important calculations
- //cout << "view_direction.y: " << view_direction.y << " " << "view_direction.x: " << view_direction.x << endl;
- }
- int pause; cin >> pause;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement