OPENGL滑雪小冒险

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<iostream>
#include<math.h>
#include <GL/glut.h>
#include <GL/GL.h>
#include <iostream>
#define DT 20
#define SL 30
#define TL 300
#define PI 3.14
using namespace std;
double cx = 100;
double cy = 200;
int judgex = 0;
int judgey = 0;
int flag = 1;
int jump = 0;
int supposey = 0;
int speedy = 0;
int rockx = 0;
int rocky = 0;
int ysure = 0;
int startf = 0;
bool gameover = FALSE;
float angle = PI / 2;
void keyboard(unsigned char key, int x, int y)
{
	// 获取键盘输入
	switch (key) {
	case 's':
		if (jump == 1)
		{
			angle += 0.3;
		}
		else
		{
			speedy = 16;
			jump = 1;
			cy += 16;
		}
		printf("jummp");
		break;

	case 'q':
	case 0x1b:
		exit(0);
	default:
		break;
	}
	// 重新绘制图形
	//glutPostRedisplay();
}
struct point
{
	int x;
	int y;
};
point pf[TL] = {};
int functime = 0;
int getpixel(int x, int y)
{
	// 定义颜色值
	GLfloat expectedColor[3] = { 0.2f, 0.7f, 1.0f };
	GLubyte pixel[3];
	glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixel);
	printf("read:%d,%d\n", x, y);
	// 转换颜色值为RGB格式
	GLfloat actualColor[3];
	actualColor[0] = pixel[0] / 255.0f;
	actualColor[1] = pixel[1] / 255.0f;
	actualColor[2] = pixel[2] / 255.0f;
	// 对比颜色值
	printf("color:%f,%f,%f\n", actualColor[0], actualColor[1], actualColor[2]);
	glColor3f(actualColor[0], actualColor[1], actualColor[2]);
	if (actualColor[0] == 0.8f)
	{
		flag = 1;
	}
	if (memcmp(actualColor, expectedColor, sizeof(expectedColor)) == 0) {
		return 0;
	}
	else {
		return 1;
	}
}
void drawrock(int x, int y)
{

	glBegin(GL_POLYGON);
	glColor3f(0.2, 0.2, 0.2);
	glVertex2i(x, y);
	glColor3f(0.3, 0.2, 0.4);
	glVertex2i(x + 10, y);
	glColor3f(0.3, 0.6, 0.4);
	glVertex2i(x + 10, y + 5);
	glColor3f(0.3, 0.2, 0.4);
	glVertex2i(x + 7, y + 7);
	glColor3f(0.3, 0.5, 0.4);
	glVertex2i(x, y + 5);
	glEnd();
	glFlush();
	glColor3f(1, 1, 1);
	rockx = x;
	rocky = y;
	if ((pow((cx - x), 2) + pow((cy - y), 2)) <= 25)
	{
		gameover = TRUE;
	}
}
void changeview()
{
	glTranslated(-20, 10, 0);
	cx += 20;
	cy -= 10;
	ysure -= 10;
}
void drawtrees(double x, double y)
{

	glColor3f(0.3, 0.3, 0);
	glRectf(x, y - 10, x + 10, y + 20);
	glColor3f(0, 1, 0);
	glBegin(GL_POLYGON);
	glVertex2d(x - 10, y + 20);
	glVertex2d(x + 5, y + 40);
	glVertex2d(x + 20, y + 20);
	glEnd();
	glBegin(GL_POLYGON);
	glVertex2d(x - 10, y + 30);
	glVertex2d(x + 5, y + 50);
	glVertex2d(x + 20, y + 30);
	glEnd();
	glBegin(GL_POLYGON);
	glVertex2d(x - 10, y + 40);
	glVertex2d(x + 5, y + 60);
	glVertex2d(x + 20, y + 40);
	glEnd();
	glFlush();
	glColor3f(1, 1, 1);

}
void score()
{
	int a = clock();
	const char* str = "Score:";
	char score[16] = { 0 };
	_itoa(cx / 10, score, 10);
	int n = strlen(str);
	glRasterPos2f(cx + 400, ysure + 400);
	for (int i = 0; i < n; i++)
		glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *(str + i));
	for (int i = 0; i < 16; i++)
		glutBitmapCharacter(GLUT_BITMAP_9_BY_15, score[i]);

	glFlush();
}
void drawcharacter(int x, int y, float angle)
{
	if (!jump)
	{
		cy = supposey;
		angle = PI / 2;
	}
	else
	{
		if (cy < supposey)
		{
			cy = supposey;
			jump = 0;
			speedy = 0;
			angle = PI / 2;
		}
		else
		{
			cy += speedy;
			speedy -= 5;
			if (speedy < -16)
			{
				speedy = -15;
			}
			printf("jummp");
		}
	}

	glColor3f(1, 0, 0);
	glBegin(GL_POLYGON);
	glVertex2d((x - 3) + cos(angle - PI / 6) * 5, (y + 5) + sin(angle - PI / 6) * 5);
	glVertex2d((x - 3) + cos(angle + PI / 6) * 5, (y + 5) + sin(angle + PI / 6) * 5);

	glVertex2d((x - 3) + cos(angle + 5 * PI / 6) * 5, (y + 5) + sin(angle + 5 * PI / 6) * 5);
	glVertex2d((x - 3) + cos(angle - 5 * PI / 6) * 5, (y + 5) + sin(angle - 5 * PI / 6) * 5);
	glEnd();
	glFlush();
}
void Init()
{
	glClearColor(0.2f, 0.7f, 1.0f, 0.0f);
}

void createbk()
{
	for (int i = 0; i < TL; i++)
	{
		pf[i].x = i * 200 + (rand() % 50) - 50;
		pf[i].y = -i * 100 + (rand() % 300) + 200;
	}
}

void drawNurbs(point p1[], int n)
{
	int i = 0;
	int j = 0;
	glColor3f(1, 1, 1);

	for (i = 0; i < n - 2; i++)
	{
		glBegin(GL_POLYGON);
		glVertex2d(p1[i].x - 50, p1[i].y - 500);
		int ff = 0;
		int fx, fy;
		int ft = 0;
		int ftx, fty;
		for (j = 0; j <= DT; j++)
		{
			float t = (float)j / DT;
			double xx = 0.5 * ((p1[i].x) * pow((1 - t), 2) + (p1[i + 1].x) * (1 + 2 * t - 2 * t * t) + (p1[i + 2].x) * t * t) - 100;
			double yy = 0.5 * ((p1[i].y) * pow((1 - t), 2) + (p1[i + 1].y) * (1 + 2 * t - 2 * t * t) + (p1[i + 2].y) * t * t);
			glVertex2d(xx, yy);
			if (fabs(cx - xx) < 10)
			{
				supposey = yy;
			}
			if ((int(xx) % 100) == 0)
			{
				ff = 1;//判断是否画石头,其实应该用bool值懒得改了
				fx = xx;
				fy = yy;
			}
			if ((int(xx) % 30) == 0)
			{
				ft = 1;//判断是否画石头
				ftx = xx;
				fty = yy;
			}
		}
		glVertex2d(p1[i + 2].x - 50, p1[i + 2].y - 500);
		glEnd();
		glFlush();
		if (ff)drawrock(fx, fy);//画石头
		if (ft)drawtrees(ftx, fty);//画树
	}


}
void Reshape(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, (double)w, 0, (double)h);
}

void mytimefunc(int value)
{
	//if (startf != 0)
	//{
	functime++;
	glutPostRedisplay();
	if (!gameover)
	{
		glutTimerFunc(100, mytimefunc, 1);
	}
	//}
}

void start()
{
	printf("OK");
	glClear(GL_COLOR_BUFFER_BIT);
	//glColor3f(0, 0, 0);
	const char* str = "Please Choose Your Hero";
	int n = strlen(str);
	glRasterPos2f(250, 400);
	for (int i = 0; i < n; i++)
		glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *(str + i));
	//glutSwapBuffers();
	glFlush();
}
void myDisplayGra(void)
{
	//if (startf == 0)start();
	//else {
	glClear(GL_COLOR_BUFFER_BIT);
	drawNurbs(pf + functime / 10, SL);
	drawcharacter(cx, cy, angle);
	score();
	glFlush();
	glutSwapBuffers();
	changeview();
	//}
}
int main(int argc, char* argv[])
{
	createbk();
	glutInit(&argc, argv);//初始化glut 
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	glutInitWindowPosition(100, 100);//1显示窗口左上角初始位置
	glutInitWindowSize(640, 480);//显示窗口大小
	glutCreateWindow("Avalanch");// 显示窗口的名称   
	Init();
	glutReshapeFunc(Reshape);
	//if (startf == 0)
	//{
	//glutDisplayFunc(start);
	//}
	glutDisplayFunc(myDisplayGra);
	glutTimerFunc(50, mytimefunc, 1);
	glutKeyboardFunc(keyboard);
	glutMainLoop();//glutMainLoop进入GLUT事件处理循环模式
	return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值