C语言实现飞机大战小游戏.zip

  • B7_768915
    了解作者
  • 11.5MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-30 02:00
    上传日期
在VS2013平台上利用C语言实现了一个飞机大战小程序,对方敌机三种形态(不同大小、生命值),而且子弹使用量受到限制,其中利用了easyX插件的图形库,需安装easyX插件。
飞机大战.zip
内容介绍
#include <stdio.h> #include <graphics.h> #include <time.h> #include <mmsystem.h> #define _CRT_SECURE_NO_WARNINGS #pragma comment(lib,"winmm.lib") #define WIN_WIDTH 480 #define WIN_HEIGHT 700 #define PLAYER_WIDTH 80 #define PLAYER_HEIGHT 100 #define BULLET_NUM 10 #define ENEMY_NUM 10 #define PLAYER_SPEED 5 #define BULLET_SPEED 6 #define ENEMY_SPEED_SMALL 4 #define ENEMY_SPEED_MEDIUM 3 #define ENEMY_SPEED_BIG 2 #define PLAYER_HP 3 #define SMALL_HP 1 #define MEDIUM_HP 2 #define BIG_HP 3 int bk1 = 0, bk2 = WIN_HEIGHT; struct Image { IMAGE background; IMAGE player, playerY; IMAGE bullet, bulletY; IMAGE enemy1, enemy1Y; IMAGE enemy2, enemy2Y; IMAGE enemy3, enemy3Y; }image; enum TYPE { SAMLL, MEDIUM, BIG, }; struct Plane { int x; int y; bool flag; union { int type; int score; }Myunion; int hp; }player, bullet[BULLET_NUM], enemy[ENEMY_NUM]; DWORD t1, t2, t3, t4; void init(); void drawImage(); void drawText(); void createBullet(); void userKey(); void BulletMove(); void createEnemy(); void enemyMove(); void colliDetec(); void bkRemove(); void playGame(); int main() { srand((unsigned int)time(NULL)); playGame(); getchar(); closegraph(); return 0; } void init() { mciSendString("open BGM.mp3 alias bgm", 0, 0, 0); mciSendString("play bgm repeat", 0, 0, 0); loadimage(&image.background, "background.png"); loadimage(&image.player, "player.png", PLAYER_WIDTH, PLAYER_HEIGHT); loadimage(&image.playerY, "playerY.png", PLAYER_WIDTH, PLAYER_HEIGHT); player.x = WIN_WIDTH / 2 - PLAYER_WIDTH / 2; player.y = WIN_HEIGHT - PLAYER_HEIGHT * 2; player.flag = true; player.hp = PLAYER_HP; player.Myunion.score = 0; loadimage(&image.bullet, "bullet.png"); loadimage(&image.bulletY, "bulletY.png"); for (int i = 0; i < BULLET_NUM; i++) { bullet[i].flag = false; } loadimage(&image.enemy1, "enemy1.png"); loadimage(&image.enemy1Y, "enemy1Y.png"); loadimage(&image.enemy2, "enemy2.png"); loadimage(&image.enemy2Y, "enemy2Y.png"); loadimage(&image.enemy3, "enemy3.png", 100, 153); loadimage(&image.enemy3Y, "enemy3Y.png", 100, 153); for (int i = 0; i < ENEMY_NUM; i++) { enemy[i].flag = false; } t1 = GetTickCount(); t3 = t1; } void drawImage() { putimage(0, bk1, &image.background); putimage(0, bk2, &image.background); putimage(player.x, player.y, &image.playerY, SRCAND); putimage(player.x, player.y, &image.player, SRCPAINT); for (int i = 0; i < BULLET_NUM; i++) { if (bullet[i].flag) { putimage(bullet[i].x, bullet[i].y, &image.bulletY, SRCAND); putimage(bullet[i].x, bullet[i].y, &image.bullet, SRCPAINT); } } for (int i = 0; i < ENEMY_NUM; i++) { if (enemy[i].flag) { if (enemy[i].Myunion.type == SAMLL) { putimage(enemy[i].x, enemy[i].y, &image.enemy1Y, SRCAND); putimage(enemy[i].x, enemy[i].y, &image.enemy1, SRCPAINT); } else if (enemy[i].Myunion.type == MEDIUM) { putimage(enemy[i].x, enemy[i].y, &image.enemy2Y, SRCAND); putimage(enemy[i].x, enemy[i].y, &image.enemy2, SRCPAINT); } else { putimage(enemy[i].x, enemy[i].y, &image.enemy3Y, SRCAND); putimage(enemy[i].x, enemy[i].y, &image.enemy3, SRCPAINT); } } } } void drawText() { char str[20] = ""; sprintf_s(str, "剩余生命值:%d", player.hp); settextstyle(30, 0, "楷体"); settextcolor(BROWN); setbkmode(TRANSPARENT); outtextxy(20, 20, str); sprintf_s(str, "已获分数:%d", player.Myunion.score); outtextxy(WIN_WIDTH / 2 + 20, 20, str); } void createBullet() { for (int i = 0; i < BULLET_NUM; i++) { if (!bullet[i].flag) { bullet[i].flag = true; bullet[i].x = player.x + PLAYER_WIDTH / 2 - 2; bullet[i].y = player.y - 11; break; } } } DWORD _stdcall playMusic(LPVOID lpParameter) { mciSendString("open Fire_1.mp3 alias fire", 0, 0, 0); mciSendString("play fire wait", 0, 0, 0); mciSendString("close fire", 0, 0, 0); return 0; } void userKey() { t2 = GetTickCount(); if (GetAsyncKeyState(VK_UP)) { player.y -= PLAYER_SPEED; if (player.y < 0) { player.y = 0; } } else if (GetAsyncKeyState(VK_DOWN)) { player.y += PLAYER_SPEED; if (player.y>WIN_HEIGHT - PLAYER_HEIGHT) { player.y = WIN_HEIGHT - PLAYER_HEIGHT; } } else if (GetAsyncKeyState(VK_LEFT)) { player.x -= PLAYER_SPEED; if (player.x < -PLAYER_WIDTH / 2) { player.x = -PLAYER_WIDTH / 2; } } else if (GetAsyncKeyState(VK_RIGHT)) { player.x += PLAYER_SPEED; if (player.x> WIN_WIDTH - PLAYER_WIDTH / 2) { player.x = WIN_WIDTH - PLAYER_WIDTH / 2; } } if (GetAsyncKeyState(VK_SPACE) && t2 - t1>300) { createBullet(); HANDLE hThread = CreateThread(NULL, 0, playMusic, NULL, NULL, NULL); t1 = t2; } } void BulletMove() { for (int i = 0; i < BULLET_NUM; i++) { if (bullet[i].flag) { bullet[i].y -= BULLET_SPEED; if (bullet[i].y <= -11) { bullet[i].flag = false; } } } } void createEnemy() { t4 = GetTickCount(); if (t4 - t3 > 2000) { for (int i = 0; i < ENEMY_NUM; i++) { if (!enemy[i].flag) { enemy[i].flag = true; int result = rand() % 10; if (result == 0) { enemy[i].Myunion.type = BIG; enemy[i].hp = BIG_HP; enemy[i].x = rand() % (WIN_WIDTH - 100); enemy[i].y = -153; } else if (result == 1 || result == 2) { enemy[i].Myunion.type = MEDIUM; enemy[i].hp = MEDIUM_HP; enemy[i].x = rand() % (WIN_WIDTH - 69); enemy[i].y = -99; } else { enemy[i].Myunion.type = SAMLL; enemy[i].hp = SMALL_HP; enemy[i].x = rand() % (WIN_WIDTH - 57); enemy[i].y = -43; } break; } } t3 = t4; } } void enemyMove() { for (int i = 0; i < ENEMY_NUM; i++) { if (enemy[i].flag) { if (enemy[i].Myunion.type == SAMLL) { enemy[i].y += ENEMY_SPEED_SMALL; } else if (enemy[i].Myunion.type == MEDIUM) { enemy[i].y += ENEMY_SPEED_MEDIUM; } else { enemy[i].y += ENEMY_SPEED_BIG; } if (enemy[i].y >= WIN_HEIGHT) { enemy[i].flag = false; } } } } void colliDetec() { for (int i = 0; i < ENEMY_NUM; i++) { if (enemy[i].flag) { for (int j = 0; j < BULLET_NUM; j++) { if (bullet[j].flag) { if ((enemy[i].y + 43 >= bullet[j].y&&enemy[i].Myunion.type == SAMLL&&enemy[i].x <= bullet[j].x&&enemy[i].x + 57 >= bullet[j].x + 5) || (enemy[i].y + 99 >= bullet[j].y&&enemy[i].Myunion.type == MEDIUM&&enemy[i].x <= bullet[j].x&&enemy[i].x + 69 >= bullet[j].x + 5) || (enemy[i].y + 153 >= bullet[j].y&&enemy[i].Myunion.type == BIG&&enemy[i].x <= bullet[j].x&&enemy[i].x + 100 >= bullet[j].x + 5)) { bullet[j].flag = false; enemy[i].hp--; if (enemy[i].hp == 0) { if (enemy[i].Myunion.type == SAMLL) { player.Myunion.score += 10 * SMALL_HP; } else if (enemy[i].Myunion.type == MEDIUM) { player.Myunion.score += 10 * MEDIUM_HP; } else { player.Myunion.score += 10 * BIG_HP; } enemy[i].flag = false; } } } } if (enemy[i].y <= player.y + PLAYER_HEIGHT&&enemy[i].x <= player.x + PLAYER_WIDTH) { if ((enemy[i].x + 57 >= player.x&&enemy[i].y + 43 >= player.y&&enemy[i].Myunion.type == SAMLL&&enemy[i].x + 57 >= player.x&&enemy[i].x <= player.x + PLAYER_WIDTH) || (enemy[i].x + 69 >= player.x&&enemy[i].y + 99 >= player.y&&enemy[i].Myunion.type == MEDIUM&&enemy[i].x + 69 >= player.x&&enemy[i].x <= player.x + PLAYER_WIDTH) || (enemy[i].x + 100 >= player.x&&enemy[i].y + 153 >= player.y&&enemy[i].Myunion.type == BIG&&enemy[i].x + 100 >= player.x&&enemy[i].x <= player.x + PLAYER_WIDTH)) { player.hp -= enemy[i].hp;
评论
    相关推荐
    • c语言小程序 计算器
      运用从语言编写的实用小程序,在程序中还利用了链表、堆栈、队列等知识,着重处理了算符的优先级。
    • 一些有用的C语言小程序
      一些C语言小程序:输入以秒为单位所表示的时间,试将其换算为几日几时几分几秒;输出a、b两数中的较大值;从键盘输入一个字母,若是大写字母则输出对应的小写字母,若是小写字母则输出对应得大写字母;输入一个年号,...
    • C语言小程序
      浙江省之前二级试卷里的一些小程序,仅供参考
    • c语言小程序代码
      该文件主要是一些c语言小程序代码,适合初学者。。
    • c语言小程序 精选
      一些基础的c语言程序,简单易懂,课直接编译执行,适合程序的初学者。
    • C语言小程序
      新手练手C语言小程序,一些趣味小游戏 王牌10 优秀程序员之路——C开发经验及技巧大汇总.pdf 王牌9 扫雷英雄榜——C常见错误及解决方案.pdf 王牌8 C常见面试题.pdf 王牌7 C程序员面试技巧.pdf 王牌6 全国计算机等级...
    • c语言小程序
      详细完整的c语言小程序开发demo,需要的库都包含了,解压一下加入一下库就可以了。
    • c语言小程序 单词小助手
      词库维护:基于文件进行管理,可以增加、删除和修改单词的中英文词义。 单词预览:系统随机显示一条记录,在屏幕上显示中英文词义。 中英单词背诵 :随机显示中文词汇,用户需输入正确的英文意义才可得分。...
    • C语言小程序
      C语言小程序;#include #include #define uint unsigned int #define uchar unsigned char uchar temp,aa,num;
    • C语言小程序小程序小程序
      C语言小程序小程序