#include <windows.h>
#include "stdio.h"
#include "stdlib.h"
#include "dos.h"
#include<time.h>
#define LEFT VK_LEFT
#define RIGHT VK_RIGHT
#define DOWN VK_DOWN
#define UP VK_UP
#define WM_EEE WM_USER+10
#pragma comment(lib,"gdi32.lib")
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_GLOBALCLASS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
WS_EX_TOOLWINDOW, /* Extended possibilites for variation */
szClassName, /* Classname */
"SNAKE", /* Title Text */
WS_SYSMENU, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
540, /* The programs width */
380, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
typedef struct snake
{
POINT node[1000];
int num;
int direct;
int life;
}snk;
snk a={{{80,60},{60,60}},2,RIGHT,1};
void rect(HDC hdc,POINT p,int a)
{
Rectangle(hdc,p.x,p.y,p.x+a,p.y+a);
}
void drawsnk(HDC hdc)
{
int i;
for(i=0;i<a.num;i++)
rect(hdc,a.node[i],19);
}
int ismax(POINT a,POINT b,POINT c,int l)
{
if((a.x<b.x)||(a.x>c.x)||(a.y>c.y)||(a.y<b.y))
return 1;
else return 0;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static HPEN hpen=CreatePen(PS_SOLID,4,RGB(200,0,10)),hpen1=CreatePen(PS_SOLID,4,RGB(0,200,0)),hpen2=CreatePen(PS_SOLID,4,RGB(0,200,200));
static HPEN hpenw=CreatePen(PS_SOLID,4,RGB(255,255,255));
static int x=0,y=0,score=0,rank=1;
static POINT bound1={0,20},bound2;
static POINT food={200,200};
char sco[50];
static int intv=200;
static POINT era;
static int yy;
int i;
srand(time(NULL));
switch (message) /* handle the messages */
{
case WM_CREATE:
SetTimer(hwnd,1,intv,NULL);
break;
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_SIZE:
bound2.x=LOWORD(lParam);
bound2.y=HIWORD(lParam);
x=bound2.x/2;y=bound2.y/2;
break;
case WM_TIMER:
era=a.node[a.num-1];
for(i=a.num-1;i>=1;i--)
a.node[i]=a.node[i-1];
switch(a.direct)
{
case UP:
a.node[0].y-=20;break;
case DOWN:
a.node[0].y+=20;break;
case LEFT:
a.node[0].x-=20;break;
case RIGHT:
a.node[0].x+=20;break;
}
PostMessage(hwnd,WM_EEE,0,0);
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_EEE:
yy=0;
hdc=GetDC(hwnd);
TextOut(hdc,yy+=20,0,"s",1);
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
if(a.node[0].x==food.x&&a.node[0].y==food.y)
{
a.num++;
a.node[a.num-1]=a.node[a.num-2];
s:
food.x=rand()%(bound2.x/20)*20;
food.y=(rand()%((bound2.y-20)/20))*20+20;
for(int i=0;i<a.num;i++)
if(food.x==a.node[i].x&&food.y==a.node[i].y)
goto s;
score+=10;
if(score%100==0)
{
intv-=30;
KillTimer(hwnd,1);
SetTimer(hwnd,1,intv,NULL);
rank++;
}
}
for(i=1;i<=a.num-1;i++)
{
if((a.node[0].x==a.node[i].x&&a.node[0].y==a.node[i].y)||ismax(a.node[0],bound1,bound2,19))
a.life=0;
}
if(a.life==1)
{
wsprintf(sco,"SCORE: %5d LV: %d",score,rank);
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,20,0,sco,strlen(sco));
SelectObject(hdc,hpen2);
MoveToEx(hdc,0,16,NULL);
LineTo(hdc,bound2.x,16);
SelectObject(hdc,hpen1);
rect(hdc,food,17);
SelectObject(hdc,hpen);
rect(hdc,a.node[0],17);
SelectObject(hdc,hpenw);
rect(hdc,era,17);
EndPaint(hwnd,&ps);
}
else
{
if(MessageBox(NULL,"a","b",0)==IDOK);
PostQuitMessage (0);
}
break;
case WM_KEYDOWN:
if((wParam+a.direct)!=54&&(wParam+a.direct)!=52)
{
switch(wParam)
{
case VK_UP:
if(a.direct!=UP&&a.direct!=DOWN)
a.direct=UP;
break;
c语言的贪吃蛇游戏,windows api实现
5星 · 超过95%的资源 需积分: 10 31 浏览量
2011-09-26
20:07:09
上传
评论 5
收藏 139KB RAR 举报
JinCraft
- 粉丝: 6
- 资源: 20
最新资源
- 111111111111111111
- Screenshot_2024-04-30-21-47-24-26.jpg
- Cpp1.cpp1111111111
- 利用ERP流程操作的整个订单流程.ppt
- 最新二开版本源码博客论坛源码,UI很漂亮,可切换皮肤界面.rar
- ModStartBlog现代化个人博客系统 v5.2.0源码.rar
- 带posix库的mingw编译器
- SoraAI是一款功能强大的AI助手,由OpenAI开发,以其出色的语音识别技术、广泛的知识库和高度的人工智能特性而备受瞩目
- Thinkphp开发大气响应式个人博客青春博客网站源码.rar
- 最新PHP博客网站程序源码 ThinkPHP.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页