上个游戏:躲方块游戏
首先先把ege库安上
然后把下面代码写进去运行
然后就得到了个抽象不标准的2048
----------------------------------------------
代码:
#include<ege.h>
#include<bits/stdc++.h>
#include<windows.h>
#include <thread>
using namespace std;
using namespace ege;
int score;
int game[4][4]={2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
color_t color(int c){
if(c==0) return WHITE;
if(c==2) return YELLOW;
if(c==4) return BLUE;
if(c==8) return GREEN;
if(c==16) return RED;
if(c==32) return CYAN;
if(c==64) return BROWN;
if(c==128) return YELLOW;
if(c==246) return BLUE;
if(c==512) return GREEN;
if(c==1024) return RED;
if(c==2048) return CYAN;
if(c==4096) return BROWN;
}
void show(){
setbkcolor(BLACK);
string str = to_string(score);
LPCSTR lpcstr = str.c_str();
cleardevice();
xyprintf(0,400,"score:");
xyprintf(150,400,lpcstr);
for(int i=0;i<400;i+=100){
for(int j=0;j<400;j+=100){
setcolor(color(game[i/100][j/100]));
str = to_string(game[i/100][j/100]);
lpcstr = str.c_str();
xyprintf(i,j+1,lpcstr);
}
}
setcolor(WHITE);
line(100,0,100,399);
line(0,100,399,100);
line(200,0,200,399);
line(0,200,399,200);
line(0,200,399,200);
line(0,400,399,400);
line(0,300,399,300);
line(300,0,300,399);
}int start(){
while(1){
show();
int n=ege::getch();
if(n==296){
for(int i=0;i<4;i++){
for(int j=3;j>=0;j--){
for(int k=j;k<3;k++){
if(game[i][k+1]!=0){
if(game[i][k]==game[i][k+1]){
game[i][k+1]*=2;
game[i][k]=0;
}else{
break;
}
}else{
game[i][k+1]=game[i][k];
game[i][k]=0;
}
}
}
}
++score;
}
if(n==294){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
for(int k=j;k>0;k--){
if(game[i][k-1]!=0){
if(game[i][k]==game[i][k-1]){
game[i][k-1]*=2;
game[i][k]=0;
}else{
break;
}
}else{
game[i][k-1]=game[i][k];
game[i][k]=0;
}
}
}
}
++score;
}
if(n==293){
for(int i=3;i>0;i--){
for(int j=0;j<4;j++){
for(int k=i;k>0;k--){
if(game[k-1][j]!=0){
if(game[k][j]==game[k-1][j]){
game[k-1][j]*=2;
game[k][j]=0;
}else{
break;
}
}else{
game[k-1][j]=game[k][j];
game[k][j]=0;
}
}
}
}
++score;
}
if(n==295){
for(int i=3;i>=0;i--){
for(int j=0;j<4;j++){
for(int k=i;k<3;k++){
if(game[k+1][j]!=0){
if(game[k][j]==game[k+1][j]){
game[k+1][j]*=2;
game[k][j]=0;
}else{
break;
}
}else{
game[k+1][j]=game[k][j];
game[k][j]=0;
}
}
}
}
++score;
}
bool tmp=0;
int x=-1,y=-1;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(game[j][i]<=0){
tmp=1;
if(x<0&&y<0){
x=j;y=i;
}else{
if(rand()%4==0){
x=j;y=i;
}
}
continue;
}
}
}
if(tmp==0){
setcolor(RED);
xyprintf(0,0,"GAMEOVER");
getch();
exit(0);
}else{
game[x][y]=2;
}
}
}
int main(){
srand(time(0));
initgraph(400,500);
setfont(99, 24, "宋体");
start();
getch();
closegraph();
}
大概样子是这个: