• 12 c++版本的坦克大战


    前言

    呵呵 这大概是 大学里面的 c++ 贪吃蛇了吧 

    有一些 面向对象的理解, 但是不多 

    这里 具体的实现 就不赘述, 仅仅是 发一下代码 以及 具体的使用 

     

     

    坦克大战

    1. #include<iostream>
    2. #include<windows.h>
    3. #include<conio.h>
    4. #include<ctime>
    5. #include<string>
    6. #include<list>
    7. using namespace std;
    8. #define N 4
    9. class Tank_War;
    10. void set_pos(int x, int y);
    11. class Object
    12. {
    13. protected:
    14. int x, y;
    15. int dir;
    16. int life;
    17. public:
    18. Object() : x(0), y(0), dir(-1),life(1) {}
    19. Object(int x, int y, int dir, int life) : x(x), y(y), dir(dir), life(life) {}
    20. void move();
    21. void paint() { }
    22. void reduce_blood() { life -= 1; }
    23. void increase_blood() { life += 1; }
    24. bool is_failure();
    25. virtual void print(){cout<<x<<"--"<<y<<"=="<<life<<"--"<<endl;}
    26. };
    27. class Missile : public Object
    28. {
    29. public:
    30. Missile() : Object() {}
    31. Missile(int x, int y, int dir, int life) : Object(x, y, dir, life) {}
    32. bool move();
    33. void paint(string icon);
    34. int get_x()const { return x; }
    35. int get_y()const { return y; }
    36. void set_info(int x, int y, int dir) { this -> x = x; this -> y = y; this -> dir = dir; };
    37. bool operator==(const Missile&_x){return (x==_x.x && y==_x.y&&dir==_x.dir); } //list中remove是需要重载==
    38. };
    39. class Tank : public Object
    40. {
    41. private:
    42. int pre_dir;
    43. list<Missile> missile_list;
    44. public:
    45. Tank() : Object(), pre_dir(-1) {}
    46. Tank(int x, int y, int dir, int life) : Object(x, y, dir,life) {pre_dir = dir;};
    47. void del_before_move();
    48. void move(Tank_War &game, string name);
    49. void paint(string icon);
    50. int get_x() const { return x; }
    51. int get_y() const { return y; }
    52. int get_dir()const { return dir; }
    53. void set_dir(int dir){ pre_dir = this -> dir; this -> dir = dir; }
    54. void set_location(int x, int y){ this -> x = x; this -> y = y; }
    55. void emission_missile();
    56. void missile_move();
    57. void check_missile(Tank_War &game, string name);
    58. bool check_missile_strike(Missile& missile, Tank& tank);
    59. // bool check_is_right(int x, int y, int dir);
    60. int distance_squre(int x_1, int y_1, int x_2, int y_2);
    61. int distance_squre(Tank& tank_2);
    62. bool operator==(const Tank&_x){return (x==_x.x && y==_x.y&&dir==_x.dir); }
    63. };
    64. class Tank_War
    65. {
    66. private:
    67. Tank lead;
    68. list<Tank> enemy_list;
    69. int tot;
    70. int grade;
    71. bool over;
    72. public:
    73. Tank_War():lead(28, 21, -1, 4), grade(0), tot(0), over(false) {};
    74. int game();
    75. void game_over() { over = true; }
    76. int draw_menu();
    77. void draw_game_interface();
    78. Tank* get_lead() { return &lead; }
    79. list<Tank>::iterator get_enemy_list_begin() { return enemy_list.begin(); }
    80. list<Tank>::iterator get_enemy_list_end() { return enemy_list.end(); }
    81. Tank* create_enemy();
    82. void enemy_move();
    83. // bool check_is_right(int x, int y, int dir);
    84. bool can_move(Tank &tank, string name);
    85. bool can_move(Tank& x, Tank& y);
    86. void check_missile(string name);
    87. void supply_enemy();
    88. int get_random_direction();
    89. list<Tank>::iterator remove_from_enemy_list(list<Tank>::iterator it) { return enemy_list.erase(it); }
    90. void reduce_tot() { tot--; }
    91. void increase_grade() { grade+=5;set_pos(68, 3);cout<<grade; }
    92. };
    93. int main()
    94. {
    95. Tank_War g;
    96. g.game();
    97. system("pause");
    98. return 0;
    99. }
    100. int Tank_War::game()
    101. {
    102. bool is_move = true;
    103. int speed = draw_menu();
    104. system("cls");
    105. draw_game_interface();
    106. supply_enemy();
    107. while(!over)
    108. {
    109. if(!lead.is_failure())
    110. {
    111. if(tot<N) supply_enemy();
    112. if(GetAsyncKeyState(VK_UP)) lead.set_dir(-1);
    113. else if(GetAsyncKeyState(VK_DOWN)) lead.set_dir(1);
    114. else if(GetAsyncKeyState(VK_LEFT)) lead.set_dir(-2);
    115. else if(GetAsyncKeyState(VK_RIGHT)) lead.set_dir(2);
    116. else is_move = false;
    117. if(is_move) lead.move(*this, "lead");
    118. enemy_move();
    119. if(GetAsyncKeyState(VK_RETURN)) lead.emission_missile();
    120. if(enemy_list.size() != 0)
    121. {
    122. check_missile("lead");
    123. check_missile("enemy");
    124. }
    125. }
    126. is_move = true;
    127. lead.missile_move();
    128. Sleep(100);
    129. }
    130. return 0;
    131. }
    132. void set_pos(int x, int y)
    133. {
    134. HANDLE cursor = GetStdHandle(STD_OUTPUT_HANDLE);
    135. COORD position = {x, y};
    136. SetConsoleCursorPosition(cursor, position);
    137. }
    138. void draw_rect(int x_1, int y_1, int x_2, int y_2, string icon)
    139. {
    140. if(x_1 > x_2) swap(x_1, x_2);
    141. if(y_1 > y_2) swap(y_1, y_2);
    142. int n_1 = y_2 - y_1 + 1;
    143. int n_2 = (x_2 - x_1)/icon.size() + 1;
    144. for(int i=0; i<n_1; i++)
    145. {
    146. set_pos(x_1, y_1+i);
    147. for(int j=0; j<n_2; j++)
    148. cout<<icon;
    149. }
    150. }
    151. int Tank_War::draw_menu()
    152. {
    153. int speed = 0;
    154. draw_rect(0, 3, 79, 3, "-");
    155. draw_rect(0, 5, 79, 5, "-");
    156. set_pos(25, 2);
    157. cout<<"welcome to airplane war games ";
    158. set_pos(25, 4);
    159. cout<<"↑ up ↓ down enter confirm";
    160. set_pos(20, 10);
    161. cout<<"1.easy easy mode enemy move slow";
    162. set_pos(20, 14);
    163. cout<<"2.difficult difficult mode enemy move fast";
    164. set_pos(20, 20);
    165. cout<<"made by He Xiong . begin at 2014_01_08";
    166. draw_rect(0, 19, 79, 19, "-");
    167. draw_rect(0, 22, 79, 22, "-");
    168. set_pos(18, 10);
    169. cout<<"->";
    170. int y = 10;
    171. while(true)
    172. {
    173. if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(VK_DOWN))
    174. {
    175. if(y == 10)
    176. {
    177. set_pos(18, y); cout<<" ";
    178. y = 14;
    179. set_pos(18, y); cout<<"->";
    180. speed = 1;
    181. }
    182. else if(y == 14)
    183. {
    184. set_pos(18, y); cout<<" ";
    185. y = 10;
    186. set_pos(18, y); cout<<"->";
    187. speed = 0;
    188. }
    189. }
    190. else if(GetAsyncKeyState(VK_RETURN)) break;
    191. Sleep(200);
    192. }
    193. return speed;
    194. }
    195. void Tank_War::draw_game_interface()
    196. {
    197. draw_rect(0, 0, 78, 0, "◆");
    198. draw_rect(0, 23, 78, 23, "◆");
    199. draw_rect(0, 1, 0, 22, "◆");
    200. draw_rect(58, 1, 58, 22, "◆");
    201. draw_rect(78, 1, 78, 22, "◆");
    202. int x = 62;
    203. set_pos(x, 3); cout<<"GRADE:0";
    204. set_pos(x, 5); cout<<"LEVEL:1";
    205. set_pos(62, 7); cout<<"↑ UP";
    206. set_pos(x, 9); cout<<"↓ down";
    207. set_pos(x, 11); cout<<"← left";
    208. set_pos(x, 13); cout<<"→ right";
    209. set_pos(x, 17); cout<<"life:■■";
    210. set_pos(x+5, 18); cout<<"■■";
    211. lead.set_location(28, 10);
    212. lead.paint("■");
    213. set_pos(0, 0);
    214. }
    215. Tank* Tank_War::create_enemy()
    216. {
    217. int n = 0, k = 0;
    218. int x = 0, y = 0, dir = 0;
    219. Tank *p = NULL;
    220. srand((unsigned)time(NULL));
    221. while(k != 4)
    222. {
    223. n = rand()%2;
    224. if(n%2 == 0)
    225. {
    226. x = 4; n = rand()%2;
    227. if(n%2 == 0) y = 2;
    228. else y = 21;
    229. }
    230. else
    231. {
    232. x = 54; n = rand()%2;
    233. if(n%2 == 0) y = 2;
    234. else y = 21;
    235. }
    236. dir = get_random_direction();
    237. p = new Tank(x, y, dir, 1);
    238. enemy_list.push_back(*p);
    239. if(can_move(*p, "enemy")){ break; }
    240. else { enemy_list.pop_back(); delete p; p = NULL; }
    241. k++;
    242. }
    243. return p;
    244. }
    245. /*
    246. bool Tank::check_is_right(int x, int y, int dir)
    247. {
    248. if(distance_squre(x, y, this -> x, this -> y) > 9)
    249. return true;
    250. else return false;
    251. }
    252. *//*
    253. bool Tank_War::check_is_right(int x, int y, int dir)
    254. {
    255. int flag = 0;
    256. if(!lead.check_is_right(x, y, dir)) ++flag;
    257. list<Tank>::iterator it = NULL;
    258. for(it=enemy_list.begin(); it!=enemy_list.end(); ++it)
    259. if(!it -> check_is_right(x, y, dir)) ++flag;
    260. if(flag == 0) return true;
    261. else return false;
    262. }
    263. */
    264. void Tank_War::supply_enemy()
    265. {
    266. int n = N - tot;
    267. Tank *p = NULL;
    268. for(int i=0; i<n; i++)
    269. {
    270. if((p = create_enemy()) != NULL)
    271. {
    272. tot++; p -> paint("■");
    273. delete p; p = NULL;
    274. }
    275. }
    276. }
    277. void Missile::paint(string icon)
    278. {
    279. set_pos(x, y); cout<<icon;
    280. }
    281. void Tank::paint(string icon)
    282. {
    283. draw_rect(x-2, y, x+2, y, icon);
    284. draw_rect(x, y-1, x, y+1, icon);
    285. if((dir+2)%2 == 0)
    286. {
    287. set_pos(x-dir, y-1); cout<<icon;
    288. set_pos(x-dir, y+1); cout<<icon;
    289. }
    290. else if((dir+2)%2 == 1)
    291. {
    292. set_pos(x-2, y-dir); cout<<icon;
    293. set_pos(x+2, y-dir); cout<<icon;
    294. }
    295. /* //不是很好,前面两点的会闪
    296. set_pos(x-2, y-1);
    297. cout<<"■■■";
    298. set_pos(x-2, y);
    299. cout<<"■■■";
    300. set_pos(x-2, y+1);
    301. cout<<"■■■";
    302. if((dir+2)%2 == 0)
    303. {
    304. set_pos(x+dir, y-1); cout<<" ";
    305. set_pos(x+dir, y+1); cout<<" ";
    306. draw_rect(x-dir, y-1, x-dir, y+1);
    307. }
    308. else if((dir+2)%2 == 1)
    309. {
    310. set_pos(x-1, y+dir); cout<<" ";
    311. set_pos(x+1, y+dir); cout<<" ";
    312. draw_rect(x-2, y-dir, x+2, y-dir);
    313. }
    314. */
    315. }
    316. bool Missile::move()
    317. {
    318. if(x>2 && x<56 && y>1 && y <22)
    319. {
    320. if((dir+2)%2 == 0)
    321. {
    322. set_pos(x, y); cout<<" ";
    323. x += dir; paint("●");
    324. }
    325. else if((dir+2)%2 == 1)
    326. {
    327. set_pos(x, y); cout<<" ";
    328. y += dir; paint("●");
    329. }
    330. return true;
    331. }
    332. else
    333. {
    334. if(x==0 || x==58 || y==0 || y==23) ;
    335. else
    336. {
    337. set_pos(x, y); cout<<" ";
    338. }
    339. return false;
    340. }
    341. }
    342. void Tank::move(Tank_War& game, string name)
    343. {
    344. if(x>2 && x<56 && y>1 && y <22)
    345. if((dir+2)%2 == 0)
    346. {
    347. Tank p(x+dir, y, dir, 1);
    348. if(x+dir>2 && x+dir<56)
    349. {
    350. if(game.can_move(p, name))
    351. {
    352. //draw_rect(x-dir, y-1, x-dir, y+1, " ");
    353. //draw_rect(x+dir, y, x+dir, y, " "); //为了解决在角落是的BUG
    354. del_before_move();
    355. x+=dir;
    356. paint("■");
    357. }
    358. }
    359. else
    360. {
    361. if(pre_dir > 0) draw_rect(x+dir, y-1, x+dir, y-1, " ");
    362. else draw_rect(x+dir, y+1, x+dir, y+1, " ");
    363. paint("■");
    364. }
    365. }
    366. else
    367. {
    368. Tank p(x, y+dir, dir, 1);
    369. if(y+dir>1 && y+dir<22)
    370. {
    371. if(game.can_move(p, name))
    372. {
    373. del_before_move();
    374. y+=dir;
    375. paint("■");
    376. }
    377. }
    378. else
    379. {
    380. if(pre_dir > 0) draw_rect(x-2, y+dir, x-2, y+dir, " ");
    381. else draw_rect(x+2, y+dir, x+2, y+dir, " ");
    382. paint("■");
    383. }
    384. }
    385. }
    386. void Tank::del_before_move()
    387. {
    388. if(pre_dir == dir)
    389. if((dir + 2) % 2 == 0) draw_rect(x-dir, y-1, x-dir, y+1, " ");
    390. else draw_rect(x-2 , y-dir, x+2, y-dir, " ");
    391. else if(pre_dir == -dir)
    392. if((dir + 2) % 2 == 0) draw_rect(x+pre_dir, y, x+pre_dir, y, " ");
    393. else draw_rect(x , y+pre_dir, x, y+pre_dir, " ");
    394. else if(pre_dir == 2/dir)
    395. if((dir + 2) % 2 == 0) draw_rect(x-dir, y, x-dir, y-pre_dir, " ");
    396. else draw_rect(x, y-dir, x-pre_dir, y-dir, " ");
    397. else
    398. if((dir + 2) % 2 == 0) draw_rect(x-dir, y, x-dir, y-pre_dir, " ");
    399. else draw_rect(x, y-dir, x-pre_dir, y-dir, " ");
    400. }
    401. bool Tank_War::can_move(Tank& tank, string name)
    402. {
    403. if(name == "lead")
    404. {
    405. list<Tank>::iterator it = NULL;
    406. if(enemy_list.size() != 0)
    407. {
    408. for(it=enemy_list.begin(); it!=enemy_list.end(); it++)
    409. {
    410. if(!can_move(*it, tank)) return false;
    411. }
    412. }
    413. }
    414. else
    415. {
    416. int flag = 0;
    417. if(!can_move(lead, tank)) return false;
    418. list<Tank>::iterator it = NULL;
    419. if(enemy_list.size() != 0)
    420. {
    421. for(it=enemy_list.begin(); it!=enemy_list.end(); it++)
    422. {
    423. if(!can_move(*it, tank)) flag++;
    424. /*
    425. if(tank == *it)
    426. {
    427. if(flag) flag = false;
    428. else return false;
    429. }
    430. else
    431. {
    432. if(!can_move(tank,tank.get_dir() , *it)) return false;
    433. }
    434. */
    435. }
    436. if(flag!=1) return false;
    437. else return true;
    438. }
    439. }
    440. return true;
    441. }
    442. bool Tank_War::can_move(Tank& tank_1, Tank& tank_2)
    443. {
    444. int x_1 = 0, x_2 = 0, y_1 = 0, y_2 = 0;
    445. int dis = tank_1.distance_squre(tank_2);
    446. if(dis > 8) return true;
    447. else if(dis == 8)
    448. {
    449. if((tank_1.get_dir()+2)%2 == 0)
    450. {
    451. if(tank_1.get_dir() > 0)
    452. {
    453. x_1 = tank_1.get_x(); x_2 = tank_2.get_x();
    454. y_1 = tank_1.get_y(); y_2 = tank_2.get_y();
    455. }
    456. else
    457. {
    458. x_1 = tank_2.get_x(); x_2 = tank_1.get_x();
    459. y_1 = tank_2.get_y(); y_2 = tank_1.get_y();
    460. }
    461. if(x_1 > x_2)
    462. {
    463. if(y_1 > y_2)
    464. {
    465. if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == 2/tank_1.get_dir()) return true;
    466. else return false;
    467. }
    468. else
    469. {
    470. if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir()) return true;
    471. else return false;
    472. }
    473. }
    474. else return true;
    475. }
    476. else
    477. {
    478. if(tank_1.get_dir() > 0)
    479. {
    480. x_1 = tank_1.get_x(); x_2 = tank_2.get_x();
    481. y_1 = tank_1.get_y(); y_2 = tank_2.get_y();
    482. }
    483. else
    484. {
    485. x_1 = tank_2.get_x(); x_2 = tank_1.get_x();
    486. y_1 = tank_2.get_y(); y_2 = tank_1.get_y();
    487. }
    488. if(y_1 > y_2)
    489. {
    490. if(x_1 > x_2)
    491. {
    492. if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == 2/tank_1.get_dir()) return true;
    493. else return false;
    494. }
    495. else
    496. {
    497. if(tank_2.get_dir() == tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir()) return true;
    498. else return false;
    499. }
    500. }
    501. else return true;
    502. }
    503. }
    504. else if(dis == 5)
    505. { //static int d=0;d++;set_pos(20,20+d);cout<<dis<<"D"<<endl;
    506. if((tank_1.get_dir()+2)%2 == 0)
    507. {
    508. if(tank_1.get_dir() > 0)
    509. {
    510. x_1 = tank_2.get_x(); x_2 = tank_1.get_x();
    511. y_1 = tank_2.get_y(); y_2 = tank_1.get_y();
    512. }
    513. else
    514. {
    515. x_1 = tank_1.get_x(); x_2 = tank_2.get_x();
    516. y_1 = tank_1.get_y(); y_2 = tank_2.get_y();
    517. }
    518. if(x_1 > x_2)
    519. {
    520. if(y_1 > y_2)
    521. {
    522. if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir()) return true;
    523. else return false;
    524. }
    525. else
    526. {
    527. if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir()) return true;
    528. else return false;
    529. }
    530. }
    531. else return false;
    532. }
    533. else
    534. {
    535. if(tank_1.get_dir() < 0)
    536. {
    537. x_1 = tank_1.get_x(); x_2 = tank_2.get_x();
    538. y_1 = tank_1.get_y(); y_2 = tank_2.get_y();
    539. }
    540. else
    541. {
    542. x_1 = tank_2.get_x(); x_2 = tank_1.get_x();
    543. y_1 = tank_2.get_y(); y_2 = tank_1.get_y();
    544. }
    545. if(y_1 > y_2)
    546. {
    547. if(x_1 > x_2)
    548. {
    549. if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == -2/tank_1.get_dir()) return true;
    550. else return false;
    551. }
    552. else
    553. {
    554. if(tank_2.get_dir() == -tank_1.get_dir() || tank_2.get_dir() == 2/tank_1.get_dir()) return true;
    555. else return false;
    556. }
    557. }
    558. else return false;
    559. }
    560. }
    561. else return false;
    562. }
    563. bool Object::is_failure()
    564. {
    565. if(life != 0) return false;
    566. else return true;
    567. }
    568. void Tank::emission_missile()
    569. {
    570. Missile p;
    571. if((dir+2)%2 == 0)
    572. p.set_info(x+2*dir, y, dir);
    573. else if((dir+2)%2 == 1)
    574. p.set_info(x, y+2*dir, dir);
    575. missile_list.push_back(p);
    576. if(p.get_x()==0 || p.get_x()==58 || p.get_y()==0 || p.get_y()==23) ;
    577. else p.paint("■");
    578. Sleep(100);
    579. }
    580. void Tank::missile_move()
    581. {
    582. list<Missile>::iterator it = NULL;
    583. if(missile_list.size() != 0) //这里一定要判空
    584. for(it=missile_list.begin(); it!=missile_list.end(); it++)
    585. {
    586. if(!it -> move())
    587. it = missile_list.erase(it); //删除此步后当前迭代器会“失效”
    588. }
    589. }
    590. void Tank_War::check_missile(string name)
    591. {
    592. if(name == "lead")
    593. lead.check_missile(*this, "lead");
    594. else
    595. {
    596. list<Tank>::iterator it = NULL;
    597. for(it = enemy_list.begin(); it != enemy_list.end(); it++)
    598. it -> check_missile(*this, "enemy");
    599. }
    600. }
    601. void Tank::check_missile(Tank_War &game, string name)
    602. {
    603. list<Missile>::iterator it_1 = NULL;
    604. list<Tank>::iterator it_2 = NULL;
    605. if(name == "lead")
    606. {
    607. if(missile_list.size() != 0)
    608. for(it_1=missile_list.begin(); it_1!= missile_list.end(); it_1++)
    609. {
    610. for(it_2=game.get_enemy_list_begin(); it_2!=game.get_enemy_list_end(); it_2++)
    611. if(it_1 != missile_list.end())
    612. if(check_missile_strike(*it_1, *it_2))
    613. {
    614. it_1 = missile_list.erase(it_1);
    615. if(it_2 -> is_failure())
    616. {
    617. game.increase_grade();
    618. it_2 -> paint(" ");
    619. it_2 = game.remove_from_enemy_list(it_2);
    620. game.reduce_tot();
    621. }
    622. }
    623. }
    624. }
    625. else
    626. {
    627. if(missile_list.size() != 0)
    628. {
    629. if(it_1 != missile_list.end())
    630. for(it_1=missile_list.begin(); it_1!= missile_list.end(); it_1++)
    631. if(check_missile_strike(*it_1, *(game.get_lead())))
    632. {
    633. it_1 = missile_list.erase(it_1);
    634. if(game.get_lead() -> is_failure()) game.game_over();
    635. else
    636. {
    637. game.get_lead() -> paint(" ");
    638. game.get_lead() -> set_location(28, 10);
    639. game.get_lead() -> paint("■");
    640. }
    641. }
    642. if(it_1 != missile_list.end())
    643. for(it_1=missile_list.begin(); it_1!= missile_list.end(); it_1++)
    644. {
    645. for(it_2=game.get_enemy_list_begin(); it_2!=game.get_enemy_list_end(); it_2++)
    646. if(check_missile_strike(*it_1, *it_2))
    647. {
    648. it_1 = missile_list.erase(it_1);
    649. it_2 -> increase_blood();
    650. }
    651. }
    652. }
    653. }
    654. }
    655. bool Tank::check_missile_strike(Missile& missile, Tank& tank)
    656. {
    657. int dis = 0;
    658. if((dis = distance_squre(missile.get_x(), missile.get_y(), tank.get_x(), tank.get_y())) <= 2)
    659. {
    660. if(dis == 2)
    661. {
    662. }
    663. else
    664. {
    665. missile.reduce_blood();
    666. tank.reduce_blood();
    667. missile.paint(" ");
    668. return true;
    669. }
    670. }
    671. return false;
    672. }
    673. void Tank_War::enemy_move()
    674. {
    675. int n = 0;
    676. srand((unsigned)time(NULL));
    677. list<Tank>::iterator it = NULL;
    678. if(enemy_list.size() != 0)
    679. {
    680. for(it=enemy_list.begin(); it != enemy_list.end(); it++)
    681. {
    682. it -> missile_move();
    683. n = rand()%3;
    684. if(n == 0)
    685. if(can_move(*it, "enemy"))
    686. {
    687. n = get_random_direction();
    688. it -> set_dir(n);
    689. it->move(*this, "enemy");
    690. }
    691. n = rand()%8;
    692. if(n == 0) it -> emission_missile();
    693. }
    694. }
    695. }
    696. int Tank::distance_squre(int x_1, int y_1, int x_2, int y_2)
    697. {
    698. return (x_2 - x_1) * (x_2 - x_1) + (y_2 - y_1) * (y_2 - y_1);
    699. }
    700. int Tank::distance_squre(Tank& tank_2)
    701. {
    702. return distance_squre(x/2, y, tank_2.get_x()/2, tank_2.get_y());
    703. }
    704. int Tank_War::get_random_direction()
    705. {
    706. int n = 0;
    707. srand((unsigned)time(NULL));
    708. if((n = rand()%4) < 2) n -= 2;
    709. else n -= 1;
    710. return n;
    711. }

     

     

    程序截图

    首页

    6e5ec33a8cee48ec9a8b3815a83072c7.png

     

     

    程序执行界面

    e0bd744179ac47c89c6b96f0c347b0ba.png

     

     

     

     

     

  • 相关阅读:
    centos 7.9 源码安装htop
    tkinter制做一个音乐下载小软件,多种音乐免费听
    【英雄哥六月集训】第 27天: 图
    EXCEL里数值列如何显示序号?如何重新排序? 怎么取得排序后的序号?
    云原生k8s的前世今生--Docker
    【学习心得】爬虫JS逆向通解思路
    逆向大漠插件/用VB6.0实现后台鼠标移动和后台鼠标左键点击
    大语言模型可以学习规则11.13
    BUUCTF Reverse/firmware
    Target EDI 850 采购订单报文详解
  • 原文地址:https://blog.csdn.net/u011039332/article/details/130783452