资源简介
数据结构课程设计之贪吃蛇(源代码),很不错的资源哦
代码片段和文件信息
// snake6.cpp : Defines the entry point for the application.
//
#include “stdafx.h“
#include “resource.h“
#include “stdio.h“
#include “stdlib.h“
#define MAX_LOADSTRING 100
HDC Hdc;
int Num;
char Text[20];
#define MAP_X 260
#define MAP_Y 50
#define MAP_LEN 20
#define MAP_XNUM 20
#define MAP_YNUM 20
#define SNAKE_MAXLEN 400
#define SNAKE_UP 0
#define SNAKE_RIGHT 1
#define SNAKE_DOWN 2
#define SNAKE_LEFT 3
typedef struct pos
{int x;
int y;
}pos;
//
//定义蛇
typedef struct Snake
{ int direct; //蛇头方向
pos body[SNAKE_MAXLEN]; //存放蛇的坐标
int Len;
}Snake;
Snake snake;
//
//定义苹果
typedef struct Apple
{int x;
int y;
}Apple;
Apple apple;
//
//定义尾巴
typedef struct Tail
{int x;
int y;
}Tail;
Tail tail;
HBRUSH hBrugreenhBruredhBruwhite;
//
//画绿格子
void ColorGreen(HDC hdcint iint j){
Selectobject(hdchBrugreen);
Rectangle(hdciji+MAP_LENj+MAP_LEN);
}
//
//画红格子
void ColorRed(HDC hdcint iint j){
Selectobject(hdchBrured);
Rectangle(hdciji+MAP_LENj+MAP_LEN);
}
//
//画白格子
void ColorWhite(HDC hdcint iint j){
Selectobject(hdchBruwhite);
Rectangle(hdciji+MAP_LENj+MAP_LEN);
}
//
//
//画白布
void ColorWhite2(HDC hdc){
HBRUSH hBru;
hBru=CreateSolidBrush(RGB(255255255));
Selectobject(hdchBru);
Rectangle(hdcMAP_XMAP_YMAP_X+MAP_LEN*MAP_XNUM-MAP_LENMAP_Y+MAP_YNUM*MAP_LEN-MAP_LEN);
}
//
//
//画蛇
void PaintSnake(HDC hdcSnake* snake){
Selectobject(hdchBrured);
for(int i=0;iLen;i++){
ColorRed(hdcsnake->body[i].xsnake->body[i].y);
}
}
//
//初始化蛇
void InitSnake(Snake* snake){
snake->direct=1;
snake->Len=3;
snake->body[0].y=snake->body[1].y=snake->body[2].y=MAP_Y;
snake->body[0].x=MAP_X;
snake->body[1].x=MAP_X+MAP_LEN;
snake->body[2].x=MAP_X+MAP_LEN*2;
}
//
//画苹果
void PaintApple(HDC hdcApple* apple){
ColorGreen(hdcapple->xapple->y);
}
//
//初始化苹果
void CreateApple(Snake* snakeApple* apple){
int a=rand()%MAP_XNUM*MAP_LEN+MAP_X;
int b=rand()%MAP_YNUM*MAP_LEN+MAP_Y;
for(int i=0;iLen;i++){
if(a==snake->body[i].x&&b==snake->body[i].y){
a=rand()%MAP_XNUM*MAP_LEN+MAP_X;
b=rand()%MAP_YNUM*MAP_LEN+MAP_Y;
i=0;}
}
apple->x=a;
apple->y=b;
}
//
//更新蛇的位置
void UpdateSnake(Snake* snake){
for(int i=0;iLen-1;i++){
snake->body[i].x=snake->body[i+1].x;
snake->body[i].y=snake->body[i+1].y;}
switch(snake->direct)
{case SNAKE_UP:
snake->body[snake->Len-1].y-=MAP_LEN;
break;
case SNAKE_RIGHT:
snake->body[snake->Len-1].x+=MAP_LEN;
break;
case SNAKE_DOWN:
snake->body[snake->Len-1].y+=MAP_LEN;
break;
case SNAKE_LEFT:
snake->body[snake->Len-1].x-=MAP_LEN;
}
}
//
//蛇长长函数
/*int SnakeGrow(Snake* snakeApple* apple){
if(snake->body[snake->Len-1].x==apple->x&&snake->body[snake->Len-1].y==apple->y){
snake->Len++;
switch(snake->direct)
{case SNAKE_UP:
snake->body[snake->Len-1].x=snake->bo
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 163893 2009-03-11 20:18 snake6\Debug\snake6.exe
文件 220900 2009-03-11 20:18 snake6\Debug\snake6.ilk
文件 22070 2009-03-11 20:18 snake6\Debug\snake6.obj
文件 1959720 2009-03-10 18:58 snake6\Debug\snake6.pch
文件 443392 2009-03-11 20:18 snake6\Debug\snake6.pdb
文件 2124 2009-03-10 18:58 snake6\Debug\snake6.res
文件 54979 2009-03-10 18:58 snake6\Debug\StdAfx.obj
文件 82944 2009-12-20 00:38 snake6\Debug\vc60.idb
文件 176128 2009-03-11 20:18 snake6\Debug\vc60.pdb
文件 2065 2009-03-10 18:58 snake6\ReadMe.txt
文件 781 2009-03-10 18:58 snake6\resource.h
文件 318 2009-03-10 18:58 snake6\small.ico
文件 12963 2009-03-11 20:18 snake6\snake6.cpp
文件 4481 2009-03-10 18:58 snake6\snake6.dsp
文件 520 2009-03-10 22:10 snake6\snake6.dsw
文件 326 2009-03-10 18:58 snake6\snake6.h
文件 1078 2009-03-10 18:58 snake6\snake6.ico
文件 66560 2009-12-20 00:41 snake6\snake6.ncb
文件 48640 2009-12-20 00:41 snake6\snake6.opt
文件 246 2009-12-20 00:38 snake6\snake6.plg
文件 3064 2009-03-10 18:58 snake6\snake6.rc
文件 293 2009-03-10 18:58 snake6\StdAfx.cpp
文件 936 2009-03-10 18:58 snake6\StdAfx.h
目录 0 2010-04-20 22:20 snake6\Debug
目录 0 2010-04-20 22:20 snake6
----------- --------- ---------- ----- ----
3268421 25
- 上一篇:SVM分类器代码
- 下一篇:《大学英语综合教程》第四册上海外语教育出版社课后答案
评论
共有 条评论