Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple Snake Game (C++)

Started by Getov Sep 1, 2012 at 7:32 AM 1 replies 14.2k views
Original Post
Getov
Getov
Hello everyone !

I've been practicing C++ for an year and started learning OpenGL not long ago.

Yesterday I've started writing this game, using OpenGL, maybe in a little unusual way.

I've been wondering is there a way to put a circle (drawn by using GL_TRIANGLE_FAN) into some kind of container like queue ?
The idea is that I use the circles for the snake body, and so I can increase the snake size after eating food.

So far I've managed to make the circle move in any direction using the arrow keys.

There is my code, it is a bit messy for now, but I was in a rush and did it for about an hour to see how it works smile.png :


// contents of Grid.h
#pragma once
class Grid
{
public:
void initialize();
};



// contents of Grid.cpp
#include "Grid.h"
#include <gl\freeglut.h>
void Grid::initialize()
{
glClearColor(0.0, 1.0, 0.0, 0.0);
glShadeModel(GL_DEPTH_TEST);
}



// contents of SetWindow.h
#pragma once
class SetWindow
{
public:
SetWindow(int windowWidth, int windowLenght,
int windowPos, char* name);
};



// contents of SetWindow.cpp
#include "SetWindow.h"
#include <gl\freeglut.h>
SetWindow::SetWindow(int windowWidth, int windowLenght,
int windowPos, char* name)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowLenght);
glutInitWindowPosition(windowPos, windowPos);
glutCreateWindow(name);
}



// contents of DrawElements.h
#pragma once
class Draw
{
private:
float x;
float y;
float r;
double M_PI;
public:
Draw();
void head(float updateX, float updateY);
};



// contents of DrawElements.cpp
#include "DrawElements.h"
#include <gl\freeglut.h>
#include <iostream>
Draw::Draw()
: x(200)
, y(200)
, r(10)
, M_PI(3.14)
{
}
void Draw::head(float updateX, float updateY)
{
glVertex2f(x + updateX, y + updateY);
for(int n = 0; n <= 100; ++n)
{
float const t = 2 * M_PI * (float)n / (float)100;
glVertex2f(x + updateX + sin(t)*r, y + updateY + cos(t)*r);
}
}



// contents of main
#include <gl\freeglut.h>
#include <cstdlib>
#include <deque>
#include "Grid.h"
#include "SetWindow.h"
#include "DrawElements.h"
using namespace std;
float updateX = 0;
float updateY = 0;
bool KEY_UP;
bool KEY_DOWN;
bool KEY_LEFT;
bool KEY_RIGHT;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Draw snake;
glBegin(GL_TRIANGLE_FAN);
snake.head(updateX, updateY);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
void handleInput(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP:
KEY_DOWN = false;
KEY_LEFT = false;
KEY_RIGHT = false;
KEY_UP = true;
break;
case GLUT_KEY_DOWN:
KEY_LEFT = false;
KEY_RIGHT = false;
KEY_UP = false;
KEY_DOWN = true;
break;
case GLUT_KEY_LEFT:
KEY_RIGHT = false;
KEY_UP = false;
KEY_DOWN = false;
KEY_LEFT = true;
break;
case GLUT_KEY_RIGHT:
KEY_UP = false;
KEY_DOWN = false;
KEY_LEFT = false;
KEY_RIGHT = true;
break;
}
}
void update(int value)
{
if (KEY_UP == true)
{
updateY += 10;
}
else if (KEY_DOWN == true)
{
updateY -= 10;
}
else if (KEY_LEFT == true)
{
updateX -= 10;
}
else if (KEY_RIGHT == true)
{
updateX += 10;
}
glutPostRedisplay();
glutTimerFunc(250, update, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
SetWindow displayWindow(800, 600, 50, "Pesho Snake");
Grid grid;
grid.initialize();
glutDisplayFunc(display);
glutSpecialFunc(handleInput);
glutReshapeFunc(reshape);
glutTimerFunc(250, update, 0);
glutMainLoop();
return EXIT_SUCCESS;
}


The Grid class was initially meant to set the coordinate system (to include reshape function there) but things got messed up so I left it like that.

Well hope you can give me any suggestions smile.png .
mmakrzem
mmakrzem
If you are using old school OpenGL you'll want to use display lists- http://www.songho.ca/opengl/gl_displaylist.html

If you are using more recent versions of OpenGL then have a look at Vertex Buffer Objects VBO.
Getov
Getov
Thanks, I think I will stick to VBO

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.