Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include "bois.h"
- int main(){
- sf::RenderWindow window(sf::VideoMode(256, 256), "Super tuiles !", sf::Style::Titlebar | sf::Style::Close);
- int tuiles[] =
- {
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 2, 2, 2, 0, 1, 1, 1,
- 0, 2, 3, 2, 1, 1, 0, 0,
- 0, 2, 2, 2, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 1, 0, 0,
- 0, 1, 1, 1, 1, 1, 0, 0,
- 0, 1, 0, 0, 0, 0, 0, 0,
- 0, 1, 0, 0, 0, 0, 0, 0
- };
- sf::Texture textureGeorge;
- if(!textureGeorge.loadFromFile("george.png")){
- std::cout << "Erreur de chargement de la texture de George" << std::endl;
- return -1;
- }
- //George est le personnage
- sf::Sprite george;
- george.setTexture(textureGeorge);
- george.setTextureRect(sf::IntRect(0, 0, 32, 32));
- int pasGeorge = 1;
- Bois bois;
- //Bois est le terrain sur lequel se balade George
- if(!bois.load("bois.png", sf::Vector2u(32, 32), tuiles, 8, 8)){
- std::cout << "Erreur de chargement de bois" << std::endl;
- return -1;
- }
- //Une horloge pour que George soit un peu plus lent
- sf::Clock clock;
- while(window.isOpen()){
- sf::Event event;
- while(window.pollEvent(event)){
- if(event.type == sf::Event::Closed){
- window.close();
- }
- }
- if(clock.getElapsedTime().asMilliseconds() > 5){
- //George vas en haut
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
- if(george.getPosition().y - pasGeorge >= 0){
- george.move(sf::Vector2f(0, -pasGeorge));
- george.setTextureRect(sf::IntRect(64, 0, 32, 32));
- }
- }
- //George vas en bas
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
- if(george.getPosition().y + pasGeorge <= 224){
- george.move(sf::Vector2f(0, pasGeorge));
- george.setTextureRect(sf::IntRect(0, 0, 32, 32));
- }
- }
- //George vas à gauche
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
- if(george.getPosition().x - pasGeorge >= 0){
- george.move(sf::Vector2f(-pasGeorge, 0));
- george.setTextureRect(sf::IntRect(32, 0, 32, 32));
- }
- }
- //George vas à droite
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
- if(george.getPosition().x + pasGeorge <= 224){
- george.move(sf::Vector2f(pasGeorge, 0));
- george.setTextureRect(sf::IntRect(96, 0, 32, 32));
- }
- }
- clock.restart();
- }
- window.clear();
- window.draw(bois);
- window.draw(george);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement