I made using SFML my first game project. It's a snake game, and I used 2 custom classes:
- snake
- fruit (snake grow by 1 when eat it)
Here's main.cpp code:
#include <SFML/Graphics.hpp>
#include "snake.h"
#include "fruit.h"
int main()
{
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
snake snake;
fruit fruit;
snake.fruitptr = &fruit;
snake.window = &app;
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
app.close();
}
snake.movement();
snake.checkCollisions();
app.clear();
app.draw(fruit);
app.draw(snake);
app.display();
}
return EXIT_SUCCESS;
}
Fruit and Snake header
#ifndef SNAKE_H
#define SNAKE_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "fruit.h"
class snake : public sf::Drawable
{
public:
snake();
virtual ~snake();
void movement();
void addElement();
void checkCollisions();
fruit* fruitptr;
virtual void draw(sf::RenderTarget& target,
sf::RenderStates states) const;
sf::RectangleShape oldPosition;
sf::RenderWindow* window;
private:
float a, b;
float x, y;
int length;
sf::Time time;
sf::RectangleShape shape;
sf::RectangleShape* snakeBody;
sf::RectangleShape* snakeBodycopy;
};
#endif // SNAKE_H
#ifndef FRUIT_H
#define FRUIT_H
#include <SFML/Graphics.hpp>
#include <time.h>
class fruit : public sf::Drawable
{
public:
fruit();
virtual ~fruit();
sf::RectangleShape shape;
virtual void draw(sf::RenderTarget& target,
sf::RenderStates states) const;
private:
};
#endif // FRUIT_H
And finally the .cpp files:
#include "snake.h"
snake::snake()
{
x = 20.f;
y = 20.f;
length = 0;
time = sf::seconds(0.1f);
shape.setFillColor(sf::Color::Red);
shape.setSize( {20.f,20.f});
shape.setPosition( {20.f,20.f});
}
snake::~snake()
{
//dtor
}
void snake::draw(sf::RenderTarget& target,
sf::RenderStates states) const
{
target.draw(this->shape, states);
if(this->length == 1){
this->snakeBody[0].setPosition(this->oldPosition.getPosition());
target.draw(this->snakeBody[0] ,states);
}
else if(this->length>1){
for(int i=this->length-1;i>0;i--){
this->snakeBody[i].setPosition(this->snakeBody[i-1].getPosition());
this->snakeBody[i].setFillColor(sf::Color::Red);
this->snakeBody[i].setSize({20.f ,20.f});
target.draw(this->snakeBody[i], states);
}
this->snakeBody[0].setFillColor(sf::Color::Red);
this->snakeBody[0].setSize({20.f , 20.f});
this->snakeBody[0].setPosition(this->oldPosition.getPosition());
target.draw(this->snakeBody[0], states);
}
}
void snake::movement()
{
this->oldPosition.setPosition(this->shape.getPosition());
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
this->a=0;
this->b=-20.f;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
this->a=-20.f;
this->b=0;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
this->a=0;
this->b=20.f;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
this->a=20.f;
this->b=0;
}
this->shape.move(a,b);
sf::sleep(this->time);
}
void snake::checkCollisions()
{
for(int i=0; i<this->length; i++)
{
if(this->shape.getGlobalBounds().intersects(this->snakeBody[i].getGlobalBounds())){
this->window->close();
std::cout<<"You Lose!"<<std::endl;
}
}
if(this->shape.getGlobalBounds().intersects(this->fruitptr->shape.getGlobalBounds()))
{
this->fruitptr->shape.setPosition( (rand()%40)*20, (rand()%30)*20);
this->addElement();
std::cout<<"+1"<<std::endl;
}
}
void snake::addElement()
{
if(this->length == 0){
this->length++;
snakeBody = new sf::RectangleShape [this->length];
snakeBody[0].setFillColor(sf::Color::Red);
snakeBody[0].setSize({20.f, 20.f});
}
else{
this->snakeBodycopy = new sf::RectangleShape [this->length];
for(int i=0;i<this->length;i++){
this->snakeBodycopy[i].setPosition(this->snakeBody[i].getPosition());
}
this->length++;
delete [] snakeBody;
this->snakeBody = new sf::RectangleShape [this->length];
for(int i=0;i<this->length-1;i++){
this->snakeBody[i].setPosition(this->snakeBodycopy[i].getPosition());
}
delete [] snakeBodycopy;
}
}
#include "fruit.h"
fruit::fruit()
{
shape.setFillColor(sf::Color::White);
shape.setSize( {20.f,20.f});
shape.setPosition( (rand()%40)*20, (rand()%30)*20);
}
fruit::~fruit()
{
//dtor
}
void fruit::draw(sf::RenderTarget& target,
sf::RenderStates states) const{
target.draw(this->shape, states);
}