Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include<vector>
- using namespace sf;
- int main()
- {
- // Create the main window
- RenderWindow window(VideoMode(800,600) , "Wall");
- window.setFramerateLimit(60);
- //Grid
- float girdSize = 50.f;
- //Creat player
- RectangleShape player(Vector2f(50 , 50));
- player.setFillColor(Color::Green);
- //Creat wall
- RectangleShape wall(Vector2f(girdSize , girdSize));
- wall.setFillColor(Color::Red);
- int red = 1;
- std::vector<RectangleShape> walls;
- //Creat acceleration, drag, maxVelocity, ..
- float acceleration = 2.5f;
- float drag = 0.5f;
- float maxVelocity = 6.f;
- Vector2f currentVelocity( 0 , 0);
- float deltaTime = 0;
- float multi = 15;
- Clock clock;
- //Main loop
- while(window.isOpen()){
- Event event;
- while(window.pollEvent(event)){
- if(event.type == Event::Closed){
- window.close();
- }
- }
- //Update
- //Change color
- if(Keyboard::isKeyPressed(Keyboard::R) && red == 1){
- wall.setFillColor(Color::Cyan);
- red = 0;
- }
- else if(Keyboard::isKeyPressed(Keyboard::R) && red == 0){
- wall.setFillColor(Color::Red);
- red = 1;
- }
- //Restart deltaTime
- deltaTime = clock.restart().asSeconds();
- //Acceleration
- if(Keyboard::isKeyPressed(Keyboard::S)){
- if(currentVelocity.y < maxVelocity){
- currentVelocity.y += acceleration * deltaTime * multi;
- }
- }
- if(Keyboard::isKeyPressed(Keyboard::D)){
- if(currentVelocity.x < maxVelocity){
- currentVelocity.x += acceleration * deltaTime * multi;
- }
- }
- if(Keyboard::isKeyPressed(Keyboard::A)){
- if(currentVelocity.x > -maxVelocity){
- currentVelocity.x -= acceleration * deltaTime * multi;
- }
- }
- if(Keyboard::isKeyPressed(Keyboard::W)){
- if(currentVelocity.y > -maxVelocity){
- currentVelocity.y -= acceleration * deltaTime * multi;
- }
- }
- //Drag
- if(currentVelocity.x > 0){
- currentVelocity.x -= drag * deltaTime * multi;
- if(currentVelocity.x < 0){
- currentVelocity.x = 0 ;
- }
- }
- if(currentVelocity.x < 0){
- currentVelocity.x += drag * deltaTime * multi;
- if(currentVelocity.x > 0){
- currentVelocity.x = 0 ;
- }
- }
- if(currentVelocity.y > 0){
- currentVelocity.y -= drag * deltaTime * multi;
- if(currentVelocity.y < 0){
- currentVelocity.y = 0 ;
- }
- }
- if(currentVelocity.y < 0){
- currentVelocity.y += drag * deltaTime * multi;
- if(currentVelocity.y > 0){
- currentVelocity.y = 0 ;
- }
- }
- //Colision with window
- if(player.getPosition().x < 0 ){
- currentVelocity.x = 0;
- player.setPosition(0 , player.getPosition().y);
- }
- if(player.getPosition().y < 0 ){
- currentVelocity.y = 0;
- player.setPosition(player.getPosition().x , 0);
- }
- if(player.getPosition().x > window.getSize().x - player.getSize().x ){
- currentVelocity.x = 0;
- player.setPosition(window.getSize().x - player.getSize().x, player.getPosition().y );
- }
- if(player.getPosition().y > window.getSize().y - player.getSize().y ){
- currentVelocity.y = 0;
- player.setPosition(player.getPosition().x, window.getSize().y - player.getSize().y );
- }
- //Set wall
- Vector2i mousePos = Mouse::getPosition(window);
- if(Mouse::isButtonPressed(Mouse::Right)){
- int check = 1;
- wall.setPosition((int) (mousePos.x / girdSize) * girdSize , (int) (mousePos.y / girdSize) * girdSize);
- //Duyet tu dau den cuoi walls
- for(size_t i = 0 ; i < walls.size() ; i++){
- if((int) (mousePos.x / girdSize) * girdSize == walls[i].getPosition().x
- && (int) (mousePos.y / girdSize) * girdSize == walls[i].getPosition().y){
- /*================*/
- check = 0;
- break;
- }
- }
- //Push back
- if(check == 1){
- walls.push_back(wall);
- }
- }
- //Erase wall
- if(Mouse::isButtonPressed(Mouse::Left)){
- for(size_t i = 0 ; i < walls.size() ; i++){
- if( (int) (mousePos.x / girdSize) * girdSize == walls[i].getPosition().x
- && (int) (mousePos.y / girdSize) * girdSize == walls[i].getPosition().y ){
- /*=================*/
- walls.erase(walls.begin() + i);
- break;
- }
- }
- }
- if(Mouse::isButtonPressed(Mouse::Middle)){
- walls.erase(walls.begin() , walls.begin() + walls.size() );
- }
- //Colisions
- for(size_t i = 0 ; i < walls.size() ;i++){
- if(walls[i].getGlobalBounds().intersects(player.getGlobalBounds())){
- //Left colission
- if(player.getGlobalBounds().left < walls[i].getGlobalBounds().left
- && player.getGlobalBounds().left + player.getGlobalBounds().width < walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
- && player.getGlobalBounds().top < walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
- && player.getGlobalBounds().top + player.getGlobalBounds().height > walls[i].getGlobalBounds().top
- ){
- currentVelocity.x *= -0.0 ;
- player.setPosition(walls[i].getGlobalBounds().left - player.getGlobalBounds().width , player.getGlobalBounds().top);
- std::cout << "left!" << i << std::endl;
- }
- //Right colission
- else if(player.getGlobalBounds().left > walls[i].getGlobalBounds().left
- && player.getGlobalBounds().left + player.getGlobalBounds().width > walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
- && player.getGlobalBounds().top < walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
- && player.getGlobalBounds().top + player.getGlobalBounds().height > walls[i].getGlobalBounds().top
- ){
- currentVelocity.x *= -0.0;
- player.setPosition(walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width , player.getGlobalBounds().top);
- std::cout << "right!" << i << std::endl;
- }
- //Bottom colission
- else if(player.getGlobalBounds().top > walls[i].getGlobalBounds().top
- && player.getGlobalBounds().top + player.getGlobalBounds().height > walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
- && player.getGlobalBounds().left < walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
- && player.getGlobalBounds().left + player.getGlobalBounds().width > walls[i].getGlobalBounds().left
- ){
- currentVelocity.y *= -0.0;
- player.setPosition(player.getGlobalBounds().left , walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height);
- std::cout << "bottom!" << i<< std::endl;
- }
- //Top colission
- else if(player.getGlobalBounds().top < walls[i].getGlobalBounds().top
- && player.getGlobalBounds().top + player.getGlobalBounds().height < walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
- && player.getGlobalBounds().left < walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
- && player.getGlobalBounds().left + player.getGlobalBounds().width > walls[i].getGlobalBounds().left
- ){
- currentVelocity.y *= 0.0;
- player.setPosition(player.getGlobalBounds().left , walls[i].getGlobalBounds().top - player.getGlobalBounds().height);
- std::cout << "top!" << i << std::endl;
- }
- }
- }
- //Player move
- player.move(currentVelocity);
- //Render
- window.clear();
- for(size_t i = 0 ; i < walls.size() ; i++){
- window.draw(walls[i]);
- }
- window.draw(player);
- window.display();
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement