| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 498 人关注过本帖, 1 人收藏
标题:游戏贪吃蛇 求注释 有些地方看不太懂
只看楼主 加入收藏
yg黄金时代
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-6-26
结帖率:100%
收藏(1)
 问题点数:0 回复次数:3 
游戏贪吃蛇 求注释 有些地方看不太懂
#include <iostream.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>  //使用当前时间做种子;
enum dir{up,down,left,right};  //枚举类型enum dir;
//围墙;
class Fence{
public:
    void InitFence();
    void OutputF();
public:
    char game[20][20];
}f; //定义对象;
//画框框;
void Fence::InitFence(){
    for(int i=0; i<20; i++)
        for(int j=0; j<20; j++){
            if(i==0||i==19||j==0||j==19)
                game[i][j]= '*';
            else game[i][j]= ' ';
    }
}
//显示框框;
void Fence::OutputF(){
    for(int i=0; i<20; i++){
        for(int j=0; j<20; j++)
            cout<<game[i][j]<<' ';
            cout<<endl;
        }
}

//蛇结点;
class SnakeNode{
private:
    int x,y;
    SnakeNode *prior,*next;
public:
    void add_head(int x,int y);
    int get_x();
    int get_y();
    void delete_tail();
}*head=NULL, *tail =NULL;
//插入头结点;
void SnakeNode::add_head(int x,int y){
    SnakeNode *q=new SnakeNode;
    q->x =x; q->y =y;
    q->next =head;
    q->prior =NULL;
    if(head) head->prior =q;
    head =q;
    if(!tail) tail =head;
    f.game[x][y]= '*';  //f对象可以在定义Fence类时定义; 且Fence类在SnakeNode类前定义;
}
int SnakeNode::get_x(){
    return x;
}
int SnakeNode::get_y(){
    return y;
}
//删除尾结点;
void SnakeNode::delete_tail(){
    SnakeNode *p =tail;
    f.game[tail->get_x()][tail->get_y()]= ' ';//把尾结点的坐标表示的'*'置为空格;
    if(tail==head)
        tail= head= NULL;
    else{
        tail= tail->prior;
        tail->next= NULL;
    }
    delete p;
}

//move移动;
class move{
public:
    dir point;    //枚举变量point: 控制方向;
    int food_x;
    int food_y;
public:
    void moving();
    void change_point(char);  //改变方向;
    void get_food();
};

void move::moving(){
    int a,b;
    a= head->get_x();  //取得头结点横坐标
    b= head->get_y();  //头结点纵坐标
    switch(point){
    case up: --a; break;
    case down: ++a; break;
    case left: --b; break;
    case right: ++b; break;
    }
    if(a==19||b==19||a==0||b==0){                //判断是否撞墙;                        
        cout<<"game over!!!"<<endl;
        exit(0);
    }
    if(a==food_x && b==food_y){                    //吃food;
        head->add_head(a,b);
        get_food();
    }
    else{
        head->add_head(a,b); //插入头结点;
        head->delete_tail(); //删除尾结点;
    }
}

void move::change_point(char keydown){
    switch(keydown){
    case 'w': point= up; break;
    case 's': point= down; break;
    case 'a': point= left; break;
    case 'd': point= right; break;
    }
}
void move::get_food(){
    srand((unsigned int) time(NULL)); //做种子(程序运行时间);
    food_x= rand()%18+1;
    food_y= rand()%18+1;
    f.game[food_x][food_y]= '*';
}

//main();
int main(){
    cout<<"Using 'w,s,a,d'to control direction!!!\n\n\n";
    //画框框和小蛇;
    move m;
    f.InitFence();
    head->add_head(4,3);
    head->add_head(4,4);
    head->add_head(4,5);
    m.get_food();
    f.OutputF();
    while (true){
        char keydown= getch(); //getch()返回键盘上读取的字符;包含头文件<conio.h>
        m.change_point(keydown);
        while(!kbhit()){ //判断有没有按键落下;
            system("cls");  //清屏函数;
            m.moving();
            f.OutputF();
            Sleep(200);
        }
    }
    return 0;
}
搜索更多相关主题的帖子: game void include 贪吃蛇 public 
2012-06-29 11:02
幽竹烟雨
Rank: 2
来 自:SunGalaxy
等 级:论坛游民
威 望:3
帖 子:43
专家分:86
注 册:2018-11-9
收藏
得分:0 
#include<iostream>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
using namespace std;
void gotoxy(int x,int y)
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])
{
int i,j;
int wall[N+1][N+1]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+1;i++)
{
for(j=0;j<N+1;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□" ;
}
cout<<endl;
}
gotoxy(N+2,1);
color(10);
cout<<"   哈哈哈哈哈"<<endl;
gotoxy(N+2,2);
color(28);
cout<<"按 W S A D 移动方向"<<endl;
gotoxy(N+2,3);
color(28);
cout<<"按任意键暂停"<<endl;
gotoxy(N+2,4);
color(18);
cout<<"得分:"<<endl;
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout<<"Game Over"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}
2018-11-09 20:35
幽竹烟雨
Rank: 2
来 自:SunGalaxy
等 级:论坛游民
威 望:3
帖 子:43
专家分:86
注 册:2018-11-9
收藏
得分:0 
刚刚那个是网上抄的,看看我自己编的吧,比较简单。。。
#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <time.h>
#include <iostream>
using namespace std;
#define MAX_WIDE 50    //修改50可以修改地图宽度
#define MAX_HIGH 16    //修改16可以修改地图高度
short dx = 1, dy = 0, randxy, score = 0;
COORD coord;

struct Snake{
short len;
short body[MAX_WIDE*MAX_HIGH];
}snake;
void draw()
{
for(short i = 0; i < snake.len; i++){
coord.X = snake.body[i] & 127;
coord.Y = snake.body[i] >> 8;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
putchar('@');    //修改此处putchar里的字符可以修改蛇身字符
}
coord.X = randxy & 127;
coord.Y = randxy >> 8;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
putchar('*');    //修改此处putchar里的字符可以修改食物字符
}
void run()
{
char key;
short i, j;
while( snake.body[0] > 0 && ( (snake.body[0] & 127) < MAX_WIDE) && (snake.body[0]>>8 < MAX_HIGH) ){
draw();
for(;kbhit();)key = getch();
switch(key){
case 'W': if (dx != 0 && dy != 1) //修改'W'可以修改上方向控制键
{dx = 0, dy = -1; break;}
else {break;}
case 'S': if (dx != 0 && dy != -1)    //修改'S'可以修改下方向控制键
{dx = 0, dy = 1; break;}
else {break;}
case 'A': if (dx != 1 && dy != 0)    //修改'A'可以修改左方向控制键
{dx = -1, dy = 0; break;}
else {break;}
case 'D': if (dx != -1 && dy != 0)    //修改'D'可以修改右方向控制键
{dx = 1, dy = 0; break;}
else {break;}
}
for(j = 1; j < snake.len; j++)
if(snake.body[j] == snake.body[0])
return;
if(randxy == snake.body[0]){
snake.len++, score += 10;
randxy = ((rand() % 16 + 0) <<8) | (rand() % 50 + 0);
}
for(i = snake.len-1; i > 0; i--)
snake.body[i] = snake.body[i-1];
snake.body[0] = ((snake.body[0] & 127) + dx) | ((snake.body[0] >>8) + dy)<<8;
Sleep(150);    //修改此处Sleep里的数值可以修改蛇移动的速度,数值越小,速度越快
system("cls");
}
}
int main()
{
snake.body[MAX_WIDE*MAX_HIGH] = 0;
snake.body[0] = (MAX_HIGH/2)<<8 | MAX_WIDE/2;
snake.len = 1;
srand((unsigned)time(NULL));
randxy = ((rand() % 16 + 0) <<8) | (rand() % 50 + 0);
SMALL_RECT rect;
rect.Top = 0, rect.Left = 0;
rect.Bottom = MAX_HIGH, rect.Right= MAX_WIDE;
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, &rect);
run();
printf("Your Score:%d\n", score);
system("pause");
char a;
cout<<"是否开始新游戏?(Y/N):";
cin>>a;
if(a=='Y'||a=='y')
{score=0;
main();
}
return 0;
}
打开大写锁定,用“WASD”控制。
2018-12-08 11:50
幽竹烟雨
Rank: 2
来 自:SunGalaxy
等 级:论坛游民
威 望:3
帖 子:43
专家分:86
注 册:2018-11-9
收藏
得分:0 
2018-12-08 11:50
快速回复:游戏贪吃蛇 求注释 有些地方看不太懂
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017975 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved