W dniu czwartek, 6 lutego 2020 23:22:42 UTC+1 użytkownik fir napisał:
> W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
> >
> > Here you are! Compiles fine
>
> oj sorry i didnt noticed that.. if
> so i will try it toomorrow
>
ok, after abother (thoug smaller dose of frustration and crashes i managed to obtain something that compiles and not crashes)
i may post it, i think
the source of this code is somewhat 'winding'
some girl coder i know took some code aof snake probably from some youtube tutorial, cutted it downe transfered in into something weird (redundant etc), then i take it to simplify it, then i take it to turn it to 3 'classes' instead of 2, but i not repaired all names so some names gere would need to be changed
(like yts Mover it should be snake, attached_window should be renamed etc)
//main.cpp
#include "mover.h"
#include "food.h"
#include "window.h"
GameWindow window;
int main()
{
window.start();
return 0;
}
#ifndef FOOD_H
#define FOOD_H
#include <SFML/Graphics.hpp>
using namespace sf;
class GameWindow;
class Mover;
class Food
{
public:
Food( GameWindow& window);
void init();
void move2RandomPosition();
void draw();
Vector2f getPosition();
private:
Vector2u size = Vector2u(20, 20);
Vector2u position = Vector2u(0, 0);
GameWindow& attached_window;
};
#endif // FOOD_H
#ifndef MOVER_H
#define MOVER_H
#include <SFML/Graphics.hpp>
using namespace sf;
class Food;
class GameWindow;
class Mover
{
public:
Mover(GameWindow& window, Food &f);
void draw();
void setDirection(int x, int y);
void checkAndResolveEdgesCollision();
void checkAndResolveFoodColision();
void checkAndResolveSelfCollision();
void update();
private:
float x, y;
float dir_x, dir_y;
float scale;
int feedSize;
std::vector<Vector2f> segments_positions;
Food& food;
GameWindow& attached_window;
};
#endif // MOVER_H
#ifndef WINDOW_H
#define WINDOW_H
#include <SFML/Graphics.hpp>
using namespace sf;
class Mover;
class GameWindow
{
public:
GameWindow();
void start();
RenderWindow window;
private:
Food food;
Mover snake;
};
#endif // WINDOW_H
//food.cpp
#include "food.h"
#include "mover.h"
#include "window.h"
#include <ctime>
#include <iostream>
using namespace sf;
using namespace std;
Food::Food(GameWindow& w): attached_window(w)
{
}
void Food::init()
{
srand(time(nullptr));
this->move2RandomPosition();
}
void Food::draw()
{
RectangleShape rec;
rec.setSize(Vector2f(size));
rec.setPosition(Vector2f(position));
rec.setOutlineColor(sf::Color::Green);
rec.setOutlineThickness(-3);
attached_window.window.draw(rec);
}
void Food::move2RandomPosition()
{
Vector2u s = attached_window.window.getSize();
position = { size.x*( rand()%(s.x/size.x) ),
size.y*( rand()%(s.y/size.y) ) };
}
Vector2f Food::getPosition()
{
return Vector2f(position);
}
//mover.cpp
#include "mover.h"
#include <iostream>
#include <algorithm>
#include "food.h"
#include "window.h"
///////////////////////////
void Mover::draw()
{
sf::RectangleShape rec;
rec.setSize(sf::Vector2f(20,20));
rec.setPosition(sf::Vector2f(x,y));
rec.setFillColor(sf::Color::Yellow);
attached_window.window.draw(rec);
attached_window.window.draw(rec);
rec.setFillColor(sf::Color::Red);
for (int i=1;i<segments_positions.size(); ++i) // cant start from 0 cause then it become part of the tail immidiatly
{
rec.setPosition(segments_positions[i]);
attached_window.window.draw(rec);
}
}
/////////////////////////////////////////////
Mover::Mover(GameWindow& w, Food& food_): attached_window(w), food(food_)
{
scale = 20;
x = 0;
y = 0;
dir_x = 1;
dir_y = 0;
segments_positions.push_back(Vector2f(x,y));
feedSize = 2;
}
void Mover::checkAndResolveEdgesCollision()
{
Vector2u s = attached_window.window.getSize();
if(x<0) x=0;
if(x>s.x-20) x-=20;
if(y<0) y=0;
if(y>s.y-20) y-=20;
}
void Mover::setDirection(int vx, int vy)
{
dir_x = vx;
dir_y = vy;
}
void Mover::checkAndResolveFoodColision()
{
if(Vector2f(x,y) == food.getPosition())
{
feedSize+=2; // make the tail longer
food.move2RandomPosition();
}
}
void Mover::checkAndResolveSelfCollision()
{
for (int i = 0; i < segments_positions.size(); ++i)
for (int j = i+1; j < segments_positions.size(); ++j)
{
if(segments_positions[i] == segments_positions[j])
{
feedSize=2;
segments_positions.resize(2);
}
}
}
void Mover::update()
{
x = x + dir_x * scale; // move by scale
y = y + dir_y * scale;
checkAndResolveEdgesCollision();
segments_positions.push_back(Vector2f(x, y));
if(segments_positions.size() > feedSize )
segments_positions.erase(segments_positions.begin());
checkAndResolveFoodColision();
checkAndResolveSelfCollision();
}
//window.cpp
#include "SFML/Graphics.hpp"
#include <iostream>
#include "mover.h"
#include "food.h"
#include "window.h"
#include <ctime>
#include <vector>
GameWindow::GameWindow(): food(*this), snake(*this, food)
{
window.create(sf::VideoMode(720, 560), "ziemo snejk");
window.setFramerateLimit(10);
}
void GameWindow::start()
{
food.init();
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed: window.close(); break;
case sf::Event::EventType::KeyPressed:
if(sf::Keyboard::Key::Up == event.key.code) snake.setDirection(0,-1);
if(sf::Keyboard::Key::Down == event.key.code) snake.setDirection(0,1);
if(sf::Keyboard::Key::Left == event.key.code) snake.setDirection(-1,0);
if(sf::Keyboard::Key::Right == event.key.code) snake.setDirection(1,0);
if(sf::Keyboard::Key::Escape == event.key.code) window.close();
break;
}
}
snake.update();
snake.draw();
food.draw();
window.display();
window.clear();
}
}
one could take project for windows with runtime needed here
http://minddetonator.htw.pl/sfml_wonsz3.zip
only mingw is needed to be at c:\mingw afair (i used mingw 5.1 and sdml 2.5.1)
i used bat for compilation
SET PATH=c:\mingw\bin;
g++ -s -std=c++11 -c food.cpp -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2
g++ -s -std=c++11 -c mover.cpp -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2
g++ -s -std=c++11 -c window.cpp -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2
g++ -s -std=c++11 main.cpp -o "game" window.o food.o mover.o -Wl,-subsystem,windows -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2
pause
would you change somethinh except those some names i would also change but am to tired?