Jump to content
  • Advertisement
Sign in to follow this  
IvayloNikolov

Like Snake movement

This topic is 2300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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] Edited by IvayloNikolov

Share this post


Link to post
Share on other sites
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 .....
{
...
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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) Edited by SimonForsman

Share this post


Link to post
Share on other sites
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] Edited by IvayloNikolov

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!