Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - lukap295

Pages: [1]
1
Graphics / Can't get snake to grow
« on: January 13, 2024, 11:35:59 am »
This question was probably asked here a 1000 times, but I don't really know how would I implement it. I made a circleShape head and a vector of circleShape for a body. I don't understand how would I position them behind the head or how could they follow the head.
#ifndef SNAKE_HPP
#define SNAKE_HPP
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
using namespace std;

class Snake : public sf::Drawable {
public:
        sf::CircleShape head;
        vector<sf::CircleShape> body;
        sf::Vector2f pos;
        sf::Vector2f bodyPos;
        Snake();
        ~Snake();
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
        void drawSnake();
        void move();
        void grow();
};


#endif
#include "Snake.hpp"
Snake::Snake() {
        pos = sf::Vector2f(0, 0);
}

Snake::~Snake() {

}

void Snake::drawSnake() {
        head.setRadius(12);
        head.setFillColor(sf::Color::Black);
        head.setPosition(400, 300);
}

void Snake::draw(sf::RenderTarget& target, sf::RenderStates states) const {
        target.draw(head);
       
}

void Snake::move() {
        head.move(pos);
   
}

void Snake::grow() {
    sf::CircleShape part;
}
If anyone has some advice I would appreciate it. This is my first time using SFML.

2
Window / Re: Using switch case to change states, but its not working
« on: January 12, 2024, 01:22:21 pm »
So i added the breaks, but now my snake wont move.

3
Window / Using switch case to change states, but its not working
« on: January 12, 2024, 12:19:07 pm »
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp>
#include "MainMenu.hpp"
#include "Game.hpp"
#include "Snake.hpp"
#include "Food.hpp"
#include "GameOver.hpp"
#include <ctime>

enum class GameState {
    MainMenu,
    Game,
    GameOver
};

int main() {
    srand(time(NULL));
    sf::RenderWindow window(sf::VideoMode(800, 600), "Zmija");
    sf::Texture texture;
    sf::Font font;
    sf::Text text;

    texture.loadFromFile("assets/grass.png");
    font.loadFromFile("assets/Salsa-Regular.ttf");
    MainMenu menu(font, "Zmija");
    GameOver over(font, "Game Over?");
    Game game;
    Snake snake;
    Food food;
    snake.drawSnake();
    game.textures();
    menu.startSetColor(sf::Color::Yellow);
    GameState state = GameState::MainMenu;
    texture.setRepeated(true);
    game.grass.setTexture(texture);
    game.grass.setTextureRect(sf::IntRect(0, 0, 800, 600));
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        switch (state) {
        case GameState::MainMenu:
        {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
                menu.startSetColor(sf::Color::Yellow);
                menu.exitSetColor(sf::Color::White);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
                    state = GameState::Game;
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                menu.startSetColor(sf::Color::White);
                menu.exitSetColor(sf::Color::Yellow);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
                    window.close();
            }
        }
        case GameState::Game:
        {
           
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
                snake.pos = sf::Vector2f(0, -0.2);
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                snake.pos = sf::Vector2f(0, 0.2);
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
                snake.pos = sf::Vector2f(-0.2, 0);
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
                snake.pos = sf::Vector2f(0.2, 0);
            }
            //udaljenost izmedu zmije i hrane
            int dist2p = sqrt(pow((snake.head.getPosition().x - food.food.getPosition().x), 2) + pow((snake.head.getPosition().y - food.food.getPosition().y), 2));
            if (snake.head.getRadius() + food.food.getRadius() >= dist2p) {
                food.food.setPosition(rand() % 770, rand() % 570);
            }
            //Sudar sa zidom
            if (snake.head.getPosition().x < 20.f)
                state = GameState::GameOver;
            if (snake.head.getPosition().y < 20.f)
                state = GameState::GameOver;
            if (snake.head.getPosition().x > 755)
                state = GameState::GameOver;
            if (snake.head.getPosition().y > 555.f)
                state = GameState::GameOver;
        }
        case GameState::GameOver:
        {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
                over.retrySetColor(sf::Color::Yellow);
                over.exitSetColor(sf::Color::White);
                snake.head.setPosition(400, 300);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
                    state = GameState::Game;
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                over.retrySetColor(sf::Color::White);
                over.exitSetColor(sf::Color::Yellow);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
                    window.close();
            }
            snake.move();
            window.clear();
        }
       
    }
        switch (state) {
        case GameState::MainMenu:
            window.draw(menu);
            break;
        case GameState::Game:
            window.draw(game);
            window.draw(food);
            window.draw(snake);
            break;
        case GameState::GameOver:
            window.draw(over);
            break;
        }
        window.display();
    }
    return 0;
}

So my problem is, every time I press Up it returns my snake to the original position, but I don't understand why because I am in a state of Game, and not in a state of GameOver. I am new to SFML.

Pages: [1]
anything