然后是控制台小游戏———打砖块;
现在看来只是简单的逻辑操作,可是在那时确难了我足足好几个小时;
这里是对游戏中所需要用到的枚举的定义,玩家所拥有的一些属性的定义(结构体),游戏地图的定义,
现在看来当时讲的课已经开始向面向对象和数据操作方面改变。
1 // stdafx.h : 标准系统包含文件的包含文件, 2 // 或是经常使用但不常更改的 3 // 特定于项目的包含文件 4 // 5 6 #pragma once 7 8 #include "targetver.h" 9 #include10 #include 11 12 #define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code)&0x80000?1:0) 13 14 15 //游戏菜单 16 enum 17 { 18 e_Menu_StartGame, 19 e_Menu_GameSet, 20 e_Menu_EndGame, 21 e_GO_StartGame, 22 e_GO_GameOver, 23 e_Go_WinGame, 24 }; 25 26 //撞球移动方向 27 enum 28 { 29 e_Move_LeftUp, 30 e_Move_LeftDown, 31 e_Move_RightUp, 32 e_Move_RightDown, 33 }; 34 35 36 struct nData 37 { 38 int X; 39 int Y; 40 int XOD; 41 int YOD; 42 }; 43 //游戏地图 44 static int nGameFrame[25][20] = { 45 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 46 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 47 { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 }, 48 { 1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1 }, 49 { 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1 }, 50 { 1, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1 }, 51 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 52 { 1, 0, 0, 0, 0, 0,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0, 0, 1 }, 68 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 69 { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 70 }; 71 // TODO: 在此处引用程序需要的其他头文件
然后是功能实现,这里只实现了45度角撞来撞去。
1 // 打砖块.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include6 #include"Windows.h" 7 8 nData g_Flap[5]; 9 nData g_Ball; 10 using namespace std; 11 int _tmain(int argc, _TCHAR* argv[]) 12 { 13 int nKeyState = 0; 14 int n_Ball_Move = 10; 15 bool bFlap = false; 16 bool bFlap2 = false; 17 18 //初始化挡板 19 for (int i = 0; i < 5; i++) 20 { 21 g_Flap[i].X = 24; 22 g_Flap[i].Y = i + 7; 23 } 24 25 //初始化撞球 26 g_Ball.X = 23; 27 g_Ball.Y = 9; 28 while (true) 29 { 30 system("CLS"); 31 32 //主菜单键盘输入 33 if (nKeyState < 3) 34 { 35 if (KEY_DOWN(VK_DOWN)) 36 { 37 nKeyState++; 38 if (nKeyState > 2) 39 { 40 nKeyState = 0; 41 } 42 } 43 else if (KEY_DOWN(VK_UP)) 44 { 45 nKeyState--; 46 if (nKeyState < 0) 47 { 48 nKeyState = 2; 49 } 50 } 51 else if (KEY_DOWN(VK_RETURN)) 52 { 53 if (nKeyState == e_Menu_StartGame) 54 { 55 nKeyState = e_GO_StartGame; 56 } 57 } 58 } 59 60 //挡板移动 61 else if (nKeyState == e_GO_StartGame) 62 { 63 if (KEY_DOWN(VK_LEFT)) 64 { 65 g_Flap[0].Y--; 66 if (g_Flap[0].Y == 0) 67 { 68 g_Flap[0].Y = 1; 69 } 70 for (int i = 1; i < 5; i++) 71 { 72 g_Flap[i].Y = g_Flap[i - 1].Y + 1; 73 } 74 75 } 76 else if (KEY_DOWN(VK_RIGHT)) 77 { 78 g_Flap[0].Y++; 79 if (g_Flap[0].Y == 15) 80 { 81 g_Flap[0].Y = 14; 82 } 83 for (int i = 1; i < 5; i++) 84 { 85 g_Flap[i].Y = g_Flap[i - 1].Y + 1; 86 } 87 } 88 //球开始移动 89 if (KEY_DOWN(VK_SPACE)) 90 { 91 n_Ball_Move = e_Move_LeftUp; 92 } 93 } 94 //判断球撞墙 95 if (g_Ball.Y > 17) 96 { 97 if (n_Ball_Move == e_Move_RightDown) 98 { 99 n_Ball_Move = e_Move_LeftDown; 100 } 101 else if (n_Ball_Move == e_Move_RightUp) 102 { 103 n_Ball_Move = e_Move_LeftUp; 104 } 105 106 } 107 if (g_Ball.X < 2) 108 { 109 if (n_Ball_Move == e_Move_LeftUp) 110 { 111 n_Ball_Move = e_Move_LeftDown; 112 } 113 else if (n_Ball_Move == e_Move_RightUp) 114 { 115 n_Ball_Move = e_Move_RightDown; 116 } 117 } 118 if (g_Ball.Y < 2) 119 { 120 if (n_Ball_Move == e_Move_LeftDown) 121 { 122 n_Ball_Move = e_Move_RightDown; 123 } 124 else if (n_Ball_Move == e_Move_LeftUp) 125 { 126 n_Ball_Move = e_Move_RightUp; 127 } 128 } 129 130 for (int n = 0; n < 5; n++) 131 { 132 if (g_Ball.X == g_Flap[n].X&&g_Ball.Y == g_Flap[n].Y) 133 { 134 bFlap2 = true; 135 break; 136 } 137 else 138 { 139 bFlap2 = false; 140 } 141 } 142 if (g_Ball.X > 23) 143 { 144 if (bFlap2 == true) 145 { 146 if (n_Ball_Move == e_Move_RightDown) 147 { 148 n_Ball_Move = e_Move_RightUp; 149 } 150 else if (n_Ball_Move == e_Move_LeftDown) 151 { 152 n_Ball_Move = e_Move_LeftUp; 153 } 154 } 155 else 156 { 157 nKeyState = e_GO_GameOver; 158 } 159 } 160 161 //打到砖块 162 if (nGameFrame[g_Ball.X][g_Ball.Y] == 2) 163 { 164 if (n_Ball_Move == e_Move_LeftDown) 165 { 166 n_Ball_Move = e_Move_LeftUp; 167 } 168 else if (n_Ball_Move == e_Move_LeftUp) 169 { 170 n_Ball_Move = e_Move_LeftDown; 171 } 172 else if (n_Ball_Move == e_Move_RightDown) 173 { 174 n_Ball_Move = e_Move_RightUp; 175 } 176 else if (n_Ball_Move == e_Move_RightUp) 177 { 178 n_Ball_Move = e_Move_RightDown; 179 } 180 nGameFrame[g_Ball.X][g_Ball.Y] = 0; 181 } 182 183 184 185 //球运动事件 186 187 if (n_Ball_Move == e_Move_LeftUp) 188 { 189 g_Ball.X--; 190 g_Ball.Y--; 191 } 192 else if (n_Ball_Move == e_Move_LeftDown) 193 { 194 g_Ball.X++; 195 g_Ball.Y--; 196 } 197 else if (n_Ball_Move == e_Move_RightDown) 198 { 199 g_Ball.X++; 200 g_Ball.Y++; 201 } 202 else if (n_Ball_Move == e_Move_RightUp) 203 { 204 g_Ball.X--; 205 g_Ball.Y++; 206 } 207 208 209 210 211 //渲染 212 if (nKeyState < 3) 213 { 214 cout << "■■■■■■■■■■■■■■■■■" << endl; 215 cout << "■■■■■■■■■■■■■■■■■" << endl; 216 cout << "■■■■ ■■■■" << endl; 217 if (nKeyState == e_Menu_StartGame) 218 { 219 cout << "■■■■ >-开始游戏 ■■■■" << endl; 220 cout << "■■■■ 游戏设置 ■■■■" << endl; 221 cout << "■■■■ 游戏结束 ■■■■" << endl; 222 } 223 else if (nKeyState == e_Menu_GameSet) 224 { 225 cout << "■■■■ 开始游戏 ■■■■" << endl; 226 cout << "■■■■ >-游戏设置 ■■■■" << endl; 227 cout << "■■■■ 游戏结束 ■■■■" << endl; 228 } 229 else if (nKeyState == e_Menu_EndGame) 230 { 231 cout << "■■■■ 开始游戏 ■■■■" << endl; 232 cout << "■■■■ 游戏设置 ■■■■" << endl; 233 cout << "■■■■ >-游戏结束 ■■■■" << endl; 234 } 235 cout << "■■■■ ■■■■" << endl; 236 cout << "■■■■■■■■■■■■■■■■■" << endl; 237 cout << "■■■■■■■■■■■■■■■■■" << endl; 238 } 239 else if (nKeyState == e_GO_StartGame) 240 { 241 for (int i = 0; i < 25; i++) 242 { 243 for (int a = 0; a < 20; a++) 244 { 245 for (int n = 0; n < 5; n++) 246 { 247 if (i == g_Flap[n].X&&a == g_Flap[n].Y) 248 { 249 bFlap = true; 250 break; 251 } 252 else 253 { 254 bFlap = false; 255 } 256 } 257 if (nGameFrame[i][a] == 1) 258 { 259 cout << "■"; 260 } 261 else if (bFlap == true) 262 { 263 cout << "█"; 264 } 265 else if (nGameFrame[i][a] == 2) 266 { 267 cout << "▓"; 268 } 269 else if (i==g_Ball.X&&a==g_Ball.Y) 270 { 271 cout << "●"; 272 } 273 else 274 { 275 cout << " "; 276 } 277 } 278 cout << endl; 279 } 280 } 281 else if (nKeyState == e_GO_GameOver) 282 { 283 cout << "YOU LOST !!!" << endl; 284 } 285 } 286 return 0; 287 }