DEV C++环境运行效果不佳或无法运行
第一种实现走迷宫游戏:
#include第二种#include #include #include #include #include using namespace std; char pr[10]= {1,' ','E','O'} ; int level,n,m,map[50][50],i,j,x,y,k,ex,ey; char c[1001]; int main() { system("cls"); puts("if you no full screen,please press full screen"); getch(); system("cls"); puts("please choose level"); again: ; cin>>level; if(level>1||level<1) { puts("haven't this level"); goto again; } puts("loading..."); sprintf(c,"%d.map",level); freopen(c,"r",stdin); cin>>n>>m; for(i=1; i<=n; i++) for(j=1; j<=m; j++) cin>>map[i][j]; fclose(stdin); for(i=1; i<=n; i++) for(j=1; j<=m; j++) if(map[i][j]==3) { x=i; y=j; } else if(map[i][j]==2) { ex=i; ey=j; } while(x!=ex&&y!=ey) { system("cls"); for(i=1; i<=n; i++,putchar('n')) for(j=1; j<=m; j++) putchar(pr[map[i][j]]); k=getch(); if(k=='w') if(map[x-1][y]&&(x-1)) { map[x-1][y]=3; map[x][y]=1; x--; } if(k=='s') if(map[x+1][y]&&(x+1)<=n) { map[x+1][y]=3; map[x][y]=1; x++; } if(k=='a') if(map[x][y-1]&&(y-1)) { map[x][y-1]=3; map[x][y]=1; y--; } if(k=='d') if(map[x][y+1]&&(y+1)<=m) { map[x][y+1]=3; map[x][y]=1; y++; } } system("cls"); return 0; }
实现贪吃蛇游戏:
#include第三种#include #include using namespace std; const int h=50,w=50,MaxLen=400; void gotoxy(short y,short x) { COORD pos= {x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); } struct node { int x,y; }; node s[MaxLen]; node food; int dir,len; int Map[h+5][w+5]; int Time[7]= {0},level; void FoodCreate() { srand ((unsigned)time(NULL)); while(1) { food.x=rand()%h,food.y=rand()%w; if(food.x>0&&food.y>0&&Map[food.x][food.y]==0)break; } gotoxy(food.x,food.y),printf("@"); gotoxy(h+1,0); } void init() { system("cls"); for(int i=0; i<=h; i++) { for(int j=0; j<=w; j++) { if(i==0||j==0||i==h||j==w)Map[i][j]=1,printf("#"); else Map[i][j]=0,printf(" "); } printf("n"); } len=2; dir=0; s[1].x=12,s[1].y=4; s[len].x=12,s[len].y=3; Map[s[1].x][s[1].y]=Map[s[len].x][s[len].y]=1; gotoxy(s[1].x,s[1].y),printf("*"); gotoxy(s[len].x,s[len].y),printf("*"); gotoxy(h+1,0); FoodCreate(); } int move() { node next=s[1]; switch(dir) { case 0: next.y++; break; case 1: next.x--; break; case 2: next.y--; break; case 3: next.x++; break; } if(Map[next.x][next.y])return 0; if(next.x==food.x&&next.y==food.y) { len++; FoodCreate(); } else { gotoxy(s[len].x,s[len].y),printf(" "); Map[s[len].x][s[len].y]=0; } gotoxy(next.x,next.y),printf("*"); gotoxy(h+1,0); Map[next.x][next.y]=1; for(int i=len; i>1; i--)s[i]=s[i-1]; s[1]=next; Sleep(100); return 1; } void GameOver() { for(int i=1; i<=3; i++) { gotoxy(s[1].x,s[1].y); printf(" "); Sleep(300); gotoxy(s[1].x,s[1].y); printf("*"); Sleep(300); } gotoxy(h+1,0); printf("GameOvern"); printf("Press any key to continue..."); } void Welcome() { printf("为了您的游戏体验,请先调整控制台字体和布局(记得不要忘了默认设置):n"); printf("右键白色框->属性->字体 选择点阵字体并调整字体大小为8×8n"); printf("再选择布局设置窗口大小,推荐60×60nn"); printf("WASD控制方向n"); printf("n任意键进入贪吃蛇皮..."); getch(); } int main() { Welcome(); init(); while(1) { if(kbhit()) { char ch=getch(); int temp=dir; switch(ch) { case 'd': temp=0; break; case 'w': temp=1; break; case 'a': temp=2; break; case 's': temp=3; break; } if((temp+dir)%2)dir=temp; } if(move()==0) { GameOver(); getch(); init(); } } }
实现推箱子游戏(此游戏基于DEV C++)
#include第四种#include #include #include using namespace std; void Game_Menu(HANDLE hout); void Game_description(HANDLE hout); void gotoxy(HANDLE hout, int x, int y); int DrawMap(HANDLE hout); void Move(HANDLE hout); int finish(); void setmap(int n); void color(int m); bool flag = true; int pass = 1; #define R 10 #define C 10 #define framex 20 #define framey 14 int map[R][C] = {0}; int map1[R][C] = { { 0,0,1,1,1,0,0,0 }, { 0,0,1,3,1,0,0,0 }, { 0,0,1,0,1,1,1,1 }, { 1,1,1,0,0,4,3,1 }, { 1,3,4,4,0,1,1,1 }, { 1,1,1,5,4,1,0,0 }, { 0,0,0,1,3,1,0,0 }, { 0,0,0,1,1,1,0,0 } }; int map2[R][C]= { {1,1,1,1,1,0,0,0,0,0}, {1,5,0,0,1,0,0,0,0,0}, {1,0,4,4,1,0,1,1,1,0}, {1,0,4,0,1,0,1,3,1,0}, {1,1,1,0,1,1,1,3,1,0}, {0,1,1,0,0,0,0,3,1,0}, {0,1,0,0,0,1,0,0,1,0}, {0,1,0,0,0,1,1,1,1,0}, {0,1,1,1,1,1,0,0,0,0} }; int map3[R][C]= { { 0,0,0,1,1,1,1,1,1,1 }, { 0,0,1,1,0,0,1,0,5,1 }, { 0,0,1,0,0,0,1,0,0,1 }, { 0,0,1,4,0,4,0,4,0,1 }, { 0,0,1,0,4,1,1,0,0,1 }, { 1,1,1,0,4,0,1,0,1,1 }, { 1,3,3,3,3,3,0,0,1,0 }, { 1,1,1,1,1,1,1,1,1,0 }, }; void Game_Menu(HANDLE hout) { system("cls"); gotoxy(hout, framex, 1); cout << "*******************"; gotoxy(hout, framex, 3); cout << " 推箱子 "; gotoxy(hout, framex, 5); cout << " 按s或S开始 "; gotoxy(hout, framex, 7); cout << " 按q或Q退出 "; gotoxy(hout, framex, 9); cout << "游戏前关闭中文输入 "; gotoxy(hout, framex, 11); cout << "*******************"; _getch(); }; void Game_description(HANDLE hout) { //操作提示 system("cls"); gotoxy(hout, framex, 1); cout << "*****************************"; gotoxy(hout, framex, 3); cout << " 按方向键上下左右移动 "; gotoxy(hout, framex, 5); cout << " 所有箱子到达目的地游戏胜利 "; gotoxy(hout, framex, 7); cout << " 按q或Q退出 "; gotoxy(hout, framex, 9); cout << "*****************************"; }; void gotoxy(HANDLE hout, int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hout, pos); }; void print(int i) { switch(i) { case 0: color(0x7); cout << " "; break; case 1: color(8); cout << "■"; break; case 3: color(0xE); cout << "◇"; break; case 4: color(4); cout << "□"; break; case 5: color(3); cout << "♀"; break; case 7: color(6); cout << "◆"; break; case 8: color(3); cout << "♀"; break; default: break; } } int DrawMap(HANDLE hout) { gotoxy(hout, framex + C, framey - 3); color(0xF); cout << "第" << pass << "关"; for(int i = 0; i < R; i++) { gotoxy(hout, framex, framey + i); for(int j = 0; j < C; j++) { print(map[i][j]); } } return 0; }; void Move(HANDLE hout) { int x = 0, y = 0; for(int i = 0; i < R; i++) { for(int j = 0; j < C; j++) { if(map[i][j] == 5 || map [i] [j] == 8) { x = i; y = j; break; } } } gotoxy(hout, framex, framey + R); color(0xF); printf("当前位置:(%d, %d)", x, y); int ch = _getch(); switch(ch) { case 'w': case 'W': case 72: if(map[x - 1][y] == 0 || map[x - 1][y] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x - 1][y] += 5; gotoxy(hout, framex + 2 * y, framey + x - 1); print(map[x - 1][y]); } else if(map[x - 1][y] == 4 || map[x - 1][y] == 7) { if(map[x - 2][y] == 0 || map[x - 2][y] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x - 1][y] += 1; gotoxy(hout, framex + 2 * y, framey + x - 1); print(map[x - 1][y]); map[x - 2][y] += 4; gotoxy(hout, framex + 2 * y, framey + x - 2); print(map[x - 2][y]); } } break; case 's': case 'S': case 80: if(map[x + 1][y] == 0 || map[x + 1][y] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x + 1][y] += 5; gotoxy(hout, framex + 2 * y, framey + x + 1); print(map[x + 1][y]); } else if(map[x + 1][y] == 4 || map[x + 1][y] == 7) { if(map[x + 2][y] == 0 || map[x + 2][y] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x + 1][y] += 1; gotoxy(hout, framex + 2 * y, framey + x + 1); print(map[x + 1][y]); map[x + 2][y] += 4; gotoxy(hout, framex + 2 * y, framey + x + 2); print(map[x + 2][y]); } } break; case 'a': case 'A': case 75: if(map[x][y - 1] == 0 || map[x][y - 1] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x][y - 1] += 5; gotoxy(hout, framex + 2 * y - 2, framey + x); print(map[x][y - 1]); } else if(map[x][y - 1] == 4 || map[x][y - 1] == 7) { if(map[x][y - 2] == 0 || map[x][y - 2] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x][y - 1] += 1; gotoxy(hout, framex + 2 * y - 2, framey + x); print(map[x][y - 1]); map[x][y - 2] += 4; gotoxy(hout, framex + 2 * y - 4, framey + x); print(map[x][y - 2]); } } break; case 'd': case 'D': case 77: if(map[x][y + 1] == 0 || map[x][y + 1] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x][y + 1] += 5; gotoxy(hout, framex + 2 * y + 2, framey + x); print(map[x][y + 1]); } else if(map[x][y + 1] == 4 || map[x][y + 1] == 7) { if(map[x][y + 2] == 0 || map[x][y + 2] == 3) { map[x][y] -= 5; gotoxy(hout, framex + 2 * y, framey + x); print(map[x][y]); map[x][y + 1] += 1; gotoxy(hout, framex + 2 * y + 2, framey + x); print(map[x][y + 1]); map[x][y + 2] += 4; gotoxy(hout, framex + 2 * y + 4, framey + x); print(map[x][y + 2]); } } break; case 'q': case 'Q': flag = false; default: break; } }; int finish() { //判断游戏结束 for(int i = 0; i < R; i++) { for(int j = 0; j < C; j++) { if(map[i][j] == 4)return 0; } } return 1; }; void setmap(int n) { switch(n) { case 1: memcpy(map, map1, sizeof(map1)); break; case 2: memcpy(map, map2, sizeof(map2)); break; case 3: memcpy(map, map3, sizeof(map3)); break; } }; void color(int m) { //改变输出符号的颜色 HANDLE consolehend; consolehend = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(consolehend, m); }; int main() { HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); Game_Menu(hout); char ch = getch(); setmap(pass); Game_description(hout); DrawMap(hout); if(ch == 'q' || ch == 'Q') { return 0; } while(flag) { Move(hout); if(finish()) { DrawMap(hout); gotoxy(hout, framex, framey + R); cout << " 恭喜,成功过关!"; gotoxy(hout, framex, framey + R + 2); cout << "重玩(R)"; ch = getch(); system("cls"); pass++; if(ch == 'r' || ch == 'R')pass--; if(pass > 3) { gotoxy(hout, framex, framey); cout << " 您已通过全部关卡!"; getch(); flag = false; } else { setmap(pass); Game_description(hout); DrawMap(hout); } } } return 0; }
实现简单24点游戏
#include第五种#include #include #include using namespace std; char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' }; char buf[4]; double nums[4]; char ope[4] = { '+','-','*','/' }; void cre() { int i = 0; int j; cout << "生成的四张牌面为:"; srand((unsigned)time(0)); for (i = 0; i<4; i++) { j =rand() % 13; buf[i] = card[j]; } cout << buf[0] << ";" << buf[1] << ";" << buf[2] << ";" << buf[3] << "。" << endl; for (i = 0; i<4; i++) { if (buf[i] == 'A') nums[i] = 1; else if(buf[i] == '2') nums[i] = 2; else if (buf[i] == '3') nums[i] = 3; else if (buf[i] == '4') nums[i] = 4; else if (buf[i] == '5') nums[i] = 5; else if (buf[i] == '6') nums[i] = 6; else if (buf[i] == '7') nums[i] = 7; else if (buf[i] == '8') nums[i] = 8; else if (buf[i] == '9') nums[i] = 9; else if (buf[i] == '10') nums[i] = 10; else if (buf[i] == 'J') nums[i] = 11; else if (buf[i] == 'Q') nums[i] = 12; else if (buf[i] == 'K') nums[i] = 13; } } double calcute(double a, double b, char index) { if (index == '+') return a + b; else if (index == '-') return a - b; else if (index == '*') return a*b; else if (index == '/') if (b != 0) return a / b; } void exh() { double temp[3], tem[2]; double sum; int judge = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { for (int m = 0; m < 3; m++) { if (nums[m + 1] == 0 && ope[i] == '/') break; temp[m] = calcute(nums[m], nums[m + 1], ope[i]); temp[(m + 1) % 3] = nums[(m + 2) % 4]; temp[(m + 2) % 3] = nums[(m + 3) % 4]; for (int n = 0; n < 2; n++) { if (temp[n + 1] == 0 && ope[j] == '/') break; tem[n] = calcute(temp[n], temp[n + 1], ope[j]); tem[(n + 1) % 2] = temp[(n + 2) % 3]; if (tem[1] == 0 && ope[k] == '/') break; sum = calcute(tem[0], tem[1], ope[k]); if (sum == 24) { judge = 1; if (m == 0 && n == 0) cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 0 && n == 1) cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 1 && n == 0) cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 1 && n == 1) cout << nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << ")" << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl; } } } } } } if (judge == 0) cout << "这四张扑克牌无法找到一个合理的解" << endl; } int main() { int i; int select = 1; cout<< " ################################################" << endl << " # #" << endl << " # 欢迎进入24点游戏 #" << endl << " # #" << endl << " ################################################" << endl; while (select) { cout<< " ################################################" << endl << " # #" << endl << " # 是否开始游戏 #" << endl << " # #" << endl << " # 0.是 1.否 #" << endl << " # #" << endl << " ################################################" << endl; cout << "请输入你的选择(0或1):"; cin >> i; switch (i) { case 0: cre(); exh(); break; case 1: select = 0; break; default: cout << "请在0和1之间选择!" << endl; } } return 0; }
实现俄罗斯方块游戏(无需图形库)
#include#include #include #include #include #define MOD 28 #define SIZE_N 19 #define SIZE_M 12 int cur_x,cur_y; int score,mark,next,map[SIZE_N][SIZE_M],Gamespeed=300; int shape[28][6]= { {0,-1,0,-2,1,0}, {0,1,1,0,2,0}, {-1,0,0,1,0,2}, {0,-1,-1,0,-2,0}, {0,-1,0,1,-1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1}, {-1,1,0,1,1,0}, {0,-1,1,0,1,1}, {-1,0,0,-1,1,-1}, {-1,-1,-1,0,0,1}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,-1,0,-2}, {-1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0}, {0,1,1,0,1,1}, {0,-1,1,0,1,-1}, {-1,0,0,-1,-1,-1}, {-1,0,-1,1,0,1}, {0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-1,0,-2,0,-3,0} }; void gotoxy(int x,int y) { COORD c; c.X=x-1; c.Y=y-1; SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c); } void Gameover() { int i,j,flag=0; for(j=1; j 1; ii--) { for(jj=1; jj



