day4

Uncategorized
639 words

day4 - 巅峰造极

C++ 走迷宫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<cstdio>
#include<cstdlib>
#include<windows.h>
#include<conio.h>
using namespace std;

int main(){
char map[50][50]={"###########",
"#O # # ",
"### # # ###",
"# # #",
"# ### #####",
"# #",
"###########"};
int x=1,y=1,p=10,q=1,i;
char ch;
for(i=0;i<=6;i++)
puts(map[i]);
while(1){
ch=getch();
if(ch=='a' && map[y][x-1]==' '){
map[y][x]=' ';
x--;
map[y][x]='O';
}
if(ch=='s' && map[y+1][x]==' '){
map[y][x]=' ';
y++;
map[y][x]='O';
}
if(ch=='d' && map[y][x+1]==' '){
map[y][x]=' ';
x++;
map[y][x]='O';
}
if(ch=='w' && map[y-1][x]==' '){
map[y][x]=' ';
y--;
map[y][x]='O';
}
if(x==p && y==q) break;
system("cls");
for(i=0;i<=6;i++)
puts(map[i]);
}
system("cls");
printf("Victory");
Sleep(3000);
return 0;
}

C++贪吃蛇项目


  1. 结构体
  2. 搭建个人仓库
  3. 程序模块化
  4. 拓展

贴代码


  1. snake.h
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    #include<cstdlib>    // 要有 
    #include<windows.h>
    #include<conio.h>
    #include<vector>
    #include<iostream>
    #include<ctime>

    #define COL 30 //行
    #define ROW 50 //列

    const int InitLength=5;

    enum Dir{UP,DOWN,LEFT,RIGHT};
    int dir=RIGHT;

    int is_food=1;
    int food_x,food_y;

    struct Snake{
    Snake(){}
    Snake(int x,int y){_x=x; _y=y;}
    int _x,_y;
    };
    std::vector<Snake> nodes;

    void SetCCPos(int x, int y) {
    HANDLE hOut; //句柄 HANDLE
    hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取标注输出句柄
    COORD pos; //在屏中的位置坐标 (x,y)
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOut, pos); //偏移光标位置
    }

    void InitSnake(){
    for(int i = InitLength;i >= 1;i--)
    nodes.push_back(Snake(i,1));
    }
    //碰撞检测
    bool collision(){
    int x=nodes[0]._x;
    int y=nodes[0]._y;
    if(x <= 0 || x >= ROW || y <= 0 || y >= COL)
    return false;
    for(int i = 1;i < nodes.size();i++)
    if(x==nodes[i]._x && y==nodes[i]._y)
    return false;
    return true;
    }

    void CreateFood(){
    food_x=rand()%(ROW - 2) + 1;
    food_y=rand()%(COL - 2) + 1;
    SetCCPos(food_x,food_y);
    std::cout<<"*";
    }

    void DrawFood(){
    if(is_food)
    {
    SetCCPos(food_x,food_y);
    std::cout<<"*";
    }
    }

    void DrawSnake(){
    for(int i = 0;i < nodes.size();i++)
    {
    SetCCPos(nodes[i]._x,nodes[i]._y);
    std::cout<<"O";
    }
    }

    void MoveSnake(){

    //蛇身的跟随
    for(int i = nodes.size() - 1;i > 0;i--)
    nodes[i]=nodes[i-1];
    //按键检测
    if(_kbhit()){
    if(GetAsyncKeyState(VK_UP))
    dir=UP;
    else
    if(GetAsyncKeyState(VK_DOWN))
    dir=DOWN;
    else
    if(GetAsyncKeyState(VK_LEFT))
    dir=LEFT;
    else
    if(GetAsyncKeyState(VK_RIGHT))
    dir=RIGHT;
    }
    switch(dir){
    case(UP): nodes[0]._y -= 1;break;
    case(DOWN): nodes[0]._y += 1;break;
    case(LEFT): nodes[0]._x -= 1;break;
    case(RIGHT): nodes[0]._x += 1;break;
    }
    }

    void Drawbackground(){
    //设置背景及边框

    for(int i = 0; i < ROW; i++)
    {
    SetCCPos(i,0);
    std::cout<<"#";
    }
    for(int i = 0; i < ROW; i++)
    {
    SetCCPos(i,COL-1);
    std::cout<<"#";
    }
    for(int i = 1; i < COL - 1; i++)
    {
    SetCCPos(0,i);
    std::cout<<"#";
    }
    for(int i = 1; i < COL - 1; i++)
    {
    SetCCPos(ROW-1,i);
    std::cout<<"#";
    }
    }

    void InitGame(){
    //初始化蛇
    InitSnake();
    //初始化背景
    system("mode con cols=50 lines=30");
    // cols=ROW lines=ROW
    Drawbackground();

    /****************** 隐 藏 光 标 *************************/
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    /************** 此 处 插 入 你 的 程 序 *****************/

    CreateFood();
    }
  2. main.cpp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    // C语言贪吃蛇 
    #include "snake.h"
    int main(){
    InitGame();
    // 设置随机数种子
    srand(time(NULL));
    while(1){
    MoveSnake();
    system("cls"); //清屏,但是会闪屏
    DrawSnake();
    Drawbackground();
    DrawFood();
    if(nodes[0]._x==food_x && nodes[0]._y==food_y){
    CreateFood();
    nodes.push_back(Snake());
    }
    if(!collision()){
    MessageBox(NULL,"你死了","",MB_OK|MB_ICONHAND);
    return 0;
    }
    Sleep(100); //等待很卡,可以不要
    }
    }