小鱼游啊游
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<math.h>
#include<Windows.h>
#include<stdio.h> //86
#include<GL/glut.h>
const int n = 300; //小鱼游啊游
const double PI = 3.1415926;
int x_0=200, y_0=200;
double x_1, x_2, y_1, y_2;// 输入 700 600 780 650
void Init() {
glClearColor(0.5f, 0.8f, 1.0f, 0.0f); //浅蓝
}
void Reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluOrtho2D(0, (double)w, 0, (double)h);
}
void DDALine(int a1, int b1, int a2, int b2) { //画线
float a, b;
float da, db, k;
da = a2 - a1;
db = b2 - b1;
k = db / da;
b = b1;
a = a1;
if (k <= 1 && k >= -1) {
for (; abs((int)(a - a2)) > 0; a += (a2 - a1) / abs(a2 - a1)) {
glVertex2f(a, b);
b = b + ((a2 - a1) / abs(a2 - a1)) * k;
}
}
else {
for (; abs((int)(b - b2)) > 0; b += (b2 - b1) / abs(b2 - b1)) {
glVertex2f(a, b);
a = a + ((b2 - b1) / abs(b2 - b1)) / k;
}
}
}
void myDisplay(void) {
int direction = 0;
int change = 1;
int count = 0;
double r1 = (x_2 - x_1) / 6;
double r2 = (x_2 - x_1) / 3;
for (int i = 100; i >0; i--) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //渐变背景
glColor3f(0.0f, 0.27f, 0.41f);
glVertex2f(0, 0);
glColor3f(0.0f, 0.27f, 0.41f);
glVertex2f(800, 0);
glColor3f(0.5f, 0.8f, 1.0f);
glVertex2f(800, 700);
glColor3f(0.5f, 0.8f, 1.0f);
glVertex2f(0,700);
glEnd();
glFlush();
glColor3f(0.4f, 0.4f, 0.4f); //石头2 700 600 780 650
glBegin(GL_POLYGON);
glVertex2f(720, 70);
glVertex2f(760, 90);
glVertex2f(800, 55);
glVertex2f(800, 0);
glVertex2f(670, 0);
glVertex2f(695, 40);
glEnd();
glFlush();
//珊瑚
glColor3f(1.0f, 0.25f, 0.2f);
glLineWidth(10);
glBegin(GL_LINES);
glVertex2f(740,0 );
glVertex2f(740, 10);//主根
glVertex2f(740, 10);
glVertex2f(770, 30);//一级分支
glVertex2f(770, 30);
glVertex2f(800, 40);//
glVertex2f(770, 30);
glVertex2f(745, 48);//
glVertex2f(750, 45);
glVertex2f(765, 60);//
glVertex2f(750, 45);
glVertex2f(735, 65);//
glVertex2f(765, 60);
glVertex2f(750, 75);//
glVertex2f(765, 60);
glVertex2f(780, 70);
///700 600 780 650
glVertex2f(740, 10);
glVertex2f(710, 20);//一级分支
glVertex2f(710, 20);
glVertex2f(740, 40);//8:5
glVertex2f(710, 20);
glVertex2f(690, 35);
//
glVertex2f(722, 25);
glVertex2f(730, 50);//
glVertex2f(690, 35);
glVertex2f(710, 55);//
glVertex2f(690, 35);
glVertex2f(670, 45);//#####
glVertex2f(707, 51);//}}}}}
glVertex2f(725, 75);
glVertex2f(710, 55);
glVertex2f(685, 70);//######
glVertex2f(725, 75);
glVertex2f(740, 80);
glVertex2f(727, 72);
glVertex2f(710, 90);//#######
glVertex2f(710, 90);
glVertex2f(730, 100);
glVertex2f(710, 90);
glVertex2f(700, 105);
glVertex2f(685, 70);
glVertex2f(700, 80);
glVertex2f(687, 66);
glVertex2f(668, 90);
glVertex2f(670, 45);
glVertex2f(680, 60);
glVertex2f(670, 45);
glVertex2f(640, 40);//
glEnd();
glFlush();//700 600 780 650
glColor3f(0.0f, 0.4f, 0.2f);水草0
glLineWidth(35);
glBegin(GL_LINE_STRIP);
glVertex2f(10, 0);
for (int i2 = 1; i2 < 13; i2 += 2) {
glVertex2f(-30, 50 * i2 - 20);
glVertex2f(50, 50 * (i2 + 1) - 20);
}
glEnd();
glFlush();
glColor3f(0.0f, 0.8f, 0.0f);水草1
glLineWidth(30);
glBegin(GL_LINE_STRIP);
for (int i2 = 0; i2 < 13; i2+=2) {
glVertex2f(20, 50*i2);
glVertex2f(80, 50*(i2+1));
}
glEnd();
glFlush();
glColor3f(0.0f, 0.4f, 0.2f);水草2
glLineWidth(30);
glBegin(GL_LINE_STRIP);
glVertex2f(90,0 );
for (int i2 = 1; i2 < 12; i2 += 2) {
glVertex2f(50, 50 * i2-20);
glVertex2f(130, 50 * (i2 + 1)-20);
}
glEnd();
glFlush();
glColor3f(0.5f, 0.5f, 0.5f); //石头1 700 600 780 650
glBegin(GL_POLYGON);
glVertex2f(-10, 70);
glVertex2f(80, 90);
glVertex2f(160, 70);
glVertex2f(120, 0);
glVertex2f(-10, 0);
glEnd();
glFlush();
glColor3f(0.0f, 0.9f, 0.2f);水草3
glLineWidth(25);
glBegin(GL_LINE_STRIP);
glVertex2f(100, 0);
for (int i2 = 1; i2 < 16; i2 += 2) {
glVertex2f(90, 40 * i2 - 20);
glVertex2f(190, 40 * (i2 + 1) - 20);
}
glEnd();
glFlush();
glColor3f(0.0f, 0.5f, 0.2f);水草4
glLineWidth(25);
glBegin(GL_LINE_STRIP);
glVertex2f(190, 0);
for (int i2 = 1; i2 < 16; i2 += 2) {
glVertex2f(160, 35 * i2 - 20);
glVertex2f(240, 35 * (i2 + 1) - 20);
}
glEnd();
glFlush();
change = change * ((int)x_1)/abs((int)x_1);
//printf("#@ %d\n", change);//test
if (change < 0) {
x_1 = -x_1;
y_1 = -y_1;
x_2 = -x_2;
y_2 = -y_2;
count++;
//printf("%d\n",change);//test
}
else if (x_1 >= 800) {
x_1 -= 800;
x_2 -= 800;
y_1 -= 800;
y_2 -= 800;
}
if (count % 2 != 0) {
direction = 1;
}
if (direction ==1 ) {
if (i % 2 == 0) {
x_1 = x_1 + i;
x_2 = x_2 + i;
}
else {
y_1 = y_1 + i;
y_2 = y_2 + i;
}
}
else {
if (i % 2 == 0) {
x_1 = x_1 - i;
x_2 = x_2 - i;
}
else {
y_1 = y_1 - i;
y_2 = y_2 - i;
}
}
glPointSize(1);
glColor3f(1.0, 1.0, 0.8);//光线 700 600 780 650
glBegin(GL_POINTS);
for (int j = 700, k = 750; j * k >=0; j -= 30, k -= 30) {
glBegin(GL_POINTS);
DDALine(0, 700, j, k);
glEnd();
glFlush();
}
glEnd();
glFlush();
glLineWidth(3); //鱼 身体
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 0.0f); //点1 嘴
glVertex2f(x_1, (y_1 + y_2) / 2);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex2f((x_1 + x_2) / 2, y_2); //点二 头
glColor3f(1.0f, 0.0f, 1.0f); //点三 尾巴开始处
glVertex2f((3 * x_2) / 2 - x_1 / 2, (y_1 + y_2) / 2);
glColor3f(1.0f, 0.0f, 0.0f); //4 尾巴上尖
//(0.5f, 0.8f, 1.0f, 0.0f);
//glVertex2f((3 * x_2 - x_1) / 2 + (x_2 - x_1) * 1, y_2);
glVertex2f(2*x_2-x_1+( x_2 - x_1) * cos( PI /(3* (i/10))) , (y_2+y_1)/2+(y_2-y_1)/2*cos(PI/(3*(i/10))));
glColor3f(1.0f, 0.0f, 0.0f); //5
//glVertex2f((3 * x_2 - x_1) / 2 + (x_2 - x_1) / 2 * 1, (y_2 + y_1) / 2);
glVertex2f((3 * x_2 - x_1) / 2 + (x_2 - x_1) / 2 * cos(PI / (6 * (i/10))), (y_2 + y_1) / 2);
glColor3f(1.0f, 0.0f, 0.0f); //6 下尖
//glVertex2f((3 * x_2 - x_1) / 2 + (x_2 - x_1) * 1, y_1);
glVertex2f(2 * x_2 - x_1 + (x_2 - x_1) * cos(PI / (3 * (i / 10))), (y_2 + y_1) / 2 + (y_1 - y_2) / 2 * cos(PI / (3 * (i / 10))));
glColor3f(1.0f, 0.0f, 1.0f); // 7 尾巴开始处
glVertex2f(1.5 * x_2 - x_1 / 2, (y_1 + y_2) / 2);
glColor3f(1.0f, 1.0f, 1.0f); //8 肚子
glVertex2f((x_1 + x_2) / 2, y_1);
glEnd();
glFlush();
glBegin(GL_POLYGON);鱼翅上
glColor3f(0.0f, 1.0f, 1.0f);
glVertex2f((x_1 + x_2) / 2, y_2);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(x_2, (5 * y_2 - y_1) / 4);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f((x_2 * 3 + x_1) / 4, (7 * y_2 + y_1) / 8);
glEnd();
glFlush();
glBegin(GL_POLYGON);鱼翅下
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f((x_1 + x_2) / 2, y_1);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(x_2, (5 * y_1 - y_2) / 4);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f((x_2 * 3 + x_1) / 4, (7 * y_1 + y_2) / 8);
glEnd();
glFlush();
glPointSize(6);
glBegin(GL_POINTS);//眼睛
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(x_1 + (x_2 - x_1) / 4, (y_1 + y_2) / 2);
glEnd();
glFlush();
glColor3f(0.6f, 0.6f, 0.6f); //石头3 700 600 780 650
glBegin(GL_POLYGON);
glVertex2f(50, 30);
glVertex2f(70, 60);
glVertex2f(160, 75);
glVertex2f(180, 0);
glVertex2f(30, 0);
glEnd();
glFlush();
if (change < 0) {//泡泡位置
int temp = y_1;
y_1 = y_2;
y_2 = temp;
change = 1;
}
glPointSize(3);
glBegin(GL_POINTS);//泡泡
glColor3f(0.9f, 0.9f, 1.0f);
for (int k = 0; k <= r1; k++) { //小泡泡
for (int j = 0; j < n; j++) {
glVertex2f(x_1 + k * cos(2 * PI / n * j), (3 * y_2 - y_1) / 2 + k * sin(2 * PI / n * j));
}
}
glEnd();
glFlush();
Sleep(500);// 700 600 780 650
glPointSize(3);
glBegin(GL_POINTS);//泡泡
glColor3f(0.9f, 0.9f, 1.0f);
for (int k = 0; k <= r2; k++) { //大泡泡
for (int j = 0; j < n; j++) {
glVertex2f((x_1 + x_2) / 2 + k * cos(2 * PI / n * j), (5 * y_2 - 3 * y_1) / 2 + k * sin(2 * PI / n * j));
}
}
glEnd();
glFlush();
int casual[36]; //一堆泡泡
glPointSize(3);
glBegin(GL_POINTS);//泡泡
glColor3f(0.9f, 0.9f, 1.0f);
srand(time(0));//Time(0)是得到当前时时间值==srand((unsigned)time(NULL));
for (int in = 0; in < 35; in++) {
casual[in] = 20 + rand() % 651; //int a=m+rand()%(n-m+1);
for (int k = 0; k <= 5; k++) { //大泡泡
for (int j = 0; j < n; j++) {
if (in % 2 == 0) {
glVertex2f(casual[in] + k * cos(2 * PI / n * j), casual[in] - in * in * 5 - 2 * in + k * sin(2 * PI / n * j));
}
else if (in % 3 == 0) {
glVertex2f(casual[in] + k * cos(2 * PI / n * j), casual[in] + 30 * in + k * sin(2 * PI / n * j));
}
else if (in % 5 == 0) {
glVertex2f(casual[in] + 13 + k * cos(2 * PI / n * j), casual[in] + 110 + in * in + k * sin(2 * PI / n * j));
}
else {
glVertex2f(casual[in] + k * cos(2 * PI / n * j), casual[in] + 150 + in * in * 4 - in * 9 + k * sin(2 * PI / n * j));
}
}
}
}
glEnd();
glFlush();
Sleep(500);
int casua[36]; //一堆水母
glPointSize(3);//水母
glColor3f(1.0f, 0.75f, 0.8f);
srand(time(0));//Time(0)是得到当前时时间值==srand((unsigned)time(NULL));
for (int in = 0; in < 35; in++) {
casua[in] = 20 + rand() % 651; //int a=m+rand()%(n-m+1);
glBegin(GL_POINTS);
for (int k = 0; k <= 35; k++) { //大泡泡
for (int j = 0; j < n; j++) {
if (in % 2 == 0) {
x_0 = casua[in];
y_0 = casua[i] - in *in* 0.88 - 7 * in + 25;
}
else if (in % 3 == 0) {
y_0 = casua[i] + in * in * 0.3 - 5 * in + 2;
}
else if (in % 5 == 0) {
y_0 = casua[i] - in * in*0.1 + 52;
}
else {
y_0 = casua[i] + 0.75 * in * in - 1 * in;
glVertex2f(x_0 + k * cos(PI / n * j), y_0 + k * sin(PI / n * j));
}
}
}
glEnd();
glFlush();
700 600 780 650
glPointSize(10);
glBegin(GL_LINES); //须
glColor3f(1.0f, 0.75f, 0.8f);
glVertex2f(x_0, y_0 - 5);
glVertex2f(x_0, y_0 - 35);
glVertex2f(x_0 + 35, y_0 - 5);
glVertex2f(x_0 + 35, y_0 - 35);
glVertex2f(x_0 - 35, y_0 - 5);
glVertex2f(x_0 - 35, y_0 - 35);
glEnd();
glFlush();
}
Sleep(500);
//y_0 +=15;
} //for循环
}
int main(int argc, char* argv[]) { //????????
glutInit(&argc, argv); //????
scanf("%lf %lf %lf %lf", &x_1, &y_1, &x_2, &y_2);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 700); ///窗口大小800*700
glutCreateWindow("First");
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}