Bonjour, je viens de finir un petit rpg et hier je me suis dit pourquoi pas créé un jeu de type snake ? Donc j'ai rush jusqu’à 2h du matt et donc je me retrouver avec un problème d'affichage ou de positionnement x)
Le code est au stade brouillon donc n'en men voulait pas si c'est dégueulasse ( surtout au niveau de l'affichage je l'avoue x) )
Voici le code :
main.cpp
header.hpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <iostream> #include "header.hpp" int main() { bool inApp{true}; sf::RenderWindow window; window.create( sf::VideoMode(400,400), "Snake", sf::Style::Default ); Snake snake(10); sf::Clock clk; unsigned long long int snakeClock{0}; while(inApp)//useless { while(window.isOpen()) { sf::Event event; while(window.pollEvent(event)) { if( event.type == event.Closed ) window.close(); if( event.type == event.KeyPressed ) // switch { if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) ) snake.setDirection( Direction::up ); else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) ) snake.setDirection( Direction::down ); else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) ) snake.setDirection( Direction::left ); else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) ) snake.setDirection( Direction::right ); } } window.clear( sf::Color::White ); window.draw(snake); window.display(); while( clk.getElapsedTime().asMicroseconds() < 100000 ) //moche { } clk.restart(); snakeClock++; if( snakeClock > 10 ) { snake.move(); snakeClock = 0; } } inApp = false; } return 0; }
Point.hpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef HEADER_INCLUDED #define HEADER_INCLUDED #include <SFML/graphics.hpp> #include <iostream> enum Direction{ up, down, left, right }; enum BoxTool{ nothing, global, h_paral, v_paral }; enum BoxWall{ wtop, wbottom, wleft, wright, wglobal }; class Box { public: Box( sf::Vector2f positionA, sf::Vector2f positionB ); void modificate( float value, BoxWall wall, BoxTool param = BoxTool::nothing ); private: sf::Vector2f _position_a; sf::Vector2f _position_b; }; class Entity { }; class Item : public Entity { }; class Point { public: Point( sf::Vector2f position, sf::Vector2f size ); void move( sf::Vector2f const &position ); void setPosition( sf::Vector2f const &position ); sf::Vector2f position() const; sf::Vector2f size() const; private: sf::RectangleShape _bodyPart; }; class Block { }; class Snake : public sf::Drawable, public sf::Transformable { public: Snake( unsigned int sizeOfPart ) : _size{sizeOfPart}, _direction{Direction::right} { _bodyPart.push_back( Point( sf::Vector2f(100,100), sf::Vector2f(_size,_size) ) ); _bodyPart.push_back( Point( sf::Vector2f(_size+100,100), sf::Vector2f(_size,_size) ) ); _bodyPart.push_back( Point( sf::Vector2f(_size*2+100,100), sf::Vector2f(_size,_size) ) ); _bodyPart.push_back( Point( sf::Vector2f(_size*3+100,100), sf::Vector2f(_size,_size) ) ); partUpdate(); } void setDirection( Direction direction ) { _direction = direction; } bool move() { for( size_t i{1}; i < _bodyPart.size(); i++ ) _bodyPart[i].setPosition( _bodyPart[i - 1].position() );///bug ? switch( _direction ) { case Direction::up : _bodyPart[0].move( sf::Vector2f( 0, -_size ) ); break; case Direction::down : _bodyPart[0].move( sf::Vector2f( 0, _size ) ); break; case Direction::left : _bodyPart[0].move( sf::Vector2f( -_size, 0 ) ); break; case Direction::right : _bodyPart[0].move( sf::Vector2f( _size, 0 ) ); break; default : break; } /// debug /// for( size_t i{0}; i < _bodyPart.size(); i++ ) std::cout << " pos : " << _bodyPart[i].position().x << ":" << _bodyPart[i].position().y << std::endl; partUpdate(); } private: void partUpdate() { _vertices.clear(); for( size_t i{0}; i < _bodyPart.size(); i++ ) /// bug ? { _vertices.push_back(sf::Vertex(sf::Vector2f(_bodyPart[i].position().x, _bodyPart[i].position().y), sf::Color::Green)); _vertices.push_back(sf::Vertex(sf::Vector2f(_bodyPart[i].position().x + _bodyPart[i].size().x, _bodyPart[i].position().y), sf::Color::Green)); _vertices.push_back(sf::Vertex(sf::Vector2f(_bodyPart[i].position().x + _bodyPart[i].size().x,_bodyPart[i].position().y + _bodyPart[i].size().y), sf::Color::Green)); _vertices.push_back(sf::Vertex(sf::Vector2f(_bodyPart[i].position().x, _bodyPart[i].position().y + _bodyPart[i].size().y), sf::Color::Green)); } } virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const { states.transform *= getTransform(); states.texture = NULL; target.draw(&_vertices[0], _vertices.size(), sf::Quads ); } Direction _direction; unsigned int const _size; std::vector<Point> _bodyPart; std::vector<sf::Vertex> _vertices; }; #endif // HEADER_INCLUDED
Mon serpent bouge presque correctement... mais juste la tête ! pas le reste du corps
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "header.hpp" Point::Point( sf::Vector2f position, sf::Vector2f size ) { _bodyPart.setFillColor(sf::Color::Red); _bodyPart.setPosition( position ); _bodyPart.setSize(size); _bodyPart.setOutlineThickness(1); _bodyPart.setOutlineColor(sf::Color( 255, 50, 50 ) ); } void Point::move( sf::Vector2f const &position ) { _bodyPart.move(position); } void Point::setPosition( sf::Vector2f const &position ) { _bodyPart.setPosition( position ); } sf::Vector2f Point::position() const { return _bodyPart.getPosition(); } sf::Vector2f Point::size() const { return _bodyPart.getSize(); }
Par presque j'entand que quand j'appuie sur la touche haut ou la touche gauche, j'envoie à move une valeur négative et bizarrement ça téléporte mon serpent à "-quelque chose exponentielle beaucoup". Donc es que move n'accepte pas les valeur négative ? et dans ce cas comment je fait pour aller en arriéré moi x)
Bref bien buguer ce code ^^ quelqu'un peux m'aider ?
Ps dans le move de mon serpent enfaite je téléporte chaque partie à la partie antérieur en gros :
Partie 1 va vers une nouvelle position
Partie 2 va vers la position de 1
Partie 3 va vers las position de 2
....
EDIT : Probléme résolu pour la tête qui se déplace seul, j'ai oublier d'inverser ma boucle for Donc il ne me reste qu'un probléme c'est le move qui ne fonctionne pas en valeur négative :/
Partager