Like Snake movement

Started by
17 comments, last by IvayloNikolov 11 years, 7 months ago
So I am really new in OpenGL. I have never made a game so I decided to start with something easy like Snake. But I can't code the movement. I can't control point's movement. When I press a button it goes to a direction and doesn't stop although I press buttons. So excuse me for my silliness I hope you don't think for a retard biggrin.png

[source lang="cpp"]GLfloat camerax=0.0;
GLfloat cameray=0.0;
bool movingleft=false;
bool movingright=false;
bool movingup=false;
bool movingdown=false;
void keyPressed (unsigned char key, int x, int y) {

if(key=='a')
{
movingleft=true;

}
if(key=='d')
{
movingright=true;

}
if(key=='w')
{
movingup=true;
}
if(key=='s')
{
movingdown=true;
}
glutPostRedisplay();
}

void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
if(movingright==true)
{
camerax+=0.001;
glutPostRedisplay();
}
if(movingup==true)
{
cameray+=0.001;
glutPostRedisplay();
}
if(movingdown==true)
{
cameray-=0.001;
glutPostRedisplay();
}
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(camerax, cameray, 0.0f);
glEnd();
glFlush();
}
[/source]
Advertisement
When you start moving in a direction, you got to set the flags of all other directions to false.

if(key=='a')
{
movingleft=true;
movingright = false;
movingup = ...
...
}


Also there needs to be an else if in the last if blocks.

if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
else if (movingright==true)
{
...
}
else if .....
{
...
}
There are much better ways to do it. When you got your way working, think about how you can possible make it better. :)

All the best.
It doesn't work the way you told me ;( I tried that way before posting the code.
[source lang="cpp"]void keyPressed (unsigned char key, int x, int y) {

if(key=='a')
{
movingleft=true;
movingright=false;
movingup=false;
movingdown=false;
}
if(key=='d')
{
movingright=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='w')
{
movingup=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='s')
{
movingdown=true;
movingleft=false;
movingup=false;
movingdown=false;
}
glutPostRedisplay();
}

void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
if(movingleft==true)
{
camerax-=0.001;
glutPostRedisplay();
}
else if(movingright==true)
{
camerax+=0.001;
glutPostRedisplay();
}
else if(movingup==true)
{
cameray+=0.001;
glutPostRedisplay();
}
else if(movingdown==true)
{
cameray-=0.001;
glutPostRedisplay();
}
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(camerax, cameray, 0.0f);
glEnd();
glFlush();
}[/source]
a few things:

1) You don't want to call glutPostRedisplay inside the conditionals, you want it after the render code.

2) You probably don't want to move the camera, once you get more bodyparts it won't make much sense.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
1. Doesn' work that way at all
2. I'm not moving the camera, they are just names of the variables.
Oh i see what you're doing there biggrin.png

put the glutPostRedisplay in your update function (you should change the "camera" variable in there aswell), you shouldn't have game logic in the display function (like you do now).

The problem you have is caused by postredisplay being called from the display function (which causes the display function to run again immediatly afterwards).

Basically right now you have a gameloop that looks something like this:

while(running) {
keypressedfunction gets called here and sets the moving_left, etc
While (moving left etc is pressed) {
display(); //this also moves the "camera"
}
Yes, with glut your code look different but that is effectivly how glut treats your current code, (glut runs its own mainloop)

the problem with this is that once the keypressed function runs the first time you'll get stuck in an infinite loop of glutpostredisplay.


The solution:

Move all your game logic (if move_left ....) blabla
to the glutIdleFunc

put glutPostRedisplay() at the end of the glutIdlefunc (nowhere else).

only have rendering code in the display function (no logic)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Like this?

[source lang="cpp"]#include <GL/glew.h>
#include <GL/glut.h>
#include<GL\GL.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
GLfloat snakex=0.0;
GLfloat snakey=0.0;
bool movingleft=false;
bool movingright=false;
bool movingup=false;
bool movingdown=false;
void keyPressed (unsigned char key, int x, int y) {
if(key=='a')
{
movingleft=true;
movingright=false;
movingup=false;
movingdown=false;
}
if(key=='d')
{
movingright=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='w')
{
movingup=true;
movingleft=false;
movingup=false;
movingdown=false;
}
if(key=='s')
{
movingdown=true;
movingleft=false;
movingup=false;
movingdown=false;
}
glutPostRedisplay();
}
void idle()
{
if(movingleft==true)
{
snakex-=0.001;

}
else if(movingright==true)
{
snakex+=0.001;
}
else if(movingup==true)
{
snakey+=0.001;
}
else if(movingdown==true)
{
snakey-=0.001;
}
glutPostRedisplay();
}
void display (void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(0.0f, 0.0f, -6.0f);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex3f(snakex, snakey, 0.0f);
glEnd();
glFlush();
}
void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glEnable( GL_TEXTURE_2D );
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("You’re first OpenGL Window");
glutKeyboardFunc(keyPressed);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}[/source]
Forgot to mention that this way it doesn't to anything at all.
Hmm, you can remove the postredisplay from the keyboard handling function atleast, it shouldn't be there, not sure why its not doing "anything" though.

Do you get a window ? any errors ? warnings ?

Does the debugger show the snakex changing ?
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement