2048 鼠标点击
a_middle
·
·
休闲·娱乐
#include<bits/stdc++.h>
#include<windows.h>
#define int long long
#define size 4
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//#define system("cls") cl
using namespace std;
int a[6][6];
int maxx;
bool IsMouseButtonDown(unsigned int button) {
return GetAsyncKeyState(button) & 0x8000;
}
POINT point;
void color(int ForgC, int BackC) {
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void gotoxy(int x,int y){
COORD pos;
HANDLE han=GetStdHandle(STD_OUTPUT_HANDLE);
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(han,pos);
}
bool die(){
for(int i=1;i<=size;i++)for(int j=1;j<=size;j++)if(a[i][j]==a[i-1][j]||a[i][j]==a[i+1][j]||a[i][j]==a[i][j-1]||a[i][j]==a[i][j+1])return 0;
return 1;
}
void rands(){
while(1){
int x=rand()%4+1,y=rand()%4+1;
if(a[x][y]==0){
if(rand()%64==1)a[x][y]=3;
if(maxx>=128){
if(rand()%6==2)a[x][y]=16;
else if(rand()%6==1||rand()%6==0)a[x][y]=8;
else a[x][y]=4;
}
else if(rand()%8==3){
a[x][y]=4;
}
else a[x][y]=2;
break;
}
}
}
void print(){
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==3){
color(15,8);
gotoxy(i*3-2,j*6-4);
cout<<" ";
gotoxy(i*3-1,j*6-4);
cout<<" ## ";
gotoxy(i*3,j*6-4);
cout<<" ";
}
else{
int x=log2(a[i][j])+1;
if(x>=2)x++;
if(x>=8)x++;
color(15,x);
gotoxy(i*3-2,j*6-4);
cout<<" ";
gotoxy(i*3-1,j*6-4);
if(a[i][j]>=10000){
cout<<""<<a[i][j]<<" ";
}else if(a[i][j]>=1000){
cout<<" "<<a[i][j]<<" ";
}else if(a[i][j]>=100){
cout<<" "<<a[i][j]<<" ";
}else if(a[i][j]>=10){
cout<<" "<<a[i][j]<<" ";
}else {
cout<<" "<<a[i][j]<<" ";
}
gotoxy(i*3,j*6-4);
cout<<" ";
}
}
}
}
void out(){
system("cls");
color(2,2);
cout<<" ";
color(15,0);
cout<<"\n";
for(int i=1;i<=12;i++){
color(2,2);
cout<<" ";
color(15,0);
cout<<" ";
color(2,2);
cout<<" ";
color(15,0);
cout<<"\n";
}
color(2,2);
cout<<" ";
color(15,0);
cout<<"\n";
}
bool ff=0;
void down(){
for(int i=size;i>=1;i--){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==a[i+1][j]||((a[i][j]==3||a[i+1][j]==3)&&!(a[i][j]==3&&a[i+1][j]==3))){
a[i+1][j]*=2;
a[i][j]=0;
for(int f=i;f>=1;f--){
a[f][j]=a[f-1][j];
}
ff=1;
}
else {
int f=i;
while(1){
if(a[f+1][j]==0&&f+1<=size)f++;
else break;
}swap(a[i][j],a[f][j]);
if(i!=f)ff=1;
}
}
}
}
void up(){
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==a[i-1][j]||((a[i][j]==3||a[i-1][j]==3)&&!(a[i][j]==3&&a[i-1][j]==3))){
a[i-1][j]*=2;
a[i][j]=0;
for(int f=i;f<=size;f++){
a[f][j]=a[f+1][j];
}
ff=1;
}
else {
int f=i;
while(1){
if(a[f-1][j]==0&&f-1>=1)f--;
else break;
}swap(a[i][j],a[f][j]);
if(i!=f)ff=1;
}
}
}
}
void left(){
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
if(a[i][j]==0)continue;
if(a[i][j]==a[i][j-1]||((a[i][j]==3||a[i][j-1]==3)&&!(a[i][j]==3&&a[i][j-1]==3))){
a[i][j-1]*=2;
a[i][j]=0;
for(int f=j;f<=size;f++){
a[i][f]=a[i][f+1];
}
ff=1;
}
else {
int f=j;
while(1){
if(a[i][f-1]==0&&f-1>=1)f--;
else break;
}swap(a[i][j],a[i][f]);
if(i!=f)ff=1;
}
}
}
}
void right(){
for(int i=1;i<=size;i++){
for(int j=size;j>=1;j--){
if(a[i][j]==0)continue;
if(a[i][j]==a[i][j+1]||((a[i][j]==3||a[i][j+1]==3)&&!(a[i][j]==3&&a[i][j+1]==3))){
a[i][j+1]*=2;
a[i][j]=0;
for(int f=j;f>=1;f--){
a[i][f]=a[i][f-1];
}
ff=1;
}
else {
int f=j;
while(1){
if(a[i][f+1]==0&&f+1<=size)f++;
else break;
}swap(a[i][j],a[i][f]);
if(i!=f)ff=1;
}
}
}
}
int x_1,x_2,y_1,y_2;
void get(){
if(KEY_DOWN('w')){
up();
return ;
}
else if(KEY_DOWN('s')){
down();
return ;
}
else if(KEY_DOWN('a')){
left();
return ;
}
else if(KEY_DOWN('d')){
right();
return ;
}
if (KEY_DOWN(VK_LBUTTON)) {
GetCursorPos(&point);
RECT rect;
HWND hwnd=GetForegroundWindow();
GetWindowRect(hwnd,&rect);
if(point.y-rect.top>=92&&point.x-rect.left>=395&&point.y-rect.top<=166&&point.x-rect.left<=456)up();
else if(point.y-rect.top>=186&&point.x-rect.left>=320&&point.y-rect.top<=257&&point.x-rect.left<=378)left();
else if(point.y-rect.top>=186&&point.x-rect.left>=398&&point.y-rect.top<=259&&point.x-rect.left<=456)down();
else if(point.y-rect.top>=186&&point.x-rect.left>=472&&point.y-rect.top<=258&&point.x-rect.left<=533)right();
}
while(KEY_DOWN(VK_LBUTTON));
}
void DisableMouseSelection(){
CONSOLE_CURSOR_INFO ConsoleCursorInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ConsoleCursorInfo);
ConsoleCursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ConsoleCursorInfo);
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hConsole, &mode);
SetConsoleMode(hConsole, mode & (~ENABLE_QUICK_EDIT_MODE));
}
short ge(){
GetCursorPos(&point);
RECT rect;
HWND hwnd=GetForegroundWindow();
GetWindowRect(hwnd,&rect);
if(point.y-rect.top>=92&&point.x-rect.left>=395&&point.y-rect.top<=166&&point.x-rect.left<=456)return 1;
else if(point.y-rect.top>=186&&point.x-rect.left>=320&&point.y-rect.top<=257&&point.x-rect.left<=378)return 2;
else if(point.y-rect.top>=186&&point.x-rect.left>=398&&point.y-rect.top<=259&&point.x-rect.left<=456)return 3;
else if(point.y-rect.top>=186&&point.x-rect.left>=472&&point.y-rect.top<=258&&point.x-rect.left<=533)return 4;
}
void pp(){
if(ge()==1)
color(0,4);
gotoxy(2,40);
cout<<"------\n";
gotoxy(3,40);
cout<<"| |\n";
gotoxy(4,40);
cout<<"| ^ |\n";
gotoxy(5,40);
cout<<"| | |\n";
gotoxy(6,40);
cout<<"------\n";
color(15,0);
if(ge()==2)
color(0,4);
gotoxy(7,32);
cout<<"------\n";
gotoxy(8,32);
cout<<"| |\n";
gotoxy(9,32);
cout<<"| <-- |\n";
gotoxy(10,32);
cout<<"| |\n";
gotoxy(11,32);
cout<<"------\n";
color(15,0);
if(ge()==3)
color(0,4);
gotoxy(7,40);
cout<<"------\n";
gotoxy(8,40);
cout<<"| | |\n";
gotoxy(9,40);
cout<<"| V |\n";
gotoxy(10,40);
cout<<"| |\n";
gotoxy(11,40);
cout<<"------\n";
color(15,0);
if(ge()==4)
color(0,4);
gotoxy(7,48);
cout<<"------\n";
gotoxy(8,48);
cout<<"| |\n";
gotoxy(9,48);
cout<<"| --> |\n";
gotoxy(10,48);
cout<<"| |\n";
gotoxy(11,48);
cout<<"------\n";
color(15,0);
// gotoxy(,20)
}
signed main(){
DisableMouseSelection();
//
// Sleep(10000);
cout<<"欢迎来到2048\n鼠标点击控制\nbug私信dsafhrbghrsbsbsbs,感谢dalao";
char x=getchar();
system("cls");
color(2,2);
cout<<" \n";
for(int i=1;i<=12;i++){
cout<<" ";
color(15,0);
cout<<" ";
color(2,2);
cout<<" \n";
}
color(2,2);
cout<<" \n";
color(15,0);
pp();
srand(time(0));
rands();
print();
while(!die()){
pp();
if(KEY_DOWN(VK_LBUTTON)||KEY_DOWN('w')||KEY_DOWN('s')||KEY_DOWN('a')||KEY_DOWN('d')){
get();
rands();
out();
print();
}
}
return 0;
}