-
个人简介
猜数字
#include <bits/stdc++.h> #include <fstream> using namespace std; long long n=0,a=0,l=1,r=0,lr=0,lr2=0,j=5,ji=1,jing=10,mm=0,mm1=0,jingjing=1,jj=1,jjing=0,jingzhi=0,cishu=1; string name,mima; void read(){ ifstream fin("游戏!!!"); fin>>n>>a>>l>>r>>lr>>j>>ji>>jing>>cishu>>name>>mima; fin.close(); } void save() { ofstream fout("游戏!!!"); fout<<n<<" "<<a<<" "<<l<<" "<<r<<" "<<lr<<" "<<j<<" "<<ji<<" "<<jing<<" "<<cishu<<" "<<name<<" "<<mima; fout.close(); } void jz(){ for (int i=1;i<=5;i++){ cout<<"加载中"; for (int j=1;j<=8;j++){ for (int k=1;k<=60000000;k++){ } cout<<'.'; } system("cls"); } } int sjs(){ srand((unsigned)time(NULL)); int r1=r-l; int azxs=rand()%r1+1; return l+azxs; } void mc(int a1,string s,int b1){ if(a1!=0){ cout<<a1; for (int i=1;i<=21000000;i++){ } } for (int i=0;i<s.size();i++){ cout<<s[i]; for (int j=1;j<=21000000;j++){ } } if(b1!=0){ cout<<b1; for (int i=1;i<=21000000;i++){ } } cout<<endl; return ; } void mc1(int a1,string s,int b1,int y){ if(a1!=0){ cout<<a1; for (int i=1;i<=21000000;i++){ } } for (int i=0;i<s.size();i++){ cout<<s[i]; for (int j=1;j<=21000000;j++){ } } if(b1!=0||y==1){ cout<<b1; for (int i=1;i<=21000000;i++){ } } cout<<endl; return ; } void mc2(int a1,string s,int b1,string y){ if(a1!=0){ cout<<a1; for (int i=1;i<=21000000;i++){ } } for (int i=0;i<s.size();i++){ cout<<s[i]; for (int j=1;j<=21000000;j++){ } } if(b1!=0){ for (int i=0;i<y.size();i++){ cout<<y[i]; for (int j=1;j<=21000000;j++){ } } } cout<<endl; return ; } void jzdn(){ if(j>=8){ j-=8; mc(0,"购买成功!",0); jingzhi=1; return ; }else{ mc(0,"不够金币!",0); return ; } } void sz2(){ if(j>=16){ j-=16; mc(0,"购买成功",0); srand((unsigned)time(NULL)); int qsc=sjs(); if(qsc<a){ l=qsc; }else{ r=qsc; } srand((unsigned)time(NULL)); int qsc1=sjs(); if(qsc1<a){ l=qsc1; }else{ r=qsc1; } return ; }else{ mc(0,"不够金币!",0); return ; } } void sz1(){ if(j>=8){ j-=8; mc(0,"购买成功",0); srand((unsigned)time(NULL)); int qsc=sjs(); if(qsc<a){ l=qsc; }else{ r=qsc; } return ; }else{ mc(0,"不够金币!",0); return ; } } void jysb(){ if(j>=6){ mc(0,"购买成功!",0); j-=6; jingjing*=2; }else{ mc(0,"不够金币!",0); } return ; } void jbsb(){ if(j>=6){ mc(0,"购买成功!",0); j-=6; jj*=2; }else{ mc(0,"不够金币!",0); } return ; } void jysb3(){ if(j>=10){ mc(0,"购买成功!",0); j-=10; jingjing*=3; }else{ mc(0,"不够金币!",0); } return ; } void jbsb3(){ if(j>=10){ mc(0,"购买成功!",0); j-=10; jj*=3; }else{ mc(0,"不够金币!",0); } return ; } void sd(){ mc(0,"欢迎来到商店!",0); mc(0,"请问买几张卡!",0); int yhj; cin>>yhj; mc(0,"1.缩减范围卡!按1!(8)金币!",0); mc(0,"2.经验双倍卡!按2!(6)金币!",0); mc(0,"3.金币双倍卡!按3!(6)金币!",0); mc(0,"4.三倍经验卡!按4!(10)金币!",0); mc(0,"5.三倍金币卡!按5!(10)金币!",0); mc(0,"6.禁止行动卡!按6!(8)金币!",0); mc(0,"7.超级缩减范围卡!按1!(16)金币!",0); for (int i=1;i<=yhj;i++){ int yu; cin>>yu; if(yu==1){ sz1(); }else if(yu==2){ jysb(); }else if(yu==3){ jbsb(); }else if(yu==4){ jysb3(); }else if(yu==5){ jbsb3(); }else if(yu==6){ jzdn(); }else if(yu==67){ sz2(); } } } void renji(){ mc(l,"--",r); mc(0,"电脑还剩生命:",mm1); srand((unsigned)time(NULL)); lr2=sjs(); cout<<lr2<<endl; if(lr2==a){ mc(0,"猜中了!",0); if(n==1){ jj*=1; jingjing*=10; }else if(n==2){ jj*=3; jingjing*=20; }else if(n==3){ jj*=5; jingjing*=35; }else{ jj*=10; jingjing*=60; } j-=jj; jing-=jingjing; ji-=jing/100; jing%=100; l=r; r=l; save(); }else if(a>lr2){ l=lr2; mc(0,"小了",0); mm1--; }else if(a<lr2){ r=lr2; mc(0,"大了",0); mm1--; } return ; } int main(){ jz(); read(); int ewds=1; if(cishu==1){ while (ewds==1){ mc(0,"请输入姓名!",0); cin>>name; cishu++; mc(0,"请输入密码!",0); cin>>mima; string qwer; mc(0,"请再次输入密码!",0); cin>>qwer; if(qwer==mima){ mc(0,"注册成功!",0); n=0,a=0,l=1,r=0,lr=0,lr2=0,j=5,ji=1,jing=10,mm=0,mm1=0,jingjing=1,jj=1,jjing=0,jingzhi=0,cishu=1; ewds=0; }else{ mc(0,"注册失败!",0); mc(0,"请重新注册!",0); } } save(); } while (ewds==1){ mc(0,"注册(1)or登录(2)!",0); int tygh; cin>>tygh; if(tygh==1){ mc(0,"请输入姓名!",0); cin>>name; cishu++; mc(0,"请输入密码!",0); cin>>mima; string qwer; mc(0,"请再次输入密码!",0); cin>>qwer; if(qwer==mima){ mc(0,"注册成功!",0); n=0,a=0,l=1,r=0,lr=0,lr2=0,j=5,ji=1,jing=10,mm=0,mm1=0,jingjing=1,jj=1,jjing=0,jingzhi=0,cishu=1; ewds=0; }else{ mc(0,"注册失败!",0); mc(0,"请重新注册!",0); } save(); }else{ mc(0,"请输入姓名!",0); cin>>name; cishu++; mc(0,"请输入密码!",0); string mima1; cin>>mima1; if(mima1!=mima){ mc(0,"登陆失败!",0); }else{ mc(0,"登陆成功!",0); ewds=0; } } } mc2(0,"欢迎玩家:",1,name); while(1){ mc1(0,"金币:",j,1); mc1(0,"等级:",ji,1); mc1(0,"经验:",jing,1); mc(0,"请选择",0); mc(0,"双人按1,单人按2!",0); int rty; cin>>rty; if(rty==2){ mc(0,"1.easy 1--20",0); mc(0,"2.medium 1--50",0); mc(0,"3.diffcult 1--100",0); mc(0,"4.very diffcult 1--200",0); cin>>n; srand((unsigned)time(NULL)); if(n==1){ a=rand()%20; r=20; }else if(n==2){ a=rand()%50; r=50; }else if(n==3){ a=rand()%100; r=100; }else if(n==4){ a=rand()%200; r=200; } if(n==1){ mm=4; }else if(n==2){ mm=6; }else if(n==3){ mm=8; }else if(n==4){ mm=9; } a+=1; mc(0,"开始!",0); l=1; mc(0,"是否买卡!",0); mc(0,"买按1,不买按2",0); int yui; cin>>yui; if(yui==1){ sd(); } while (l!=r){ if(mm==0){ mc(0,"没命了!",0); jing-=10; j-=2; save(); break; }else if(mm==1){ mc(0,"你即将没命!是否买卡!",0); mc(0,"买按1,不买按2",0); int yh; cin>>yh; if(yh==1){ sd(); } } mc(l,"--",r); mc(0,"还剩生命:",mm); cin>>lr; if(lr==a){ mc(0,"猜中了!",0); if(n==1){ jj*=1; jingjing*=10; }else if(n==2){ jj*=3; jingjing*=20; }else if(n==3){ jj*=5; jingjing*=35; }else{ jj*=10; jingjing*=60; } jjing=ji*100; j+=jj; jing+=jingjing; ji+=jing/jjing; jing%=jjing; save(); break; }else if(a>lr){ l=lr; mc(0,"小了",0); }else if(a<lr){ r=lr; mc(0,"大了",0); } mm--; } }else if(rty==1){ jj=2; jingjing=2; mc(0,"1.easy 1--40",0); mc(0,"2.medium 1--100",0); mc(0,"3.diffcult 1--200",0); mc(0,"4.very diffcult 1--400",0); cin>>n; srand((unsigned)time(NULL)); if(n==1){ a=rand()%40; r=40; }else if(n==2){ a=rand()%100; r=100; }else if(n==3){ a=rand()%200; r=200; }else if(n==4){ a=rand()%400; r=400; } if(n==1){ mm=4; mm1=4; }else if(n==2){ mm=6; mm1=6; }else if(n==3){ mm=8; mm1=8; }else if(n==4){ mm=10; mm1=10; } a+=1; mc(0,"开始!",0); l=1; mc(0,"是否买卡!",0); mc(0,"买按1,不买按2",0); int yui; cin>>yui; if(yui==1){ sd(); } while (l!=r){ if(mm==0){ mc(0,"没命了!",0); jing-=10; j-=2; save(); break; }else if(mm==1){ mc(0,"你即将没命!是否买卡!",0); mc(0,"买按1,不买按2",0); int yh; cin>>yh; if(yh==1){ sd(); } } mc(l,"--",r); mc(0,"玩家还剩生命:",mm); cin>>lr; if(lr==a){ mc(0,"猜中了!",0); if(n==1){ jj*=1; jingjing*=10; }else if(n==2){ jj*=3; jingjing*=20; }else if(n==3){ jj*=5; jingjing*=35; }else{ jj*=10; jingjing*=60; } j+=jj; jing+=jingjing; ji+=jing/100; jing%=100; save(); break; }else if(a>lr){ l=lr; mc(0,"小了",0); }else if(a<lr){ r=lr; mc(0,"大了",0); } mm--; if(jingzhi==1){ jingzhi=0; continue; } renji(); } } } }
贪吃蛇
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h> #include <conio.h> #define WIDTH 30 #define HEIGHT 20 enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; enum Direction dir; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; bool gameOver; void setCursorPosition(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void initGame() { gameOver = false; dir = STOP; x = WIDTH / 2; y = HEIGHT / 2; srand(time(NULL)); fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; score = 0; nTail = 0; } void draw() { system("cls"); for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (j == 0) printf("#"); if (i == y && j == x) printf("O"); else if (i == fruitY && j == fruitX) printf("F"); else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { printf("o"); print = true; } } if (!print) printf(" "); } if (j == WIDTH - 1) printf("#"); } printf("\n"); } for (int i = 0; i < WIDTH + 2; i++) printf("#"); printf("\n"); printf("分数: %d\n", score); printf("使用WASD控制方向,按X退出游戏\n"); } void input() { if (_kbhit()) { switch (_getch()) { case 'a': if (dir != RIGHT) dir = LEFT; break; case 'd': if (dir != LEFT) dir = RIGHT; break; case 'w': if (dir != DOWN) dir = UP; break; case 's': if (dir != UP) dir = DOWN; break; case 'x': gameOver = true; break; } } } void logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: /* 2024tysc1081gvjirty6g84e3vrgntuhr4ugfh4riog njkrvugjjjjjjjjjjjjjjjjjjrfuibngjktyujhgi5rtmbn3*/ x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) gameOver = true; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) gameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; nTail++; } } int main() { initGame(); hideCursor(); while (!gameOver) { draw(); input(); logic(); Sleep(100); } setCursorPosition(WIDTH/2 - 5, HEIGHT/2); printf("游戏结束!"); setCursorPosition(WIDTH/2 - 8, HEIGHT/2 + 1); printf("最终分数: %d", score); setCursorPosition(0, HEIGHT + 3); return 0; }
迷宫(基础版)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 #define ESC 27 #define ENTER 13 #define R_KEY 114 #define r_KEY 82 #define WALL 1 #define PATH 0 #define START 2 #define END 3 #define PLAYER 4 #define COLOR_RED 12 #define COLOR_GREEN 10 #define COLOR_YELLOW 14 #define COLOR_BLUE 9 #define COLOR_WHITE 15 #define COLOR_CYAN 11 int **maze; int player_x, player_y; int end_x, end_y; int width, height; int steps = 0; clock_t start_time; void setColor(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); } void initMaze() { int i, j; if (maze != NULL) { for (i = 0; i < height; i++) { free(maze[i]); } free(maze); } maze = (int**)malloc(height * sizeof(int*)); for (i = 0; i < height; i++) { maze[i] = (int*)malloc(width * sizeof(int)); } for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { maze[i][j] = WALL; } } srand(time(NULL)); player_x = 1; player_y = 1; maze[player_y][player_x] = START; end_x = width - 2; end_y = height - 2; maze[end_y][end_x] = END; int stack[height * width][2]; int top = 0; int **visited = (int**)malloc(height * sizeof(int*)); for (i = 0; i < height; i++) { visited[i] = (int*)calloc(width, sizeof(int)); } stack[top][0] = player_x; stack[top][1] = player_y; top++; visited[player_y][player_x] = 1; int dirs[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; while(top > 0) { top--; int x = stack[top][0]; int y = stack[top][1]; if(maze[y][x] != START && maze[y][x] != END) maze[y][x] = PATH; for(i = 0; i < 4; i++) { int r = rand() % 4; int temp_x = dirs[i][0]; int temp_y = dirs[i][1]; dirs[i][0] = dirs[r][0]; dirs[i][1] = dirs[r][1]; dirs[r][0] = temp_x; dirs[r][1] = temp_y; } int count = 0; int next_x[4], next_y[4]; for(i = 0; i < 4; i++) { int nx = x + dirs[i][0] * 2; int ny = y + dirs[i][1] * 2; if(nx > 0 && nx < width - 1 && ny > 0 && ny < height - 1 && !visited[ny][nx]) { next_x[count] = nx; next_y[count] = ny; count++; } } if(count > 0) { stack[top][0] = x; stack[top][1] = y; top++; int r = rand() % count; int nx = next_x[r]; int ny = next_y[r]; int wx = x + (nx - x) / 2; int wy = y + (ny - y) / 2; maze[wy][wx] = PATH; stack[top][0] = nx; stack[top][1] = ny; top++; visited[ny][nx] = 1; } } for (i = 0; i < height; i++) { free(visited[i]); } free(visited); steps = 0; start_time = clock(); } void drawMaze() { system("cls"); double time_spent = (double)(clock() - start_time) / CLOCKS_PER_SEC; setColor(COLOR_CYAN); printf("===== 迷宫游戏 =====\n"); setColor(COLOR_YELLOW); printf("难度: "); if (width == 15) printf("简单 "); else if (width == 30) printf("中等 "); else printf("困难 "); printf(" | 步数: %d | 用时: %.1f秒\n", steps, time_spent); setColor(COLOR_WHITE); printf("====================\n"); int i, j; for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { if(i == player_y && j == player_x) { setColor(COLOR_RED); printf("P "); } else { switch(maze[i][j]) { case WALL: setColor(COLOR_WHITE); printf("# "); break; case PATH: setColor(COLOR_WHITE); printf(" "); break; case START: setColor(COLOR_GREEN); printf("S "); break; case END: setColor(COLOR_BLUE); printf("E "); break; } } } setColor(COLOR_WHITE); printf("\n"); } setColor(COLOR_YELLOW); printf("\n操作说明: 方向键移动 | R键: 重新开始 | ESC键: 退出游戏\n"); setColor(COLOR_WHITE); } int movePlayer(int direction) { int new_x = player_x; int new_y = player_y; switch(direction) { case UP: new_y--; break; case DOWN: new_y++; break; case LEFT: new_x--; break; case RIGHT: new_x++; break; } if(new_x >= 0 && new_x < width && new_y >= 0 && new_y < height) { if(maze[new_y][new_x] != WALL) { player_x = new_x; player_y = new_y; steps++; if(player_x == end_x && player_y == end_y) { return 1; } } } return 0; } void setCursorPosition(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void selectDifficulty() { int key; int difficulty = 1; while(1) { system("cls"); setColor(COLOR_CYAN); printf("===== 选择迷宫难度 =====\n\n"); setColor(COLOR_WHITE); printf("使用上下方向键选择,按Enter确定\n\n"); if(difficulty == 1) setColor(COLOR_GREEN); else setColor(COLOR_WHITE); printf(" 简单 (15x10)\n"); if(difficulty == 2) setColor(COLOR_GREEN); else setColor(COLOR_WHITE); printf(" 中等 (30x15)\n"); if(difficulty == 3) setColor(COLOR_GREEN); else setColor(COLOR_WHITE); printf(" 困难 (45x20)\n"); key = _getch(); if(key == 0xE0) { key = _getch(); if(key == UP && difficulty > 1) difficulty--; else if(key == DOWN && difficulty < 3) difficulty++; } else if(key == ENTER) { switch(difficulty) { case 1: width = 15; height = 10; break; case 2: width = 30; height = 15; break; case 3: width = 45; height = 20; break; } break; } else if(key == ESC) { exit(0); } } setColor(COLOR_WHITE); } void showHelp() { system("cls"); setColor(COLOR_CYAN); printf("===== 迷宫游戏帮助 =====\n\n"); setColor(COLOR_WHITE); printf("游戏目标: 从起点(S)移动到终点(E)\n\n"); printf("操作控制:\n"); printf(" 方向键: 移动玩家(P)\n"); printf(" R键: 重新生成迷宫并开始新游戏\n"); printf(" ESC键: 退出游戏\n\n"); printf("游戏特色:\n"); printf(" - 三种难度级别可供选择\n"); printf(" - 记录移动步数和通关时间\n"); printf(" - 每次游戏生成独特的迷宫\n\n"); setColor(COLOR_YELLOW); printf("按任意键返回游戏...\n"); setColor(COLOR_WHITE); _getch(); } int main() { int key; int game_over = 0; maze = NULL; SetConsoleTitle("迷宫游戏 - 升级版本"); hideCursor(); selectDifficulty(); showHelp(); initMaze(); while(1) { game_over = 0; while(!game_over) { drawMaze(); key = _getch(); if(key == ESC) { game_over = 1; break; } else if(key == R_KEY || key == r_KEY) { initMaze(); break; } else if(key == 0xE0) { key = _getch(); if(movePlayer(key)) { drawMaze(); setCursorPosition(0, height + 5); setColor(COLOR_GREEN); printf("恭喜你,成功走出迷宫!\n"); printf("总步数: %d 步 | 总用时: %.1f 秒\n", steps, (double)(clock() - start_time) / CLOCKS_PER_SEC); setColor(COLOR_YELLOW); printf("按R键重新开始,按ESC键退出...\n"); setColor(COLOR_WHITE); while(1) { key = _getch(); if(key == ESC) { game_over = 1; break; } else if(key == R_KEY || key == r_KEY) { initMaze(); break; } } } } } if(key == ESC) { break; } } if (maze != NULL) { for (int i = 0; i < height; i++) { free(maze[i]); } free(maze); } system("cls"); printf("游戏结束,感谢游玩!\n"); printf("按任意键退出...\n"); _getch(); return 0; }
迷宫(升级版)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #include <string.h> #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 #define ESC 27 #define ENTER 13 #define R_KEY 114 #define r_KEY 82 #define H_KEY 104 #define h_KEY 72 #define P_KEY 112 #define p_KEY 80 #define WALL 1 #define PATH 0 #define START 2 #define END 3 #define PLAYER 4 #define VISITED 5 #define COLOR_RED 12 #define COLOR_GREEN 10 #define COLOR_YELLOW 14 #define COLOR_BLUE 9 #define COLOR_WHITE 15 #define COLOR_CYAN 11 #define COLOR_MAGENTA 13 #define COLOR_GRAY 8 int **maze; int player_x, player_y; int end_x, end_y; int width, height; int steps = 0; clock_t start_time; int level = 1; int hint_remaining = 3; int high_score[3] = {0}; char high_score_name[3][20] = {"AAA", "BBB", "CCC"}; int game_paused = 0; void setColor(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); } void initMaze() { int i, j; if (maze != NULL) { for (i = 0; i < height; i++) { free(maze[i]); } free(maze); } maze = (int**)malloc(height * sizeof(int*)); for (i = 0; i < height; i++) { maze[i] = (int*)malloc(width * sizeof(int)); } for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { maze[i][j] = WALL; } } srand(time(NULL)); player_x = 1; player_y = 1; maze[player_y][player_x] = START; end_x = width - 2; end_y = height - 2; maze[end_y][end_x] = END; int stack[height * width][2]; int top = 0; int **visited = (int**)malloc(height * sizeof(int*)); for (i = 0; i < height; i++) { visited[i] = (int*)calloc(width, sizeof(int)); } stack[top][0] = player_x; stack[top][1] = player_y; top++; visited[player_y][player_x] = 1; int dirs[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; while(top > 0) { top--; int x = stack[top][0]; int y = stack[top][1]; if(maze[y][x] != START && maze[y][x] != END) maze[y][x] = PATH; for(i = 0; i < 4; i++) { int r = rand() % 4; int temp_x = dirs[i][0]; int temp_y = dirs[i][1]; dirs[i][0] = dirs[r][0]; dirs[i][1] = dirs[r][1]; dirs[r][0] = temp_x; dirs[r][1] = temp_y; } int count = 0; int next_x[4], next_y[4]; for(i = 0; i < 4; i++) { int nx = x + dirs[i][0] * 2; int ny = y + dirs[i][1] * 2; if(nx > 0 && nx < width - 1 && ny > 0 && ny < height - 1 && !visited[ny][nx]) { next_x[count] = nx; next_y[count] = ny; count++; } } if(count > 0) { stack[top][0] = x; stack[top][1] = y; top++; int r = rand() % count; int nx = next_x[r]; int ny = next_y[r]; int wx = x + (nx - x) / 2; int wy = y + (ny - y) / 2; maze[wy][wx] = PATH; stack[top][0] = nx; stack[top][1] = ny; top++; visited[ny][nx] = 1; } } for (i = 0; i < height; i++) { free(visited[i]); } free(visited); steps = 0; start_time = clock(); if(level > 1 && hint_remaining < 3) hint_remaining++; } void drawMaze() { system("cls"); double time_spent = (double)(clock() - start_time) / CLOCKS_PER_SEC; setColor(COLOR_CYAN); printf("===== 迷宫游戏 (第 %d 关) =====\n", level); setColor(COLOR_YELLOW); printf("尺寸: %dx%d | 步数: %d | 用时: %.1f秒 | 提示: %d次\n", width, height, steps, time_spent, hint_remaining); setColor(COLOR_WHITE); printf("================================\n"); int i, j; for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { if(i == player_y && j == player_x) { setColor(COLOR_RED); printf("P "); } else { switch(maze[i][j]) { case WALL: setColor(COLOR_WHITE); printf("# "); break; case PATH: setColor(COLOR_GRAY); printf(" "); break; case VISITED: setColor(COLOR_GRAY); printf(". "); break; case START: setColor(COLOR_GREEN); printf("S "); break; case END: setColor(COLOR_BLUE); printf("E "); break; } } } setColor(COLOR_WHITE); printf("\n"); } setColor(COLOR_YELLOW); printf("\n操作: 方向键移动 | R:重新开始 | H:提示 | P:暂停 | ESC:退出\n"); setColor(COLOR_WHITE); } int movePlayer(int direction) { int new_x = player_x; int new_y = player_y; switch(direction) { case UP: new_y--; break; case DOWN: new_y++; break; case LEFT: new_x--; break; case RIGHT: new_x++; break; } if(new_x >= 0 && new_x < width && new_y >= 0 && new_y < height) { if(maze[new_y][new_x] != WALL) { if(maze[player_y][player_x] == PATH) maze[player_y][player_x] = VISITED; player_x = new_x; player_y = new_y; steps++; if(player_x == end_x && player_y == end_y) { return 1; } } } return 0; } void setCursorPosition(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void showHint() { if(hint_remaining <= 0) return; hint_remaining--; int dx = (end_x > player_x) ? 1 : (end_x < player_x) ? -1 : 0; int dy = (end_y > player_y) ? 1 : (end_y < player_y) ? -1 : 0; int hint_x = player_x, hint_y = player_y; if(dx != 0) { hint_x += dx; if(maze[hint_y][hint_x] == WALL) { hint_x = player_x; hint_y += dy; } } else if(dy != 0) { hint_y += dy; if(maze[hint_y][hint_x] == WALL) { hint_y = player_y; hint_x += dx; } } drawMaze(); setCursorPosition(hint_x * 2, 3 + hint_y); setColor(COLOR_MAGENTA); printf("?"); setColor(COLOR_WHITE); setCursorPosition(0, height + 4); printf("提示: 朝 %c 方向前进可能是个好选择...", dx == 1 ? '右' : dx == -1 ? '左' : dy == 1 ? '下' : '上'); Sleep(2000); } void saveHighScores() { FILE *file = fopen("maze_highscores.txt", "w"); if(file == NULL) return; for(int i = 0; i < 3; i++) { fprintf(file, "%s %d\n", high_score_name[i], high_score[i]); } fclose(file); } void loadHighScores() { FILE *file = fopen("maze_highscores.txt", "r"); if(file == NULL) { saveHighScores(); return; } for(int i = 0; i < 3; i++) { fscanf(file, "%s %d", high_score_name[i], &high_score[i]); } fclose(file); } void checkHighScore(int score) { for(int i = 0; i < 3; i++) { if(score < high_score[i] || high_score[i] == 0) { for(int j = 2; j > i; j--) { strcpy(high_score_name[j], high_score_name[j-1]); high_score[j] = high_score[j-1]; } setCursorPosition(0, height + 8); printf("新的高分!请输入你的名字 (最多19个字符): "); fflush(stdin); fgets(high_score_name[i], 20, stdin); high_score_name[i][strcspn(high_score_name[i], "\n")] = '\0'; high_score[i] = score; saveHighScores(); break; } } } void showHighScores() { system("cls"); setColor(COLOR_CYAN); printf("===== 迷宫游戏高分榜 =====\n\n"); setColor(COLOR_WHITE); for(int i = 0; i < 3; i++) { if(high_score[i] == 0) { printf("%d. 暂无记录\n", i+1); } else { printf("%d. %-20s %d 步\n", i+1, high_score_name[i], high_score[i]); } } setColor(COLOR_YELLOW); printf("\n按任意键返回...\n"); setColor(COLOR_WHITE); _getch(); } void selectDifficulty() { int key; int difficulty = 1; while(1) { system("cls"); setColor(COLOR_CYAN); printf("===== 选择迷宫难度 =====\n\n"); setColor(COLOR_WHITE); printf("使用上下方向键选择,按Enter确定\n\n"); if(difficulty == 1) setColor(COLOR_GREEN); else setColor(COLOR_WHITE); printf(" 简单\n"); if(difficulty == 2) setColor(COLOR_GREEN); else setColor(COLOR_WHITE); printf(" 中等\n"); if(difficulty == 3) setColor(COLOR_GREEN); else setColor(COLOR_WHITE); printf(" 困难\n"); key = _getch(); if(key == 0xE0) { key = _getch(); if(key == UP && difficulty > 1) difficulty--; else if(key == DOWN && difficulty < 3) difficulty++; } else if(key == ENTER) { switch(difficulty) { case 1: width = 15; height = 10; break; case 2: width = 30; height = 15; break; case 3: width = 45; height = 20; break; } break; } else if(key == ESC) { exit(0); } } setColor(COLOR_WHITE); } void showHelp() { system("cls"); setColor(COLOR_CYAN); printf("===== 迷宫游戏帮助 =====\n\n"); setColor(COLOR_WHITE); printf("游戏目标: 从起点(S)移动到终点(E)\n\n"); printf("操作控制:\n"); printf(" 方向键: 移动玩家(P)\n"); printf(" R键: 重新生成当前关卡\n"); printf(" H键: 使用提示(每关有限次数)\n"); printf(" P键: 暂停游戏\n"); printf(" ESC键: 退出游戏\n\n"); setColor(COLOR_YELLOW); printf("按任意键返回游戏...\n"); setColor(COLOR_WHITE); _getch(); } void pauseGame() { game_paused = 1; setCursorPosition(0, height + 5); setColor(COLOR_MAGENTA); printf("游戏已暂停 - 按P键继续...\n"); setColor(COLOR_WHITE); while(1) { int key = _getch(); if(key == P_KEY || key == p_KEY) { game_paused = 0; break; } else if(key == ESC) { exit(0); } } } int main() { int key; int game_over = 0; maze = NULL; SetConsoleTitle("迷宫游戏 - 高级版"); hideCursor(); loadHighScores(); selectDifficulty(); showHighScores(); showHelp(); initMaze(); while(1) { game_over = 0; while(!game_over) { drawMaze(); key = _getch(); if(key == ESC) { game_over = 1; break; } else if(key == R_KEY || key == r_KEY) { initMaze(); break; } else if(key == H_KEY || key == h_KEY) { showHint(); } else if(key == P_KEY || key == p_KEY) { pauseGame(); } else if(key == 0xE0) { key = _getch(); if(movePlayer(key)) { drawMaze(); setCursorPosition(0, height + 5); setColor(COLOR_GREEN); printf("恭喜你,完成第 %d 关!\n", level); printf("步数: %d | 用时: %.1f秒\n", steps, (double)(clock() - start_time) / CLOCKS_PER_SEC); checkHighScore(steps); level++; width += 5; height += 3; if(width > 50) width = 50; if(height > 25) height = 25; setColor(COLOR_YELLOW); printf("按任意键进入第 %d 关...\n", level); setColor(COLOR_WHITE); _getch(); initMaze(); break; } } } if(key == ESC) { break; } } if (maze != NULL) { for (int i = 0; i < height; i++) { free(maze[i]); } free(maze); } system("cls"); printf("游戏结束,感谢游玩!\n"); printf("按任意键退出...\n"); _getch(); return 0; }
打砖块(基础版)
#include <iostream> #include <conio.h> #include <windows.h> using namespace std; // 游戏区域尺寸 const int width = 40; const int height = 20; // 游戏元素位置和状态 int paddleX, ballX, ballY, ballDirX, ballDirY; bool bricks[10][30]; // 砖块数组 int score; bool gameOver; // 初始化游戏 void Setup() { // 初始化挡板位置 paddleX = width / 2 - 3; // 初始化球的位置和方向 ballX = width / 2; ballY = height - 3; ballDirX = 1; ballDirY = -1; // 初始化砖块 for(int i = 0; i < 3; i++) { for(int j = 0; j < width - 2; j++) { bricks[i][j] = true; } } score = 0; gameOver = false; } // 绘制游戏界面 void Draw() { system("cls"); // 清屏 // 上边界 for(int i = 0; i < width + 2; i++) { cout << "#"; } cout << endl; // 游戏区域 for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { // 左右边界 if(j == 0) { cout << "#"; } // 绘制球 if(i == ballY && j == ballX) { cout << "O"; } // 绘制挡板 else if(i == height - 2 && j >= paddleX && j < paddleX + 6) { cout << "="; } // 绘制砖块 else if(bricks[i][j]) { cout << "■"; } else { cout << " "; } // 右边界 if(j == width - 1) { cout << "#"; } } cout << endl; } // 下边界 for(int i = 0; i < width + 2; i++) { cout << "#"; } cout << endl; // 显示分数 cout << "分数: " << score << endl; cout << "使用A和D键移动挡板,Q键退出游戏" << endl; } // 处理输入 void Input() { if(_kbhit()) { switch(_getch()) { case 'a': if(paddleX > 0) paddleX--; break; case 'd': if(paddleX < width - 6) paddleX++; break; case 'q': gameOver = true; break; } } } // 游戏逻辑 void Logic() { // 移动球 ballX += ballDirX; ballY += ballDirY; // 碰撞检测 - 左右边界 if(ballX <= 0 || ballX >= width - 1) { ballDirX *= -1; } // 碰撞检测 - 上边界 if(ballY <= 0) { ballDirY *= -1; } // 游戏结束 - 球碰到下边界 if(ballY >= height - 1) { gameOver = true; cout << "游戏结束! 你的分数是: " << score << endl; } // 碰撞检测 - 挡板 if(ballY == height - 3 && ballX >= paddleX && ballX < paddleX + 6) { ballDirY *= -1; } // 碰撞检测 - 砖块 if(ballY < 3 && bricks[ballY][ballX]) { bricks[ballY][ballX] = false; ballDirY *= -1; score += 10; } // 胜利条件 - 所有砖块都被打掉 bool allDestroyed = true; for(int i = 0; i < 3; i++) { for(int j = 0; j < width - 2; j++) { if(bricks[i][j]) { allDestroyed = false; break; } } if(!allDestroyed) break; } if(allDestroyed) { gameOver = true; cout << "恭喜你获胜! 你的分数是: " << score << endl; } } int main() { Setup(); while(!gameOver) { Draw(); Input(); Logic(); Sleep(60); // 控制游戏速度 } return 0; }
打砖块(升级版)
#include <iostream> #include <conio.h> #include <windows.h> #include <cstdlib> #include <ctime> using namespace std; // 游戏区域尺寸 const int width = 60; const int height = 25; // 游戏元素 int paddleX, paddleWidth; int ballX[5], ballY[5], ballDirX[5], ballDirY[5]; int numBalls; int brickType[10][width]; // 0:无砖块 1:普通 2:强化 3:加分 int score; int lives; int level; bool gameOver; bool gameWin; // 道具系统 enum PowerUpType { NONE, WIDEN, NARROW, MULTIBALL }; struct PowerUp { int x, y; PowerUpType type; bool active; } powerUp; // 初始化游戏 void Setup(int lvl = 1) { // 初始化挡板 paddleX = width / 2 - 5; paddleWidth = 10; // 初始化球 numBalls = 1; ballX[0] = width / 2; ballY[0] = height - 5; ballDirX[0] = (rand() % 2 == 0) ? 1 : -1; ballDirY[0] = -1; // 清空其他球 for(int i = 1; i < 5; i++) { ballX[i] = ballY[i] = 0; ballDirX[i] = ballDirY[i] = 0; } // 初始化关卡 level = lvl; for(int i = 0; i < 10; i++) { for(int j = 0; j < width; j++) { brickType[i][j] = 0; } } // 根据关卡生成不同布局的砖块 srand(time(0)); int startRow = 2; int brickDensity = 70 + level * 5; // 随关卡增加砖块密度 if(brickDensity > 95) brickDensity = 95; for(int i = startRow; i < startRow + 5 + level; i++) { for(int j = 1; j < width - 1; j++) { if(rand() % 100 < brickDensity) { // 随机生成不同类型的砖块 int r = rand() % 10; if(r < 7) brickType[i][j] = 1; // 普通砖块 else if(r < 9) brickType[i][j] = 2; // 强化砖块 else brickType[i][j] = 3; // 加分砖块 } } } // 初始化道具 powerUp.active = false; powerUp.type = NONE; // 初始化分数和生命 if(lvl == 1) { score = 0; lives = 3; } gameOver = false; gameWin = false; } // 绘制游戏界面 void Draw() { system("cls"); // 清屏 // 上边界 for(int i = 0; i < width + 2; i++) { cout << "#"; } cout << endl; // 游戏区域 for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { // 左右边界 if(j == 0) { cout << "#"; } // 绘制球 bool isBall = false; for(int b = 0; b < numBalls; b++) { if(i == ballY[b] && j == ballX[b]) { cout << "O"; isBall = true; break; } } if(isBall) { // 球已绘制,继续下一个位置 continue; } // 绘制挡板 if(i == height - 3 && j >= paddleX && j < paddleX + paddleWidth) { cout << "="; } // 绘制砖块 else if(brickType[i][j] == 1) { cout << "■"; // 普通砖块 } else if(brickType[i][j] == 2) { cout << "□"; // 强化砖块 } else if(brickType[i][j] == 3) { cout << "◆"; // 加分砖块 } // 绘制道具 else if(powerUp.active && i == powerUp.y && j == powerUp.x) { cout << "★"; // 道具 } else { cout << " "; } // 右边界 if(j == width - 1) { cout << "#"; } } cout << endl; } // 下边界 for(int i = 0; i < width + 2; i++) { cout << "#"; } cout << endl; // 游戏信息 cout << "分数: " << score << " 生命: " << lives << " 关卡: " << level << endl; cout << "控制: A(左) D(右) Q(退出) 下一关: N" << endl; } // 处理输入 void Input() { if(_kbhit()) { switch(_getch()) { case 'a': case 'A': if(paddleX > 1) paddleX--; break; case 'd': case 'D': if(paddleX + paddleWidth < width - 1) paddleX++; break; case 'q': case 'Q': gameOver = true; break; case 'n': case 'N': // 允许跳到下一关 Setup(level + 1); break; } } } // 检查是否所有砖块都被清除 bool allBricksCleared() { for(int i = 0; i < 10; i++) { for(int j = 0; j < width; j++) { if(brickType[i][j] != 0) { return false; } } } return true; } // 生成随机道具 void spawnPowerUp(int x, int y) { if(rand() % 5 == 0) { // 20%的概率掉落道具 powerUp.x = x; powerUp.y = y; powerUp.active = true; // 随机选择道具类型 int r = rand() % 3; if(r == 0) powerUp.type = WIDEN; else if(r == 1) powerUp.type = NARROW; else powerUp.type = MULTIBALL; } } // 应用道具效果 void applyPowerUp(PowerUpType type) { switch(type) { case WIDEN: if(paddleWidth < 15) paddleWidth += 3; score += 50; break; case NARROW: if(paddleWidth > 5) paddleWidth -= 2; score += 20; break; case MULTIBALL: if(numBalls < 5) { for(int i = numBalls; i < numBalls + 2 && i < 5; i++) { ballX[i] = ballX[0]; ballY[i] = ballY[0]; ballDirX[i] = (rand() % 2 == 0) ? 1 : -1; ballDirY[i] = -1; numBalls++; } } score += 100; break; default: break; } powerUp.active = false; } // 游戏逻辑 void Logic() { // 移动所有球 for(int b = 0; b < numBalls; b++) { ballX[b] += ballDirX[b]; ballY[b] += ballDirY[b]; } // 移动道具 if(powerUp.active) { powerUp.y++; // 道具落地消失 if(powerUp.y >= height - 3) { powerUp.active = false; } // 检查道具是否被挡板接住 else if(powerUp.y == height - 4 && powerUp.x >= paddleX && powerUp.x < paddleX + paddleWidth) { applyPowerUp(powerUp.type); } } // 碰撞检测 - 每个球单独处理 for(int b = 0; b < numBalls; b++) { // 左右边界 if(ballX[b] <= 1 || ballX[b] >= width - 2) { ballDirX[b] *= -1; } // 上边界 if(ballY[b] <= 1) { ballDirY[b] *= -1; } // 下边界 - 球丢失 if(ballY[b] >= height - 4) { // 移除这个球 for(int i = b; i < numBalls - 1; i++) { ballX[i] = ballX[i+1]; ballY[i] = ballY[i+1]; ballDirX[i] = ballDirX[i+1]; ballDirY[i] = ballDirY[i+1]; } numBalls--; // 如果所有球都丢失了 if(numBalls == 0) { lives--; if(lives <= 0) { gameOver = true; return; } else { // 重新生成一个球 numBalls = 1; ballX[0] = width / 2; ballY[0] = height - 5; ballDirX[0] = (rand() % 2 == 0) ? 1 : -1; ballDirY[0] = -1; } } // 重新检查当前索引的球 b--; continue; } // 碰撞检测 - 挡板 if(ballY[b] == height - 4 && ballX[b] >= paddleX && ballX[b] < paddleX + paddleWidth) { ballDirY[b] *= -1; // 根据击中挡板的位置调整水平方向 int hitPos = ballX[b] - paddleX; if(hitPos < paddleWidth / 3) { ballDirX[b] = -1 - (paddleWidth/3 - hitPos)/3; } else if(hitPos > paddleWidth * 2 / 3) { ballDirX[b] = 1 + (hitPos - paddleWidth*2/3)/3; } else { ballDirX[b] = 0; } } // 碰撞检测 - 砖块 if(ballY[b] < 10 && brickType[ballY[b]][ballX[b]] != 0) { int type = brickType[ballY[b]][ballX[b]]; // 普通砖块和加分砖块一击即破 if(type == 1 || type == 3) { brickType[ballY[b]][ballX[b]] = 0; // 加分砖块分数更高 if(type == 1) score += 10 + level * 2; else score += 50 + level * 5; // 有几率生成道具 spawnPowerUp(ballX[b], ballY[b]); } // 强化砖块需要多次击打 else if(type == 2) { brickType[ballY[b]][ballX[b]] = 1; // 变为普通砖块 score += 5 + level; } // 反弹 ballDirY[b] *= -1; } } // 检查是否通关 if(allBricksCleared()) { gameWin = true; gameOver = true; } } // 显示游戏开始界面 void showStartScreen() { system("cls"); cout << "\n\n"; cout << "\t=====================================\n"; cout << "\t 超级打砖块游戏 \n"; cout << "\t=====================================\n\n"; cout << "\t游戏说明:\n"; cout << "\t- 使用A键和D键移动挡板\n"; cout << "\t- 打掉所有砖块通关\n"; cout << "\t- 不同类型的砖块有不同效果:\n"; cout << "\t ■ 普通砖块 - 10分\n"; cout << "\t □ 强化砖块 - 需要多次击打\n"; cout << "\t ◆ 加分砖块 - 50分\n"; cout << "\t- 收集★道具获得特殊效果\n"; cout << "\t- 按N键可以直接进入下一关\n"; cout << "\t- 按Q键退出游戏\n\n"; cout << "\t按任意键开始游戏...\n"; _getch(); } // 显示游戏结束界面 void showGameOverScreen() { system("cls"); cout << "\n\n"; if(gameWin) { cout << "\t=====================================\n"; cout << "\t 恭喜通关! \n"; cout << "\t=====================================\n\n"; } else { cout << "\t=====================================\n"; cout << "\t 游戏结束 \n"; cout << "\t=====================================\n\n"; } cout << "\t你的得分: " << score << endl; cout << "\t你达到了第 " << level << " 关\n\n"; cout << "\t按任意键退出...\n"; _getch(); } int main() { showStartScreen(); Setup(); while(!gameOver) { Draw(); Input(); Logic(); // 随关卡增加游戏速度 Sleep(60 - level * 5); if(60 - level * 5 < 20) Sleep(20); } showGameOverScreen(); return 0; }
俄罗斯方块(容易红温)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #include <conio.h> // 游戏常量定义 #define WIDTH 12 // 游戏区域宽度 #define HEIGHT 20 // 游戏区域高度 #define BLOCK_SIZE 4 // 方块尺寸(4x4) // 方块形状定义 (7种基本形状) int shapes[7][BLOCK_SIZE][BLOCK_SIZE] = { {{1,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}}, // I {{1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0}}, // O {{1,1,1,0}, {0,1,0,0}, {0,0,0,0}, {0,0,0,0}}, // T {{1,1,1,0}, {1,0,0,0}, {0,0,0,0}, {0,0,0,0}}, // J {{1,1,1,0}, {0,0,1,0}, {0,0,0,0}, {0,0,0,0}}, // L {{0,1,1,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0}}, // S {{1,1,0,0}, {0,1,1,0}, {0,0,0,0}, {0,0,0,0}} // Z }; // 方块颜色 (与形状对应) int colors[7] = {12, 14, 13, 9, 10, 11, 14}; // 游戏变量 int board[HEIGHT][WIDTH] = {0}; // 游戏区域 int currentX, currentY; // 当前方块位置 int currentShape; // 当前方块形状 int currentRotation; // 当前方块旋转状态 int nextShape; // 下一个方块 int score; // 分数 int gameOver; // 游戏是否结束 // 函数声明 void setup(); void draw(); void input(); void logic(); void createNewPiece(); int checkCollision(int x, int y, int rotation); void rotatePiece(); void mergePiece(); int clearLines(); void gameOverScreen(); void gotoxy(int x, int y); void hideCursor(); void setColor(int color); int main() { setup(); // 游戏主循环 while (!gameOver) { draw(); input(); logic(); Sleep(300); // 控制下落速度 } gameOverScreen(); return 0; } // 初始化游戏 void setup() { srand(time(0)); score = 0; gameOver = 0; // 初始化游戏板边界 for (int i = 0; i < HEIGHT; i++) { board[i][0] = -1; board[i][WIDTH-1] = -1; } for (int i = 0; i < WIDTH; i++) { board[HEIGHT-1][i] = -1; } // 创建第一个和下一个方块 nextShape = rand() % 7; createNewPiece(); hideCursor(); } // 创建新方块 void createNewPiece() { currentShape = nextShape; nextShape = rand() % 7; currentRotation = 0; currentX = WIDTH / 2 - BLOCK_SIZE / 2; currentY = 1; // 检查是否游戏结束 (新方块无法放置) if (checkCollision(currentX, currentY, currentRotation)) { gameOver = 1; } } // 绘制游戏界面 void draw() { system("cls"); // 清屏 // 绘制游戏边框 setColor(7); gotoxy(0, 0); for (int i = 0; i < WIDTH + 2; i++) { printf("-"); } printf("\n"); // 绘制游戏区域 for (int y = 0; y < HEIGHT - 1; y++) { printf("|"); // 左边界 for (int x = 0; x < WIDTH; x++) { // 绘制已有方块 if (board[y][x] == -1) { setColor(7); printf("-"); } else if (board[y][x] > 0) { setColor(board[y][x]); printf("[]"); } // 绘制当前方块 else { int isPartOfPiece = 0; for (int i = 0; i < BLOCK_SIZE; i++) { for (int j = 0; j < BLOCK_SIZE; j++) { if (shapes[currentShape][(currentRotation + i) % 4][j]) { if (currentX + j == x && currentY + i == y) { setColor(colors[currentShape]); printf("[]"); isPartOfPiece = 1; break; } } } if (isPartOfPiece) break; } if (!isPartOfPiece) { printf(" "); } } } printf("|"); // 右边界 // 绘制下一个方块预览 if (y == 5) { setColor(7); printf(" 下一个:"); } if (y >= 7 && y < 7 + BLOCK_SIZE) { setColor(7); printf(" "); for (int j = 0; j < BLOCK_SIZE; j++) { if (shapes[nextShape][0][j]) { setColor(colors[nextShape]); printf("[]"); } else { printf(" "); } } } // 绘制分数 if (y == 12) { setColor(7); printf(" 分数: %d", score); } // 绘制控制说明 if (y == 14) { setColor(7); printf(" 左: <-"); } if (y == 15) { setColor(7); printf(" 右: ->"); } if (y == 16) { setColor(7); printf(" 下: ↓"); } if (y == 17) { setColor(7); printf(" 旋转: ↑"); } if (y == 18) { setColor(7); printf(" 退出: ESC"); } printf("\n"); } // 绘制底部边框 for (int i = 0; i < WIDTH + 2; i++) { printf("-"); } printf("\n"); setColor(7); // 重置颜色 } // 处理输入 void input() { if (_kbhit()) { switch (_getch()) { case 75: // 左箭头 if (!checkCollision(currentX - 1, currentY, currentRotation)) { currentX--; } break; case 77: // 右箭头 if (!checkCollision(currentX + 1, currentY, currentRotation)) { currentX++; } break; case 80: // 下箭头 if (!checkCollision(currentX, currentY + 1, currentRotation)) { currentY++; } break; case 72: // 上箭头 (旋转) rotatePiece(); break; case 27: // ESC键 gameOver = 1; break; } } } // 游戏逻辑 void logic() { // 方块下落 if (!checkCollision(currentX, currentY + 1, currentRotation)) { currentY++; } else { // 合并方块到游戏板 mergePiece(); // 清除完整行 score += clearLines() * 100; // 创建新方块 createNewPiece(); } } // 检查碰撞 int checkCollision(int x, int y, int rotation) { for (int i = 0; i < BLOCK_SIZE; i++) { for (int j = 0; j < BLOCK_SIZE; j++) { if (shapes[currentShape][(rotation + i) % 4][j]) { if (board[y + i][x + j] > 0 || board[y + i][x + j] == -1) { return 1; // 碰撞 } } } } return 0; // 无碰撞 } // 旋转方块 void rotatePiece() { int newRotation = (currentRotation + 1) % 4; if (!checkCollision(currentX, currentY, newRotation)) { currentRotation = newRotation; } } // 合并方块到游戏板 void mergePiece() { for (int i = 0; i < BLOCK_SIZE; i++) { for (int j = 0; j < BLOCK_SIZE; j++) { if (shapes[currentShape][(currentRotation + i) % 4][j]) { board[currentY + i][currentX + j] = colors[currentShape]; } } } } // 清除完整的行 int clearLines() { int linesCleared = 0; for (int y = HEIGHT - 2; y > 0; y--) { int lineComplete = 1; // 检查行是否完整 for (int x = 1; x < WIDTH - 1; x++) { if (board[y][x] == 0) { lineComplete = 0; break; } } // 如果行完整,清除并下移上面的行 if (lineComplete) { linesCleared++; for (int yy = y; yy > 1; yy--) { for (int x = 1; x < WIDTH - 1; x++) { board[yy][x] = board[yy - 1][x]; } } // 顶部填充空行 for (int x = 1; x < WIDTH - 1; x++) { board[1][x] = 0; } // 重新检查当前行,因为新下移的行可能也完整 y++; } } return linesCleared; } // 游戏结束屏幕 void gameOverScreen() { system("cls"); gotoxy(WIDTH / 2 - 5, HEIGHT / 2); printf("游戏结束!"); gotoxy(WIDTH / 2 - 8, HEIGHT / 2 + 1); printf("最终得分: %d", score); gotoxy(WIDTH / 2 - 12, HEIGHT / 2 + 3); printf("按任意键退出..."); getch(); } // 移动光标位置 void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } // 隐藏光标 void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } // 设置文本颜色 void setColor(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); }
正在更新 回到顶部
-
最近活动
- 2025铁一集团新苗for循环专题练习赛 IOI
- 2025铁一集团新苗线下测试2 IOI
- 2025铁一集团新苗线上模拟测试5 OI
- 2025铁一集团新苗线上模拟测试4 OI
- 2025铁一集团新苗线上模拟赛3 OI
- 2025铁一集团新苗线下测试1 IOI
- 2025铁一集团新苗day8作业-while2 作业
- 2025铁一集团新苗线上(8月4日)-循环专题练习 作业
- 2025铁一集团新苗复习-for循环专题练习1 作业
- 2025铁一集团新苗day15作业-结构体和函数 作业
- 2025铁一集团新苗day14作业-二维数组基础 作业
- 2025铁一集团新苗秋季班作业1-二维数组和二维字符数组 作业
- 2025铁一集团新苗day13作业-普通排序和桶排序 作业
- 2025铁一集团新苗day12作业-数组标记的应用 作业
- 2025铁一集团新苗day11作业-字符、字符数组和字符串 作业
- 2025铁一集团新苗day10作业-一维数组基础 作业
- 2025铁一集团新苗day9作业-多重循环 作业
- 2025铁一集团新苗day7作业-循环语句while1 作业
- 2025铁一集团新苗线上模拟赛1 OI
- 2025铁一集团新苗day6作业-for语句3(数据的在线处理) 作业
- 2025铁一集团新苗day5作业-for语句2(枚举和筛选) 作业
- 2025铁一集团新苗day4作业-循环for语句 作业
- 2025铁一集团新苗day3作业-if条件语句 作业
- 2025铁一集团新苗day2作业-表达式 作业
- 2025铁一集团新苗day1作业-C++程序结构 作业
- 第五届oiClass信息学夏令营线上正式邀请赛3 OI
- 第五届oiClass信息学夏令营线上正式邀请赛2 OI
- 第五届oiClass信息学夏令营线上正式邀请赛1 OI
- 2024新苗--C++程序结构 作业
- 第五届oiClass信息学夏令营线上模拟测试1 OI
- 2025铁一集团新苗复习-for循环专题练习2 作业
- 第五届oiClass信息学夏令营day7作业-for循环专题练习1 作业
- 第五届oiClass信息学夏令营线上模拟测试3 OI
- 第五届oiClass信息学夏令营day13作业-循环专题练习 作业
- 第五届oiClass信息学夏令营day12作业-多重循环 作业
- 第五届oiClass信息学夏令营day11作业-while2 作业
- 第五届oiClass信息学夏令营day10作业-while1 作业
- 第五届oiClass信息学夏令营day5作业-for语句2 作业
- 第五届oiClass信息学夏令营day4作业-for语句1 作业
- 第五届oiClass信息学夏令营day3作业-if语句 作业
- 第五届oiClass信息学夏令营day2作业-表达式 作业
- 第五届oiClass信息学夏令营day1作业-C++程序结构 作业
- 2023年第四届oiClass夏令营线上选拔赛 OI
- 夏令营day17作业-一维数组2 作业
- 夏令营day16作业-一维数组1 作业
- 新初一夏令营day6作业-for语句3 作业
- 夏令营第二周模拟测试 OI
- 夏令营day9作业-for语句综合练习 作业
- 第五届oiClass信息学夏令营day6作业-for语句3 作业
- 夏令营day5作业-for语句2 作业
- 夏令营day4作业-for语句1 作业
- 夏令营day3作业-if语句 作业
- 夏令营day2作业-表达式 作业
- 夏令营day1作业-C++程序结构 作业
-
Stat
-
Rating