贪吃蛇代码(C语言)
更新时间:2023-05-12 11:24:01 阅读量: 实用文档 文档下载
//2010.06.09
//zook0k
///zook0k/
//
//
//qq:83310093
#include <windows.h>
#include <stdio.h>
#include<time.h>
#define C_W 516
#define C_H 548
//#define C_W 1024
//#define C_H 1024
#define GO_RIGHT 0x01
#define GO_DOWN 0x02
#define GO_LEFT 0x03
#define GO_UP 0x04
#define SNAKE_NUMBER 30
typedef struct node_struct
{
unsigned char direction;
unsigned char cnt;
}s_node,*s_node_handle;
s_node s_count[SNAKE_NUMBER ];
typedef struct SNAKE
{
unsigned char Head_X;
unsigned char Head_Y;
unsigned char Tail_X;
unsigned char Tail_Y;
unsigned char h_index;
unsigned char t_index;
unsigned char food_state;
unsigned char score;
unsigned char snake_state;
} Snake_Data,*Snake_Data_handle;
Snake_Data snk_1;
#define MAP_X 64
#define MAP_Y 64
unsigned char game_map[MAP_Y][MAP_X];
LRESULT CALLBACK Win_tetris_Proc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
snk_1.Head_X = 0x01;//head x
snk_1.Head_Y = 0x00;//head y
snk_1.Tail_X = 0x00;//tail x
snk_1.Tail_Y = 0x00;//tail y
snk_1.h_index=0;
snk_1.t_index=0;
snk_1.food_state=0;
snk_1.score=0;
snk_1.snake_state = 1;
s_count[snk_1.h_index].cnt=2;
s_count[snk_1.h_index].direction=GO_RIGHT;
s_count[snk_1.t_index].cnt=2;
s_count[snk_1.t_index].direction=GO_RIGHT;
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=Win_tetris_Proc;
wndcls.lpszClassName="Game_tetris";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
//Game_Tetris
HWND hwnd;
hwnd=CreateWindow("Game_tetris","Game_Snake(/zook0k/)",WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX & ~WS_THICKFRAME,
0,0,C_W,C_H,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
//initial snake
HDC hdc;
hdc=GetDC(hwnd);
HBRUSH hbr;
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 16;
rect.bottom = 8;
hbr= CreateSolidBrush(RGB(255,0,0));
FillRect(hdc,&rect,hbr);
ReleaseDC(hwnd,hdc);
game_map[0][0]=1;
game_map[0][1]=1;
//initial rand
SetT
imer(hwnd,1,100,NULL) ;
srand((int)time(0));
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
//TranslateMessage
(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK Win_tetris_Proc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
char szChar[20] = "score:";
unsigned char xx,yy;
switch(uMsg)
{
case WM_KEYDOWN:
{
if(32 == wParam)
{
if(1 == snk_1.snake_state)
{
snk_1.snake_state = 0;
}
else
{
snk_1.snake_state = 1;
}
}
if(1 == snk_1.snake_state)
{
if((wParam > 36)&&(wParam < 41))
{
if(38 == wParam)
{
if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT))
{
snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
s_count[snk_1.h_index].direction = GO_UP;
s_count[snk_1.h_index].cnt = 1;
}
}
else if(40 == wParam)
{
if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT))
{
snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
s_count[snk_1.h_index].direction = GO_DOWN;
s_count[snk_1.h_index].cnt = 1;
}
}
else if(39 == wParam)
{
if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP))
{
snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
s_count[snk_1.h_index].direction = GO_RIGHT;
s_count[snk_1.h_index].cnt = 1;
}
}
else if(37 == wParam)
{
if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP))
{
snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;
s_count[snk_1.h_index].direction = GO_LEFT;
s_count[snk_1.h_index].cnt = 1;
}
}
}
}
break;
}
case WM_TIMER:
//case WM_PAINT:
time_t t;
HDC hdc;
hdc=GetDC(hwnd);
HBRUSH hbr;
RECT rect;
if(1 == snk_1.snake_state)
{
//head
CHECK:
switch(s_count[snk_1.h_index].direction)
{
case GO_RIGHT:
{
if(snk_1.Head_X < 63)snk_1.Head_X++;
else snk_1.Head_X = 0;
break;
}
case GO_LEFT:
{
if(snk_1.Head_
X >0 )snk_1.Head_X--;
else snk_1.Head_X = 63;
break;
}
case GO_DOWN :
{
if(snk_1.Head_Y < 63)snk_1.Head_Y++;
else
snk_1.Head_Y = 0;
break;
}
case GO_UP:
{
if(snk_1.Head_Y > 0)snk_1.Head_Y--;
else snk_1.Head_Y = 63;
break;
}
default:
{
break;
}
}
s_count[snk_1.h_index].cnt++;
if(0 == game_map[snk_1.Head_Y][snk_1.Head_X])//no point
{
game_map[snk_1.Head_Y][snk_1.Head_X] = 1;
}
else if(1 == game_map[snk_1.Head_Y][snk_1.Head_X])//game over
{
KillTimer(hwnd,1);
sprintf(szChar,"score:%d",snk_1.score);
MessageBox(hwnd,szChar,"GAME OVER!",0);
}
else if(2 == game_map[snk_1.Head_Y][snk_1.Head_X])//eat food
{
game_map[snk_1.Head_Y][snk_1.Head_X] = 1;
snk_1.food_state = 0;
snk_1.score++;
goto CHECK;
}
rect.left = snk_1.Head_X*8;
rect.top = snk_1.Head_Y*8;
rect.right = rect.left + 8;
rect.bottom = rect.top + 8;
hbr= CreateSolidBrush(RGB(255,0,0));
FillRect(hdc,&rect,hbr);//show head point
//tail
game_map[snk_1.Tail_Y][snk_1.Tail_X] = 0;
rect.left = snk_1.Tail_X*8;
rect.top = snk_1.Tail_Y*8;
rect.right = rect.left + 8;
rect.bottom = rect.top + 8;
//hbr= CreateSolidBrush(RGB(0,100,0));
hbr= CreateSolidBrush(RGB(0,0,0));//clear tail point
FillRect(hdc,&rect,hbr);
switch(s_count[snk_1.t_index].direction)
{
case GO_RIGHT:
{
if(snk_1.Tail_X < 63)snk_1.Tail_X++;
else snk_1.Tail_X = 0;
break;
}
case GO_LEFT:
{
if(snk_1.Tail_X >0 )snk_1.Tail_X--;
else snk_1.Tail_X = 63;
break;
}
case GO_DOWN :
{
if(snk_1.Tail_Y < 63)snk_1.Tail_Y++;
else snk_1.Tail_Y = 0;
break;
}
case GO_UP:
{
if(snk_1.Tail_Y > 0)snk_1.Tail_Y--;
else snk_1.Tail_Y = 63;
break;
}
default:
{
break;
}
}
if(s_count[snk_1.t_index].cnt == 2)
{
snk_1.t_index = (snk_1.t_index + 1)%SNAKE_NUMBER ;
}
else
{
s_count[snk_1.t_index].cnt--;
}
//output food
if(0 == snk_1.food_state)
{
snk_1.food_state = 1;
do{
xx = rand()%3970
%63;
yy = rand()%3970/63;
}while(1 == game_map[yy][xx]);
game_map[yy][xx]=2;
rect.left = xx*8;
rect.top = yy*8;
rect.right = rect.le
ft + 8;
rect.bottom = rect.top + 8;
hbr= CreateSolidBrush(RGB(155,110,10));
FillRect(hdc,&rect,hbr);//show food
srand((unsigned) time(&t));
}
ReleaseDC(hwnd,hdc);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
正在阅读:
贪吃蛇代码(C语言)05-12
党的群众路线教育实践活动调研报告12-12
乡镇教育督导评估自查报告12-12
多传感器火灾报警系统设计11-07
11G101-1试题及答案06-01
PClintError01-15
21 道岔大修施工作业指导书 204-09
公共事业管理本科英文简历08-08
防腐保温专业施工组织设计(范本)04-28
情况说明事件情况说明范文03-26
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 贪吃
- 语言
- 代码
- 2014年学生资助政策宣传活动总结1
- 15、售后服务与维保方案-动漫3
- 隔墙砌筑施工方案(正)6.22
- 新课程中英语语法教学的重要性
- Chapter 2 Credit Instruments(3)
- Join In Book 4 Unit 2 Children of our world 探究性作业(1)
- 高中物理学业水平测试知识点填空
- 环境监测现场采样
- 功能性食品论文(1).
- 盛路通信:2016年半年度报告
- 【火线100天】2015中考数学 第11讲 一次函数
- 初中英语构词法前缀后缀词汇一览表
- 2016年护士资格考点:病房护士守则每日一练(4月4日)
- 二手房购房协议书
- 八年级湘教版思想品德上册第四单元复习提纲
- 李再勇市长在贵阳市庆祝五一国家劳动节暨表彰大会的讲话稿
- 促销管理-百宝箱业务恢复接入及业务申报的通知 精品
- 常见矿物、岩石识别实验报告
- 学校园文化建设总结
- 2012年中科院物理化学(乙)内部习题集