赞
踩
#include"acllib.h" #include<stdio.h> #include<time.h> #include<stdlib.h> //定义变量 const int winwidth = 640, winheight = 500; ACL_Image airplane, airenemy,airzidan ;//载入图片 ACL_Sound snd1,snd2;//载入音频 const char* a = "airplane.jpeg";//选择图片 const char* b = "enemy.jpg";//选择图片 const char* c = "a.bmp";//选择图片 const int max = 999999; const int max1 = 999999; int num = 0; int num1 = 0; int moveid = 0, movetimerinterval = 40;//定时器 int createid = 1, createtimerinterval = 1000;//定时器 int zidanid1 = 2, zidantimerinterval1 = 20;//定时器 int zidanid2 = 3, zidantimerinterval2 = 700;//定时器 int hp = 5;//初始化血量 int score = 0;//初始化分数 int difficulty = 1; struct sprite { int width, height; int x, y; int distx, disty; }; struct sprite plane, * enemy[max] = { NULL }, * zidan1[max1] = { NULL }, * zidan2[max1] = { NULL };//参数数据结构 typedef struct rect { int x, y; int w, h; }rect; //制定函数 void paint(); void keyEvent(int key, int e); void timeEvent(int id); void fault(); int collision(rect r1, rect r2); int Setup() { srand((unsigned)time(NULL)); initWindow("airplane", DEFAULT, DEFAULT, winwidth, winheight); loadImage(a, &airplane);//载入友机图片 loadImage(b, &airenemy);//载入敌机图片 loadImage(c, &airzidan);//载入子弹图片 loadSound("bumb1.wav", &snd1);//载入爆炸声效 loadSound("bumb2.wav", &snd2);//载入爆炸声效 //定义我方飞机的参数 plane.width = 100; plane.height = 100; plane.distx= 4; plane.x = 250; plane.y = 400; //定义敌方飞机的参数 if (enemy==NULL) { enemy[num]->width = 50; enemy[num]->height = 50; switch (difficulty) { case 1: enemy[num]->disty = 2; break; case 2: enemy[num]->disty = 3; break; case 3: enemy[num]->disty = 4; break; case 4: enemy[num]->disty = 5; break; case 5: enemy[num]->disty = 6; break; default: break; } enemy[num]->x = rand() % (winwidth ); enemy[num]->y = 10; } //定义左子弹的参数 if (zidan1 == NULL) { zidan1[num1]->width = 10; zidan1[num1]->height = 10; zidan1[num1]->disty = 2; zidan1[num1]->x = plane.x+12.5; zidan1[num1]->y = 400; } //定义右子弹的参数 if (zidan2 == NULL) { zidan2[num1]->width = 10; zidan2[num1]->height = 10; zidan2[num1]->disty = 2; zidan2[num1]->x = plane.x + 87.5; zidan2[num1]->y = 400; } registerTimerEvent(timeEvent); //定义定时器 startTimer(moveid, movetimerinterval); startTimer(createid, createtimerinterval); startTimer(zidanid1, zidantimerinterval1); startTimer(zidanid2, zidantimerinterval2); registerKeyboardEvent(keyEvent); paint(); return 0; } //绘制图形 void paint() { if (hp <= 0) { fault(); } else { beginPaint(); clearDevice(); putImageScale(&airplane, plane.x, plane.y, plane.width, plane.height); for (int i = 0; i < num - 1; i++) { if (enemy[i]) putImageScale(&airenemy, enemy[i]->x, enemy[i]->y, enemy[i]->width, enemy[i]->height); } for (int j = 0; j < num1 - 1; j++) { if (zidan1[j]) putImageScale(&airzidan, zidan1[j]->x, zidan1[j]->y, zidan1[j]->width, zidan1[j]->height); } for (int m = 0; m < num1 - 1; m++) { if (zidan2[m]) putImageScale(&airzidan, zidan2[m]->x, zidan2[m]->y, zidan2[m]->width, zidan2[m]->height); } char str1[50]; sprintf_s(str1, "HP:%d", hp); setTextSize(30); paintText(10, 10, str1); char str2[50]; sprintf_s(str2, "score:%d", score); setTextSize(30); paintText(500, 10, str2); char str3[50]; sprintf_s(str3, "difficulty:%d", difficulty); setTextSize(30); paintText(200, 10, str3); } endPaint(); } //键盘控制 void keyEvent(int key, int e) { if (e != KEY_DOWN) return; switch (key) { case VK_LEFT: plane.x -= plane.distx; if (plane.x <= 0) plane.x = 0; break; case VK_RIGHT: plane.x += plane.distx; if (plane.x >= winwidth - plane.width) plane.x = winwidth - plane.width; break; default: break; } paint(); } //自动移动 void timeEvent(int id) { int i,j; if (id == 0) { for (i = 0; i < num; i++) { if (enemy[i]) { enemy[i]->y += enemy[i]->disty; if (enemy[i]->y > plane.y - enemy[i]->height ) { playSound(snd1, 0); hp--; delete(enemy[i]); enemy[i] = NULL; if (hp == 0) fault(); } } } } if (id == 1) { if (num < max) { num++; if (enemy[num] == NULL) { enemy[num] = (struct sprite*)malloc(sizeof(struct sprite)); enemy[num]->width = 50; enemy[num]->height = 50; switch (difficulty) { case 1: enemy[num]->disty = 2; break; case 2: enemy[num]->disty = 3; break; case 3: enemy[num]->disty = 4; break; case 4: enemy[num]->disty = 5; break; case 5: enemy[num]->disty = 6; break; default: break; } enemy[num]->y = 10; enemy[num]->x = rand() % (winheight); } if (enemy[num]->y > plane.y - enemy[num]->height) { playSound(snd1, 0); hp--; delete(enemy[num]); enemy[num] = NULL; if (hp == 0) fault(); } } } if (id == 2) { for (j = 0; j < num1; j++) { if (zidan1[j]) { zidan1[j]->y -= zidan1[j]->disty; } } } if (id == 3) { if (num1 < max1) { num1++; if (zidan1[num1] == NULL) { zidan1[num1] = (struct sprite*)malloc(sizeof(struct sprite)); zidan1[num1]->width = 10; zidan1[num1]->height = 10; zidan1[num1]->disty = 2; zidan1[num1]->y = 400; zidan1[num1]->x = plane.x + 87.5; } } } if (id == 2) { for (j = 0; j < num1; j++) { if (zidan2[j]) { zidan2[j]->y -= zidan2[j]->disty; } } } if (id == 3) { if (num1 < max1) { num1++; if (zidan2[num1] == NULL) { zidan2[num1] = (struct sprite*)malloc(sizeof(struct sprite)); zidan2[num1]->width = 10; zidan2[num1]->height = 10; zidan2[num1]->disty = 2; zidan2[num1]->y = 400; zidan2[num1]->x = plane.x + 12.5; } } } rect r1 = { 0 }, r2 = { 0 }, r3 = { 0 }; for (int i = 0; i < num1; i++) { for (int j = 0; j < num; j++) { if (enemy[j]) { r1.x = enemy[j]->x; r1.y = enemy[j]->y; r1.w = enemy[j]->width; r1.h = enemy[j]->height; } if (zidan1[i]) { r2.x = zidan1[i]->x; r2.y = zidan1[i]->y; r2.w = zidan1[i]->width; r2.h = zidan1[i]->height; int c = collision(r1, r2); if (c) { score++; delete(zidan1[i]); zidan1[i] = NULL; delete(enemy[j]); enemy[j] = NULL; playSound(snd2, 0); if (score % 50 == 0) difficulty++; } } } } for (int i = 0; i < num1; i++) { for (int j = 0; j < num; j++) { if (enemy[j]) { r1.x = enemy[j]->x; r1.y = enemy[j]->y; r1.w = enemy[j]->width; r1.h = enemy[j]->height; } if (zidan2[i]) { r3.x = zidan2[i]->x; r3.y = zidan2[i]->y; r3.w = zidan2[i]->width; r3.h = zidan2[i]->height; int d = collision(r1, r3); if (d) { score++; delete(zidan2[i]); zidan2[i] = NULL; delete(enemy[j]); enemy[j] = NULL; playSound(snd2, 0); if (score % 50 == 0) difficulty++; } } } } paint(); } //失败界面 void fault() { beginPaint(); clearDevice(); char str[50]; setTextColor(RED); sprintf_s(str, "Game Over"); setTextSize(100); paintText(90, 200, str); stopSound(snd1); stopSound(snd2); endPaint(); } //碰撞检测 int collision(rect r1, rect r2) { int c = 1; if (r1.x<r2.x && r1.x + r1.w>r2.x) { if (r1.y > r2.y && r2.y + r2.h > r1.y) return c; if (r1.y < r2.y && r1.y + r1.h > r2.y) return c; } else { if (r1.x > r2.x && r2.x + r2.w > r1.x) { if (r1.y > r2.y && r2.y + r2.h > r1.y) return c; if (r1.y < r2.y && r1.y + r1.h > r2.y) return c; } } c = 0; return c; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。