C++ 控制台推箱子小游戏

 
 
 
 
 
  1 // 游戏菜单.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include<iostream>
  6 #include"windows.h"
  7 #define KEY_DOWN(vk_code)              (GetAsyncKeyState(vk_code)&0x8000?1:0)
  8 
  9 
 10 
 11 
 12 using namespace std;
 13 
 14 
 15 sDATA g_Player;
 16 
 17 sDATA g_Box[3];
 18 
 19 int _tmain(int argc, _TCHAR* argv[])
 20 {
 21     int nKeyState = 0;
 22     bool bBox = false;
 23     int nBoxWin = 0;
 24 
 25     //初始化人物
 26     g_Player.X = 5;
 27     g_Player.Y = 6;
 28     g_Player.XOD = 5;
 29     g_Player.YOD = 6;
 30     //初始化箱子
 31     for (int m = 0; m < 3; m++)
 32     {
 33         g_Box[m].X = m + 7;
 34         g_Box[m].Y = m + 8;
 35         g_Box[m].XOD = m + 7;
 36         g_Box[m].YOD = m + 8;
 37     }
 38 
 39     //键盘输入
 40     while (true)
 41     {
 42         system("CLS");
 43 
 44         if (nKeyState < 3)
 45         {
 46             if (KEY_DOWN(VK_DOWN))
 47             {
 48                 nKeyState = abs(++nKeyState % 3);
 49             }
 50             else if (KEY_DOWN(VK_UP))
 51             {
 52                 --nKeyState;
 53                 if (nKeyState < 0)
 54                 {
 55                     nKeyState = 2;
 56                 }
 57             }
 58             if (KEY_DOWN(VK_RETURN))
 59             {
 60                 if (nKeyState == n_Menu_PlayGame)
 61                 {
 62                     nKeyState = n_GO_PlayGame;
 63                 }
 64             }
 65         }
 66 
 67 
 68         //备份箱子坐标
 69         for (int m = 0; m < 3; m++)
 70         {
 71             g_Box[m].XOD = g_Box[m].X;
 72             g_Box[m].YOD = g_Box[m].Y;
 73         }
 74         //推箱子
 75         for (int m = 0; m < 3; m++)
 76         {
 77             if (g_Player.X == g_Box[m].X && g_Player.Y == g_Box[m].Y)
 78             {
 79                 g_Box[m].X += (g_Player.X - g_Player.XOD);
 80                 g_Box[m].Y += (g_Player.Y - g_Player.YOD);
 81             }
 82         }
 83         //判断箱子是否撞墙
 84         for (int m = 0; m < 3; m++)
 85         {
 86             if (nGameFrame[g_Box[m].X][g_Box[m].Y] == 1)
 87             { 
 88                 g_Box[m].X = g_Box[m].XOD;
 89                 g_Box[m].Y = g_Box[m].YOD;
 90                 g_Player.X = g_Player.XOD;
 91                 g_Player.Y = g_Player.YOD;
 92             }
 93         }
 94         //判断箱子是否撞箱子
 95         for (int m = 0; m < 3; m++)
 96         {
 97             for (int j = 0; j < 3; j++)
 98             {
 99                 if (m != j&&g_Box[m].X == g_Box[j].X&&g_Box[m].Y == g_Box[j].Y)
100                 {
101                     g_Box[m].X = g_Box[m].XOD;
102                     g_Box[m].Y = g_Box[m].YOD;
103                     g_Player.X = g_Player.XOD;
104                     g_Player.Y = g_Player.YOD;
105                 }
106             }
107         }
108 
109         //判断胜利
110         for (; nBoxWin < 3; nBoxWin++)
111         {
112             if (nGameFrame[g_Box[nBoxWin].X][g_Box[nBoxWin].Y] != 2)
113             {
114                 break;
115             }
116         }
117         if (nBoxWin >= 3)
118         {
119             nKeyState = n_Go_Win;
120         }
121 
122         // 渲染
123         if (nKeyState < 3)
124         {
125             cout << "■■■■■■■■■■■■" << endl;
126             cout << "■■■■■■■■■■■■" << endl;
127             if (nKeyState == n_Menu_PlayGame)
128             {
129                 cout << "■■  >-开始游戏    ■■" << endl;
130                 cout << "■■    游戏设置    ■■" << endl;
131                 cout << "■■    结束游戏    ■■" << endl;
132 
133             }
134             else if (nKeyState == n_Menu_GameSet)
135             {
136                 cout << "■■    开始游戏    ■■" << endl;
137                 cout << "■■  >-游戏设置    ■■" << endl;
138                 cout << "■■    结束游戏    ■■" << endl;
139             }
140             else if (nKeyState == n_Menu_EndGame)
141             {
142                 cout << "■■    开始游戏    ■■" << endl;
143                 cout << "■■    游戏设置    ■■" << endl;
144                 cout << "■■  >-结束游戏    ■■" << endl;
145             }
146             cout << "■■■■■■■■■■■■" << endl;
147             cout << "■■■■■■■■■■■■" << endl;
148         }
149         else if (nKeyState == n_GO_PlayGame)
150         {
151             for (int i = 0; i < 20; i++)
152             {
153                 for (int a = 0; a < 20; a++)
154                 {
155                     for (int m = 0; m < 3; m++)
156                     {
157                         if (i==g_Box[m].X&&a==g_Box[m].Y)
158                         {
159                             bBox = true;
160                             break;
161                         }
162                         else
163                         {
164                             bBox = false;
165                         }
166                         
167                     }
168                     if (nGameFrame[i][a] == 1)
169                     {
170                         cout << "";
171                     }
172                     else if (i==g_Player.X&&a==g_Player.Y)
173                     {
174                         cout << "";
175                     }
176                     else if (bBox == true)
177                     {
178                         cout << "¤";
179                     }
180                     else if (nGameFrame[i][a] == 2)
181                     {
182                         cout << "×";
183                     }
184                     else
185                     {
186                         cout << "  ";
187                     }
188                 }
189                 cout << endl;
190             }
191         }
192         else if (nKeyState == n_Go_Win)
193         {
194             cout << "胜利!" << endl;
195         }
196 
197 
198         //人物移动
199         g_Player.XOD = g_Player.X;
200         g_Player.YOD = g_Player.Y;
201         if (KEY_DOWN(VK_UP))
202         {
203             g_Player.X--;
204         }
205         else if (KEY_DOWN(VK_DOWN))
206         {
207             g_Player.X++;
208         }
209         else if (KEY_DOWN(VK_LEFT))
210         {
211             g_Player.Y--;
212         }
213         else if (KEY_DOWN(VK_RIGHT))
214         {
215             g_Player.Y++;
216         }
217 
218         if (nGameFrame[g_Player.X][g_Player.Y] == 1)
219         {
220             g_Player.X = g_Player.XOD;
221             g_Player.Y = g_Player.YOD;
222         }
223 
224     }
225 
226 
227 
228     return 0;
229 }
控制台推箱子小游戏

 

 

posted @ 2018-09-03 21:25  梦涵的帅爸爸  阅读(983)  评论(0编辑  收藏  举报