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
|
int SNAKE_size = 1;
sf::Vector2f SNAKE_SIZE{15.f, 15.f};
struct snake_body
{
int x;
int y;
sf::RectangleShape body{SNAKE_SIZE};
//body.setFillColor(sf::Color::Green);
};
class Snake{
public:
int x = 0;
int y = 0;
int end_x;
int end_y;
sf::RectangleShape snek_head{SNAKE_SIZE};
std::vector<snake_body> snake_bod;
void update_pos(){
int cur_x = snek_head.getPosition().x;
int cur_y = snek_head.getPosition().y;
int body_x;
int body_y;
for (int i = 0; i < snake_bod.size(); i++)
{
body_x = snake_bod[i].x;
body_y = snake_bod[i].y;
snake_bod[i].y = cur_y;
snake_bod[i].x = cur_x;
snake_bod[i].body.setPosition(cur_x,cur_y);
cur_x = body_x;
cur_y = body_y;
}
}
void draw(sf::RenderWindow& win){
for (int i = 0; i < snake_bod.size(); i++)
{
win.draw(snake_bod[i].body);
}
win.draw(snek_head);
}
void move(sf::Vector2f dir){
update_pos();
snek_head.move(dir.x,dir.y);
}
void add_body(){
SNAKE_size = snake_bod.size();
end_x = snake_bod.back().x;
end_y = snake_bod.back().y;
snake_bod.push_back(snake_body());
snake_bod.back().x = snek_head.getPosition().x;
snake_bod.back().y = snek_head.getPosition().y;
for (int i = 0; i < snake_bod.size(); i++){
snake_bod[i].body.setFillColor(sf::Color::Green);
}
snek_head.setFillColor(sf::Color::Yellow);
update_pos();
}
};
||||||||||||||||||||||main.cpp|||||||||||||||||||||||
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "snake.cpp"
#include <iostream>
#include <vector>
sf::Vector2f direction;// (0,1):Up (0,-1):Down (-1,0):Left (1,0):Right
float STEP = 15.f;
int main(){
sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
//window.setFramerateLimit(60);
sf::View view(sf::FloatRect(0, 0, window.getSize().y, window.getSize().x));
float dt = 0;
float dtMult;
float Time;
float Time1;
float move_t = 0.1;
bool move_b = true;
sf::Clock clock;
Snake snake;
snake.snake_bod.push_back(snake_body());
snake.add_body();
snake.add_body();
snake.add_body();
int i = 0;
while (window.isOpen())
{
float currentTime = clock.restart().asSeconds();
float fps = 1.f / currentTime;
Time += currentTime;
Time1 += currentTime;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::Resized) view.setSize(window.getSize().x , window.getSize().y);
else if (1 == sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) direction = sf::Vector2f{STEP,0};
else if (1 == sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) direction = sf::Vector2f{-STEP,0};
else if (1 == sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) direction = sf::Vector2f{0,-STEP};
else if (1 == sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) direction = sf::Vector2f{0,STEP};
else if (1 == sf::Keyboard::isKeyPressed(sf::Keyboard::H)) STEP = 6;
}
if ((Time1 <= move_t and move_b == true )){
snake.move(direction);
std::cout << "here"<<std::endl;
move_b = false;
}
else if ((Time1 >= move_t)) {
move_b = true;
Time1 = 0;
}
window.clear();
window.setView(view);
snake.draw(window);
window.display();
}
return 0;
}
|