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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#pragma once
#include <SFML\Graphics.hpp>
#include <vector>
class Snake
{
public: //constructor + run function
Snake();
void run();
private: //Window
sf::RenderWindow mWindow;
sf::RectangleShape dHead;
sf::RectangleShape dBody;
std::vector<sf::RectangleShape> dSnake;
private:
void processEvents();
void update();
void render();
void handlePlayerInput(sf::Keyboard::Key,bool pressed);
void updateMovement();
void setMovementBooleansFalse();
bool mIsMovingUp,mIsMovingDown,mIsMovingLeft,mIsMovingRight;
void switchSides();
sf::Time waitTime;
void addTailPiece();
void moveTail();
};
Snake::Snake() : mWindow(sf::VideoMode(640, 480), "Snake 1.0")
{
{//head
dHead.setFillColor(sf::Color::Black);
dHead.setSize(sf::Vector2f(20.f,20.f));
dHead.setPosition((mWindow.getSize().x/2),(mWindow.getSize().y/2));
}
{//tail
dBody = dHead;
dBody.setFillColor(sf::Color::Blue);
}
{//snake
dSnake.push_back(dHead);
}
{//food
mFood = dHead;
mFood.setFillColor(sf::Color::Red);
placeFood();
}
{//time
waitTime = sf::milliseconds(140);
}
}
void Snake::run()
{
while(mWindow.isOpen())
{
processEvents();
update();
render();
}
}
void Snake::processEvents()
{
sf::Event sEvent;
while(mWindow.pollEvent(sEvent))
{
switch (sEvent.type)
{
case sf::Event::KeyPressed:
handlePlayerInput(sEvent.key.code,true);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Snake::update()
{
sf::Clock clock;
clock.restart();
while(clock.getElapsedTime() <= waitTime)
{
processEvents();
}
updateMovement();
addTailPiece();
}
void Snake::render()
{
mWindow.clear(sf::Color::White);
for(int i = 0; i < dSnake.size(); i++)
{
mWindow.draw(dSnake.at(i));
}
//mWindow.draw(mFood);
mWindow.display();
}
void Snake::handlePlayerInput(sf::Keyboard::Key key,bool isPressed)
{
setMovementBooleansFalse();
if (key == sf::Keyboard::Up)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::Down)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::Left)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::Right)
mIsMovingRight = isPressed;
}
void Snake::updateMovement()
{
moveTail();
sf::Vector2f movement(0.f,0.f);
if(mIsMovingUp)
movement.y -= dHead.getSize().x;
if(mIsMovingDown)
movement.y += dHead.getSize().x;
if(mIsMovingLeft)
movement.x -= dHead.getSize().x;
if(mIsMovingRight)
movement.x += dHead.getSize().x;
dSnake.at(0).move(movement);
switchSides();
}
void Snake::setMovementBooleansFalse()
{
mIsMovingUp = false;
mIsMovingDown = false;
mIsMovingLeft = false;
mIsMovingRight = false;
}
void Snake::switchSides()
{
sf::Vector2u j=mWindow.getSize();
unsigned int a = j.x;
unsigned int b = j.y;
sf::Vector2f i=dSnake.at(0).getPosition();
float x = i.x;
float y = i.y;
if(x <= -20.0)
{
dSnake.at(0).setPosition(a,y);
}
else if(x >= a)
{
dSnake.at(0).setPosition(0,y);
}
if(y <= -20.0)
{
dSnake.at(0).setPosition(x,b);
}
else if(y >= b)
{
dSnake.at(0).setPosition(x,0);
}
}
void Snake::addTailPiece()
{
dSnake.push_back(dBody);
}
void Snake::moveTail()
{
for(int i = dSnake.size(); i > 1; i--)
{
dSnake.at(i--).setPosition(dSnake.at(i-=2).getPosition());
}
}
|