Snake game

Hey guys

I'm building a snake game for a project using shapes as food. I've created cpp and hpp files for the 3 shapes and have also created a controllable snake.

I'm stuck with how to make these shapes appear at random intervals and also how to input collision detection for the collection of the shapes.

Below is the snake and shapes code i've got so far.
Cheers


player_snake.cpp

#include "player_snake.hpp"


//PlayerSnake Implementation
//
PlayerSnake::PlayerSnake()
{
prg::application.addKeyListener(*this);
}

PlayerSnake::~PlayerSnake()
{
prg::application.removeKeyListener(*this);
}

bool PlayerSnake::onKey(const prg::IKeyEvent::KeyEvent& key_event)
{
if(key_event.key_state == KeyEvent::KB_DOWN) {
switch(key_event.key) {

case KeyEvent::KB_LEFT_KEY:
changeDirection(Direction::West);
break;
case KeyEvent::KB_RIGHT_KEY:
changeDirection(Direction::East);
break;
case KeyEvent::KB_UP_KEY:
changeDirection(Direction::North);
break;
case KeyEvent::KB_DOWN_KEY:
changeDirection(Direction::South);
break;
}
}
return true;
}



circle.cpp:

#include "circle.hpp"


Circle::Circle(const Vector2D& location) :
Shape(location)
{
vertices_=
{

{5, 0}, //P1
{5, 1}, //P2
{4.6, 2}, //P3
{4, 3}, //P4
{3, 4}, //P5
{2, 4.6}, //P6
{1, 5}, //P7
{0, 5}, //P8

{-1, 5}, //P9
{-2, 4.6}, //P10
{-3, 4}, //P11
{-4, 3}, //P12
{-4.6, 2}, //P13
{-5, 1}, //P14
{-5, 0}, //P15

{-5, -1}, //P16
{-4.6, -2}, //P17
{-4, -3}, //P18
{-3, -4}, //P19
{-2, -4.6}, //P20
{-1, -5}, //P21
{0, -5}, //P22

{1, -5}, //P23
{2, -4.6}, //P24
{3, -4}, //P25
{4, -3}, //P26
{4.6, -2}, //P27
{5, -1}, //P28
{5, 0} //P29

};
}


square.cpp:

#include "square.hpp"


Square::Square(const Vector2D& location) :
Shape(location)
{
vertices_=
{

{5, 5},
{5, -5},
{-5, -5},
{-5, 5},

};

}


triangle.cpp:

#include "triangle.hpp"


Triangle::Triangle(const Vector2D& location) :
Shape(location)
{
vertices_=
{

{ 0, 5.00},//P01
{5, -5},//P02
{-5, -5},//P03

};
}






player_snake.hpp

#if !defined PLAYER_SNAKE_HPP
#define PLAYER_SNAKE_HPP

#include "snake.hpp"
#include <prg_interactive.hpp>

#endif //PLAYER_SNAKE_HPP



circle.hpp:

#if !defined CIRCLE_HPP
#define CIRCLE_HPP
#include "shape.hpp"

class Circle : public Shape{
public :
Circle (const Vector2D& location);
};




#endif



square.hpp:

#if !defined SQUARE_HPP
#define SQUARE_HPP
#include "shape.hpp"

class Square : public Shape{
public :
Square (const Vector2D& location);
};




#endif



triangle.hpp:

#if !defined TRIANGLE_HPP
#define TRIANGLE_HPP
#include "shape.hpp"

class Triangle : public Shape{
public :
Triangle (const Vector2D& location);
};




#endif




Friendly advice,
People are more likely to reply if you use code blocks ([code])
It makes things easier to read.

Please read here: http://www.cplusplus.com/forum/beginner/1/
you could create (or use if you are using some other compiler) a gotoxy function.

then you can also use it to detect a collision..

btw i didn't get what you meant by

how to make these shapes appear at random intervals

Dont the shapes (food) come along when you just eat one?
Last edited on
you could create (or use if you are using some other compiler) a gotoxy function.

Did you just say that he should use goto?
I hope that's not the case because goto makes code tremendously hard to read.

btw:
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//player_snake.cpp

#include "player_snake.hpp"

PlayerSnake::PlayerSnake()
{
    prg::application.addKeyListener(*this);
}

PlayerSnake::~PlayerSnake()
{
    prg::application.removeKeyListener(*this);
}

bool PlayerSnake::onKey(const prg::IKeyEvent::KeyEvent& key_event)
{
    if(key_event.key_state == KeyEvent::KB_DOWN) {
        switch(key_event.key) {
            
            case KeyEvent::KB_LEFT_KEY:
                changeDirection(Direction::West);
                break;
            case KeyEvent::KB_RIGHT_KEY:
                changeDirection(Direction::East);
                break;
            case KeyEvent::KB_UP_KEY:
                changeDirection(Direction::North);
                break;
            case KeyEvent::KB_DOWN_KEY:
                changeDirection(Direction::South);
                break;
        }
    }
    return true;
}



// circle.cpp:
    
#include "circle.hpp"
    
Circle::Circle(const Vector2D& location) :
    Shape(location)
{
    vertices_=
    {
        {5, 0}, //P1
        {5, 1}, //P2
        {4.6, 2}, //P3
        {4, 3}, //P4
        {3, 4}, //P5
        {2, 4.6}, //P6
        {1, 5}, //P7
        {0, 5}, //P8
        
        {-1, 5}, //P9
        {-2, 4.6}, //P10
        {-3, 4}, //P11
        {-4, 3}, //P12
        {-4.6, 2}, //P13
        {-5, 1}, //P14
        {-5, 0}, //P15
        
        {-5, -1}, //P16
        {-4.6, -2}, //P17
        {-4, -3}, //P18
        {-3, -4}, //P19
        {-2, -4.6}, //P20
        {-1, -5}, //P21
        {0, -5}, //P22
        
        {1, -5}, //P23
        {2, -4.6}, //P24
        {3, -4}, //P25
        {4, -3}, //P26
        {4.6, -2}, //P27
        {5, -1}, //P28
        {5, 0} //P29
    };
}


// square.cpp:
    
#include "square.hpp"
    
Square::Square(const Vector2D& location) :
    Shape(location)
{
    vertices_=
    {
        {5, 5},
        {5, -5},
        {-5, -5},
        {-5, 5},
    };
}


// triangle.cpp:
    
#include "triangle.hpp"
        
Triangle::Triangle(const Vector2D& location) :
    Shape(location)
{
    vertices_=
    {
        
        { 0, 5.00},//P01
        {5, -5},//P02
        {-5, -5},//P03
        
    };
}



// player_snake.hpp

#if !defined PLAYER_SNAKE_HPP
#define PLAYER_SNAKE_HPP

#include "snake.hpp"
#include <prg_interactive.hpp>

#endif //PLAYER_SNAKE_HPP



// circle.hpp:
    
#if !defined CIRCLE_HPP
#define CIRCLE_HPP
#include "shape.hpp"
    
class Circle : public Shape{
public :
    Circle (const Vector2D& location);
};

#endif



// square.hpp:
    
#if !defined SQUARE_HPP
#define SQUARE_HPP
#include "shape.hpp"
    
class Square : public Shape{
public :
    Square (const Vector2D& location);
};

#endif



// triangle.hpp:
    
#if !defined TRIANGLE_HPP
#define TRIANGLE_HPP
#include "shape.hpp"
    
class Triangle : public Shape{
public :
    Triangle (const Vector2D& location);
};

#endif  
Last edited on
gotoxy is a function which sets the cursor point to position x,y (just like co-ordinates in a graph)... its not like other goto functions.....
Topic archived. No new replies allowed.