哈哈,刚做完的一个小程序源代码,大家看看,给点意见!

chjshen 2003-08-20 09:46:47
/*
Name: 贪吃蛇
Copyright:
Author: chjshen
Date: 20-08-2003 17:58
Description: 一个贪吃蛇的例程,完成了基本的功能,有很多的BUG,不要见笑呀
*/


#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
//定义一些常用的变量
HWND hwnd_food=NULL,hwnd_snake_jie[16],hwnd;
HINSTANCE hInstance;
int direction=6;//蛇的方向,6为左,8为上,2为下,4为右
int snake_length=3;
//定义蛇的节的结构
struct snake{
HWND hwnd_snake;
int current_x;
int current_y;
int next_x;
int next_y;
};
struct snake snake_jie[16],food;
//定义初始化函数、移动蛇节的函数、随机出现食物的函数、
//越界函数、是否吃到食物、吃食物函数
int init();
int move();
int move_jie();
int rand_food();
int is_out();
int is_eat_food();
int eat_food();
int restart();
//随机出现食物
int rand_food()
{
int x,y;
x=rand()%20;
x=x*20;
y=rand()%20;
y=y*20;
food.current_x=x;
food.current_y=y;
food.next_x =0;
food.next_y =0;
hwnd_food=CreateWindow ("button","",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
x,y, 20, 20,hwnd,(HMENU)snake_length,hInstance,NULL);
food.hwnd_snake=hwnd_food;
hwnd_snake_jie[snake_length]=hwnd_food;
snake_jie[snake_length].current_x=food.current_x;
snake_jie[snake_length].current_y=food.current_y;
snake_jie[snake_length].next_x=food.next_x;
snake_jie[snake_length].next_y=food.next_y;
return 0;
}
//初始化函数
int init()
{
int i,x=20,y=40;
direction=6;

for(i=0;i<3;i++)
{
hwnd_snake_jie[i]=CreateWindow("button","",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
x+20*i,y,20,20,hwnd,(HMENU)i,hInstance,NULL);
snake_jie[i].hwnd_snake=hwnd_snake_jie[i];
snake_jie[i].current_x=x+20*i;
snake_jie[i].current_y=y;
snake_jie[i].next_x =x+20*(i+1);
snake_jie[i].next_y =y;
}
}
//移动蛇节的函数
int move()
{
switch(direction)
{
case 2:
snake_jie[snake_length-1].next_x=snake_jie[snake_length-1].current_x;
snake_jie[snake_length-1].next_y=snake_jie[snake_length-1].current_y+20;
move_jie();
break;
case 4:
snake_jie[snake_length-1].next_x=snake_jie[snake_length-1].current_x-20;
snake_jie[snake_length-1].next_y=snake_jie[snake_length-1].current_y;
move_jie();
break;
case 8:
snake_jie[snake_length-1].next_x=snake_jie[snake_length-1].current_x;
snake_jie[snake_length-1].next_y=snake_jie[snake_length-1].current_y-20;
move_jie();
break;
case 6:
snake_jie[snake_length-1].next_x=snake_jie[snake_length-1].current_x+20;
snake_jie[snake_length-1].next_y=snake_jie[snake_length-1].current_y;
move_jie();
break;
}

return 0;

}
//移动蛇节的中间函数
int move_jie()
{ int i;
for(i=snake_length-1;i>=0;i--)
{
SetWindowPos(hwnd_snake_jie[i],NULL,
snake_jie[i].next_x,
snake_jie[i].next_y,
0,0,SWP_NOSIZE);
snake_jie[i].current_x=snake_jie[i].next_x;
snake_jie[i].current_y=snake_jie[i].next_y;
}
for(i=0;i<snake_length-1;i++)
{
snake_jie[i].next_x=snake_jie[i+1].current_x;
snake_jie[i].next_y=snake_jie[i+1].current_y;
}
}
//越界函数
int is_out()
{
if(snake_jie[snake_length-1].current_x>400
||snake_jie[snake_length-1]. current_y>400
||snake_jie[snake_length-1].current_x<0
||snake_jie[snake_length-1].current_y<0)
{
KillTimer(hwnd,1);
return 1;
}
else
return 0;
}
//是否吃到食物函数
int is_eat_food()
{
if(snake_jie[snake_length-1].next_x==food.current_x
&&snake_jie[snake_length-1].next_y==food.current_y)
return 1;
else
return 0;
}
//吃食物函数
int eat_food()
{
if(snake_length<=15)
{
//nd_snake=hwnd_snake_jie[snake_length];
snake_length=snake_length+1;
snake_jie[snake_length].current_x=food.current_x;
snake_jie[snake_length].current_y=food.current_y;
snake_jie[snake_length].hwnd_snake=food.hwnd_snake;
snake_jie[snake_length].next_x =food.next_x;
snake_jie[snake_length].next_y =food.next_y;
snake_jie[snake_length-1].next_x =food.current_x;
snake_jie[snake_length-1].next_y =food.current_y;

rand_food();
return 1;
}
else
return 0;

}
//重新开始
int restart()
{
int i;
for(i=0;i<snake_length;i++)
{
DestroyWindow(hwnd_snake_jie[i]);
}
}
//////
...全文
41 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
collide 2003-08-23
  • 打赏
  • 举报
回复
希望用DIRECTX写一下,看你的程序,你使用BUTTON窗口,来做蛇节的.
潘李亮 2003-08-22
  • 打赏
  • 举报
回复
呵呵,好习惯是要在平时养成的。小程序的代码你都弄的乱七八糟你还指望大程序能弄好。

我是对事不对人
bopengbopeng 2003-08-22
  • 打赏
  • 举报
回复
个人作品,代码再恶心都没所谓,只要能运行。
wltsuj 2003-08-22
  • 打赏
  • 举报
回复
继续
chinagdh 2003-08-22
  • 打赏
  • 举报
回复
Very GOOD!
chjshen 2003-08-22
  • 打赏
  • 举报
回复
zzwu(未名) DEV-c++,g 一个c++的编译器,个人做的,免费。
谢谢大家的意见,我会努力的!
再提的详细点呀?
chjshen 2003-08-22
  • 打赏
  • 举报
回复
forestassure(今天人) :
可能是你在复制的过程中,哪个地方出了问题,
我的主文件名是main.c
全部是在C下完成的,没有用到c++ 的功能
你也可以试试呀
-----------------------------------------------
-----------------------------------------------
另,我不是学计算机专业的,纯一个爱好者,做这个程序的时候没有参考任何的其他类似程序,所以问题较多,自己想到哪里就写到哪里,我会记住Nhsoft(我不是高手) 的话的,做小程序也要养成好的习惯,尽量把程序写的清晰一些,易读些,真是谢谢大家了.
forestassure 2003-08-22
  • 打赏
  • 举报
回复
请问:
我在vc 6.0下
出错
snake2.obj : error LNK2001: unresolved external symbol "long __stdcall WindowProcedure(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProcedure@@YGJPAUHWND__@@IIJ@Z)
如何纠正?
DarthVader 2003-08-21
  • 打赏
  • 举报
回复
好的 看看先 :)
robinford 2003-08-21
  • 打赏
  • 举报
回复
比如变量定义就基本都不合法。
还要努力呀
robinford 2003-08-21
  • 打赏
  • 举报
回复
呵呵,有点象抄的东西哟:)
不过还是很不错。但是问题很多呀
zzwu 2003-08-21
  • 打赏
  • 举报
回复
请问"Dev-C++"是什么开发工具?
chjshen 2003-08-21
  • 打赏
  • 举报
回复
怎么,没有人看,我顶!
chjshen 2003-08-20
  • 打赏
  • 举报
回复
这是我花了一天的时间在Dev-C++中写的,编译通过了,我想里面肯定有很多的不合理的地方,请大家看看,多提提意见!
chjshen 2003-08-20
  • 打赏
  • 举报
回复
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{

WNDCLASSEX wincl; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
/* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
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) COLOR_BACKGROUND;

/* 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 (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"贪吃蛇例程", /* Title Text */
WS_CAPTION|WS_SYSMENU, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
405, /* The programs width */
405, /* 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);
hInstance=hThisInstance;
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int hwndx=40,hwndy=40;
switch (message)
{
case WM_CREATE:
SetTimer(hwnd,1,300,0);
break;
case WM_SIZE:
hwndx=(LOWORD(lParam));
hwndy=(HIWORD(lParam));
break;
case WM_TIMER:
if(hwnd_food==NULL)
{init();
rand_food();}
//判断是否越界
if(is_out())
{
if(IDNO==MessageBox(NULL,TEXT("失败!是否重新开始?"),TEXT("action"),
MB_YESNO|MB_ICONWARNING))
{
PostQuitMessage(0);
return 0;
}
else
{
SetTimer(hwnd,1,200,NULL);
if(hwnd_food==NULL)
rand_food();
restart();
init();
move();
}
}
else
{
//是否吃到食物;
if(is_eat_food())//yes
eat_food();
else//NO
move();
}
if(snake_length==15)
{
KillTimer(hwnd,1);
if(IDYES==MessageBox(hwnd,TEXT("胜利!退出吗?"),TEXT("action"),MB_YESNO|MB_ICONHAND))
PostQuitMessage(0);
}

break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_DOWN:
direction=2;
break;
case VK_LEFT:
direction=4;
break;
case VK_UP:
direction=8;
break;
case VK_RIGHT:
direction=6;
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}

8,301

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧