龙空技术网

C语言单链表——打造飞机大战

xue婷 1994

前言:

而今小伙伴们对“单链表c语言例题”都比较珍视,小伙伴们都需要分析一些“单链表c语言例题”的相关知识。那么小编也在网摘上收集了一些有关“单链表c语言例题””的相关知识,希望你们能喜欢,姐妹们快快来学习一下吧!

昨天写了一篇数据结构的知识点,写了一些单链表的知识,今天的话就把单链表知识点运用到项目里面,写一个飞机大战的游戏,只是由于图片素材没有找的那么美观,效果图就没那么好看了额,感觉还不错很好玩,只是的话代码挺多的呢,我就暂时发一部分了,这个的话就抱歉了,就暂时发到,写到子弹的代码吧;

////////////////////////

//包含头文件

//

#include <windows.h>

#include <time.h>

#include "resource.h"

#include <stdlib.h>

#include <stdio.h>

////////////////////////

//包含库文件

//

#pragma comment (lib,"winmm.lib") //要使得我们的游戏有音乐 PlaySound()

#pragma comment(lib,"Msimg32.lib")

/////////////////////////

//宏定义

//

#define TIME_DIREN101 //定义定时器

#define TIME_DIRENMOVE102

#define TIME_ZIDAN103

#define TIME_ZIDANMOVE105

#define TIME_DIRENRELEASE104

typedef struct Node //敌人 自己 子弹

{

int x;

int y;

struct Node *pnext;

}DiRen,FeiJi,ZiDan;

DiRen *pDiRen = NULL;

ZiDan *pZiDan = NULL;

FeiJi *pFeiJi = NULL;

static int score = 0;

static wchar_t nScore[20];

intg_iBGOffset = 0;

void ZaoDiRen();//做敌人

void ShowDiRen(struct Node *pHead, HWND hWnd);//显示敌人

void ZaoZiJi(HWND hWnd);//显示自己

void ZaoZiDan();

void ShowZiDan(ZiDan *pHead, HWND hWnd);//显示子弹

void DiRenMove(DiRen *pHead);

void ZiDanMove(ZiDan *pHead);

void ReleaseZiDan(ZiDan **pHead);

void ReleaseDiRen(DiRen **pHead);

void Shoot(HWND hWnd, FeiJi *ziji, DiRen **diren, ZiDan **zidan);

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

////////////////////////

//WinMain()函数

//

//windows应用程序的入口,我们的程序就从这里开始

//

int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

//创建窗口4步:1、设计完整的窗口类 窗口的样子

WNDCLASSEX wndClass= { 0 };

wndClass.style= CS_HREDRAW|CS_VREDRAW|CS_OWNDC;

wndClass.cbClsExtra=0;

wndClass.cbWndExtra=0;

wndClass.hbrBackground=(HBRUSH)5;

wndClass.hInstance=hInstance;

wndClass.hCursor=NULL;

wndClass.hIcon=NULL;

wndClass.lpfnWndProc=WndProc;

wndClass.lpszClassName=L"AirPlane";

wndClass.lpszMenuName=NULL;

wndClass.hIconSm=NULL;

wndClass.cbSize = sizeof(WNDCLASSEX);

//创建窗口4步:2、注册窗口

if (!RegisterClassEx(&wndClass))

return -1;

//创建窗口4步:3、创建窗口

HWND hWnd = CreateWindow(L"AirPlane", L"雷霆战机",

WS_OVERLAPPEDWINDOW,100, 100, 600, 600, NULL,

NULL, hInstance, NULL);

//创建窗口4步:4、移动、显示、更新窗口

//MoveWindow();

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

PlaySound(L"The Mass .wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);//循环播放我们的背景音乐

//消息循环

MSG msg = { 0 };

while (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);//把键盘的虚拟值翻译成字符消息

DispatchMessage(&msg);//把消息传给系统 由系统来调用过程函数

}

return 0;

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

int i = 1;

int jscore;

HDChdc;

HDCmemdc;

HBITMAP hBitmap;

//BITMAPbBitmap;

switch (message)

{

case WM_TIMER:

hdc = GetDC(hwnd); //得到设备句柄

hBitmap = (HBITMAP)LoadImage(NULL, L"MAP_600X600.bmp", IMAGE_BITMAP, 600, 600, LR_LOADFROMFILE); //敌机

//hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));//载入背景位图

//GetObject(hBitmap, sizeof(bBitmap), &bBitmap);

memdc = CreateCompatibleDC(hdc);

SelectObject(memdc, hBitmap);

//BitBlt(hdc, 0, 0, 600, 600, memdc, 0, 0, SRCCOPY);

//BitBlt(hdc, 0, 0, 600, g_iBGOffset, memdc, 600 - g_iBGOffset, 0, SRCCOPY);

//BitBlt(hdc, 0, g_iBGOffset, 600, 600 - g_iBGOffset, memdc, 0, 0, SRCCOPY);

BitBlt(hdc, 0, 0, 600, g_iBGOffset, memdc, 0, 600 - g_iBGOffset, SRCCOPY);

BitBlt(hdc, 0, g_iBGOffset, 600, 600 - g_iBGOffset, memdc, 0, 0, SRCCOPY);

g_iBGOffset += 1; //让背景滚动量+5

if (g_iBGOffset == 600)//如果背景滚动量达到了背景宽度值,就置零

g_iBGOffset = 0;

wsprintf(nScore,L"%d",score);

jscore = score;

while ((jscore = jscore / 10) > 0)

i++;

TextOut(hdc, 0, 0, L"分数:",4);

TextOut(hdc, 30, 0, nScore, i);

DeleteDC(memdc);

ReleaseDC(hwnd,hdc);

DeleteObject(hBitmap);

ZaoZiJi(hwnd);

if (TIME_ZIDAN == wParam)

{

ZiDanMove(pZiDan);

ReleaseZiDan(&pZiDan);

}

else if (TIME_DIREN == wParam)

{

ZaoDiRen();

}

else if (TIME_DIRENRELEASE == wParam)

{

ReleaseDiRen(&pDiRen);

}

ShowDiRen(pDiRen, hwnd);

DiRenMove(pDiRen);

ShowZiDan(pZiDan, hwnd);

Shoot(hwnd, pFeiJi, &pDiRen, &pZiDan);

break;

case WM_CREATE:

srand( unsigned(time(NULL)));

pFeiJi = (struct Node*)malloc(sizeof(struct Node));

pFeiJi->x = 200;

pFeiJi->y = 500;

SetTimer(hwnd, TIME_DIREN, 200, NULL);

SetTimer(hwnd, TIME_DIRENMOVE, 200, NULL);

SetTimer(hwnd, TIME_ZIDAN, 100, NULL);

SetTimer(hwnd, TIME_ZIDANMOVE, 100, NULL);

SetTimer(hwnd, TIME_DIRENRELEASE, 300, NULL);

break;

//case WM_SYSKEYDOWN

case WM_KEYDOWN:

switch (wParam)

{

case VK_LEFT:

if (pFeiJi->x > 0)

pFeiJi->x -= 20;

break;

case VK_RIGHT:

if (pFeiJi->x < 550)

pFeiJi->x += 20;

break;

case VK_UP:

if (pFeiJi->y>0)

pFeiJi->y -= 20;

break;

case VK_DOWN:

if (pFeiJi->y < 550)

pFeiJi->y += 20;

break;

case VK_SPACE:

ZaoZiDan();

break;

case VK_ESCAPE:

DestroyWindow(hwnd);

break;

}

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

}

return DefWindowProc(hwnd, message, wParam, lParam);

}

/////////////////////////

//做敌人

//

void ZaoDiRen()

{

DiRen *u;

u = (struct Node*)malloc(sizeof(struct Node));

u->x = rand() % 550; //敌人随机出现的坐标

u->y = -10;

u->pnext = NULL;

if (NULL == pDiRen)

{

pDiRen = u;

}

else

{

u->pnext = pDiRen;

pDiRen = u;

}

}

/////////////////////////

//显示敌人

//

void ShowDiRen(struct Node *pHead, HWND hWnd)

{

HDC hdc;

HDC memdc;

HBITMAP hBitmap;

//BITMAP bBitmap;

hdc = GetDC(hWnd); //得到设备句柄

hBitmap = (HBITMAP)LoadImage(NULL, L"DIJI_50X50.bmp", IMAGE_BITMAP, 50, 50, LR_LOADFROMFILE); //敌机

//hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2));//载入背景位图

//GetObject(hBitmap, sizeof(bBitmap), &bBitmap);

memdc = CreateCompatibleDC(hdc);

SelectObject(memdc, hBitmap);

while (pHead != NULL)

{

//透明遮罩法

//BitBlt(hdc, pHead->x, pHead->y, 40, 40, memdc, 40, 0, SRCAND); //add

//BitBlt(hdc, pHead->x, pHead->y, 40, 40, memdc, 0, 0, SRCPAINT); //or

TransparentBlt(hdc, pHead->x, pHead->y, 50, 50, memdc, 0, 0, 50, 50, RGB(0, 0, 0));

pHead = pHead->pnext;

}

DeleteDC(memdc);

ReleaseDC(hWnd, hdc);

DeleteObject(hBitmap);

}

/////////////////////////

//ziji

//

void ZaoZiJi(HWND hWnd) //自己

{

HDC hdc;

HDC memdc;

HBITMAP hBitmap;

//BITMAP bBitmap;

hdc = GetDC(hWnd);//得到设备句柄

hBitmap = (HBITMAP)LoadImage(NULL, L"ZIJI_60X60.bmp",IMAGE_BITMAP,60,60, LR_LOADFROMFILE); //自己

//hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP3));//载入背景位图

//GetObject(hBitmap, sizeof(bBitmap), &bBitmap);

memdc = CreateCompatibleDC(hdc);

SelectObject(memdc, hBitmap);

////透明遮罩法

//BitBlt(hdc, pFeiJi->x, pFeiJi->y, 40, 40, memdc, 40, 0, SRCAND);

//BitBlt(hdc, pFeiJi->x, pFeiJi->y, 40, 40, memdc, 0, 0, SRCPAINT);

TransparentBlt(hdc, pFeiJi->x, pFeiJi->y, 60, 60, memdc, 0, 0, 60, 60, RGB(0, 0, 0));

DeleteDC(memdc);

ReleaseDC(hWnd, hdc);

DeleteObject(hBitmap);

}

void ZaoZiDan() //子弹

{

ZiDan *u;

u = (ZiDan*)malloc(sizeof(ZiDan));

u->x = pFeiJi->x; //敌人随机出现的坐标

u->y = pFeiJi->y+5;

u->pnext = NULL;

if (NULL == pZiDan)

{

pZiDan = u;

}

else

{

u->pnext = pZiDan;

pZiDan = u;

}

}

最终的效果图发一下(需要素材图片的话可以加群:466572167)

然后是,用空格键发射子弹

涉及到的知识点,自然也不止一点点啦,知识点是:win32基本框架 单链表 GDI编程。其实学好语法,知识点只是次要的,要把思路理清,编程思维是很重要的哈,对哦,差点忘了最后还有一部分代码没发,需要的话,可以加下群466572167,群里有完整的,在群里可以探讨交流学习,也很晚了,小编暂时说到这,各位晚安。

标签: #单链表c语言例题