PDA

View Full Version : chicken game



angel111
04-27-2011, 11:05 AM
hii 'm designin a game as a part of my project....
here s d code ...pls help me in "KEYBOARD INTERACTION"...





#include<GL/glut.h>
#include<stdio.h>
#include<windows.h>
GLint initsc=1,m;
float x1=0,x2=0,k1=0,k2=0;


void delay(int dtime)
{
int i,j,k;

for(i=0;i<dtime;i++);
for(j=0;j<dtime;j++);
for(k=0;k<dtime;k++);
}


void dispchar(int x,int y,char *s,void *font)
{
int len,i;

glRasterPos2f(x,y);
len=(int)strlen(s);

for(i=0;i<len;i++)
glutBitmapCharacter(font,s[i]);
}



void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON &amp;&amp; state==GLUT_DOWN &amp;&amp; (x>=4 &amp;&amp; x<=13) &amp;&amp; (y>=7 &amp;&amp; y<=10))
{
initsc=0;
return;
}
else if(btn==GLUT_LEFT_BUTTON &amp;&amp; state==GLUT_DOWN &amp;&amp; (x>=30 &amp;&amp; x<=40) &amp;&amp; (y>=7 &amp;&amp; y<=10))
{

exit(0);
}

glutPostRedisplay();
}

void escp(unsigned char key,int x,int y)
{
if(key==27)
{
glutDestroyWindow(m);
exit(0);
}
}

void presskey(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_UP : k2=0.5; break;
case GLUT_KEY_DOWN : k2=-0.5; break;
case GLUT_KEY_LEFT : k1=-0.5; break;
case GLUT_KEY_RIGHT : k1=0.5; break;
}
glutPostRedisplay();
}


void releasekey(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_UP : k2=0; break;
case GLUT_KEY_DOWN : k2=0; break;
case GLUT_KEY_LEFT : k1=0; break;
case GLUT_KEY_RIGHT : k1=0; break;
}
//glutPostRedisplay();
}


void drawcar1()
{


glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f(2+x1 ,13);
glVertex2f(6+x1 ,13);
glVertex2f(6 +x1,16);
glVertex2f(2 +x1,16);
glVertex2f(2 +x1,13);


glVertex2f(6+x1 ,13);
glVertex2f(7 +x1,14);
glVertex2f(7+x1 ,15);
glVertex2f(6+x1 ,16);
glVertex2f(6 +x1,13);
glEnd();

glLineWidth(2);
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2f(2 +x1,13);
glVertex2f(2.6 +x1,14);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2f(6 +x1,13);
glVertex2f(5 +x1,14);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2f(6 +x1,16);
glVertex2f(5+x1 ,15);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2f(2 +x1,16);
glVertex2f(2.6 +x1,15);
glEnd();


glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2f(2.6 +x1,14);
glVertex2f(5+x1 ,14);
glVertex2f(5 +x1,15);
glVertex2f(2.6+x1 ,15);
glVertex2f(2.6+x1 ,14);
glEnd();
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2f(6 +x1,13);
glVertex2f(7 +x1,14);
glVertex2f(7+x1 ,15);
glVertex2f(6 +x1,16);
glVertex2f(6 +x1,13);


glVertex2f(2 +x1,13);
glVertex2f(6 +x1,13);
glVertex2f(6 +x1,16);
glVertex2f(2 +x1,16);
glVertex2f(2 +x1,13);
glEnd();

glPointSize(3); //draw headlight
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);

glVertex2f(7+x1,14);
glVertex2f(7+x1,15);
glEnd();

glPointSize(4);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(3+x1,13);
glVertex2f(5+x1,13);
glVertex2f(3+x1,16);
glVertex2f(5+x1,16);
glEnd();
//glFlush();
//glutSwapBuffers();
}

void drawcar2()
{
glBegin(GL_POLYGON);
glColor3f(1,0,0);
glVertex2f(2+x2, 27);
glVertex2f(6+x2 ,27);
glVertex2f(6+x2 ,30);
glVertex2f(2+x2 ,30);
glVertex2f(2+x2 ,27);
glVertex2f(6+x2 ,27);
glVertex2f(7+x2 ,28);
glVertex2f(7+x2 ,29);
glVertex2f(6+x2 ,30);
glVertex2f(6+x2 ,27);
glEnd();

glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(2+x2 ,27);
glVertex2f(2.6+x2 ,28);
glEnd();

glLineWidth(2);
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(6+x2 ,27);
glVertex2f(5+x2 ,28);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(6 +x2,30);
glVertex2f(5 +x2,29);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(2+x2 ,30);
glVertex2f(2.6 +x2,29);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(2.6 +x2,28);
glVertex2f(5+x2, 28);
glVertex2f(5+x2 ,29);
glVertex2f(2.6 +x2,29);
glVertex2f(2.6+x2 ,28);
glEnd();
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(6 +x2,27);
glVertex2f(7 +x2,28);
glVertex2f(7 +x2,29);
glVertex2f(6 +x2,30);
glVertex2f(6+x2 ,27);

glVertex2f(2+x2 ,27);
glVertex2f(6+x2, 27);
glVertex2f(6+x2 ,30);
glVertex2f(2+x2 ,30);
glVertex2f(2 +x2,27);
glEnd();

glPointSize(3); //draw headlight
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2f(7+x2,28);
glVertex2f(7+x2,29);
glEnd();

glPointSize(4);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(3+x2,27);
glVertex2f(5+x2,27);
glVertex2f(3+x2,30);
glVertex2f(5+x2,30);
glEnd();
//glFlush();
//glutSwapBuffers();
}

float x3=0;

void drawcar3()
{
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex2f(42-x3 ,33.5);
glVertex2f(47-x3 ,33.5);
glVertex2f(47-x3 ,36.5);
glVertex2f(42-x3 ,36.5);
glVertex2f(42-x3 ,33.5);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex2f(42-x3 ,34.1);
glVertex2f(42-x3 ,35.9);
glVertex2f(41-x3 ,35.9);
glVertex2f(41 -x3,34.1);
glVertex2f(42-x3 ,34.1);
glEnd();

glLineWidth(2);
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(42-x3 ,33.5);
glVertex2f(47 -x3,33.5);
glVertex2f(47-x3 ,36.5);
glVertex2f(42-x3 ,36.5);
glVertex2f(42-x3 ,33.5);


glVertex2f(42 -x3,34.1);
glVertex2f(42 -x3,35.9);
glVertex2f(41 -x3,35.9);
glVertex2f(41-x3 ,34.1);
glVertex2f(42-x3 ,34.1);
glEnd();

glPointSize(3); //draw headlight
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);

glVertex2f(41-x3,34.1);
glVertex2f(41-x3,35.9);
glEnd();

glPointSize(4);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(43-x3,33.5);
glVertex2f(46-x3,33.5);
glVertex2f(43-x3,36.5);
glVertex2f(46-x3,36.5);
glEnd();
//glFlush();
//glutSwapBuffers();
}

void drawcar4()
{
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex2f(42 -x3,19.5);
glVertex2f(47-x3 ,19.5);
glVertex2f(47-x3 ,22.5);
glVertex2f(42 -x3,22.5);
glVertex2f(42 -x3,19.5);
glEnd();

glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex2f(42 -x3,20.1);
glVertex2f(42 -x3,21.9);
glVertex2f(41-x3 ,21.9);
glVertex2f(41-x3 ,20.1);
glVertex2f(42-x3 ,20.1);
glEnd();

glLineWidth(2);
glBegin(GL_LINE_STRIP);
glColor3f(0,0,0);
glVertex2f(42-x3 ,19.5);
glVertex2f(47-x3 ,19.5);
glVertex2f(47 -x3,22.5);
glVertex2f(42-x3 ,22.5);
glVertex2f(42-x3 ,19.5);


glVertex2f(42-x3 ,20.1);
glVertex2f(42-x3 ,21.9);
glVertex2f(41-x3 ,21.9);
glVertex2f(41-x3 ,20.1);
glVertex2f(42 -x3,20.1);
glEnd();

glPointSize(3); //draw headlight
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2f(41-x3,20.1);
glVertex2f(41-x3,21.9);
glEnd();

glPointSize(4);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2f(43-x3,19.5);
glVertex2f(46-x3,19.5);
glVertex2f(43-x3,22.5);
glVertex2f(46-x3,22.5);
glEnd();
//glFlush();
//glutSwapBuffers();
}



void drawkoli()
{
glBegin(GL_POLYGON);
glColor3f(1.0,0.8,0.0);
glVertex2f(24+k1, 8+k2);
glVertex2f(23.5+k1,7+k2);
glVertex2f(23.2+k1 ,6+k2);
glVertex2f(23.2+k1 ,5+k2);
glVertex2f(23.5+k1 ,4+k2);
glVertex2f(23.8+k1 ,3.2+k2);
glVertex2f(24.2+k1 ,3+k2);
glVertex2f(24.8+k1 ,3+k2);
glVertex2f(25.2+k1 ,3.2+k2);
glVertex2f(25.5+k1 ,4+k2);
glVertex2f(25.8+k1 ,5+k2);
glVertex2f(25.8+k1 ,6+k2);
glVertex2f(25.5+k1 ,7+k2);
glVertex2f(25+k1 ,8+k2);
glVertex2f(24+k1 ,8+k2);


glVertex2f(23.7+k1 ,7.4+k2);
glVertex2f(23.5+k1 ,8+k2);
glVertex2f(23.5+k1 ,9+k2);
glVertex2f(23.8 +k1,9.6+k2);
glVertex2f(24 +k1,10+k2);
glVertex2f(24.3 +k1,10.4+k2);
glVertex2f(24.5+k1 ,10+k2);
glVertex2f(24.7+k1 ,10.4+k2);
glVertex2f(25+k1 ,10+k2);
glVertex2f(25.2+k1 ,9.6+k2);
glVertex2f(25.5+k1 ,9+k2);
glVertex2f(25.5 +k1,8+k2);
glVertex2f(25.3+k1 ,7.4+k2);
glEnd();

glBegin(GL_POLYGON);
glColor3f(1.0,0.2,0.0);
glVertex2f(24.3+k1 ,10.4+k2);
glVertex2f(24.5+k1 ,11+k2);
glVertex2f(24.7+k1 ,10.4+k2);
glVertex2f(24.5+k1 ,10+k2);
glVertex2f(24.3+k1 ,10.4+k2);

glEnd();


glBegin(GL_LINE_LOOP);
glColor3f(0,0,0);
glVertex2f(23.6+k1, 3.6+k2);
glVertex2f(23.3+k1, 3.2+k2);


glVertex2f(23.5 +k1,3.4+k2);
glVertex2f(23.2 +k1,3.5+k2);


glVertex2f(23.5+k1, 3.4+k2);
glVertex2f(23.5+k1, 3.1+k2);
glEnd();



glBegin(GL_LINE_LOOP);
glColor3f(0,0,0);
glVertex2f(25.4+k1, 3.6+k2);
glVertex2f(25.5+k1, 3+k2);


glVertex2f(25.4+k1, 3.4+k2);
glVertex2f(25.3+k1, 3+k2);


glVertex2f(25.4+k1 ,3.4+k2);
glVertex2f(25.7+k1 ,3.1+k2);
glEnd();

glPointSize(3); //chckn eye
glBegin(GL_POINTS);
glColor3f(0,0,0);
glVertex2f(23.8+k1,9.7+k2);
glVertex2f(25+k1,9.8+k2);
glEnd();
//glFlush();
//glutSwapBuffers();
}

void drawworm()
{
glColor3f(1.0,0.4,0.0);
glBegin(GL_POLYGON);

glVertex2f(42 ,43);
glVertex2f(42, 41.4);
glVertex2f(42.4 ,41.4);
glVertex2f(42.4, 43);
glVertex2f(42 ,43);
glEnd();
glColor3f(1.0,0.4,0.0);
glBegin(GL_POLYGON);
glVertex2f(42 ,41.4);
glVertex2f(42.4, 41);
glVertex2f(44 ,41);
glVertex2f(45.4 ,41.2);
glVertex2f(44, 41.4);
glVertex2f(42.8 ,41.4);
glVertex2f(42.4, 41.6);
glVertex2f(42 ,41.4);
glEnd();

glColor3f(1.0,0.4,0.0);
glBegin(GL_POLYGON);
glVertex2f(42 ,43);
glVertex2f(41.8 ,43.2);
glVertex2f(41.8 ,43.6);
glVertex2f(42.2 ,44);
glVertex2f(42.6 ,43.6);
glVertex2f(42.6 ,43.2);
glVertex2f(42.4, 43);
glVertex2f(42 ,43);
glEnd();

glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_STRIP);
glVertex2f(42.2, 44);
glVertex2f(42 ,44.6);


glVertex2f(42.2 ,44);
glVertex2f(42.4, 44.6);
glEnd();


glPointSize(5.0);
glBegin(GL_POINTS);
glColor3f(1.0,1.0,1.0); //worm eye
glVertex2f(42,43.6);
glVertex2f(42.4,43.6);
glEnd();
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3f(0.0,0.0,0.0);
glVertex2f(42,43.5);
glVertex2f(42.4,43.5);
glEnd(); //worm eye end
}


void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0.0,50.0,0.0,50.0);
//glEnable(GL_BLEND);
glutIgnoreKeyRepeat(1);
glutKeyboardFunc(escp);
glutSpecialFunc(presskey);
glutSpecialUpFunc(releasekey);
}



void display(void)
{
if(initsc==1)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,0);

dispchar(8,48,"",GLUT_BITMAP_TIMES_ROMAN_24);
dispchar(18,45,"CG MINI PROJECT",GLUT_BITMAP_TIMES_ROMAN_24);
dispchar(0,44.5,"__________________________________________________ _______________________________",GLUT_BITMAP_TIMES_ROMAN_24);



glColor3f(1,0,0);
dispchar(5,40,"The project on:-",GLUT_BITMAP_TIMES_ROMAN_24);

glColor3f(0.9,0.0,0.7);
dispchar(5,37,"'CRAZY CHICKEN'(GAMING)",GLUT_BITMAP_TIMES_ROMAN_24);

glColor3f(0.1,0.5,0.7);
dispchar(5,35,"- - - - - - - - - - - - - - - - -",GLUT_BITMAP_TIMES_ROMAN_24);


glColor3f(0.3,0.7,0.2);
dispchar(5,26,"PROJECT ASSOCIATES",GLUT_BITMAP_TIMES_ROMAN_24);
dispchar(3,25.5,"__________________________",GLUT_BITMAP_TIMES_ROMAN_24);

glColor3f(0.1,0.9,0.7);
dispchar(5,22,"",GLUT_BITMAP_TIMES_ROMAN_24);
dispchar(5,20,"",GLUT_BITMAP_TIMES_ROMAN_24);

glColor3f(0.6,0.8,.1);
glBegin(GL_POLYGON);
glVertex2f(4,7);
glVertex2f(13,7);
glVertex2f(13,10);
glVertex2f(4,10);
glEnd();
glColor3f(0,0,0);
dispchar(6,8,"START",GLUT_BITMAP_TIMES_ROMAN_24);



glColor3f(0.6,0.8,.1);
glBegin(GL_POLYGON);
glVertex2f(30,7);
glVertex2f(40,7);
glVertex2f(40,10);
glVertex2f(30,10);
glEnd();
glColor3f(0,0,0);
dispchar(33.5,8,"EXIT",GLUT_BITMAP_TIMES_ROMAN_24);

glFlush();
glutSwapBuffers();
glutMouseFunc(mouse);
initsc=0;
//delay(99999999);
delay(99999999);
delay(99999999);
}

else
{

for(;;)
{
glClearColor(0.0,0.8,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1.0,0,0);
dispchar(2,48,"Lives=3",GLUT_BITMAP_TIMES_ROMAN_24);


if(k1||k2)
{
//glLineWidth(2);
glutPostRedisplay();
}drawkoli();

drawworm();

glBegin(GL_POLYGON); //draw road
glColor3f(0.5,0.5,0.5);
glVertex2i(0,12);
glVertex2i(50,12);
glVertex2i(50,38);
glVertex2i(0,38);
glEnd();

glBegin(GL_POLYGON); //draw yellow line
glColor3f(1.0,1.0,0.0);
glVertex2i(0,25);
glVertex2i(50,25);
glVertex2i(50,24);
glVertex2i(0,24);
glEnd();

glColor3f (1.0, 1.0, 1.0);
glEnable (GL_LINE_STIPPLE);
glLineWidth (6.0);
glLineStipple (4, 0x00FF); /* dotted */
glBegin(GL_LINES);
glColor3f(1.0,1.0,1.0);
glVertex2f (0.0,18.5);
glVertex2f (50.0,18.5);
glVertex2f(0.0,31.5);
glVertex2f(50.0,31.5);
glEnd();
glDisable(GL_LINE_STIPPLE);

if(x1<50)
x1=x1+.05;
else
x1=0;

if(x2<50)
x2+=0.055;
else
x2=0;


if(x3<50)
x3=x3+.05;
else
x3=0;
drawcar1();
drawcar2();
drawcar3();
drawcar4();


glFlush();
}
delay(999);
glutSwapBuffers();
}
}


void main(int argc,char **argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1024,768);
glutInitWindowPosition(00,00);
int m=glutCreateWindow("CRAZY CHICKEN");
glutDisplayFunc(display);
Init();
glutMainLoop();
}

angel111
04-27-2011, 11:09 AM
the code s bit long...pls xecute it n help me in moving chickn using arrow keys...plss urgent

dorbie
04-27-2011, 03:21 PM
It's not enough to register your glut display func, you also need to register other callbacks you need with the suitable event handlers you have written (or more likely someone wrote for you).

e.g. glutKeyboardFunc

http://www.opengl.org/resources/libraries/glut/spec3/node45.html

This is not an OpenGL graphics question. There is a forum for libraries like GLUT.