admin 管理员组

文章数量: 887007


2024年1月5日发(作者:silverlight50下载)

#include

#include "resource1.h"

#include

#include

#include

//蛇的结构体

typedef struct SNAKE

{

int x;

int y;

struct SNAKE *pNext;

}Snake;

//豆的结构体

typedef struct BEAN

{

int x;

int y;

struct BEAN *pNext;

}Bean;

//函数原型

void CreateSnake(Snake **pSnake);

void deleteAll(Snake **pSnake);

void ShowSnake(Snake *pSnake);

void CreateBean(Bean **pBean,Snake *pSnake);

void ShowBean(Bean *pBean);

void DeleteBean(Bean **pBean);

void SnakeRun(Snake **pSnake);

void SnakeGrowUp(Snake *pSnake);

int CanEatBean(Snake *pSnake,Bean *pBean);

int CanDie(Snake *pSnake);

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

char g_ClassName[20] = "ClassName";

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

HBITMAP hBitMap;

1 / 11

int CALLBACK WinMain(HINSTANCE hInstance,

HINSTANCE hPreInstance,

LPSTR pCmdLine,

int nCmdShow)

{

HBRUSH hBrush = CreateSolidBrush(RGB(100,100,410));

HICON hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_wnd_icon));

HICON hIconsm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_wnd_iconsm));

hBitMap = LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_wdnbitmap));

//设计类

WNDCLASSEX wndClass;

xtra = NULL;

= sizeof(wndClass);

xtra = NULL;

kground = hBrush;

r = LoadCursor(hInstance,IDC_ARROW);

= hIcon;

m = hIconsm;

nce = hInstance;

dProc = WndProc;

assName = g_ClassName;

= CS_HREDRAW|CS_VREDRAW;

nuName = NULL;

//注册

if (!RegisterClassEx(&wndClass))

{

}

MessageBox(NULL,"注册失败","消息",MB_OK);

//创建

HWND hwnd = CreateWindow(g_ClassName,

"贪吃蛇",

WS_OVERLAPPEDWINDOW,

200,

50,

600+16,

600+38,

NULL,

NULL,

2 / 11

hInstance,

NULL);

if (!hwnd)

{

}

MessageBox(NULL,"创建失败!","消息",MB_OK);

//显示

ShowWindow(hwnd,nCmdShow);

//消息循环

MSG msg;

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

{

TranslateMessage(&msg); //翻译

DispatchMessage(&msg); //回调函数

}

return 0;

}

HDC dc = NULL;

Bean *pBean = NULL;

Snake *pSnake = NULL;

int VK = VK_RIGHT;

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

{

switch (nMsg)

{

case WM_CREATE:

{

CreateSnake(&pSnake); //创建蛇

CreateBean(&pBean,pSnake);

dc = GetDC(hwnd);

}

break;

case WM_KEYDOWN:

{

switch (wParam)

{

case VK_RETURN:

//按下后就开始

SetTimer(hwnd,1,150,NULL);

3 / 11

break;

case VK_UP:

VK = VK_UP;

break;

case VK_DOWN:

VK = VK_DOWN;

break;

case VK_LEFT:

VK = VK_LEFT;

break;

case VK_RIGHT:

VK = VK_RIGHT;

break;

case ' ':

KillTimer(hwnd,1);

break;

}

break;

}

case WM_TIMER:

{

/*创建背景*/

RECT rect;

GetWindowRect(hwnd,&rect);

HDC hmeDc = CreateCompatibleDC(dc);

SelectObject(hmeDc,hBitMap);

StretchBlt(dc,0,0,,,hmeDc,0,0,500,600,SRCCOPY);

DeleteDC(hmeDc);

/*背景创建结束*/

//各种函数

if (CanDie(pSnake))

{

KillTimer(hwnd,1);

MessageBox(NULL,"笨蛋,撞到自己身上,我死啦!","消息",MB_OK);

}

if ( CanEatBean(pSnake,pBean) )

{

SnakeGrowUp(pSnake);

4 / 11

}

DeleteBean(&pBean);

CreateBean(&pBean,pSnake);

SnakeRun(&pSnake);

}

else

{

SnakeRun(&pSnake);

}

ShowSnake(pSnake);

ShowBean(pBean);

case WM_PAINT:

{

/*创建背景*/

RECT rect;

GetWindowRect(hwnd,&rect);

HDC hmeDc = CreateCompatibleDC(dc);

SelectObject(hmeDc,hBitMap);

StretchBlt(dc,0,0,,,hmeDc,0,0,500,600,SRCCOPY);

/*背景创建结束*/

}

ShowSnake(pSnake); //显示蛇

ShowBean(pBean); //显示豆

DeleteDC(hmeDc);

break;

case WM_CLOSE:

deleteAll(&pSnake);

DeleteBean(&pBean);

DestroyWindow(hwnd);

break;

case WM_DESTROY:

ReleaseDC(hwnd,dc);

PostQuitMessage(0);

break;

case WM_QUIT:

break;

}

5 / 11

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

}

/////////////////////////////////函数///////////////////////////////////////////////////////////

//创建蛇Create Snake()

void CreateSnake(Snake **pSnake)

{

Snake *u = NULL,

*w = NULL;

int i = 3; //开始的蛇有三截

int x = 0; //蛇头的初始位置

int y = 0; //蛇尾的初始位置

while (i--)

{

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

u->pNext = NULL;

u->x = x;

u->y = y;

x +=20;

if (NULL == *pSnake)

{

*pSnake = u;

}

else

{

w->pNext = u;

}

w = u;

}

/*蛇的三届创建完毕*/

}

//创建豆

void CreateBean(Bean **pBean,Snake *pSnake)

{

Snake *ji;

int x = 0; //随机的位置x坐标

int y = 0; //随机的位置y坐标

srand(time(NULL));

6 / 11

x = ((unsigned)rand()%29)*20;

y = ((unsigned)rand()%29)*20;

ji = pSnake;

while(pSnake)

{

if ((x == pSnake->x) && (y == pSnake->y) )

{

x = ((unsigned)rand()%29)*20;

y = ((unsigned)rand()%29)*20;

pSnake = ji;

}

pSnake = pSnake->pNext;

}

Bean *u;

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

u->pNext = NULL;

u->x = x;

u->y = y;

*pBean = u;

}

//删掉全部蛇

void deleteAll(Snake **pSnake)

{

Snake *del;

while(*pSnake)

{

del = *pSnake;

(*pSnake) = (*pSnake)->pNext;

free(del);

}

}

//删掉豆

void DeleteBean(Bean **pBean)

{

free(*pBean);

*pBean = NULL;

}

//显示蛇

void ShowSnake(Snake *pSnake)

{

while (pSnake)

{

7 / 11

}

}

Rectangle(dc,pSnake->x,pSnake->y,pSnake->x+20,pSnake->y+20);

pSnake = pSnake->pNext;

//显示豆

void ShowBean(Bean *pBean)

{

Rectangle(dc,pBean->x,pBean->y,pBean->x+20,pBean->y+20);

}

//贪吃蛇移动

void SnakeRun(Snake **pSnake)

{

Snake *ji, //记录变化的蛇头

*round; //循环遍历

int x, //蛇头的当前位置x坐标

y; //蛇头的当前位置y坐标 ji = *pSnake;

round = *pSnake;

while (round->pNext)

{

round = round->pNext;

}

x = round->x;

y = round->y;

switch(VK)

{

case VK_UP:

if (y == 0)

{

y = 600;

}

y -=20;

break;

case VK_DOWN:

if (y==600)

{

y = 0;

}

y +=20;

break;

8 / 11

case VK_LEFT:

if (x == 0)

{

x = 600;

}

x -= 20;

break;

case VK_RIGHT:

if (x == 600)

{

x = 0;

}

x +=20;

break;

}

*pSnake = (*pSnake)->pNext;

ji->x = x;

ji->y = y;

ji->pNext = NULL;

round->pNext = ji;

}

//下一步能吃到豆吗? 返回1吃到

int CanEatBean(Snake *pSnake,Bean *pBean)

{

while (pSnake->pNext)

{

pSnake = pSnake->pNext;

}

if ((pSnake->x==pBean->x)&&(pSnake->y==pBean->y))

{

return 1;

}

return 0;

}

//长大

void SnakeGrowUp(Snake *pSnake)

{

Snake *u;

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

u->pNext = NULL;

u->x = pBean->x;

u->y = pBean->y;

while(pSnake->pNext)

{

9 / 11

pSnake = pSnake->pNext;

}

pSnake->pNext = u;

}

//蛇会死吗 返回1会死

int CanDie(Snake *pSnake)

{

Snake *ji = NULL;

int x = 0,

y = 0;

ji = pSnake;

while (pSnake->pNext)

{

pSnake = pSnake->pNext;

}

x = pSnake->x;

y = pSnake->y;

switch(VK)

{

case VK_UP:

y -=20;

break;

case VK_DOWN:

y +=20;

break;

case VK_LEFT:

x -= 20;

break;

case VK_RIGHT:

x +=20;

break;

}

while(ji->pNext)

{

if ((x == ji->x) && (y == ji->y))

{

return 1;

}

ji = ji->pNext;

}

return 0;

}

10 / 11

(范文素材和资料部分来自网络,供参考。可复制、编制,期待你的好评与关注)

11 / 11


本文标签: 位置 创建 函数 期待