1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
int main()
{
enum Direction {Down, Left, Right, Up};
Vector2i source(1,Down);
float frameCounter = 0, switchFrame = 100, frameSpeed = 500;
RenderWindow Window;
Window.create(VideoMode(800,600), "15 Puzzle");
Window.setKeyRepeatEnabled(true);
Texture pTexture;
Texture wTexture;
Sprite playerImage;
Sprite wallImage;
if(!pTexture.loadFromFile("Player.png"))//To see if the image loads
cout<<"Error could not load images"<<endl;
playerImage.setTexture(pTexture);
playerImage.setPosition(100,100);
if(!wTexture.loadFromFile("Wall.png", IntRect(0,0,32,32)))//To see if the image loads
cout<<"Error could not load image"<<endl;
wallImage.setTexture(wTexture);
while(Window.isOpen())
{
Event stuff;
while(Window.pollEvent(stuff))
{
switch(stuff.type)
{
case Event::Closed:
Window.close();
break;
case Event::TextEntered:
if(stuff.text.unicode==27)
Window.close();
break;
}
}
Window.setFramerateLimit(300);
if(Keyboard::isKeyPressed(Keyboard::Up))
{
source.y = Up;
playerImage.move(0,-1);
}
else if(Keyboard::isKeyPressed(Keyboard::Left))
{
source.y = Left;
playerImage.move(-1,0);
}
else if(Keyboard::isKeyPressed(Keyboard::Right))
{
source.y = Right;
playerImage.move(1,0);
}
else if(Keyboard::isKeyPressed(Keyboard::Down))
{
source.y = Down;
playerImage.move(0,1);
}
frameCounter += frameSpeed * myClock.restart().asSeconds();
if(frameCounter>=switchFrame)
{
source.x++;
if(source.x *32 >= pTexture.getSize().x)
source.x = 0;
}
playerImage.setTextureRect(IntRect(source.x * 32, source.y * 32, 32, 32));
Window.draw(wallImage);
Window.draw(playerImage);
Window.display();
Window.clear();
}
}
|