#include<windows.h>
#include "resource1.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//蛇的结构体
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;
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;
wndClass.cbClsExtra = NULL;
wndClass.cbSize = sizeof(wndClass);
wndClass.cbWndExtra = NULL;
wndClass.hbrBackground = hBrush;
wndClass.hCursor = LoadCursor(hInstance,IDC_ARROW);
wndClass.hIcon = hIcon;
wndClass.hIconSm = hIconsm;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WndProc;
wndClass.lpszClassName = g_ClassName;
wndClass.style = CS_HREDRAW|CS_VREDRAW;
wndClass.lpszMenuName = NULL;
//注册
if (!RegisterClassEx(&wndClass))
{
MessageBox(NULL,"注册失败","消息",MB_OK);
}
//创建
HWND hwnd = CreateWindow(g_ClassName,
"贪吃蛇",
WS_OVERLAPPEDWINDOW,
200,
50,
600+16,
600+38,
NULL,
NULL,
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);
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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论