Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <SFML\Graphics.hpp>
- #include <SFML\Network.hpp>
- #include <SFML\System.hpp>
- #include <Windows.h>
- #include <list>
- #include <vector>
- #include <iterator>
- using namespace std;
- class Sprites : public sf::Drawable, public sf::Transformable
- {
- public:
- Sprites()
- {
- s.setSize(sf::Vector2f(40,40));
- s.setFillColor(sf::Color::Blue);
- //s.setPosition(300,240);
- b.setSize(sf::Vector2f(40,40));
- b.setFillColor(sf::Color::Blue);
- for(int i=0;i<4;i++)
- {
- int pos = 70;
- b.setPosition(pos*i,30);
- rs.push_back(b);
- }
- }
- void fungsi()
- {
- }
- virtual void draw(sf::RenderTarget &target, sf::RenderStates state)const
- {
- state.transform *= getTransform();
- // target.draw(s,state);
- for(int i=0;i<rs.size();i++)
- target.draw(rs.at(i));
- }
- private:
- vector<sf::RectangleShape> rs;
- sf::RectangleShape s;
- sf::RectangleShape b;
- };
- void anime2()
- {
- sf::RenderWindow win(sf::VideoMode(600,480),"SFML");
- Sprites sprite;
- sprite.setRotation(30);
- sprite.setPosition((float)win.getSize().x/2,0);
- vector<int> m;
- sf::Clock clock;
- while(win.isOpen())
- {
- sf::Event event;
- while(win.pollEvent(event))
- {
- if(event.type == sf::Event::Closed)
- win.close();
- }
- sf::Time time1 = clock.getElapsedTime();
- sf::Time time2 = sf::milliseconds(100);
- int time5 = clock.getElapsedTime().asSeconds();
- cout<<time5;
- if(time1 >= time2)
- {
- sprite.move(sf::Vector2f(0, 3));
- clock.restart();
- }
- win.clear();
- win.draw(sprite);
- win.display();
- }
- }
- class wall
- {
- public:
- wall()
- {
- rect_wall.setSize(sf::Vector2f(32 , 32));
- rect_wall.setFillColor(sf::Color::Blue);
- }
- public:
- sf::RectangleShape rect_wall;
- };
- class bunga : public sf::Drawable
- {
- public:
- bunga(string file,int size_tile,int size_map,int array_texture[6][10])
- {
- if(!texture.loadFromFile(file))
- return;
- size_of_tile = size_tile;
- texture_vertical = (texture.getSize().y/size_tile);
- texture_horizontal = (texture.getSize().x/size_tile);
- map_size = size_map;
- //set_texture = array_texture;
- for(int i = 0; i < 6; i++)
- {
- for(int k = 0; k < 10; k++)
- {
- ract.setSize(sf::Vector2f(map_size, map_size));
- ract.setTexture(&texture);
- ract.setPosition(sf::Vector2f(map_size * k, map_size * i));
- sf::Vector2i pos(size_of_tile * (array_texture[i][k] % texture_horizontal), size_of_tile * (array_texture[i][k] / texture_vertical));
- sf::Vector2i rec(size_of_tile, size_of_tile);
- ract.setTextureRect(sf::IntRect(pos , rec));
- CountTile.push_back(ract);
- }
- }
- }
- void Tilemap()
- {
- }
- private:
- virtual void draw(sf::RenderTarget &target, sf::RenderStates state)const
- {
- for(int a = 0; a < CountTile.size(); a++)
- {
- target.draw(CountTile.at(a),state);
- }
- }
- private:
- vector<sf::RectangleShape> CountTile;
- sf::RectangleShape ract;
- sf::Texture texture;
- int size_of_tile;
- int texture_vertical;
- int texture_horizontal;
- int set_texture[6][10];
- int map_size;
- };
- class Human : public sf::Drawable, public sf::Transformable
- {
- public:
- Human()
- {
- if(!t_player.loadFromFile("spritewalking.png."))
- return;
- sp.setTexture(&t_player);
- sp.setSize(sf::Vector2f(56 , 56));
- }
- private:
- virtual void draw(sf::RenderTarget &target, sf::RenderStates state)const
- {
- state.transform *= getTransform();
- target.draw(sp, state);
- //target.draw(missle_ract, state);
- }
- public:
- sf::Texture t_player;
- sf::RectangleShape sp;
- };
- class missle
- {
- public:
- missle()
- {
- //missle_ract = new sf::RectangleShape;
- missle_ract.setSize(sf::Vector2f(10,5));
- missle_ract.setFillColor(sf::Color::Red);
- direction = 0;
- //Realod_mis = new vector<sf::RectangleShape>();
- }
- void Move()
- {
- if(direction == 1)
- missle_ract.move(0 , -0.5f);
- if(direction == 2)
- missle_ract.move(0.5f , 0);
- if(direction == 3)
- missle_ract.move(0 , 0.5f);
- if(direction == 4)
- missle_ract.move(-0.5f , 0);
- //Realod_mis->push_back(*missle_ract);
- }
- public:
- sf::RectangleShape missle_ract;
- int direction;
- //vector<sf::RectangleShape> *Realod_mis;
- };
- void CreateWall()
- {
- }
- void MyTile()
- {
- sf::RenderWindow win(sf::VideoMode(640,384),"Tilemap");
- bool check_top = true;
- bool check_down = true;
- bool check_right = true;
- bool check_left = true;
- int move_sp = 56;
- int walking_sp = 0;
- int walking_speed = 0;
- float waktu = 0;
- int value;
- int map[6][10] =
- {
- { 27 , 27 , 27 , 24 , 27 , 27 , 27 , 24 , 27 , 27},
- { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
- { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
- { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
- { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0},
- { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0}
- };
- bunga b("gambar.png",32,64,map);
- b.Tilemap();
- Human player;
- player.setPosition((win.getSize().x/2) + (player.t_player.getSize().x/2),win.getSize().y/2 + (player.t_player.getSize().y/2));
- player.sp.setTextureRect(sf::IntRect(move_sp * 0, move_sp * 3, move_sp, move_sp));
- vector<missle>::const_iterator iter;
- vector<missle> missle_array;
- missle missle_player;
- vector<wall>::const_iterator iter2;
- vector<wall> wall_array;
- wall defense_wall;
- for(int i = 0; i < 10; i++)
- {
- wall_array.push_back(defense_wall);
- }
- sf::Clock clock;
- sf::Time time1;
- while(win.isOpen())
- {
- sf::Event event;
- while(win.pollEvent(event))
- {
- if(event.type == sf::Event::Closed)
- win.close();
- }
- value = 0;
- for(iter2 = wall_array.begin(); iter2 != wall_array.end(); iter2++)
- {
- if(player.sp.getGlobalBounds().intersects(wall_array[value].rect_wall.getGlobalBounds()))
- {
- cout<<"Collision"<<endl;
- }
- value++;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- {
- if(check_top == true)
- {
- player.move(sf::Vector2f(0 , -0.2f));
- player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 3, move_sp, move_sp));
- missle_player.direction = 1;
- check_down = true;
- check_top = true;
- check_left = true;
- check_right = true;
- }
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- if(check_right == true)
- {
- player.move(sf::Vector2f(0.2f , 0));
- player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 2, move_sp, move_sp));
- missle_player.direction = 2;
- check_down = true;
- check_top = true;
- check_left = true;
- check_right = true;
- }
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- {
- if(check_down == true)
- {
- player.move(sf::Vector2f(0 , 0.2f));
- player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 0, move_sp, move_sp));
- missle_player.direction = 3;
- check_down = true;
- check_top = true;
- check_left = true;
- check_right = true;
- }
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- if(check_left == true)
- {
- player.move(sf::Vector2f(-0.2f , 0));
- player.sp.setTextureRect(sf::IntRect(move_sp * walking_sp, move_sp * 1, move_sp, move_sp));
- missle_player.direction = 4;
- check_down = true;
- check_top = true;
- check_left = true;
- check_right = true;
- }
- }
- walking_speed++;
- if(walking_speed == 5)
- {
- if(walking_sp == 3)
- {
- walking_sp = 0;
- }
- walking_sp++;
- walking_speed = 0;
- }
- waktu = clock.getElapsedTime().asMilliseconds();
- if(waktu >= 300)
- {
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
- {
- missle_player.missle_ract.setPosition(sf::Vector2f(player.getPosition().x + player.sp.getSize().x/2 , player.getPosition().y + player.sp.getSize().y/2));
- missle_array.push_back(missle_player);
- }
- clock.restart();
- }
- win.clear();
- win.draw(b);
- //======================wall=========================
- for(int i = 0 ; i < wall_array.size(); i++)
- {
- wall_array[i].rect_wall.setPosition(sf::Vector2f(32 * i, 32));
- win.draw(wall_array[i].rect_wall);
- }
- value = 0;
- for(iter = missle_array.begin(); iter != missle_array.end(); iter++)
- {
- missle_array[value].Move();
- win.draw(missle_array[value].missle_ract);
- value++;
- // missle_player.Realod_mis->erase(missle_player.Realod_mis->begin() + i);
- }
- win.draw(player);
- win.display();
- }
- return;
- }
- int main()
- {
- //Anime();
- MyTile();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement