可以移动的恐龙--用键盘让恐龙具有生命 dino.dat文件绘制恐龙 a d w s移动恐龙 源代码

#include<windows.h>
#include<math.h>
#include <gl/Glut.h>
#include <fstream>
#include <iostream>
#include<vector>

using namespace std;

void myDisplay(void);
void myInit(void);
void readFromFile(char *fileName);
void myKeyboard(unsigned char key,int x , int y);

struct GLPoint{
    GLint x, y;
};

bool moved = true;                //为了一开始能够显示
GLint xDirection = 0;
GLint yDirection = 0;
static GLint numPolys = 0;        //总共线条的个数
//static GLint numLines = 0;

vector <GLPoint> pt ;            //用来存放各个顶点的值
vector <GLint> numPoints;        //每条线包含的顶点个数

const GLint screenWidth = 640;
const GLint screenHeight = 480;

int main(int argc, char ** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(screenWidth,screenHeight);
    glutInitWindowPosition(100,150);
    glutCreateWindow("Dinosaur");

    glutDisplayFunc(myDisplay);
    glutKeyboardFunc(myKeyboard);
    myInit();
    glutMainLoop();

    return 0;
}

void readFromFile(char *fileName){
    //cout<<a<<" "<<s<<" "<<d<<" "<<w<<endl;
    fstream inStream;
    GLint num;    //记录每条线的顶点个数
    inStream.open(fileName,ios::in);
    if (inStream.fail())
    {
        return;
    }
    cout<<"file open succeed"<<endl;
    glClear(GL_COLOR_BUFFER_BIT);
    inStream>>numPolys;                    //线带的总条数(因为每条线带是多个顶点,顾用poly)
    for(int j=0;j<numPolys;j++){        //每条折线
         inStream>>num;
        numPoints.push_back(num);        //每条线带 所包含的 顶点个数
        for(int i=0;i<num;++i){
            GLPoint *pp = new GLPoint();
            inStream>>pp->x>>pp->y;
            pt.push_back(*pp);            //顶点值存入数组中
        }
    }

    inStream.close();
    cout<<"file closed"<<endl;
}

void myKeyboard(unsigned char key,int x , int y){
    switch (key)
    {
    case 'w':moved = true;xDirection=0;yDirection=1;glutPostRedisplay();
        break;
    case 'd':moved = true;xDirection = 1;yDirection=0;glutPostRedisplay();
        break;
    case 's':moved = true;xDirection = 0;yDirection=-1;glutPostRedisplay();
        break;
    case 'a':moved = true;xDirection = -1;yDirection=0;glutPostRedisplay();
        break;
    default:break;
    }
}

void myDisplay(void){
    
    glClear(GL_COLOR_BUFFER_BIT);
    
    cout<<endl;
    cout<<endl;
    cout<<endl;
    GLint number = 0;
    for (int j=0;j<numPolys;j++)        //多少条线,就有多少次循环
    {
        cout<<"线条个数:"<<numPolys<<endl;
        glBegin(GL_LINE_STRIP);
        cout<<"顶点坐标:";
        for (int i = 0;i<numPoints[j];i++)
        {
            pt[number].x += xDirection;
            pt[number].y += yDirection;
            glVertex2i(pt[number].x,pt[number].y);
            number++;
        }
        glEnd();
        cout<<endl;
    }
    glutSwapBuffers();
}

void myInit(void){
    glClearColor(1.0,1.0,1.0,0.0);
    glColor3f(0.0f, 0.0f, 0.0f);        

    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);

    readFromFile("D:\\dino.dat");

}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【资源说明】 Python开发基于机器学习实现自动玩Google小恐龙游戏源码+项目说明+注释拉满.zip 如何用人工智能自动玩游戏 ## 一、前言 让AI玩游戏的思想早在上世纪就已经有了,那个时候更偏向棋类游戏。像是五子棋、象棋等。在上世纪“深蓝”就击败了国际象棋冠军,而到2016年“Alpha Go”击败了人类围棋冠军。 到现在,AI涉略的不仅仅是棋类游戏。像是超级马里奥、王者荣耀这种游戏,AI也能有比较好的表现。今天我们就来用一个实际的例子讨论AI自动玩游戏这一话题,本文会用非常简单的机器学习算法让AI自动玩Google小恐龙游戏。 ## 二、Google小恐龙与监督学习 ### 2.1、Google小恐龙 如果你使用的是Chrome浏览器,那么相信你应该见过下面这个恐龙: ![在这里插入图片描述](https://img-blog.csdnimg.cn/f6cd242ae82248759165688539d6c521.png#pic_center) 当我们用Chrome断网访问网页时,就会显示这个恐龙,或者直接在地址栏输入:[chrome://dino](chrome://dino)直接访问该游戏。 游戏的玩法非常简单,只需要按空格键即可。比如下面左图,快碰到障碍物,这时需要按空格,而下面右图没有障碍(或离障碍比较远),则不需要按按键。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/2ba95fbf96e84d0aa3bc49fe1fb9fc9b.png#pic_center) 当然还有出现鸟的情况,我们也可以归为跳的情况。大家可以玩一下。 ### 2.2、监督学习 玩游戏很多时候会使用一个叫强化学习的方式来实现,而本文使用比较简单的监督学习来实现。 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值