-
个人简介
净化,我带的是净化
"The uglier the action, the stronger the kill; The more handsome the action, the more difficult it is to play the ball"
Dawn has arrived, that's all.
你干嘛~~还海哟...
XMW控制软件密码(worning): xiaoma00125
----------------------[AC]---------------------
/C++万能头/:#include<bits/stdc++.h>
C++快读
#include<bits/stdc++.h> #define ll long long using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - 48, c = getchar(); } return x * f; } int main() { return 0; }
freopen("test.txt","w",stdout)——将控制台控制(输出)改为文件控制(输出),用于oj上某些题要求,且此代码必须写于int main(){}大括号之中`
freopen("A.in","r",stdin)(输入) freopen("A.out","w",stdout)(输出),规则如上
0~n个一的求法:(1<<n)-1
从小到大 数组 排序:
一. sort——sort(列表名称,列表名称+列表元素数量)
二. bool——bool cmp(int x,int y) { //return x<y; 从小到大排序 //return x>y; 从大到小排序 }
三.
1、栈列的定义 stack<关键词> 栈名;
2、栈列的基本操作 栈名.操作函数名() 栈名.push(入栈元素) //入栈 栈名.pop() //出栈 栈名.stack() //返回值为栈头 栈名.size() //返回值为栈中元素个数 栈名.empty() //栈空返回 true 不空返回false 栈名.empty() //栈空返回 true 不空返回false
要执行出栈和查看栈顶操作时--
栈必须非空
//基础栈代码 #include<bits/stdc++.h> using namespace std; int main() { //创建一个栈 // stack<类型> 栈名1; stack stk; stk.push(1); stk.push(3); stk.push(2); stk.pop();//这个不能输出 cout << "栈顶" << stk.top() << endl; cout << "栈中元素个数" << stk.size() << endl; cout << "栈是否为空,空是true,不空是false" << stk.empty() << endl; //连续输入多个元素 int n; cin >> n; for(int i=1;i<=n;i++) { int x; cin >>x; stk.push(x); } //遍历栈中元素 while( !stk.empty() ) { cout << stk.top() << " "; stk.pop(); } return 0; }
queue<关键词> 队名; 2、队列的基本操作 队名.操作函数名() 队名.push(入栈元素) //入队 队名.pop() //出队 队名.front() //返回值为队头 队名.back() //返回队尾 队名.size() //返回值为队中元素个数 队名.empty() //栈空返回 true 不空返回false !队名.empty() //栈空返回 true 不空返回false
**min(a,b);//返回a,b的较小值 max(a,b);//返回a,b的较大值 abs(x);//返回x的绝对值 pow(a,b);//返回a的b次方 sqrt(a);//返回更号a**
按位与& (同1则1,一0则0)
例如
3&5 0011 & 0101 ----------- 0001 3&5 = 1
按位或| (同0则0,一1则1)
例如
3|5 0011 | 0101 ----------- 0111 3|5 = 7
按位取反~ (01互换)
例如1
~3(8位二进制数) ~ 0000 0011 ---------------- 1111 1100 ~3=-4
按位异或^ (不同为1,相同为0)
例如
3^5 0011 ^ 0101 ----------- 0110 3^5 = 6
位运算符
左移运算:
<<
用法:
x<<y
作用:将表示 x 的二进制数的每一位左移 y 位,移出去的数就丢掉,空余地方用 0 补位。
例如:一个二进制数 10101011 将其左移 3 位,得到 01011000。
右移运算:
>>
用法:
x>>y
作用:将表示 x 的二进制数的每一位右移 y 位,移出去的数就丢掉,空余地方用 0 补位。
例如:一个二进制数 10101011 将其右移 3 位,得到 00010101。
按位与运算:
&
用法:
x&y
作用:按位进行与运算。
例如:1101 和 0011 进行与运算就为:0001。
按位或运算:
|
用法:
x|y
作用:按位进行或运算。
例如:1101 和 0011 进行或运算就为:1111。
按位异或运算:
^
用法:
x^y
作用:按位进行异或运算。
例如:1101 和 0011 进行异或运算就为:0001。
按位非运算:
~
用法:
~x
作用:按位进行非运算。
例如:1101进行非运算就为:0010。
状态压缩常用位运算符技巧
1.取出x的第k位:
y = x&(1<<(k-1));//i<<(k-1)能够做成一个第k为1,其余位为0,如:10000 的二进制数,再结合位与运算就能提取到变量x的二进制中第k位数为1还是0了,常用于判断
2.将x第k位取反:
x ^= (1<<(k-1));//通过左移制作一个10000般的二进制数,然后结合异或运算的特点,将变量x的二进制中第k位数取反
3.将x第k位变为1:
x |= (1<<(k-1));//通过左移制作一个10000般的二进制数,然后结合异或运算的特点,将变量x的二进制中第k位数修改为1
4.将x第k位变为0:
x &= (~(1<<(k-1))); //通过左移制作一个0001 0000般的二进制数,然后位非运算将其修改为1110 0000般的二进制数,最后结合位与运算的特点,将变量x的二进制中第k位数修改为0
5.将x最靠右的1去掉:
x = x&(x-1); //减去1会将数字二进制中末尾的1去掉,然后需要借位的地方全变为1,如原1010 0000,减去1后变成1001 1111,再进位与运算得1000 0000,相当于去掉末尾1
6.取出x最靠右的1:
y = x&(-x); //结合负数的二进制的特点,如数字20的二进制为0001 0100,-20的二进制为1110 1100,再进行位与运算能够获取到二进制数100也就是4,及提前数字x中包含的最大2的指数值
7.判断是否有两个连续的一:
if(x&(x<<1)) cout<<"YES"; //左移后的数字会进行偏移,如13的二进制0000 1101,左移后未0001 1010,再进行位与运算,连续的1会在偏移后有至少一个1重叠,让结果不为0,如果结果为0,说明不存在连续的1;
memset(数组名,0,sizeof 数组名);//只能置为0或者-1
C++二分查找(示例代码)
#include<bits/stdc++.h> using namespace std; int arr[200005]; int main(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>arr[i]; } sort(arr,arr+1+n); for(int i=1;i<=m;i++){ int t; cin>>t; int ans; int left=1,right=n; while(left<=right){ int mid = (left+right)/2; if(arr[mid]>=t){ ans = mid; right = mid-1; } if(arr[mid]<t) left = mid+1; } if(arr[ans]==t) cout<<"有这个数"; else cout<<"没有这个数"; } return 0; }
递归排序(利用树生成,树,图论等)
//n个球摸m次可放回 #include<iostream> #include<cstdio> using namespace std; int q[105];//q[x] = y 表示第x次摸到的是y号球 int n,m;//n个球摸m次 void dfs(int k)//表示第k次摸球 n个球摸m次 { if(k>m)//摸第m+1次结束 { for(int i=1;i<=m;i++)cout << q[i] << " "; cout << endl; return ; } for(int i=1;i<=n;i++)//一共有1~n个球 { q[k] = i;//第k次摸了i号球 dfs(k+1);//第k+1次摸球 } } int main() { cin >> n >> m; dfs(1); return 0; }
//n个球摸m次不放回 #include<iostream> #include<cstdio> using namespace std; int q[105]; int vis[105];//vis[1]=1 int n,m; void dfs(int k)//表示第k次摸球 n个球摸m次 { if(k>m) { for(int i=1;i<=m;i++)cout << q[i] << " "; cout << endl; return ; } for(int i=1;i<=n;i++) { if( vis[i] ) continue; q[k] = i; vis[i] = 1; dfs(k+1); vis[i] = 0;//回溯 } } int main() { cin >> n >> m; dfs(1); return 0; }
//n个球摸m个的情况 #include<iostream> #include<cstdio> using namespace std; int q[105]; int vis[105];//vis[1]=1 int n,m; void dfs(int k,int pre)//表示第k次摸球 n个球摸m次 { if(k>m) { for(int i=1;i<=m;i++)cout << q[i] << " "; cout << endl; return ; } for(int i=pre+1;i<=n;i++) { if( vis[i] ) continue; q[k] = i; vis[i] = 1; dfs(k+1,i); vis[i] = 0;//回溯 } } int main() { cin >> n >> m; dfs(1,0); return 0; }
C++ 广搜
//示例代码 #include using namespace std; int n,m;//n行,m列 char mat[505][505];//地图 int step[505][505];//step[x][y] 表示起点到第x行第y列的最少步数 bool vis[505][505];//vis[x][y]=1 表示第x行第y列走过了 void bfs(int sx,int sy) { // memset( 数组名 , 0 , sizeof 数组名);//只能置为0或者-1 memset( step , 0 , sizeof step); memset( vis , 0 , sizeof vis); queue quex; queue quey; quex.push(sx); quey.push(sy); step[sx][sy] = 0; vis[sx][sy] = 1; while( !quex.empty() ) { int nowx = quex.front(); quex.pop(); int nowy = quey.front(); quey.pop(); //守株待兔 if(mat[nowx][nowy]=='T') { cout << step[nowx][nowy]+1 ; return ; } for(int i=0;i<4;i++) { int dx[]={0,0,-1,1}; int dy[]={1,-1,0,0}; int tx = nowx + dx[i]; int ty = nowy + dy[i]; if(tx<1||tx>n||ty<1||ty>m) continue;//不能越界 if(vis[tx][ty]) continue;//不能重复 if(mat[tx][ty]=='X') continue;//不能穿墙 step[tx][ty] = step[nowx][nowy] + 1; vis[tx][ty] = 1; quex.push(tx); quey.push(ty); } } cout << -1; } int main() { cin >> n >> m; int sx,sy; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin >> mat[i][j]; if(mat[i][j]=='S') { sx=i; sy=j; } } } bfs(sx,sy); return 0; }
//代码复制区(按空格开启连点) #include<stdio.h> #include<windows.h> int main() { while(1) { if(GetAsyncKeyState(VK_SPACE))//按下空格,开始执行 { while(1) { mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP,0,0,0,0); Sleep(723); if(GetAsyncKeyState(VK_ESCAPE))return 0; } } } return 0; }
C++小游戏(五子棋)
#include <iostream> using namespace std; int b[20][20]; int n; int m; void qipan() { system("cls"); int k = 1; for (int k = 0;k < n;++k) { for (int j = 0;j < m;++j) { cout << " ---- "; } cout << endl; for (int i = 0;i < n;++i) { for (int j = 0;j < m;++j) { if (b[i][j] < 10 && b[i][j] >= 0) cout << "| " << 0 << b[i][j] << " |"; else if (b[i][j] == -1) cout << "| " << "█" << " |"; else if (b[i][j] == -2) cout << "|" << " ○ " << "|"; else cout << "| " << b[i][j] << " |"; k++; } cout << endl; for (int j = 0;j < m;++j) { cout << " ---- "; } cout << endl; } } } void success() { long time; for (;;) { system("color a"); for (time = 0; time < 99999999; time++); system("color b"); for (time = 0; time < 99999999; time++); system("color c"); for (time = 0; time < 99999999; time++); system("color d"); for (time = 0; time < 99999999; time++); system("color e"); for (time = 0; time < 99999999; time++); system("color f"); for (time = 0; time < 99999999; time++); system("color 0"); for (time = 0; time < 99999999; time++); system("color 1"); for (time = 0; time < 99999999; time++); system("color 2"); for (time = 0; time < 99999999; time++); system("color 3"); for (time = 0; time < 99999999; time++); system("color 4"); for (time = 0; time < 99999999; time++); system("color 5"); for (time = 0; time < 99999999; time++); system("color 6"); for (time = 0; time < 99999999; time++); system("color 7"); for (time = 0; time < 99999999; time++); system("color 8"); for (time = 0; time < 99999999; time++); system("color 9"); } } int main() { cout << "请选择你们想要的棋盘大小(棋盘的面积请不要大于100)" << endl; cout << "棋盘的长:(推荐长度为10)" << endl; cin >> m; cout << "棋盘的宽:(推荐长度为10)" << endl; cin >> n; for (int i = 0;i < n;++i) { for (int j = 0;j < m;++j) { b[i][j] = i * m + j; } } qipan(); while (1) { int x; cout << "请玩家一输入:" << endl; cin >> x; if (x == 648) { cout << "玩家一使用系统外挂" << endl; cout << "游戏结束" << endl; cout << "玩家一获胜" << endl; success(); return 0; } for (int i = 0;i < n;++i) { for (int j = 0;j < m;++j) { if (b[i][j] == x) b[i][j] = -1; } } for (int i = 0;i < n;++i) { for (int j = 0;j < m - 4;++j) { if ((b[i][j] == -1 && b[i][j + 1] == -1 && b[i][j + 2] == -1 && b[i][j + 3] == -1 && b[i][j + 4] == -1) || (b[i][j] == -1 && b[i + 1][j] == -1 && b[i + 2][j] == -1 && b[i + 3][j] == -1 && b[i + 4][j] == -1) || (b[i][j] == -1 && b[i + 1][j + 1] == -1 && b[i + 2][j + 2] == -1 && b[i + 3][j + 3] == -1 && b[i + 4][j + 4] == -1)) { cout << "游戏结束" << endl; cout << "玩家一获胜" << endl; success(); return 0; } else if (i >= 4 && (b[i][j] == -1 && b[i - 1][j + 1] == -1 && b[i - 2][j + 2] == -1 && b[i - 3][j + 3] == -1 && b[i - 4][j + 4] == -1)) { cout << "游戏结束" << endl; cout << "玩家一获胜" << endl; success(); return 0; } } } qipan(); cout << "请玩家二输入:" << endl; cin >> x; if (x == 648) { cout << "玩家二使用系统外挂" << endl; cout << "游戏结束" << endl; cout << "玩家二获胜" << endl; success(); return 0; } for (int i = 0;i < n;++i) { for (int j = 0;j < m;++j) { if (b[i][j] == x) b[i][j] = -2; } } qipan(); for (int i = 0;i < n;++i) { for (int j = 0;j < m - 4;++j) { if ((b[i][j] == -2 && b[i][j + 1] == -2 && b[i][j + 2] == -2 && b[i][j + 3] == -2 && b[i][j + 4] == -2) || (b[i][j] == -2 && b[i + 1][j] == -2 && b[i + 2][j] == -2 && b[i + 3][j] == -2 && b[i + 4][j] == -2) || (b[i][j] == -2 && b[i + 1][j + 1] == -2 && b[i + 2][j + 2] == -2 && b[i + 3][j + 3] == -2 && b[i + 4][j + 4] == -2)) { cout << "游戏结束" << endl; cout << "玩家二获胜" << endl; success(); return 0; } else if (i >= 4 && (b[i][j] == -2 && b[i - 1][j + 1] == -2 && b[i - 2][j + 2] == -2 && b[i - 3][j + 3] == -2 && b[i - 4][j + 4] == -2)) { cout << "游戏结束" << endl; cout << "玩家二获胜" << endl; success(); return 0; } } } } }
————————[AC]————————
C++小游戏(简易枪战)
#include<iostream> #include<cstdio> #include<windows.h> #include<conio.h> #pragma warning(disable : 4996) using namespace std; int SIZ = 20; HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; HANDLE hCon; enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE }; void SetColor(Color c) { if (hCon == NULL) hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon, c); } SYSTEMTIME sys; //sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek struct PLAYER { int x, y; int hp; int gun; int direct; } p1, p2; int map[1005][1005]; int abs(int x) { if (x < 0) return -x; return x; } void locate(int x, int y) { coord.X = y - 1; coord.Y = x - 1; SetConsoleCursorPosition(hout, coord); } void print_map() { locate(1, 1); SetColor(GRAY); for (int i = 1; i <= SIZ; i++) cout << "■"; locate(SIZ, 1); for (int i = 1; i <= SIZ; i++) cout << "■"; for (int i = 2; i < SIZ; i++) { locate(i, 1); cout << "■"; locate(i, SIZ * 2 - 1); cout << "■"; } locate(SIZ + 1, 1); SetColor(WHITE); } void create_tree(int x, int y) { map[x][y] = map[x + 1][y] = map[x - 1][y] = map[x][y + 1] = map[x][y - 1] = 2; } void use_map(int x) { if (x == 1) { SIZ = 20; SetColor(DARKGREEN); map[16][6] = map[15][6] = map[17][6] = map[16][7] = map[16][5] = map[14][13] = map[13][12] = map[13][13] = 2; for (int i = 2; i < SIZ; i++) { for (int j = 2; j < SIZ; j++) { if (map[i][j] == 2) { locate(i, j * 2 - 1); cout << "■"; } } } SetColor(GRAY); for (int i = 5; i <= 15; i++) { map[i][i] = 1; locate(i, i * 2 - 1); cout << "■"; } SetColor(WHITE); } else if (x == 2) { SIZ = 30; SetColor(GRAY); for (int i = 4; i <= 26; i++) { if (i == 13 || i == 14 || i == 15) continue; map[i][4] = map[4][i] = map[26][i] = map[i][26] = 1; } for (int i = 1; i <= SIZ; i++) { for (int j = 1; j <= SIZ; j++) { if (map[i][j] == 1) { locate(i, j * 2 - 1); cout << "■"; } } } SetColor(DARKGREEN); for (int i = 10; i <= 20; i++) { if (i == 13 || i == 17) continue; map[i][10] = map[10][i] = map[20][i] = map[i][20] = 2; } create_tree(5, 5); create_tree(18, 18); for (int i = 1; i <= SIZ; i++) { for (int j = 1; j <= SIZ; j++) { if (map[i][j] == 2) { locate(i, j * 2 - 1); cout << "■"; } } } SetColor(WHITE); } } void cleanbody(int x, int y); void putbody(int x, int y, int z); void player_init() { p1.hp = p2.hp = 300; p1.gun = p2.gun = 1; p1.direct = 4; p2.direct = 2; p1.x = 2; p1.y = 2; p2.x = SIZ - 1; p2.y = SIZ - 1; putbody(p1.x, p1.y, 1); putbody(p2.x, p2.y, 2); } void mapinit() { for (int i = 1; i <= SIZ; i++) { map[i][1] = map[1][i] = map[SIZ][i] = map[i][SIZ] = 1; } } void init() { printf("Use Which Map?\n"); int x; cin >> x; system("cls"); use_map(x); mapinit(); print_map(); player_init(); } void putbody(int x, int y, int z) { if (z == 1) SetColor(BLUE); else if (z == 2) SetColor(RED); locate(x, y * 2 - 1); cout << "■"; SetColor(WHITE); } void cleanbody(int x, int y) { locate(x, y * 2 - 1); cout << " "; } /* LIST direct: w 1 a 2 s 3 d 4 gun: usp 1 mimigun 2 awp 3 block: void 0 stone 1 tree 2 player 3 clip 4 */ bool judge(int x, int y) { if (map[x][y] == 1) return false; if (map[x][y] == 2) return false; if (map[x][y] == 3) return false; return true; } bool judge_gun(int x, int y, int a) { if (map[x][y] == 1) return 0; if (map[x][y] == 2) return 0; if (map[x][y] == 3) { if (p1.x == x && p1.y == y) { if (a == 1)p1.hp -= 50; else if (a == 2)p1.hp -= 100; else if (a == 3)p1.hp -= 300; } else { if (a == 1)p2.hp -= 50; else if (a == 2)p2.hp -= 100; else if (a == 3)p2.hp -= 300; } return 0; } return 1; } int cnt; struct Clip { int x, y; int derect; int force; int start; bool flag; } clip[1000000]; void create_clip(int y, int x, int a, int b,int g) { int X, Y; if (y == 1) { if (!judge_gun(a - 1, b,g)) return; X = a - 1; Y = b; } else if (y == 2) { if (!judge_gun(a, b - 1,g)) return; X = a; Y = b - 1; } else if (y == 3) { if (!judge_gun(a + 1, b,g)) return; X = a + 1; Y = b; } else if (y == 4) { if (!judge_gun(a, b + 1,g)) return; X = a; Y = b + 1; } cnt++; GetLocalTime(&sys); clip[cnt].start = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600; clip[cnt].x = X; clip[cnt].y = Y; if (x == 1) { clip[cnt].derect = p1.direct; } else if (x == 2) { clip[cnt].derect = p2.direct; } } void shoot(int x,int g) { if (x == 1) { create_clip(p1.direct, 1, p1.x, p1.y,g); } else if (x == 2) { create_clip(p2.direct, 2, p2.x, p2.y,g); } } void clean_clip(int x, int y) { locate(x, y * 2 - 1); cout << " "; locate(1, 1); } void print_clip(int x, int y, int i) { if (clip[i].flag) { clean_clip(x, y); return; } locate(x, y * 2 - 1); SetColor(YELLOW); cout << "''"; locate(1, 1); } void clipmove(int g) { GetLocalTime(&sys); int t = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600; for (int i = 1; i <= cnt; i++) { if (clip[i].flag) continue; if (abs(clip[i].start - t) > 50) { clip[i].start = t; int x = clip[i].x; int y = clip[i].y; if (clip[i].derect == 1) { if (!judge_gun(clip[i].x - 1, clip[i].y,g)) { clip[i].flag = 1; clean_clip(x, y); continue; } clean_clip(clip[i].x, clip[i].y); clip[i].x--; print_clip(clip[i].x, clip[i].y, i); } else if (clip[i].derect == 2) { if (!judge_gun(clip[i].x, clip[i].y - 1,g)) { clip[i].flag = 1; clean_clip(x, y); continue; } clean_clip(clip[i].x, clip[i].y); clip[i].y--; print_clip(clip[i].x, clip[i].y, i); } else if (clip[i].derect == 3) { if (!judge_gun(clip[i].x + 1, clip[i].y,g)) { clip[i].flag = 1; clean_clip(x, y); continue; } clean_clip(clip[i].x, clip[i].y); clip[i].x++; print_clip(clip[i].x, clip[i].y, i); } else if (clip[i].derect == 4) { if (!judge_gun(clip[i].x, clip[i].y + 1,g)) { clip[i].flag = 1; clean_clip(x, y); continue; } clean_clip(clip[i].x, clip[i].y); clip[i].y++; print_clip(clip[i].x, clip[i].y, i); } } } } int judge_hp() { int x = p1.hp; int y = p2.hp; if (x < 0 && y<0 && x > y) swap(x, y); if (x <= 0) { locate(1, 1); system("cls"); printf("GAME OVER!\nTHE WINNER IS P2!\n"); return 1; } else if (y <= 0) { locate(1, 1); system("cls"); printf("GAME OVER!\nTHE WINNER IS P1!\n"); return 1; } return 0; } void prog(int g) { int t; char ch; while (true) { if (kbhit()) { ch = getch(); if (ch == 'w' && judge(p1.x - 1, p1.y)) { p1.direct = 1; cleanbody(p1.x, p1.y); map[p1.x][p1.y] = 0; putbody(--p1.x, p1.y, 1); map[p1.x][p1.y] = 3; } else if (ch == '8' && judge(p2.x - 1, p2.y)) { p2.direct = 1; cleanbody(p2.x, p2.y); map[p2.x][p2.y] = 0; putbody(--p2.x, p2.y, 2); map[p2.x][p2.y] = 3; } else if (ch == 'a' && judge(p1.x, p1.y - 1)) { p1.direct = 2; cleanbody(p1.x, p1.y); map[p1.x][p1.y] = 0; putbody(p1.x, --p1.y, 1); map[p1.x][p1.y] = 3; } else if (ch == '4' && judge(p2.x, p2.y - 1)) { p2.direct = 2; cleanbody(p2.x, p2.y); map[p2.x][p2.y] = 0; putbody(p2.x, --p2.y, 2); map[p2.x][p2.y] = 3; } else if (ch == 's' && judge(p1.x + 1, p1.y)) { p1.direct = 3; cleanbody(p1.x, p1.y); map[p1.x][p1.y] = 0; putbody(++p1.x, p1.y, 1); map[p1.x][p1.y] = 3; } else if (ch == '5' && judge(p2.x + 1, p2.y)) { p2.direct = 3; cleanbody(p2.x, p2.y); map[p2.x][p2.y] = 0; putbody(++p2.x, p2.y, 2); map[p2.x][p2.y] = 3; } else if (ch == 'd' && judge(p1.x, p1.y + 1)) { p1.direct = 4; cleanbody(p1.x, p1.y); map[p1.x][p1.y] = 0; putbody(p1.x, ++p1.y, 1); map[p1.x][p1.y] = 3; } else if (ch == '6' && judge(p2.x, p2.y + 1)) { p2.direct = 4; cleanbody(p2.x, p2.y); map[p2.x][p2.y] = 0; putbody(p2.x, ++p2.y, 2); map[p2.x][p2.y] = 3; } else if (ch == '0') { shoot(2,g); } else if (ch == ' ') { shoot(1,g); } Sleep(20); } clipmove(g); t=judge_hp(); if (t == 1) { Sleep(2000); break; } } } int welcome() { system("cls"); printf("操作方法:\n玩家1 wasd控制移动,空格攻击\n玩家2 数字小键盘4568控制移动,0攻击\n"); printf("Which GUN do you want to choose?\n"); printf("1.usp------2.mimigun------3.awp\n"); int a; cin >> a; return a; } int main() { int i; for (;;) { int g; g = welcome(); GetLocalTime(&sys); init(); prog(g); printf("Continue?\n1.Yes------2.NO\n"); cin >> i; if (i == 2)exit(0); } return 0; }
————————AC———————— C++小游戏(俄罗斯方块)
#include <iostream> #include <string> #include <ctime> #include <cstdlib> #include <windows.h> #include <conio.h> using namespace std; int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } }; int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } }; int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } }; int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } }; int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } }; int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } }; int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } }; int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } }; int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } }; int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } }; int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } }; int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } }; int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } }; int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } }; int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } }; int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } }; int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } }; int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } }; int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } }; void initialWindow(HANDLE hOut);//初始化窗口 void initialPrint(HANDLE hOut);//初始化界面 void gotoXY(HANDLE hOut, int x, int y);//移动光标 void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置 bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞 void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块 void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块 void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移 void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移 void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度 int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落 void myStop(HANDLE hOut, int block[4][4]);//游戏暂停 void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束 void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值 int main() { int map[21][12]; int blockA[4][4];//候选区的方块 int blockB[4][4];//下落中的方块 int positionX, positionY;//方块左上角的坐标 bool check;//检查方块还能不能下落 char key;//用来存储按键 int val;//用来控制下落速度 int fraction;//用来存储得分 int checkpoint;//用来存储关卡 int times; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄 initialWindow(hOut); initial: gotoXY(hOut, 0, 0); initialPrint(hOut); check = true; val = 50; fraction = 0; checkpoint = 1; times = val; for (int i = 0; i < 20; ++i) { for (int j = 1; j < 11; ++j) { map[i][j] = 0; } } for (int i = 0; i < 20; ++i) { map[i][0] = map[i][11] = 1; } for (int i = 0; i < 12; ++i) { map[20][i] = 1; } srand((unsigned)time(NULL)); roundBlock(hOut, blockA); while (true) { if (check) { eliminateRow(hOut, map, val, fraction, checkpoint); check = false; positionX = -3; positionY = 4; if (collisionDetection(blockA, map, positionX, positionY)) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { blockB[i][j] = blockA[i][j]; } } roundBlock(hOut, blockA); } else { gameOver(hOut, blockA, map); goto initial; } } printBlock(hOut, blockB, positionX, positionY); if (_kbhit()) { key = _getch(); switch (key) { case 72: myUp(hOut, blockB, map, positionX, positionY); break; case 75: myLeft(hOut, blockB, map, positionX, positionY); break; case 77: myRight(hOut, blockB, map, positionX, positionY); break; case 80: switch (myDown(hOut, blockB, map, positionX, positionY)) { case 0: check = false; break; case 1: check = true; break; case 2: gameOver(hOut, blockB, map); goto initial; default: break; } break; case 32: myStop(hOut, blockA); break; case 27: exit(0); default: break; } } Sleep(20); if (0 == --times) { switch (myDown(hOut, blockB, map, positionX, positionY)) { case 0: check = false; break; case 1: check = true; break; case 2: gameOver(hOut, blockB, map); goto initial; default: break; } times = val; } } cin.get(); return 0; } void initialWindow(HANDLE hOut) { SetConsoleTitle("俄罗斯方块"); COORD size = { 80, 25 }; SetConsoleScreenBufferSize(hOut, size); SMALL_RECT rc = { 0, 0, 79, 24 }; SetConsoleWindowInfo(hOut, true, &rc); CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; SetConsoleCursorInfo(hOut, &cursor_info); } void initialPrint(HANDLE hOut) { SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); for (int i = 0; i < 20; ++i) { cout << "■ ■☆ ☆" << endl; } gotoXY(hOut, 26, 0); cout << "☆☆☆☆☆☆☆☆☆☆☆"; gotoXY(hOut, 0, 20); cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆"; gotoXY(hOut, 26, 1); cout << "分 数: "; gotoXY(hOut, 26, 2); cout << "关 卡: "; gotoXY(hOut, 26, 4); cout << "下一方块:"; gotoXY(hOut, 26, 9); cout << "操作方法:"; gotoXY(hOut, 30, 11); cout << "↑:旋转 ↓:速降"; gotoXY(hOut, 30, 12); cout << "→:右移 ←:左移"; gotoXY(hOut, 30, 13); cout << "空格键:开始/暂停"; gotoXY(hOut, 30, 14); cout << "Esc 键:退出"; gotoXY(hOut, 26, 16); cout << "关 于:"; gotoXY(hOut, 30, 18); cout << "俄罗斯方块V1.0"; gotoXY(hOut, 35, 19); cout << "作者:李国良"; } void gotoXY(HANDLE hOut, int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOut, pos); } void roundBlock(HANDLE hOut, int block[4][4]) { clearBlock(hOut, block, 5, 15); switch (rand() % 19) { case 0: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block00[i][j]; } } break; case 1: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block01[i][j]; } } break; case 2: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block02[i][j]; } } break; case 3: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block03[i][j]; } } break; case 4: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block04[i][j]; } } break; case 5: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block05[i][j]; } } break; case 6: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block06[i][j]; } } break; case 7: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block07[i][j]; } } break; case 8: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block08[i][j]; } } break; case 9: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block09[i][j]; } } break; case 10: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block10[i][j]; } } break; case 11: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block11[i][j]; } } break; case 12: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block12[i][j]; } } break; case 13: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block13[i][j]; } } break; case 14: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block14[i][j]; } } break; case 15: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block15[i][j]; } } break; case 16: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block16[i][j]; } } break; case 17: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block17[i][j]; } } break; case 18: for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block18[i][j]; } } break; default: break; } printBlock(hOut, block, 5, 15); } bool collisionDetection(int block[4][4], int map[21][12], int x, int y) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1) { return false; } } } return true; } void printBlock(HANDLE hOut, int block[4][4], int x, int y) { switch (block[0][0]) { case 10: case 11: SetConsoleTextAttribute(hOut, FOREGROUND_GREEN); break; case 12: case 13: case 14: case 15: SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); break; case 16: case 17: case 18: case 19: SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); break; case 20: case 21: case 22: case 23: SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY); break; case 24: case 25: SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY); break; case 26: case 27: SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY); break; case 28: SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); break; default: break; } for (int i = 0; i < 4; ++i) { if (i + x >= 0) { for (int j = 0; j < 4; ++j) { if (block[i][j] == 1) { gotoXY(hOut, 2 * (y + j), x + i); cout << "■"; } } } } } void clearBlock(HANDLE hOut, int block[4][4], int x, int y) { for (int i = 0; i < 4; ++i) { if (i + x >= 0) { for (int j = 0; j < 4; ++j) { if (block[i][j] == 1) { gotoXY(hOut, 2 * (y + j), x + i); cout << " "; } } } } } void gameOver(HANDLE hOut, int block[4][4], int map[21][12]) { SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); gotoXY(hOut, 9, 8); cout << "GAME OVER"; gotoXY(hOut, 8, 9); cout << "空格键:重来"; gotoXY(hOut, 8, 10); cout << "ESC键:退出"; char key; while (true) { key = _getch(); if (key == 32) { return; } if (key == 27) { exit(0); } } } int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y) { if (collisionDetection(block, map, x + 1, y)) { clearBlock(hOut, block, x, y); ++x; return 0; } if (x < 0) { return 2; } for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (block[i][j] == 1) { map[x + i][y + j] = 1; SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); gotoXY(hOut, 2 * (y + j), x + i); cout << "■"; } } } return 1; } void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) { if (collisionDetection(block, map, x, y - 1)) { clearBlock(hOut, block, x, y); --y; } } void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) { if (collisionDetection(block, map, x, y + 1)) { clearBlock(hOut, block, x, y); ++y; } } void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) { switch (block[0][0]) { case 10: if (collisionDetection(block01, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block01[i][j]; } } } break; case 11: if (collisionDetection(block00, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block00[i][j]; } } } else if (collisionDetection(block00, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block00[i][j]; } } --y; } else if (collisionDetection(block00, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block00[i][j]; } } ++y; } else if (collisionDetection(block00, map, x, y - 2)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block00[i][j]; } } y = y - 2; } else if (collisionDetection(block00, map, x, y + 2)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block00[i][j]; } } y = y + 2; } break; case 12: if (collisionDetection(block03, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block03[i][j]; } } } else if (collisionDetection(block03, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block03[i][j]; } } --y; } else if (collisionDetection(block03, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block03[i][j]; } } ++y; } break; case 13: if (collisionDetection(block04, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block04[i][j]; } } } else if (collisionDetection(block04, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block04[i][j]; } } --y; } else if (collisionDetection(block04, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block04[i][j]; } } ++y; } break; case 14: if (collisionDetection(block05, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block05[i][j]; } } } else if (collisionDetection(block05, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block05[i][j]; } } --y; } else if (collisionDetection(block05, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block05[i][j]; } } ++y; } break; case 15: if (collisionDetection(block02, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block02[i][j]; } } } else if (collisionDetection(block02, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block02[i][j]; } } --y; } else if (collisionDetection(block02, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block02[i][j]; } } ++y; } break; case 16: if (collisionDetection(block07, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block07[i][j]; } } } else if (collisionDetection(block07, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block07[i][j]; } } --y; } else if (collisionDetection(block07, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block07[i][j]; } } ++y; } break; case 17: if (collisionDetection(block08, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block08[i][j]; } } } else if (collisionDetection(block08, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block08[i][j]; } } --y; } else if (collisionDetection(block08, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block08[i][j]; } } ++y; } break; case 18: if (collisionDetection(block09, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block09[i][j]; } } } else if (collisionDetection(block09, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block09[i][j]; } } --y; } else if (collisionDetection(block09, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block09[i][j]; } } ++y; } break; case 19: if (collisionDetection(block06, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block06[i][j]; } } } else if (collisionDetection(block06, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block06[i][j]; } } --y; } else if (collisionDetection(block06, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block06[i][j]; } } ++y; } break; case 20: if (collisionDetection(block11, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block11[i][j]; } } } else if (collisionDetection(block11, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block11[i][j]; } } --y; } else if (collisionDetection(block11, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block11[i][j]; } } ++y; } break; case 21: if (collisionDetection(block12, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block12[i][j]; } } } else if (collisionDetection(block12, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block12[i][j]; } } --y; } else if (collisionDetection(block12, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block12[i][j]; } } ++y; } break; case 22: if (collisionDetection(block13, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block13[i][j]; } } } else if (collisionDetection(block13, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block13[i][j]; } } --y; } else if (collisionDetection(block13, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block13[i][j]; } } ++y; } break; case 23: if (collisionDetection(block10, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block10[i][j]; } } } else if (collisionDetection(block10, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block10[i][j]; } } --y; } else if (collisionDetection(block10, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block10[i][j]; } } ++y; } break; case 24: if (collisionDetection(block15, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block15[i][j]; } } } else if (collisionDetection(block15, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block15[i][j]; } } --y; } else if (collisionDetection(block15, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block15[i][j]; } } ++y; } break; case 25: if (collisionDetection(block14, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block14[i][j]; } } } else if (collisionDetection(block14, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block14[i][j]; } } --y; } else if (collisionDetection(block14, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block14[i][j]; } } ++y; } break; case 26: if (collisionDetection(block17, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block17[i][j]; } } } else if (collisionDetection(block17, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block17[i][j]; } } --y; } else if (collisionDetection(block17, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block17[i][j]; } } ++y; } break; case 27: if (collisionDetection(block16, map, x, y)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block16[i][j]; } } } else if (collisionDetection(block16, map, x, y - 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block16[i][j]; } } --y; } else if (collisionDetection(block16, map, x, y + 1)) { clearBlock(hOut, block, x, y); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { block[i][j] = block16[i][j]; } } ++y; } break; default: break; } } void myStop(HANDLE hOut, int block[4][4]) { clearBlock(hOut, block, 5, 15); SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY); gotoXY(hOut, 30, 7); cout << "游戏暂停"; char key; while (true) { key = _getch(); if (key == 32) { gotoXY(hOut, 30, 7); cout << " "; printBlock(hOut, block, 5, 15); return; } if (key == 27) { exit(0); } } } void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint) { SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); for (int i = 19; i >= 0; --i) { int x = 0; for (int j = 1; j < 11; ++j) { x += map[i][j]; } if (x == 10) { fraction += 100; if (val > 1 && fraction / 1000 + 1 != checkpoint) { checkpoint = fraction / 1000 + 1; val -= 5; } for (int m = i; m > 0; --m) { for (int n = 1; n < 11; ++n) { map[m][n] = map[m - 1][n]; gotoXY(hOut, 2 * n, m); if (map[m][n] == 1) { cout << "■"; } else { cout << " "; } } } ++i; } } gotoXY(hOut, 36, 1); cout << fraction; gotoXY(hOut, 36, 2); cout << checkpoint; }
————————AC———————— C++小游戏(盗版贪吃蛇)
#ifndef SNAKE_H #define SNAKE_H #include<iostream> #include<windows.h> #include<vector> using namespace std; const int wall=25; class Snakeclass { private: COORD tailcoord,foodcoord; vector<COORD> snake; vector<COORD> map2; int snakesize,j,i,a; int m,mark,speech; bool pattern; public: Snakeclass(){m=wall/2,i=0,j=2,a=0,snakesize=4,mark=0,speech=500;} ~Snakeclass(){} void showmenu();//菜单 void showmap1();//经典地图 void showmap2(int);//街机地图 void drawsnake();//初始化蛇身坐标 void snakemove();//蛇移动判断 void showmark();//显示当前分数 void food();//食物生成 void judgedie();//判断死亡 void customspass();//关卡切换 void savemark();//记录分数 void showsavemark();//读取记录分数 void showface(); //显示表情 void gotoxy(int x,int y);//控制光标函数 void HideCursor();//隐藏光标函数 void color(int a);//控制颜色函数 }; #endif //函数定义和主程序 #include<iostream> #include<math.h> #include<fstream> #include<iomanip> #include<windows.h> #include<ctime> #include<cstdlib> #include<conio.h> #include<vector> void Snakeclass::gotoxy(int x,int y)//位置函数,输入坐标后,光标会移动到那个位置以便进行输出 { COORD pos; pos.X=2*x; //下面输出的图形都占两个位置 pos.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);//光标控制的位置 } void Snakeclass::HideCursor()//隐藏光标 { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void Snakeclass::color(int a)//颜色函数 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);//只有一个参数 } void Snakeclass::showmap1() { COORD cod; for(int i=0;i<=wall;++i) { for(int j=0;j<=wall;++j) { if(i==0||i==wall||j==0||j==wall) { gotoxy(i ,j); color(10); cout<<"■"; }//围墙 else { gotoxy(i ,j); color(3); cout<<"□" ; }//可以玩的区域 } } for(int i=0;i!=snakesize;++i) { cod.X=m; cod.Y=wall-snakesize+i; snake.push_back(cod); } showface(); } void Snakeclass::showmap2(int a) { COORD cod; switch(a) { case 1:break; case 2:for(int i=m-2;i!=m+3;++i) { for(int j=m-2;j!=m+3;++j) { cod.X=i; cod.Y=j; map2.push_back(cod); } for(int i=5;i!=10;++i) { cod.X=i; cod.Y=5; map2.push_back(cod); cod.X=5; cod.Y=i; map2.push_back(cod); cod.X=10+i; cod.Y=5; map2.push_back(cod); cod.X=19; cod.Y=i; map2.push_back(cod); cod.X=i; cod.Y=19; map2.push_back(cod); cod.X=5; cod.Y=10+i; map2.push_back(cod); cod.X=10+i; cod.Y=19; map2.push_back(cod); cod.X=19; cod.Y=10+i; map2.push_back(cod); } }break; case 3:for(int i=7;i!=22;++i) { cod.X=i; cod.Y=9; map2.push_back(cod); } for(int j=9;j!=13;++j) { cod.X=7; cod.Y=j; map2.push_back(cod); } for(int j=9;j!=19;++j) { cod.X=21; cod.Y=j; map2.push_back(cod); } for(int i=4;i!=22;++i) { cod.X=i; cod.Y=18; map2.push_back(cod); cod.X=i; cod.Y=6; map2.push_back(cod); } for(int i=4;i!=19;++i) { cod.X=i; cod.Y=15; map2.push_back(cod); } for(int j=12;j!=16;++j) { cod.X=18; cod.Y=j; map2.push_back(cod); } for(int j=6;j!=16;++j) { cod.X=4; cod.Y=j; map2.push_back(cod); }break; } for(vector<COORD>::iterator beg=map2.begin();beg!=map2.end();++beg) { gotoxy(beg->X,beg->Y); color(10); cout<<"■"; } } void Snakeclass::showmenu() { showmap1(); a=0; COORD cod1,cod2; char ch; bool well=true,well1; gotoxy(m-2,m-2); cout<<"●经典模式"; gotoxy(m-2,m+2); cout<<"●街机模式"; cod1.X=m-4; cod1.Y=m-2; cod2.X=m-4; cod2.Y=m+2; while(well) { well1=true; COORD codtemp=cod1; gotoxy(codtemp.X,codtemp.Y); color(14); cout<<"★"; gotoxy(cod2.X,cod2.Y); color(3); cout<<"□" ; if(kbhit()) { gotoxy(wall,wall); ch=getch(); if(ch!='W'&&ch!='w'&&ch!='D'&&ch!='d'&&ch!='S'&&ch!='s') { well1=false; } if(well1) { switch(ch) { case 'W': case 'w':codtemp=cod1,cod1=cod2,cod2=codtemp;break; case 'S': case 's':codtemp=cod1,cod1=cod2,cod2=codtemp;break; case 'D': case 'd':codtemp.X+=2;break; } } if(codtemp.X==m-2&&codtemp.Y==m+2) { pattern=true; well=false; } if(codtemp.X==m-2&&codtemp.Y==m-2) { pattern=false; well=false; } } } } void Snakeclass::drawsnake() { for(vector<COORD>::iterator beg=snake.begin();beg!=snake.end()-1;++beg) { gotoxy(beg->X,beg->Y); color(14); cout<<"★"<<endl; } showmark(); gotoxy(wall+2,0); cout<<" 《贪吃蛇》"; gotoxy(wall+2,1); cout<<"w,a,s,d控制方向,空格暂停,按住L_SHIFT加速。"; gotoxy(wall+2,wall); color(2); cout<<"制作:刁民工作室"; } void Snakeclass::snakemove() { showsavemark(); int speechtemp1,speechtemp2; char ch,sh; COORD cd; bool well=false,well2=false,well3=true,well4=true; while(1) { cd.X=snake.begin()->X; cd.Y=snake.begin()->Y; if(kbhit()) { ch=getch(); if(well3) { food(); well3=false; } } if(GetAsyncKeyState(VK_LSHIFT) & 0x8000) { speechtemp2=50; } else { speechtemp2=speechtemp1; } if(((sh=='a'||sh=='A')&&(ch=='D'||ch=='d'))||((sh=='D'||sh=='d')&&(ch=='A'||ch=='a'))||((sh=='W'||sh=='w')&&(ch=='S'||ch=='s'))||((sh=='S'||sh=='s')&&(ch=='W'||ch=='w'))||(ch!='A'&&ch!='a'&&ch!='W'&&ch!='w'&&ch!='D'&&ch!='d'&&ch!='S'&&ch!='s'&&ch!=' ')) {ch=sh;} switch(ch) { case 'W': case 'w':--cd.Y,well=true;break; case 'S': case 's':++cd.Y,well=true;break; case 'A': case 'a':--cd.X,well=true;break; case 'D': case 'd':++cd.X,well=true;break; case ' ':well=false;break; } if(ch!=' ') sh=ch; if(well) { if(snake.begin()->X==foodcoord.X&&snake.begin()->Y==foodcoord.Y) { well2=true; mark+=10; if(mark%50==0) { ++i; showface(); gotoxy(wall+2,m+2); color(14); cout<<"升级加速!第"<<i<<"阶段"; speech-=25; } if(pattern) { if(mark%100==0) { if(j==2) { j=3; } else j=2; customspass(); } } snake.begin()->X=foodcoord.X; snake.begin()->Y=foodcoord.Y; snake.push_back(foodcoord); color(14); showmark(); } if(well2) { food(); well2=false; } vector<COORD>::reverse_iterator rbeg=snake.rbegin(); while(rbeg!=snake.rend()-1) { rbeg->X=(rbeg+1)->X; rbeg->Y=(rbeg+1)->Y; ++rbeg; } snake.begin()->X=cd.X; snake.begin()->Y=cd.Y; judgedie(); tailcoord.X=snake.rbegin()->X; tailcoord.Y=snake.rbegin()->Y; gotoxy(snake.begin()->X,snake.begin()->Y); color(14); cout<<"★"<<endl; gotoxy(tailcoord.X,tailcoord.Y); color(3); cout<<"□"; } speechtemp1=speech; Sleep(speechtemp2); } } void Snakeclass::customspass() { if(pattern) { snake.clear(); map2.clear(); showmap1(); showmap2(j); drawsnake(); snakemove(); } else { snake.clear(); map2.clear(); showmap1(); drawsnake(); snakemove(); } } void Snakeclass::food() { begin: srand((unsigned)time(NULL)); foodcoord.X=rand()%(wall-1)+1; foodcoord.Y=rand()%(wall-1)+1; for(vector<COORD>::iterator beg=snake.begin();beg!=snake.end();++beg) { if(foodcoord.X==beg->X&&foodcoord.Y==beg->Y) { goto begin; } } for(vector<COORD>::iterator beg=map2.begin();beg!=map2.end();++beg) { if(foodcoord.X==beg->X&&foodcoord.Y==beg->Y) { goto begin; } } gotoxy(foodcoord.X,foodcoord.Y); color(15); cout<<"●"; } void Snakeclass::showmark() { gotoxy(wall+2,m+1); cout<<"分数:"<<mark; } void Snakeclass::savemark() { ofstream file("贪吃蛇排行榜.txt",ios::out|ios::app); file<<mark<<endl; file.close(); } void Snakeclass::showsavemark() { color(14); int a,n=0,temp; ifstream file("贪吃蛇排行榜.txt",ios::in); while(file>>a) ++n; file.close(); ifstream file2("贪吃蛇排行榜.txt",ios::in); int num[n]; for(int i=0;i!=n;++i) { file2>>num[i]; } file2.close(); for(int i=0;i!=n;++i) { for(int j=i+1;j!=n;++j) { if(num[i]<num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } gotoxy(wall+2,m+4); cout<<"分数排行:"<<endl; for(int i=0;i!=n;++i) { if(i==5) break; gotoxy(wall+2,m+5+i); cout<<"NO."<<i+1<<":"<<num[i]<<endl; } } void Snakeclass::judgedie() { if(snake.begin()->X==0||snake.begin()->X==wall||snake.begin()->Y==0||snake.begin()->Y==wall) { gotoxy(m-3,m); color(14); cout<<"你把墙撞穿了!"; savemark(); showsavemark(); gotoxy(wall+6,wall); exit(0); } for(vector<COORD>::iterator beg=map2.begin();beg!=map2.end();++beg) { if(snake.begin()->X==beg->X&&snake.begin()->Y==beg->Y) { gotoxy(m-3,m); color(14); cout<<"你把墙撞穿了!"; savemark(); showsavemark(); gotoxy(wall+6,wall); exit(0); } } for(vector<COORD>::iterator beg=snake.begin()+1;beg!=snake.end();++beg) { if(snake.begin()->X==beg->X&&snake.begin()->Y==beg->Y) { gotoxy(m-3,m); color(14); cout<<"你把自己吃了!"; savemark(); showsavemark(); gotoxy(wall+6,wall); exit(0); } } } void Snakeclass::showface() { if(a==1) { a=2; } else a=1; switch(a) { case 1:color(15); gotoxy(wall+2,3); cout<<" "; gotoxy(wall+2,4); cout<<"(”\\( ●—● )"; gotoxy(wall+2,5); cout<<" "; gotoxy(wall+2,6); cout<<" \\ / ○ \\ \\ "; gotoxy(wall+2,7); cout<<" "; gotoxy(wall+2,8); cout<<" ( )” "; gotoxy(wall+2,9); cout<<" "; gotoxy(wall+2,10); cout<<" \\ __ /\\ __ / "; gotoxy(wall+2,11); cout<<" ";break; case 2:color(14); gotoxy(wall+2,3); cout<<" へ /|"; gotoxy(wall+2,4); cout<<" /\7 ∠_/"; gotoxy(wall+2,5); cout<<" / │ / /"; gotoxy(wall+2,6); cout<<"│ Z _,< / /`ヽ"; gotoxy(wall+2,7); cout<<"│ ヽ / 〉"; gotoxy(wall+2,8); cout<<"Y ` / / \\"; gotoxy(wall+2,9); cout<<"ィ● 、 ● ()〈 /"; gotoxy(wall+2,10); cout<<"() へ | \〈"; gotoxy(wall+2,11); cout<<" >- 、_ ィ ";break; } } int main() { Snakeclass csnake; csnake.HideCursor(); csnake.showmenu(); csnake.customspass(); return 0; }
-
通过的题目
-
最近活动
题目标签
- 初窥门径
- 50
- 顺序结构
- 44
- 驾轻就熟
- 26
- 略有小成
- 18
- 其他
- 16
- 融会贯通
- 14
- 递归
- 14
- 排序
- 11
- 搜索
- 11
- 结构体
- 10
- 模拟
- 8
- 数据结构
- 7
- 队列
- 7
- BFS
- 7
- 位运算
- 6
- 循环嵌套
- 6
- 枚举
- 6
- 蓝桥杯
- 5
- 动态规划
- 5
- 分支结构
- 4