使用graphics.h
// 依賴頭檔案
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
// 音樂播放所需庫
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
// 視窗大小定義
#define W 600
#define H 600
// 地方戰斗機鏈表節點
struct enemy
{
int x;
int y;
int t;
struct enemy *next;
};
// 我方戰斗機結構
struct plane
{
int x;
int y;
};
// 子彈鏈表節點
struct zidan
{
int x;
int y;
struct zidan *next;
};
// 方向按鍵 Key值
enum Direction
{
up = 72, down = 80, left = 75, right = 77
}direct;
// 函式宣告
void CreatEnemy();
void createzidan();
void showenemy(struct enemy *penemy);
void moveenemy(struct enemy *penemy);
void showzidan();
void movezidan();
void destroy();
int chk();
// 全域變數宣告
IMAGE img1,img2,img3,img;
int w1,h1,w2,h2,h,w,h3,w3;
struct enemy *pEnemy=NULL;
struct zidan *pzidan=NULL;
struct plane* ple;
char mark[20];
// 分數初始化為0
int markk=0;
// 幀數初始化
int r=0;
// 分數顯示區域
RECT rr = {500, 0, 600, 30};
// 主程式
void main()
{
// 創建我方戰斗機
ple=(struct plane*)malloc(sizeof(struct plane));
// 設定主戰斗機初始坐標
ple->x=275;
ple->y=450;
// 初始化視窗,設定寬度與高度
initgraph(W,H);
// 加載背景音樂
mciSendString("play Era.mp3 repeat",0,0,0);
// 加載背景圖片
loadimage(NULL,"MAP_600X600.bmp");
// 加載敵機圖片至 img1 img2
loadimage(&img1, _T("321.bmp"));
loadimage(&img2, _T("DIJI_100X37.bmp"));
// 我方戰斗機圖片 img
loadimage(&img, _T("ZIJI600X300.bmp"));
// 加載子彈圖片 至img
loadimage(&img3, _T("ZIDAN_120X25.bmp"));
// 獲取圖片長寬大小
w1 = img1.getwidth();
h1 = img1.getheight();
w2 = img2.getwidth();
h2 = img2.getheight();
w3 = img3.getwidth();
h3 = img3.getheight();
w = img.getwidth();
h = img.getheight();
// 設定字體顏色
settextcolor(RED);
//設定字體大小與內容
settextstyle(16, 0, _T("����"));
//進入訊息回圈
while(1)
{
// 檢測是否按鍵
while(kbhit())
{
// 獲取按鍵key值 (上下左右)
switch(getch())
{
case up:
if(ple->y-20>0)
ple->y-=20;break;
case down:
if(ple->y+57<600)
ple->y+=20;break;
case right:
if(ple->x+95<600)
ple->x+=20;break;
case left:
if(ple->x-20>0)
ple->x-=20;break;
}
}
// // 每 10 幀創建一架敵機
if(r%20==0)
{
CreatEnemy();
moveenemy(pEnemy);
}
// 每 10 幀創建一顆子彈
if(r%10==0)
{
createzidan();
movezidan();
}
// 更新幀數
r++;
// 初始化幀數
if(r==1000) {
r=0;
}
// 重新加載地圖
loadimage(NULL,"MAP_600X600.bmp");
// 顯示戰斗機
showenemy(pEnemy);
// 設定戰斗機圖片放置的位置
putimage(ple->x,ple->y,w/2,h,&img,w/2,0,SRCCOPY);
// 顯示子彈
showzidan();
// 在螢屏上列印分數
drawtext(_T(mark), &rr, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
// 休眠 30 ms
Sleep(30);
// 銷毀廢棄子彈與敵機
destroy();
}
system("pause");
closegraph();
}
// 創建敵機
void CreatEnemy()
{
struct enemy *pnew;
pnew=(struct enemy*)malloc(sizeof(struct enemy));
pnew->x=rand()%550;
pnew->y=0;
pnew->t=rand()%2;
pnew->next=NULL;
if(pEnemy==NULL)
pEnemy=pnew;
else
{
pnew->next=pEnemy;
pEnemy=pnew;
}
}
// 顯示敵機
void showenemy(struct enemy *penemy)
{
struct enemy *p=penemy;
while(p!=NULL)
{
if(p->t==1)
putimage(p->x,p->y,w1/2,h1,&img1,w1/2,0,SRCCOPY);
else
putimage(p->x,p->y,w2/2,h2,&img2,w2/2,0,SRCCOPY);
p=p->next;
}
}
// 移動敵機
void moveenemy(struct enemy *penemy)
{
struct enemy *p=penemy;
while(p!=NULL)
{
p->y=p->y+37;
p=p->next;
}
}
// 創建子彈
void createzidan()
{
struct zidan *pnew;
pnew=(struct zidan*)malloc(sizeof(struct zidan));
pnew->x=ple->x;
pnew->y=ple->y-20;
pnew->next=NULL;
if(pzidan==NULL)
pzidan=pnew;
else
{
pnew->next=pzidan;
pzidan=pnew;
}
}
// 顯示子彈
void showzidan()
{
struct zidan *q=pzidan;
while(q!=NULL)
{
putimage(q->x,q->y,w3/2,h3,&img3,0,0,SRCCOPY);
q=q->next;
}
}
// 移動子彈
void movezidan()
{
struct zidan *p=pzidan;
while(p!=NULL)
{
p->y=p->y-30;
p=p->next;
}
}
// 銷毀子彈 (遍歷子彈鏈表,清除坐標不在區域內的子彈)
void destroy()
{
struct enemy *a,*b;
struct zidan *c,*d;
a=b=pEnemy;
while(a!=NULL)
{
c=d=pzidan;
while(c!=NULL)
{
if(c->x<(a->x+50)&&c->x>(a->x-75)&&c->y<(a->y+37)&&c->y>(a->y-30))
{
d->next=c->next;
if (c!=pzidan)
free(c);
c=d;
b->next=a->next;
if (a != pEnemy)
free(a);
a=b;
markk++;
itoa(markk,mark,10);
}
d=c;
c=c->next;
}
b=a;
if (a!=NULL)
a=a->next;
}
a = b = pEnemy;
while (a != NULL)
{
if (a->y > 600)
{
b->next = a->next;
if (a != pEnemy)
free(a);
a = b;
}
b = a;
a = a->next;
}
c = d = pzidan;
while (c != NULL)
{
if (c->y > 600)
{
d->next = c->next;
if (c != pzidan)
free(c);
c = d;
}
d = c;
c = d->next;
}
}

完整資源 見:
https://download.csdn.net/download/qq_37746978/14503377
后續更新,歡迎評論區交流
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/249946.html
標籤:其他
上一篇:C語言三子棋
下一篇:spike
