C++贪吃蛇
求C++贪吃蛇完整程序研究。如果是分了块的请把它整合成一块,要直接复制调试能通过的。如果太长请发到我的邮箱。还请注释,我初学啊。
追问:#include "pcc32.h"
#include "jkey32.h"这两个头文件你写在哪里啊?
求C++贪吃蛇完整程序研究。如果是分了块的请把它整合成一块,要直接复制调试能通过的。如果太长请发到我的邮箱。还请注释,我初学啊。
追问:#include "pcc32.h"
#include "jkey32.h"这两个头文件你写在哪里啊?
答案:/* * * * * * * * * * * * * * * * *
// Project: RedSnake(贪吃蛇)
// Author: HUST.RedOC
// Email: popapple@gmail.com
// Date: 2009.06.27 23.42.02
* * * * * * * * * * * * * * * * */#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "pcc32.h"
#include "jkey32.h"// 定义地图的尺寸及坐标(均使用双字符长度)
#define MAP_WIDTH 24
#define MAP_HEIGHT 16
#define MAP_BASE_X 3
#define MAP_BASE_Y 3// 定义蛇的相关参数
#define SNAKE_MIN_LEN 5// 定义地图块的状态,分别为[空格|蛇头|蛇身|食物]
#define BS_SPACE 0
#define BS_SHEAD 1
#define BS_SBODY 2
#define BS_STAIL 3
#define BS_FOOD 4// 定义各对象的颜色,颜色定义详见[pcc32.h],顺序同上.
PCCOLOR myColors[] = {ORANGE, RED_ORANGE, RED, LIGHT_GREEN, YELLOW};// 定义各对象的形状,注意是宽字符[◆◇□●★¤~◎+],顺序同上.
char mySharps[][3] = {"□", "◆", "■", "+", "★"};// 定义蛇的运动方向[上|下|左|右]
#define DIR_UP 1
#define DIR_DOWN 2
#define DIR_LEFT 3
#define DIR_RIGHT 4typedef struct _SnakeBody
{
uint8 x, y;
}POINT2D, *PPOINT2D;POINT2D mySnake[MAP_WIDTH*MAP_HEIGHT] = {{0}};
POINT2D myFood = {0};
int snakeLength = 0;
int snakeDir = DIR_RIGHT;
int isFood = 0;
int isOver = 0;void drawMap(void);
void initSnake(int len);
void drawSnake(void);
void moveSnake(void);
void drawBlock(int x, int y, int bs);
int isInSnake(int x, int y);
void drawFood(void);int main()
{
int isPause = 1;
setCursorVisible(0);
setConsoleTitle("Snake by RedOC. 2009.06.27");
initSnake(SNAKE_MIN_LEN);
drawMap();
drawSnake();
while (!isOver)
{
if (!isPause)
{
moveSnake();
if (!isFood)
drawFood();
}
delayMS(200 - snakeLength*2);
if (jkHasKey())
{
switch (jkGetKey())
{
case JK_UP:
if (snakeDir != DIR_DOWN)
snakeDir = DIR_UP;
break;
case JK_DOWN:
if (snakeDir != DIR_UP)
snakeDir = DIR_DOWN;
break;
case JK_LEFT:
if (snakeDir != DIR_RIGHT)
snakeDir = DIR_LEFT;
break;
case JK_RIGHT:
if (snakeDir != DIR_LEFT)
snakeDir = DIR_RIGHT;
break;
case JK_ESC:
case JK_ENTER:
case JK_SPACE:
isPause = !isPause;
break;
default:
break;
}
}
}
gotoTextPos(MAP_BASE_X + MAP_WIDTH - 7, MAP_BASE_Y + MAP_HEIGHT + 1);
printf("Game Over! Score: %d.", snakeLength - SNAKE_MIN_LEN);
getch();
return 0;
}void drawMap(void)
{
int i, j;
setTextColor(myColors[BS_SPACE]);
for (i = 0; i < MAP_HEIGHT; i++)
{
gotoTextPos(MAP_BASE_X*2, MAP_BASE_Y + i);
for (j = 0; j < MAP_WIDTH; j++)
{
printf("%2s", mySharps[BS_SPACE]);
}
}
}void initSnake(int len)
{
int i;
for (i = 0; i < len; i++)
{
mySnake[i].x = 10 - i;
mySnake[i].y = 3;
}
snakeLength = len;
isOver = 0;
}void drawSnake(void)
{
int i;
if (snakeLength < SNAKE_MIN_LEN)
return;setTextColor(myColors[BS_SHEAD]);
gotoTextPos((MAP_BASE_X + mySnake[0].x)*2, MAP_BASE_Y + mySnake[0].y);
printf(mySharps[BS_SHEAD]);setTextColor(myColors[BS_SBODY]);
for (i = 1; i < snakeLength - 1; i++)
{
gotoTextPos((MAP_BASE_X + mySnake[i].x)*2, MAP_BASE_Y + mySnake[i].y);
printf(mySharps[BS_SBODY]);
}setTextColor(myColors[BS_STAIL]);
gotoTextPos((MAP_BASE_X + mySnake[snakeLength-1].x)*2, MAP_BASE_Y + mySnake[snakeLength-1].y);
printf(mySharps[BS_STAIL]);return;
}void moveSnake(void)
{
int i;
int hx = mySnake[0].x;
int hy = mySnake[0].y;
if (snakeLength < SNAKE_MIN_LEN)
return;
switch (snakeDir)
{
case DIR_UP:
hy--;
break;
case DIR_DOWN:
hy++;
break;
case DIR_LEFT:
hx--;
break;
case DIR_RIGHT:
hx++;
break;
default:
break;
}
if (hx < 0 || hx >= MAP_WIDTH || hy < 0 || hy >= MAP_HEIGHT || isInSnake(hx, hy))
{
isOver = 1;
return;
}
if (hx == myFood.x && hy == myFood.y)
{
snakeLength++;
isFood = 0;
}
else
drawBlock(mySnake[snakeLength-1].x, mySnake[snakeLength-1].y, BS_SPACE);
for (i = snakeLength - 1; i >= 0; i--)
{
mySnake[i+1].x = mySnake[i].x;
mySnake[i+1].y = mySnake[i].y;
}
mySnake[0].x = hx;
mySnake[0].y = hy;
drawSnake();
return;
}void drawBlock(int x, int y, int bs)
{
if (x < 0 || x >= MAP_WIDTH)
return;
if (y < 0 || y >= MAP_HEIGHT)
return;
gotoTextPos((MAP_BASE_X + x)*2, MAP_BASE_Y + y);
setTextColor(myColors[bs]);
printf("%2s", mySharps[bs]);
return;
}int isInSnake(int x, int y)
{
int i;
for (i = 0; i < snakeLength; i++)
if (x == mySnake[i].x && y == mySnake[i].y)
return 1;
return 0;
}void drawFood(void)
{
srand((uint32)time(NULL));
do
{
myFood.x = rand() % MAP_WIDTH;
myFood.y = rand() % MAP_HEIGHT;
} while (isInSnake(myFood.x, myFood.y));
drawBlock(myFood.x, myFood.y, BS_FOOD);
isFood = 1;
}
http://www.zhaoxi.net/s?wd=C%2B%2B%E8%B4%AA%E5%90%83%E8%9B%87&ie=UTF-8&oe=UTF-8&bar=13&tn=baofengyingyin_cb
代码很多,但是你是初学有很多的东西可以学,要看你自己的能力了,先找个初级的练练即可
可留下qq与你联系
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/
DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&