贪吃蛇代码-c语言-vc6.0
- 格式:doc
- 大小:31.50 KB
- 文档页数:6


贪吃蛇c++语⾔程序,C++语⾔贪吃蛇游戏源代码.doc
C语⾔贪吃蛇游戏源代码
/*
Author : 柳印奇
email : liuyinqi2012@
Description : 蛇年来临创作⼀个与蛇有关的经典游戏《贪吃蛇》,与同学们分享蛇年美好的时光。欢迎交流与探讨,直接将代码粘贴到VC6.0的环境下即可运⾏。
← 左转
→ 右转
↓ 向下
↑ 向上
积分达到⼀定程度,会有换命的活动,命最多6条。
难度会随积分的上升逐渐上升,最多到6的难度。
*/
#include
#include
#include
#include
#pragma comment(lib, "winmm.lib")
using namespace std;
#define GameW 20
#define GameH 11
const int CtrlLeft = GameW*2+4 + 3;
struct Point {
Point(){}
Point& operator=(const Point& rhs) {
_x = rhs._x;
_y = rhs._y;
return *this;
}
Point(int x, int y) {_x = x, _y = y;}
int _x, _y;
};
HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);HANDLE g_hInput = GetStdHandle(STD_INPUT_HANDLE);
Point g_ptCursor(0,0);
BOOL isChecking = FALSE;
BOOL g_bGameOver = FALSE;
BOOL bCreateEgg = FALSE;
int g_nGameBack[GameH][GameW];
int nowKeyInfo = -1;
int g_nDiff = 1;
int g_nLife = 3;
int g_nScore = 0;
传播优秀Word版文档 ,希望对您有帮助,可双击去除!
#include
#include
#include
#include
#include
const int H = 8; //地图的高
const int L = 16; //地图的长
char GameMap[H][L]; //游戏地图
int key; //按键保存
int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙)
int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向
int dy[4] = {-1, 1, 0, 0};
struct Snake //蛇的每个节点的数据类型
{
int x, y; //左边位置
int now; //保存当前节点的方向, 0,1,2,3分别为左右上下
}Snake[H*L];
const char Shead = '@'; //蛇头
const char Sbody = '#'; //蛇身
const char Sfood = '*'; //食物
const char Snode = '.'; //'.'在地图上标示为空
void Initial(); //地图的初始化
void Create_Food(); //在地图上随机产生食物
void Show(); //刷新显示地图
void Button(); //取出按键,并判断方向
void Move(); //蛇的移动
void Check_Border(); //检查蛇头是否越界
void Check_Head(int x, int y); //检查蛇头移动后的位置情况
int main()
{
超简单贪吃蛇c语言代码编写
贪吃蛇其实就是实现以下几步——
1:蛇的运动(通过“画头擦尾”来达到蛇移动的视觉效果)
2:生成食物
3:蛇吃食物(实现“画头不擦尾”)
4:游戏结束判断(也就是蛇除了食物,其余东西都不能碰)
#include
#include
#include
#include
#include
#define width 60
#define hight 25
#define SNAKESIZE 200//蛇身的最长长度
int key=72;//初始化蛇的运动方向,向上
int changeflag=1;//用来标识是否生成食物,1表示蛇还没吃到食物,0表示吃到食物
int speed=0;//时间延迟
struct {
int len;
//用来记录蛇身每个方块的坐标
int x[SNAKESIZE];
int y[SNAKESIZE];
int speed;
}snake;
struct
{
int x;int y;
}food;
void gotoxy(int x,int y)//调用Windows的API函数,可以在控制台的指定位置直接操作,这里可暂时不用深究
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//■○
void drawmap()
{
//打印图框
for (int _y = 0; _y < hight; _y++)
{
for (int x = 0; x < width; x+=2)
{
if (x == 0 || _y == 0 || _y == hight - 1 || x == width - 2)
{
gotoxy(x, _y);
C语⾔实现贪吃蛇游戏(命令⾏)
这是⼀个纯C语⾔写的贪吃蛇游戏,供⼤家参考,具体内容如下
#include
#include
#include
#include
#include
#define SNAKE_LENGTH 100//定义蛇的最⼤长度
#define SCREEN_WIDETH 80
#define SCREEN_HEIGHT 30
//定义每⼀节蛇的坐标
struct coor{
int x;
int y;
};
//枚举⽅向
enum CH {
right = VK_RIGHT,
left = VK_LEFT,
up = VK_UP,
down = VK_DOWN
};
//定义蛇的属性
struct snake{
int len;//当前蛇的长度
struct coor coord[SNAKE_LENGTH];//每⼀节蛇的坐标
enum CH CH;//定义蛇的⽅向
int SPEED;
int flag;//定义蛇的状态 1表⽰存活 0表⽰死亡
}snake;
//光标移动函数
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//初始化游戏界⾯
void init_sence()
{
//初始化上下墙
for (int i = 0; i < SCREEN_WIDETH; i += 2)
{
gotoxy(i,0);
printf("■");
gotoxy(i, SCREEN_HEIGHT);
printf("■");
}
//初始化左右墙
for (int i = 0; i <=SCREEN_HEIGHT; i++)
{
gotoxy(0, i);
printf("■");
gotoxy(SCREEN_WIDETH,i);