Windows程序设计 贪吃蛇c

Windows程序设计 贪吃蛇c,第1张

Windows程序设计 贪吃蛇c

看Windows程序有段时间了,终于动手写东西。


贪吃蛇算是一个开始吧,下面的贪吃蛇很简单,也有很多地方需要修改,还有情况没有考虑QAQ 但这不是我的目的了。






思路很简单:建个链表储存蛇身节点即可。


#include <windows.h>
#include <time.h> #define ID_TIMER 1
#define TIMERSET 600
char score,temp=;
int flag,tempx,tempy,
foodx,foody,key;//运动方向,食物坐标,标记
bool havebody[][];
bool over = false; struct Snake
{
Snake *next;
int x;
int y;
};
struct Snake *head;
struct Snake *creat()//链表初始化
{
struct Snake *p1,*p2,*p3;
head=(struct Snake*)malloc(sizeof(Snake));
p1=(struct Snake*)malloc(sizeof(Snake));
p2=(struct Snake*)malloc(sizeof(Snake));
p3=(struct Snake*)malloc(sizeof(Snake)); head->x=;head->y=;
head->next=p1;
p1->x=;p1->y=;
p1->next=p2;
p2->x=;p2->y=;
p2->next=p3;
p3->x=;p3->y=;
p3->next=NULL;
return head;
}; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
TCHAR szAppName[] = TEXT("dy");
WNDCLASS wndcls;
wndcls.cbClsExtra = ;
wndcls.cbWndExtra = ;
wndcls.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndcls.hCursor = LoadCursor(hInstance, IDC_ARROW);
wndcls.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WndProc;
wndcls.lpszClassName = szAppName;
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); HWND hwnd = CreateWindow (szAppName, TEXT ("dyww"),
WS_OVERLAPPEDWINDOW,
, ,
, ,
NULL, NULL, hInstance, NULL) ; ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd); MSG msg;
while(GetMessage(&msg, NULL, , ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} void food(HWND hwnd)//食物
{
foodx = rand() % , foody = rand() %;
while(havebody[foodx][foody])
{
foodx = rand() % , foody = rand() % ;
}
havebody[foodx][foody] = true;
InvalidateRect(hwnd, NULL, TRUE);
} void checkdeath()//检查是否死亡
{
Snake *p= head,*end;
do
{
p=p->next;
}while(p->next!=NULL);
end=p;
if(p->x <||p->x > || p->y < || p->y >)
over=true;
p=head->next;
do
{
if(p->x==end->x&&p->y==end->y){over=true;}
p=p->next;
}while(p->next!=NULL);
}
bool havefood(Snake *p)
{
if(foodx==p->x&&foody==p->y)
return true;
else
return false;
} struct Snake *insert(int x,int y)//插入节点
{
struct Snake *p1,*p;
p=(struct Snake*)malloc(sizeof(Snake));
p1=head;
p->x=head->x;p->y=head->y;
p->next=head->next;
head->x=tempx;head->y=tempy;
head->next=p;
havebody[head->x][head->y]=true;
return head;
}; Snake *mainrun(HWND hwnd)
{
struct Snake *p;
p=head;
int temp=,deadfood=;
tempx=p->x;tempy=p->y;//储存头节点的值
do
{
if(p->next!=NULL){
if(temp==){havebody[p->x][p->y]=false; p->x=p->next->x;p->y=p->next->y;havebody[p->x][p->y]=true;temp++;}
else
{p->x=p->next->x;p->y=p->next->y;havebody[p->x][p->y]=true;}}
p=p->next;
}while(p->next!=NULL);
//havebody[p->x][p->y]=false;
if(flag==) p->x--;
if(flag==) p->y--;
if(flag==) p->x++;
if(flag==) p->y++;
if(havefood(p)) {insert(tempx,tempy);deadfood=;score+=;}
checkdeath();
havebody[p->x][p->y]=true;
InvalidateRect(hwnd, NULL, TRUE);
if(deadfood) food(hwnd);
return head;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//窗口过程
{
HDC hdc;
PAINTSTRUCT ps;
TCHAR szScore[]=TEXT("GAME OVER 得分:"),
szBuffer[]; int x, y; switch(message)
{
case WM_CREATE://各种初始化
flag=;key=;score=;
srand(time(NULL));
creat();
food(hwnd);
havebody[foodx][foody]=true;
SetTimer(hwnd, ID_TIMER, TIMERSET, NULL);
return ; case WM_TIMER:
mainrun(hwnd);
if(over)
{
KillTimer(hwnd, ID_TIMER);
InvalidateRect(hwnd, NULL, TRUE);
}
else InvalidateRect(hwnd, NULL, TRUE);
return ; case WM_KEYDOWN:
if(over) return ;
switch(wParam)
{
case VK_UP:
if(flag != )
{
flag = ;
mainrun(hwnd);
}
break;
case VK_DOWN:
if(flag != )
{
flag = ;
mainrun(hwnd);
}
break;
case VK_LEFT:
if(flag != )
{
flag = ;
mainrun(hwnd);
}
break;
case VK_RIGHT:
if(flag != )
{
flag = ;
mainrun(hwnd);
}
break;
default:
break;
}
return ; case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
Rectangle(hdc,, , , );
if(over)
{SelectObject(hdc, GetStockObject(RGB(,,)));Rectangle(hdc,, , , );
TextOut(hdc, , , szScore, lstrlen(szScore));
TextOut(hdc, , , szBuffer, wsprintf(szBuffer, TEXT("%4d"), score));
//TextOut(hdc, 198, 190, szBuffer, wsprintf(szBuffer, TEXT("%4d"), temp));
}
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
for(x = ; x <; x++)
for(y = ; y <; y++)
{ if(havebody[x][y])
Rectangle(hdc,x*,y*,(x+)*,(y+)*);
}
EndPaint (hwnd, &ps) ;
return ; case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage();
return ;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/588687.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存