Advertisement
Guest User

obj

a guest
Apr 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.47 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <SFML\Graphics.hpp>
  4. #include <SFML\Network.hpp>
  5. #include <SFML\System.hpp>
  6. #include <Windows.h>
  7. #include <list>
  8. #include <vector>
  9. #include <iterator>
  10. using namespace std;
  11.  
  12.  
  13. class Sprites : public sf::Drawable, public sf::Transformable
  14. {
  15. public:
  16.     Sprites()
  17.     {
  18.         s.setSize(sf::Vector2f(40,40));
  19.         s.setFillColor(sf::Color::Blue);
  20.         //s.setPosition(300,240);
  21.         b.setSize(sf::Vector2f(40,40));
  22.         b.setFillColor(sf::Color::Blue);
  23.         for(int i=0;i<4;i++)
  24.         {
  25.             int pos = 70;
  26.             b.setPosition(pos*i,30);
  27.             rs.push_back(b);
  28.         }
  29.        
  30.     }
  31.     void fungsi()
  32.     {
  33.        
  34.     }
  35.  
  36.     virtual void draw(sf::RenderTarget &target, sf::RenderStates state)const
  37.     {
  38.         state.transform *= getTransform();
  39.     //  target.draw(s,state);
  40.         for(int i=0;i<rs.size();i++)
  41.             target.draw(rs.at(i));
  42.  
  43.     }
  44.  
  45. private:
  46.     vector<sf::RectangleShape> rs;
  47.     sf::RectangleShape s;
  48.     sf::RectangleShape b;
  49.    
  50. };
  51.  
  52. void anime2()
  53. {
  54.     sf::RenderWindow win(sf::VideoMode(600,480),"SFML");
  55.         Sprites sprite;
  56.         sprite.setRotation(30);
  57.         sprite.setPosition((float)win.getSize().x/2,0);
  58.         vector<int> m;
  59.    
  60.         sf::Clock clock;
  61.  
  62.         while(win.isOpen())
  63.         {
  64.             sf::Event event;
  65.             while(win.pollEvent(event))
  66.             {
  67.                 if(event.type == sf::Event::Closed)
  68.                     win.close();
  69.             }
  70.  
  71.  
  72.             sf::Time time1 = clock.getElapsedTime();
  73.             sf::Time time2 = sf::milliseconds(100);
  74.             int time5 = clock.getElapsedTime().asSeconds();
  75.             cout<<time5;
  76.             if(time1 >= time2)
  77.             {
  78.                 sprite.move(sf::Vector2f(0, 3));
  79.                 clock.restart();
  80.             }
  81.  
  82.             win.clear();
  83.             win.draw(sprite);
  84.             win.display();
  85.         }
  86. }
  87.  
  88. class wall
  89. {
  90. public:
  91.     wall()
  92.     {
  93.         rect_wall.setSize(sf::Vector2f(32 , 32));
  94.         rect_wall.setFillColor(sf::Color::Blue);
  95.     }
  96.  
  97.    
  98.    
  99. public:
  100.     sf::RectangleShape rect_wall;
  101.    
  102. };
  103.  
  104. class bunga : public sf::Drawable
  105. {
  106. public:
  107.     bunga(string file,int size_tile,int size_map,int array_texture[6][10])
  108.     {
  109.         if(!texture.loadFromFile(file))
  110.             return;
  111.        
  112.         size_of_tile = size_tile;
  113.         texture_vertical = (texture.getSize().y/size_tile);
  114.         texture_horizontal = (texture.getSize().x/size_tile);
  115.         map_size = size_map;
  116.         //set_texture = array_texture;
  117.  
  118.         for(int i = 0; i < 6; i++)
  119.         {
  120.             for(int k = 0; k < 10; k++)
  121.             {
  122.                 ract.setSize(sf::Vector2f(map_size, map_size));
  123.                 ract.setTexture(&texture);
  124.                 ract.setPosition(sf::Vector2f(map_size * k, map_size * i));
  125.                 sf::Vector2i pos(size_of_tile *  (array_texture[i][k] % texture_horizontal), size_of_tile * (array_texture[i][k] / texture_vertical));
  126.                 sf::Vector2i rec(size_of_tile, size_of_tile);
  127.                 ract.setTextureRect(sf::IntRect(pos , rec));
  128.                 CountTile.push_back(ract);
  129.             }
  130.         }
  131.     }
  132.  
  133.     void Tilemap()
  134.     {
  135.        
  136.        
  137.  
  138.     }
  139.  
  140. private:
  141.     virtual void draw(sf::RenderTarget &target, sf::RenderStates state)const
  142.     {
  143.         for(int a = 0; a < CountTile.size(); a++)
  144.         {
  145.             target.draw(CountTile.at(a),state);
  146.         }
  147.     }
  148. private:
  149.     vector<sf::RectangleShape> CountTile;
  150.     sf::RectangleShape ract;
  151.     sf::Texture texture;
  152.     int size_of_tile;
  153.     int texture_vertical;
  154.     int texture_horizontal;
  155.     int set_texture[6][10];
  156.     int map_size;
  157. };
  158.  
  159. class Human : public sf::Drawable, public sf::Transformable
  160. {
  161.  
  162. public:
  163.     Human()
  164.     {
  165.         if(!t_player.loadFromFile("spritewalking.png."))
  166.             return;
  167.  
  168.         sp.setTexture(&t_player);
  169.         sp.setSize(sf::Vector2f(56 , 56));
  170.  
  171.     }
  172.  
  173.    
  174.  
  175. private:
  176.     virtual void draw(sf::RenderTarget &target, sf::RenderStates state)const
  177.     {
  178.         state.transform *= getTransform();
  179.         target.draw(sp, state);
  180.         //target.draw(missle_ract, state);
  181.     }
  182.  
  183. public:
  184.     sf::Texture t_player;
  185.     sf::RectangleShape sp;
  186.  
  187. };
  188.  
  189. class missle
  190. {
  191. public:
  192.     missle()
  193.     {
  194.         //missle_ract = new  sf::RectangleShape;
  195.         missle_ract.setSize(sf::Vector2f(10,5));
  196.         missle_ract.setFillColor(sf::Color::Red);
  197.         direction = 0;
  198.         //Realod_mis = new vector<sf::RectangleShape>();
  199.     }
  200.  
  201.     void Move()
  202.     {
  203.         if(direction == 1)
  204.             missle_ract.move(0 , -0.5f);
  205.         if(direction == 2)
  206.             missle_ract.move(0.5f , 0);
  207.         if(direction == 3)
  208.             missle_ract.move(0 , 0.5f);
  209.         if(direction == 4)
  210.             missle_ract.move(-0.5f , 0);
  211.         //Realod_mis->push_back(*missle_ract);
  212.  
  213.     }
  214.  
  215.    
  216.  
  217.  
  218.  
  219. public:
  220.     sf::RectangleShape missle_ract;
  221.     int direction;
  222.     //vector<sf::RectangleShape> *Realod_mis;
  223. };
  224.  
  225.  
  226. void CreateWall()
  227.     {
  228.        
  229.     }
  230.  
  231. void MyTile()
  232. {
  233.     sf::RenderWindow win(sf::VideoMode(640,384),"Tilemap");
  234.    
  235.     bool check_top = true;
  236.     bool check_down = true;
  237.     bool check_right = true;
  238.     bool check_left = true;
  239.     int move_sp = 56;
  240.     int walking_sp = 0;
  241.     int walking_speed = 0;
  242.     float waktu = 0;
  243.     int value;
  244.     int map[6][10] =
  245.     {
  246.         { 27 , 27 , 27 , 24 , 27 , 27 , 27 , 24 , 27 , 27},
  247.         { 0 , 0 , 0 , 0 , 0 , 0  , 0 , 0 , 0 , 0},
  248.         { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
  249.         { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
  250.         { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
  251.         { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0}
  252.     };
  253.  
  254.     bunga b("gambar.png",32,64,map);
  255.     b.Tilemap();
  256.  
  257.     Human player;
  258.     player.setPosition((win.getSize().x/2) + (player.t_player.getSize().x/2),win.getSize().y/2 + (player.t_player.getSize().y/2));
  259.     player.sp.setTextureRect(sf::IntRect(move_sp * 0, move_sp * 3, move_sp, move_sp));
  260.  
  261.     vector<missle>::const_iterator iter;
  262.     vector<missle> missle_array;
  263.     missle missle_player;
  264.  
  265.     vector<wall>::const_iterator iter2;
  266.     vector<wall> wall_array;
  267.     wall defense_wall;
  268.     for(int i = 0; i < 10; i++)
  269.         {
  270.             wall_array.push_back(defense_wall);
  271.         }
  272.  
  273.  
  274.     sf::Clock clock;
  275.     sf::Time time1;
  276.  
  277.     while(win.isOpen())
  278.     {
  279.         sf::Event event;
  280.         while(win.pollEvent(event))
  281.         {
  282.             if(event.type == sf::Event::Closed)
  283.                 win.close();
  284.         }
  285.  
  286.                
  287.         value = 0;
  288.         for(iter2 = wall_array.begin(); iter2 != wall_array.end(); iter2++)
  289.         {
  290.  
  291.             if(player.sp.getGlobalBounds().intersects(wall_array[value].rect_wall.getGlobalBounds()))
  292.             {
  293.                 cout<<"Collision"<<endl;
  294.  
  295.             }
  296.  
  297.             value++;
  298.  
  299.         }
  300.        
  301.  
  302.  
  303.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  304.         {
  305.            
  306.             if(check_top == true)
  307.             {
  308.                 player.move(sf::Vector2f(0 , -0.2f));
  309.                 player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 3, move_sp, move_sp));
  310.                 missle_player.direction = 1;
  311.                 check_down = true;
  312.                 check_top = true;
  313.                 check_left = true;
  314.                 check_right = true;
  315.             }
  316.            
  317.         }
  318.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  319.         {
  320.             if(check_right == true)
  321.             {
  322.                 player.move(sf::Vector2f(0.2f , 0));
  323.                 player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 2, move_sp, move_sp));
  324.                 missle_player.direction = 2;
  325.                 check_down = true;
  326.                 check_top = true;
  327.                 check_left = true;
  328.                 check_right = true;
  329.             }
  330.         }
  331.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  332.         {
  333.             if(check_down == true)
  334.             {
  335.                 player.move(sf::Vector2f(0 , 0.2f));
  336.                 player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 0, move_sp, move_sp));
  337.                 missle_player.direction = 3;
  338.                 check_down = true;
  339.                 check_top = true;
  340.                 check_left = true;
  341.                 check_right = true;
  342.             }
  343.         }
  344.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  345.         {
  346.             if(check_left == true)
  347.             {
  348.                 player.move(sf::Vector2f(-0.2f , 0));
  349.                 player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 1, move_sp, move_sp));
  350.                 missle_player.direction = 4;
  351.                 check_down = true;
  352.                 check_top = true;
  353.                 check_left = true;
  354.                 check_right = true;
  355.             }
  356.         }
  357.         walking_speed++;
  358.         if(walking_speed == 5)
  359.         {
  360.             if(walking_sp == 3)
  361.             {
  362.                 walking_sp = 0;
  363.                
  364.             }
  365.             walking_sp++;
  366.             walking_speed = 0;
  367.         }
  368.        
  369.         waktu = clock.getElapsedTime().asMilliseconds();
  370.  
  371.         if(waktu >= 300)
  372.         {
  373.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  374.             {
  375.                 missle_player.missle_ract.setPosition(sf::Vector2f(player.getPosition().x + player.sp.getSize().x/2 , player.getPosition().y + player.sp.getSize().y/2));
  376.                 missle_array.push_back(missle_player);
  377.                
  378.             }
  379.  
  380.             clock.restart();
  381.         }
  382.        
  383.        
  384.         win.clear();
  385.  
  386.         win.draw(b);
  387.    
  388.         //======================wall=========================
  389.  
  390.        
  391.  
  392.        
  393.        
  394.         for(int i = 0 ; i < wall_array.size(); i++)
  395.         {
  396.             wall_array[i].rect_wall.setPosition(sf::Vector2f(32 * i, 32));
  397.             win.draw(wall_array[i].rect_wall);
  398.            
  399.         }
  400.  
  401.  
  402.         value = 0;
  403.         for(iter = missle_array.begin(); iter != missle_array.end(); iter++)
  404.         {
  405.             missle_array[value].Move();
  406.             win.draw(missle_array[value].missle_ract);
  407.             value++;
  408.             // missle_player.Realod_mis->erase(missle_player.Realod_mis->begin() + i);
  409.         }
  410.         win.draw(player);
  411.  
  412.         win.display();
  413.     }
  414.  
  415.     return;
  416. }
  417.  
  418. int main()
  419. {
  420.     //Anime();
  421.        
  422.     MyTile();
  423.    
  424.     return 0;
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement