|
|
发表于 2020-5-23 03:07:19
|
显示全部楼层
本帖最后由 java2python 于 2020-5-23 20:12 编辑
重复代码不少(楼主别生气):
控制台光标位置可以设定的(没有闪烁),
另外a[x][y])写法,改成行优先a[y][x],比如printf,实际打印的a[y],x只是一个行中的列位置
取得游戏数据放到了get_new_game函数中,
由于修改贴不了代码格式,只能再回一贴:
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <conio.h>
- static int dxy[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
- static char tans_table[4][3] = {{'O',' ','O'},{'O','*','@'},{'@',' ','O'},{'@','*','@'}};
- HANDLE init_con(){
- SMALL_RECT winPon={0,0,25,10};
- HANDLE con=GetStdHandle(STD_OUTPUT_HANDLE);
- COORD buf={26,11};
- SetConsoleWindowInfo(con,1,&winPon);
- SetConsoleScreenBufferSize(con,buf);
- SetConsoleTextAttribute(con, 6 | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
- CONSOLE_CURSOR_INFO cinfo;
- cinfo.dwSize = 1;
- cinfo.bVisible = false;
- SetConsoleCursorInfo(con,&cinfo);
- return con;
- }
- void print_char(HANDLE con,int y,int x,char c){
- if(c=='\0') return;
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(con, coord);
- int fore_color=7;
- if(c == 'O') fore_color = 6;
- else if(c == 'S') fore_color = 7;
- else if(c == '#') fore_color = 12;
- else if(c == '*') fore_color = 3;
- else if(c == '@') fore_color = 2;
- SetConsoleTextAttribute(con, fore_color | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
- putchar(c);
- }
- void get_new_game(char a[20][20],int no,COORD *s_pos,COORD star[],int *pStar_cnt){
- if(no == 1){
- char aa[20][20]={" ###",
- " #*#",
- " # #",
- "####O######",
- "#* OS O *#",
- "#####O#####",
- " # #",
- " #*#",
- " ###",};
- for(int y=0;y<20;y++) for(int x=0;x<20;x++) a[y][x] = aa[y][x];
- }else if(no == 2){
- char aa[20][20]={"##########",
- "## ###",
- "##O### #",
- "# S O O #",
- "#** # O #",
- "##**# #",
- "##########",};
- for(int y=0;y<20;y++) for(int x=0;x<20;x++) a[y][x] = aa[y][x];
- }
- int star_cnt = 0;
- for(int y=0;y<20;y++) for(int x=0;x<20;x++) {
- //a[y][x] = aa[y][x];
- if(a[y][x] == 'S'){
- s_pos->X = x;
- s_pos->Y = y;
- }
- if(a[y][x] == '*'){
- star[star_cnt].X = x;
- star[star_cnt].Y = y;
- star_cnt++;
- }
- }
- *pStar_cnt = star_cnt;
- }
- int play(HANDLE con,int no)
- {
- char a[20][20];
- COORD s_pos,next_pos,next2_pos;
- COORD star[10];
- int star_cnt = 0;
- get_new_game(a,no,&s_pos,star,&star_cnt);
- int direct=0;
- int cur_a,next_a,next2_a,loc_change,star_in_pos=0;
- char ch;
- for (int y=0;y<=7;y++) for(int x=0;x<20;x++) print_char(con,y,x,a[y][x]);
- while (star_in_pos != star_cnt)//是否过关
- {
- ch=getch();
- if (ch=='s') direct = 0;//按下s键
- else if (ch=='w') direct = 1;//按下w键
- else if (ch=='a') direct = 2;//按下a键
- else if (ch=='d') direct = 3;//按下d键
- else direct = -1;
- if(0<=direct && direct <=3)
- {
- loc_change = 0;
- next_pos.Y = s_pos.Y+dxy[direct][0];
- next_pos.X = s_pos.X+dxy[direct][1];
- next2_pos.Y = next_pos.Y+dxy[direct][0];
- next2_pos.X = next_pos.X+dxy[direct][1];
- next_a = a[next_pos.Y][next_pos.X];
- next2_a = a[next2_pos.Y][next2_pos.X];
- if (next_a !='#' && next_a !='O' && next_a!='@')//从此开始判断
- {
- loc_change = 1;
- }else {
- for(int i=0;i<4;i++){
- if (next_a == tans_table[i][0] && next2_a == tans_table[i][1])
- {
- a[next2_pos.Y][next2_pos.X]=tans_table[i][2];
- print_char(con,next2_pos.Y,next2_pos.X,tans_table[i][2]);
- loc_change = 1;
- break;
- }
- }
- }
- if(loc_change == 1){
- a[s_pos.Y][s_pos.X]=' ';
- a[next_pos.Y][next_pos.X]='S';
- print_char(con,s_pos.Y,s_pos.X,' ');
- print_char(con,next_pos.Y,next_pos.X,'S');
- for(int i=0;i<star_cnt;i++){
- if(s_pos.X == star[i].X && s_pos.Y== star[i].Y){
- cur_a = a[s_pos.Y][s_pos.X];
- if(cur_a != 'S' && cur_a != '@') {
- a[s_pos.Y][s_pos.X]='*';
- print_char(con,s_pos.Y,s_pos.X,'*');
- }
- }
- }
- s_pos.Y = next_pos.Y;s_pos.X = next_pos.X;
- star_in_pos = 0;
- for(int i=0;i<star_cnt;i++){
- if(a[star[i].Y][star[i].X] == '@') star_in_pos++;
- }
- }
- }
- }
- system("cls");
- printf("挑战成功!");
- system("pause");
- system("cls");
- return 0;
- }
- int main(){
- HANDLE con = init_con();
- play(con,1);
- play(con,2);
- }
复制代码
|
评分
-
查看全部评分
|