大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang

/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
 
char qipan[20][80];
int snake = 1;
 
int change(int *head, int *tail, int *score, char *direction, int zuobiao[2][80])
{
    int x, y;
switch(*direction)
{
        case 72:
            x = zuobiao[0][*head]-1;
            y = zuobiao[1][*head];
            break;
        case 87:
            x = zuobiao[0][*head]-1;
            y = zuobiao[1][*head];
            break;
        case 80:
            x = zuobiao[0][*head]+1;
            y = zuobiao[1][*head];
            break;
        case 83:
            x = zuobiao[0][*head]+1;
            y = zuobiao[1][*head];
            break;
        case 75:
            x = zuobiao[0][*head];
            y = zuobiao[1][*head]-1;
            break;
        case 65:
            x = zuobiao[0][*head];
            y = zuobiao[1][*head]-1;
            break;
        case 68:
            x = zuobiao[0][*head];
            y = zuobiao[1][*head]+1;
            break;
    default:
            x = zuobiao[0][*head];
            y = zuobiao[1][*head]+1;
}
if(qipan[x][y] == '_' || qipan[x][y] == '|' || qipan[x][y] == '*')
return 1;
    if(qipan[x][y] == ' ')
    {
        qipan[zuobiao[0][*tail]][zuobiao[1][*tail]]=' ';
        *tail=(*tail+1)%80;
        qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
        *head=(*head+1)%80;
        zuobiao[0][*head]=x;
        zuobiao[1][*head]=y;
        qipan[x][y]='#';
        return 0;
    }
    if(qipan[x][y] == '@')
    {
        qipan[zuobiao[0][*head]][zuobiao[1][*head]]='*';
        *head=(*head+1)%80;
        zuobiao[0][*head]=x;
        zuobiao[1][*head]=y;
        qipan[x][y]='#';
        *score += 1;
        return 0;
    }
}
void gotoxy(int x,int y)//位置函数
{
COORD pos;
 
pos.X=x;
 
pos.Y=y;
 
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
 
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
 
}
void show_start()
{
 
    gotoxy(20,0);
    color(1);
    int u;
    for(u = 0; u < 18; u ++)
        printf("* ");
    gotoxy(20, 12);
    for(u = 0; u < 18; u ++)
        printf("* ");
    gotoxy(28,2);
    color(13);
    printf("S N A K E  G A M E");
    gotoxy(31, 5);
    color(14);
    printf("#");
    gotoxy(34, 5);
    color(11);
    printf("One  snake");
    gotoxy(34, 7);
    printf("Two  snakes");
    gotoxy(28,9);
    printf("The speed you want: ");
 
    gotoxy(31, 5);
    char temp = 'a';
    color(14);
    while(1)
    {
        while(!kbhit());
        temp = getch();
        if(temp == 72 || temp == 80)
        {
            gotoxy(31,5+2*(snake-1));
            printf(" ");
            snake = snake % 2 + 1;
            gotoxy(31,5+2*(snake-1));
            printf("#");
            gotoxy(31,5+2*(snake-1));
        }
        if(temp == 13)
            break;
    }
 
    gotoxy(27,10);
    color(10);
    printf("GOOD  LUCK  TO  YOU  !");
}
 
 
 
 
 
 
int main()
{
    srand(time(0));
 
    int head1 = 3, tail1 = 0, score1 = 0;
    int head2 = 3, tail2 = 0, score2 = 0;
    int zuobiao1[2][80];
    int zuobiao2[2][80];
 
    /*棋盘 20×80 初始化*/
for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 80; j++)
        {
            qipan[i][j] = ' ';
        }
    }
    for(int i = 0; i < 20; i++)
    {
        qipan[i][0] = qipan[i][79] = '|';//第一列、最后一列是墙20*1
    }
    for(int i = 0; i < 80; i++)
    {
        qipan[0][i] = qipan[19][i] = '_';//第一行、最后一行是墙1*80
    }
 
    /*蛇的坐标 2×8 初始化, 将蛇放到棋盘上*/
    int x1 = 1, x2 = 18, y, temp = 0;
 
    for(int m = tail1; m < head1; m++)
    {
        zuobiao1[0][m] = x1;//初始行号
        y = zuobiao1[1][m] = ++temp;//初始列号
        qipan[x1][y] = '*';
    }
    zuobiao1[0][head1] = x1;//初始行号
    y = zuobiao1[1][head1] = ++temp;//初始列号
    qipan[x1][y] = '#';//蛇头
 
    show_start();
 
    if(snake == 2)
    {
        temp = 0;
        for(int m = tail2; m < head2; m++)
        {
            zuobiao2[0][m] = x2;//初始行号
            y = zuobiao2[1][m] = ++temp;//初始列号
            qipan[x2][y] = '*';
        }
        zuobiao2[0][head2] = x2;//初始行号
        y = zuobiao2[1][head2] = ++temp;//初始列号
        qipan[x2][y] = '#';//蛇头
    }
 
    clock_t start;
int timeover;
char direction1 = 77;//72 88 75 77
char direction2 = 68;//87 83 65 68
    char new_direction;
    char new_direction1 ;
    char new_direction2 ;
 
int gamespeed;
gotoxy(48, 9);
color(14);
scanf("%d", &gamespeed);
    int rand_i = rand()%18 + 1;
    int rand_j = rand()%78 + 1;
    qipan[rand_i][rand_j] = '@';
 
    system("cls");
    gotoxy(10, 1);
    color(10);
    printf("Your present score: ");
    gotoxy(30, 1);
    color(11);
    printf("%d", score1);
    gotoxy(0,4);
    for(int i = 0; i < 20; i++)//打印出棋盘
        for(int j = 0; j < 80; j++)
        {
            if(qipan[i][j] == '*' || qipan[i][j] == '#')
                color(13);
            else if(qipan[i][j] == '@')
                color(12);
            else
                color(15);
                printf("%c", qipan[i][j]);
        }
    while(1)//87 83 65 68
    {
        for(int i = 0; i < 20; i++)//打印出棋盘
            for(int j = 0; j < 80; j++)
            {
                if(qipan[i][j] == '*' || qipan[i][j] == '#')
                {
                    gotoxy(j, i+4);
                    color(13);
                }
                if(qipan[i][j] == '@')
                {
                    gotoxy(j, i+4);
                    color(12);
                }
                if(qipan[i][j] == '_' || qipan[i][j] == '|')
                {
                    gotoxy(j, i+4);
                    color(15);
                }
                printf("%c", qipan[i][j]);
            }
        for(int q = 0; q < 3; q++)
        {
            gotoxy(rand()%78 + 1, rand()%18 + 5 );
            color(14);
            printf("+");
        }
        start = clock();
        timeover = 1;
        while(!kbhit() && (timeover = clock() - start/CLOCKS_PER_SEC <= gamespeed));
        if(timeover)//有按键
        {
            if(snake == 1)
                getch();
            new_direction = getch();
            if(snake == 2)
            {
                if(new_direction == 87 || new_direction ==83 ||new_direction == 65||new_direction == 68)
                {
                    new_direction2 = new_direction;
                    new_direction1 = direction1;
                    if(new_direction2 + direction2 == 170 || new_direction2 + direction2 == 133)
                    {
                        change(&head1, &tail1, &score1, &direction1, zuobiao1);
                        change(&head2, &tail2, &score2, &direction2, zuobiao2);
                        continue;
                    }
                }
                if(new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77)
                {
                    new_direction1 = new_direction;
                    new_direction2 = direction2;
                    if(new_direction1 + direction1 == 152)
                    {
                        change(&head1, &tail1, &score1, &direction1, zuobiao1);
                        change(&head2, &tail2, &score2, &direction2, zuobiao2);
                        continue;
                    }
                }
            }
            if(snake == 1 && (new_direction == 72 || new_direction ==80 ||new_direction == 75||new_direction == 77))
            {
                new_direction1 = new_direction;
                if(new_direction1 + direction1 == 152)
                {
                    change(&head1, &tail1, &score1, &direction1, zuobiao1);
                    continue;
                }
            }
        }
        else
        {
            if(snake == 2)
            {
                    new_direction2 = direction2;
                    new_direction1 = direction1;
            }
            else
            {
                new_direction1 = direction1;
            }
        }
        if(snake == 1)
        {
            direction1 = new_direction1;
            if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
            {
                system("cls");
                gotoxy(30,10);
                color(7);
                printf("G A M E  O V E R !");
                gotoxy(0,20);
                color(1);
                return 0;
            }
        }
        if(snake == 2)
        {
            direction1 = new_direction1;
            if(change(&head1, &tail1, &score1, &direction1, zuobiao1) == 1)
            {
                system("cls");
                gotoxy(30,8);
                color(7);
                printf("G A M E  O V E R !");
                gotoxy(30,10);
                printf("Snake Two is the Hero!");
                gotoxy(0,20);
                color(1);
                return 0;
            }
 
            direction2 = new_direction2;
            if(change(&head2, &tail2, &score2, &direction2, zuobiao2) == 1)
            {
                system("cls");
                gotoxy(30,8);
                color(7);
                printf("G A M E  O V E R !");
                gotoxy(30,10);
                printf("Snake One is the Hero!");
                gotoxy(0,20);
                color(1);
                return 0;
            }
 
        }
 
        int randnew = 0;
        for(int i = 0; i < 20; i++)//打印出棋盘
            for(int j = 0; j < 80; j++)
                if(qipan[i][j] == '@')
                {
                    randnew ++;
                }
        if(randnew == 0)
        {
            rand_i = rand()%18 + 1;
            rand_j = rand()%78 + 1;
            qipan[rand_i][rand_j] = '@';
        }
        if(snake == 2 && randnew == 1)
        {
            rand_i = rand()%18 + 1;
            rand_j = rand()%78 + 1;
            qipan[rand_i][rand_j] = '@';
        }
 
        gotoxy(30, 1);
        printf("%d", score1);
        if(snake == 2)
        {
            gotoxy(30,2);
            printf("%d", score2);
        }
    }
}

双人贪吃蛇小游戏C++原创的更多相关文章

  1. C++ 简单的控制台贪吃蛇小游戏

    由于比较懒,所以不怎么写,觉得这样不应该.我应该对自己学的做出整理,不管是高端低端,写出来是自己的. // 贪吃蛇.cpp : 定义控制台应用程序的入口点. // #include "std ...

  2. 贪吃蛇小游戏-----C语言实现

    1.分析 众所周知,贪吃蛇游戏是一款经典的益智游戏,有PC和手机等多平台版本,既简单又耐玩.该游戏通过控制蛇头方向吃食物,从而使得蛇变得越来越长,蛇不能撞墙,也不能装到自己,否则游戏结束.玩过贪吃蛇的 ...

  3. JS高级---案例:贪吃蛇小游戏

    案例:贪吃蛇小游戏 可以玩的小游戏,略复杂,过了2遍,先pass吧 先创建构造函数,再给原型添加方法.分别创建食物,小蛇和游戏对象. 食物,小蛇的横纵坐标,设置最大最小值,运动起来的函数,按上下左右键 ...

  4. Java GUI学习,贪吃蛇小游戏

    JAVA GUI练习 贪吃蛇小游戏 前几天虽然生病了,但还是跟着狂神学习了GUI的方面,跟着练习了贪吃蛇的小项目,这里有狂神写的源码点我下载,还有我跟着敲的点我下载,嘿嘿,也就注释了下重要的地方,这方 ...

  5. 用GUI实现java版贪吃蛇小游戏

    项目结构 新建一个JFrame窗口,作为程序入口 public class GameStart{ public static void main(String[] args) { JFrame jFr ...

  6. Java 用java GUI写一个贪吃蛇小游戏

    目录 主要用到 swing 包下的一些类 上代码 游戏启动类 游戏数据类 游戏面板类 代码地址 主要用到 swing 包下的一些类 JFrame 窗口类 JPanel 面板类 KeyListener ...

  7. html5面向对象做一个贪吃蛇小游戏

    canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说 ...

  8. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...

  9. Java贪吃蛇小游戏

    贪吃蛇 思路 首先构思游戏布局,计算合理的坐标系. 绘制静态数据(广告.初始小蛇.提示信息.棋盘) 添加键盘监听事件,改变游戏状态以及小蛇运动方向 添加定时器,让小蛇在一段时间内移动一定的距离 随机产 ...

随机推荐

  1. 数据结构&&算法基础知识

    写本篇主要是为了将基础知识梳理一遍,天天加一些基本东西,以后复习时可以返回来看看. 数据结构&&基础算法: 基本算法: 二分查找 二叉树: 二叉树的各种遍历 位操作: 排序: 排序算法 ...

  2. Struts2标签库之iterator

    传说中的第一种方式,这种在Struts2.1权威指南的例子中也木有说明白: <%@ page language="java" contentType="text/h ...

  3. BOT、BT、PPP形式介绍(1)

    BOT.BT.PPP形式介绍 BOT1.什么是BOT     BOT是英文Build-Operate-Transfer的缩写,即“建设-经营-转让”.实质上是基础设施投资.建设和经营的一种方式,以政府 ...

  4. hdu1521:排列组合---指数型母函数

    题意: n种元素,每种有 ni个,选出 m 个的排列有多少种 题解: 指数型母函数的裸题 x^n 项的系数为  an/n!.... 代码如下: #include <iostream> #i ...

  5. 什么是空间复杂度(What is actually Space Complexity ?)

    属于空间复杂度(Space Complexity)在很多情况下被错认为是附属空间(Auxiliary Space),下面是附属空间和空间复杂度的定义. 附属空间(Auxiliary Space)是算法 ...

  6. Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询

    HQL查询方式 这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的语法差不太多.条件查询.分页查询.连接查询.嵌套查询,写起来与SQL语法基本一致,唯一不同的就是把表名换成了 ...

  7. 解决Fetching android sdk component information加载过久问题

    安装完成后,如果直接启动,Android Studio会去获取 android sdk 组件信息,这个过程相当慢,还经常加载失败,导致Android Studio启动不起开.解决办法就是不去获取and ...

  8. _js day12

  9. Enable-Migrations 在应用程序配置文件中找不到xx连接字符串

    在解决方案中有多个项目时,使用Enable-Migrations 命令进行数据迁移时,出现以下错误: 尝试在Enable-Migrations 命令中指定-projectName也不行,最后将要操作的 ...

  10. oracle中简单查询语句的格式及执行顺序分析

    一条简单的查询sql格式如下: SELECT ... FROM .... [WHERE ...] --过滤单行 [GROUP BY ...   [HAVING ...]]--GROUP BY对前面wh ...