Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <vector>
- #include <iostream>
- #include <ctime>
- using namespace sf;
- using std::vector;
- class Player
- {
- private: float x, y;
- public: float speed;
- int dir;
- Player()
- {
- Snk.push_back(Snake(152, 152));
- Snk.push_back(Snake(1, 4));
- Snk.push_back(Snake(5, 3));
- }
- struct Snake
- {
- RectangleShape SnakeBody;
- int x;
- int y;
- Snake(float x_, float y_) :
- x(x_),
- y(y_)
- {
- SnakeBody.setSize(Vector2f(10, 10));
- SnakeBody.setFillColor(Color::Red);
- SnakeBody.setPosition(100, 100);
- }
- };
- vector <Snake> Snk;
- void Update(float time)
- {
- speed = 50;
- move();
- switch (dir)
- {
- case 1: x += speed * time;
- break;
- case 2: x -= speed * time;
- break;
- case 3: y += speed * time;
- break;
- case 4: y -= speed * time;
- }
- }
- void move()
- {
- if (Keyboard::isKeyPressed(Keyboard::D)) {
- dir = 1;
- }
- if (Keyboard::isKeyPressed(Keyboard::A)) {
- dir = 2;
- }
- if (Keyboard::isKeyPressed(Keyboard::S)) {
- dir = 3;
- }
- if (Keyboard::isKeyPressed(Keyboard::W)) {
- dir = 4;
- }
- }
- void Snake_move(RenderWindow & window)
- {
- for (int i = Snk.size() - 1; i > 0; i --) {
- Snk[i].x = Snk[i - 1].x;
- Snk[i].y = Snk[i - 1].y;
- }
- }
- };
- int main()
- {
- const int WINDOW_WIDTH = 400;
- const int WINDOW_HEIGHT = 400;
- RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), L"Змейка");
- srand(time(NULL));
- Clock clock;
- Event event;
- Player player;
- float x, y;
- float dir;
- while (window.isOpen()) {
- float time = clock.getElapsedTime().asMilliseconds() / 1000.f;
- clock.restart();
- while (window.pollEvent(event)) {
- if (event.type == Event::Closed) {
- window.close();
- }
- }
- window.clear();
- player.Snake_move(window);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement