zoukankan      html  css  js  c++  java
  • 贪吃蛇C/C++(Easyx)

    设计的很粗糙诶。不过也算那年跟着学的第一个小游戏了,后面还想做其他有难度的,不过最终都是搁置了,也算是一种遗憾了吧。

    根据用户输入控制蛇,判断是否吃到食物,判断是否撞到墙壁等来实现。

    就不多说了,还是很简单的(小声哔哔,毕竟写了注释的)。

      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <cstdlib>
      4 #include <conio.h>
      5 #include <ctime>
      6 #include <windows.h>
      7 
      8 #define High 25            // 游戏画面尺寸
      9 #define Width 45
     10 
     11 using namespace std;
     12 
     13 int food_x, food_y;
     14 int moveDirection;            // 表示小蛇移动方向,1/2/3/4分别表示上下左右;
     15 int canvas[High][Width] = { 0 };    // 二维数组存储
     16                                 // 0为空格,-1为边框#,1为蛇头O,>1为蛇身*
     17 
     18 void gotoxy(int x, int y) {        // 光标移动到想x,y
     19     HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
     20     COORD pos;
     21     pos.X = x;
     22     pos.Y = y;
     23     SetConsoleCursorPosition(handle, pos);
     24 }
     25 
     26 void Startup() {
     27     int i, j;
     28 
     29     // 边框设定
     30     for (i = 0; i < High; i++) {
     31         canvas[i][0] = -1;
     32         canvas[i][Width - 1] = -1;
     33     }
     34     for (j = 0; j < Width; j++) {
     35         canvas[0][j] = -1;
     36         canvas[High - 1][j] = -1;
     37     }
     38 
     39     // 蛇设定
     40     canvas[High / 2][Width / 2] = 1;
     41     for (i = 1; i < 5; i++)
     42         canvas[High / 2][Width / 2 - i] = i + 1;
     43 
     44     // 运动方向初始化
     45     moveDirection = 4;
     46 
     47     // food
     48     srand((unsigned)time(0));
     49     food_x = rand() % (High - 5) + 2;
     50     food_y = rand() % (Width - 5) + 2;
     51     canvas[food_x][food_y] = -2;
     52 }
     53 
     54 void Show() {
     55     int i, j;
     56     for (i = 0; i < High; i++) {
     57         for (j = 0; j < Width; j++) {
     58             if (canvas[i][j] == 0)
     59                 cout << " ";        // 输出空格
     60             else if (canvas[i][j] == -1)
     61                 cout << "#";        // 边框
     62             else if (canvas[i][j] == 1)
     63                 cout << "O";        // 蛇头
     64             else if (canvas[i][j] > 1)
     65                 cout << "*";        // 蛇身
     66             else if (canvas[i][j] == -2)
     67                 cout << "F";        // food
     68         }
     69         cout << endl;
     70     }
     71 }
     72 
     73 void MoveSnakeByDirection() {
     74     int i, j;
     75     int max = 0;
     76     int oldTail_x, oldTail_y;        // 记录旧的蛇尾Tail
     77     int oldHead_x, oldHead_y;        // 记录旧的蛇头Head
     78 
     79     for(i = 1;i < High -1;i++)
     80         for (j = 1; j < Width; j++) {
     81             if (canvas[i][j] > 0) {
     82                 canvas[i][j]++;
     83                 if (max < canvas[i][j]) {        // 旧蛇尾
     84                     max = canvas[i][j];
     85                     oldTail_x = i;
     86                     oldTail_y = j;
     87                 }
     88                 if (canvas[i][j] == 2) {        //  旧蛇头
     89                     oldHead_x = i;
     90                     oldHead_y = j;
     91                 }
     92             }
     93         }
     94 
     95     int newHead_x, newHead_y;
     96 
     97     switch (moveDirection) {        // 新蛇头产生
     98     case 1: newHead_x = oldHead_x - 1;    // 向上
     99          newHead_y = oldHead_y;
    100         break;
    101     case 2:    newHead_x = oldHead_x + 1;    // 向下
    102          newHead_y = oldHead_y; 
    103         break;
    104     case 3: newHead_x = oldHead_x;        // 向左
    105          newHead_y = oldHead_y - 1; 
    106         break;
    107     case 4: newHead_x = oldHead_x;        // 向右
    108          newHead_y = oldHead_y + 1;
    109         break;
    110     }
    111 
    112     // 判断是否吃到食物
    113     if (canvas[newHead_x][newHead_y] == -2) {
    114         // 食物消失
    115         canvas[food_x][food_y] = 0;
    116         // 新事物产生
    117         srand((unsigned)time(0));
    118         food_x = rand() % (High - 5) + 2;
    119         food_y = rand() % (Width - 5) + 2;
    120         canvas[food_x][food_y] = -2;
    121         // 长度加一
    122     }
    123     else {
    124         // 没有吃到事物的时候长度不变
    125         canvas[oldTail_x][oldTail_y] = 0;        // 旧蛇尾消失
    126     }
    127 
    128 
    129     // 判断是否游戏失败:撞自身或边框
    130     if (canvas[newHead_x][newHead_y] > 0 || canvas[newHead_x][newHead_y] == -1) {
    131         cout << "游戏失败!!!" << endl;
    132         exit(0);
    133     }
    134     else
    135         canvas[newHead_x][newHead_y] = 1;
    136 }
    137 
    138 
    139 void UpdateWithoutInput() {
    140     MoveSnakeByDirection();
    141 }
    142 
    143 void UpdateWithInput() {
    144     char input;
    145     if (_kbhit()) {        // 判断是否有输入
    146         input = _getch();
    147         if (input == 'a')
    148             moveDirection = 3;
    149         if (input == 'w')
    150             moveDirection = 1;
    151         if (input == 'd')
    152             moveDirection = 4;
    153         if (input == 's')
    154             moveDirection = 2;
    155     }
    156 }
    157 
    158 int main()
    159 {
    160     Startup();        // 游戏数据初始化
    161     int i = 4;
    162     while (1) {        // 循环显示
    163         gotoxy(0, 0);
    164         Show();            // 显示画面
    165         Sleep(200);
    166         UpdateWithoutInput();        // 与用户输入无关的更新
    167         UpdateWithInput();            // 与用户输入有关的更新
    168     }
    169     return 0;
    170 }
  • 相关阅读:
    POJ 3258 (NOIP2015 D2T1跳石头)
    POJ 3122 二分
    POJ 3104 二分
    POJ 1995 快速幂
    409. Longest Palindrome
    389. Find the Difference
    381. Insert Delete GetRandom O(1)
    380. Insert Delete GetRandom O(1)
    355. Design Twitter
    347. Top K Frequent Elements (sort map)
  • 原文地址:https://www.cnblogs.com/2015-16/p/14772304.html
Copyright © 2011-2022 走看看