C语言3D贪吃蛇

  • y6_473626
    了解作者
  • 22.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-16 10:04
    上传日期
结合OpenGL使用C语言写的3D版贪吃蛇,源代码并且附有可执行文件.exe。亲测通过,加油哦。(不涉及高深语法,简单易懂)
3D贪吃蛇.zip
  • 3D贪吃蛇
  • 可执行文件.exe
    56.8KB
  • main.cpp
    22.7KB
  • 源码.cbp
    1.4KB
内容介绍
//贪吃蛇源代码 #include <windows.h> #include <stdio.h> #include <GL/glut.h> #include <math.h> #define MAX_CHAR 128 //字符个数 //第一步:初始化---背景色为白色 void init() { glClearColor (0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); //启动深度测试模式 } //全局变量---以k开头的表示键盘控制的图形 //通关控制键eatnum,win int eatnum = 0; //当前吃到的图形数 int win = 10; //控制图形大小 float teasize = 0.32f; float kteasize = 0.2f; //控制图形移动 float xadd = 0.0,yadd = 0.0; float kxadd = 0.0,kyadd = 0.0; //控制图形颜色 float r=1.0f,g=0.0f,b=0.0f; float kr=1.0f,kg=1.0f,kb=1.0f; //控制选择哪个图形 int shape = 1; //图形编号 int num = 6; //图形个数 //全局变量:旋转角度 float theta=0.0; //控制左右旋转的旋转角度 float thetaup=0.0; //控制上下旋转的旋转角度 float ktheta=0.0,kthetaup=0.0; //控制移动方向---初始为0,不动。1向上,2向下,3向左,4向右 int direct = 1; int kdirect = 0; //边框的大小---up上边界---down上边界---left上边界---right上边界 float up = 1.9,down = -1.9,left = -1.9, right = 1.9; //控制线绘制还是实心 boolean line = true; //控制碰壁 int peng = 0; //碰边界次数---当碰边界次数达到pmax int pmax = 2; //碰壁上限 int kpeng = 0; //键盘操作的图形也不得碰壁次数超过kpmax次 int kpmax = 2; //两个图形之间的距离dis = (xadd-kxadd)^2+(yadd-kyadd)^2 double dis = 9.0; double maxR = 0.07; //两者之间的距离小于这个值时表示吃掉 //速度 int speed = 140; //绘制金字塔 //四方体的半边长 //float haltb = 0.3f; /*#################################################################*/ void drawPyramid(float haltb) //该金字塔在以原点为中心,边长为1的立方体范围内 { glBegin(GL_TRIANGLES); // Start Drawing A Triangle glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, haltb, 0.0f); // Top Of Triangle (Front) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f(-haltb,-haltb, haltb); // Left Of Triangle (Front) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f( haltb,-haltb, haltb); // Right Of Triangle (Front) glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, haltb, 0.0f); // Top Of Triangle (Right) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f( haltb,-haltb, haltb); // Left Of Triangle (Right) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f( haltb,-haltb, -haltb); // Right Of Triangle (Right) glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, haltb, 0.0f); // Top Of Triangle (Back) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f( haltb,-haltb, -haltb); // Left Of Triangle (Back) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f(-haltb,-haltb, -haltb); // Right Of Triangle (Back) glColor3f(1.0f,0.0f,0.0f); // Red glVertex3f( 0.0f, haltb, 0.0f); // Top Of Triangle (Left) glColor3f(0.0f,0.0f,1.0f); // Blue glVertex3f(-haltb,-haltb,-haltb); // Left Of Triangle (Left) glColor3f(0.0f,1.0f,0.0f); // Green glVertex3f(-haltb,-haltb, haltb); // Right Of Triangle (Left) glEnd(); glBegin(GL_POLYGON); //金字塔底面正方形 glColor3f(0.5f,0.5f,0.5f); //底面为灰色 glVertex3f(-haltb,-haltb, haltb); glVertex3f(haltb,-haltb, haltb); glVertex3f(haltb,-haltb, -haltb); glVertex3f(-haltb,-haltb,-haltb); glEnd(); } //绘制立方体 void drawcube(float haltb) { glBegin(GL_QUADS); // Draw A Quad glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green glVertex3f( haltb, haltb,-haltb); // Top Right Of The Quad (Top) //glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f(-haltb, haltb,-haltb); // Top Left Of The Quad (Top) //glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-haltb, haltb,haltb); // Bottom Left Of The Quad (Top) //glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f( haltb, haltb,haltb); // Bottom Right Of The Quad (Top) glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange //glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f( haltb, -haltb,haltb); // Top Right Of The Quad (Bottom) //glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f(-haltb,-haltb,haltb); // Top Left Of The Quad (Bottom) //glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-haltb,-haltb,-haltb); // Bottom Left Of The Quad (Bottom) //glVertex3f( 1.0f,-1.0f,-1.0f); glVertex3f( haltb,-haltb,-haltb); // Bottom Right Of The Quad (Bottom) glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red //glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f( haltb, haltb,haltb); // Top Right Of The Quad (Front) //glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-haltb, haltb,haltb); // Top Left Of The Quad (Front) //glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f(-haltb,-haltb,haltb); // Bottom Left Of The Quad (Front) //glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f( haltb,-haltb,haltb); // Bottom Right Of The Quad (Front) glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow //glVertex3f( 1.0f,-1.0f,-1.0f); glVertex3f( haltb,-haltb,-haltb); // Top Right Of The Quad (Back) //glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f( -haltb, -haltb,-haltb); // Top Left Of The Quad (Back) //glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f( -haltb, haltb,-haltb); // Bottom Left Of The Quad (Back) //glVertex3f( 1.0f, 1.0f,-1.0f); glVertex3f( haltb, haltb,-haltb); // Bottom Right Of The Quad (Back) glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue //glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f( -haltb, haltb,haltb); // Top Right Of The Quad (Left) //glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f( -haltb, haltb,-haltb); // Top Left Of The Quad (Left) //glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f( -haltb, -haltb,-haltb); // Bottom Left Of The Quad (Left) //glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f( -haltb, -haltb,haltb); // Bottom Right Of The Quad (Left) glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet //glVertex3f( 1.0f, 1.0f,-1.0f); glVertex3f( haltb, haltb,-haltb); // Top Right Of The Quad (Right) //glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f( haltb, haltb,haltb); // Top Left Of The Quad (Right) //glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f( haltb, -haltb,haltb); // Bottom Left Of The Quad (Right) //glVertex3f( 1.0f,-1.0f,-1.0f); glVertex3f( haltb, -haltb,-haltb); // Bottom Right Of The Quad (Right) glEnd(); } //绘制You Lose void youlose() { glColor3f(0.0f,1.0f,0.0f); //单位矩阵 glLoadIdentity(); glPushMatrix(); //压栈 //平移---左上方 glTranslatef(-1,1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f); glutWireTeapot(teasize); glPopMatrix(); //出栈 glPushMatrix(); //压栈 //平移---右上方 glTranslatef(1,1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f); //绘制线框圆环 glutWireTorus(teasize-0.2,teasize+0.2,20,20); glPopMatrix(); //出栈 glPushMatrix(); //压栈 //平移---左下方 glTranslatef(-1,-1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f); //绘制线框圆锥 glutWireCone(teasize,teasize,20,20); glPopMatrix(); //出栈 glPushMatrix(); //压栈 //平移---右下方 glTranslatef(1,-1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f); //仅当第二个参数为1时,以垂直向上为旋转轴 //绘制金字塔 drawPyramid(teasize); glPopMatrix(); //出栈 } //绘制You Win void youwin() { glColor3f(1.0f,0.0f,0.0f); //单位矩阵 glLoadIdentity(); glPushMatrix(); //压栈 //平移---左上方 glTranslatef(-1,1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f); glutWireTeapot(teasize); glPopMatrix(); //出栈 glPushMatrix(); //压栈 //平移---右上方 glTranslatef(1,1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f); //绘制线框圆环 glutWireTorus(teasize-0.2,teasize+0.2,20,20); glPopMatrix(); //出栈 glPushMatrix(); //压栈 //平移---左下方 glTranslatef(-1,-1,-5.0f); glRotatef(theta,0.0f,1.0f,0.0f);
评论
    相关推荐