#include<iostream.h>
#include<graphics.h>
#include <stdlib.h> //gcvt(b,10,ptr);
#include<conio.h>
#include<time.h>
enum key{up,down,left,right,part,esc,again,no};
struct Block
{
int state;
int color;
};
struct Tagblock
{
int x;
int y;
int color;
int shape;
};
class game
{
public:
void gamestar();
void chutag();
void basewindow();
void putwindow();
int movecheck(key);
void move(key,int);
void hitmsg();
void changeblock();
void getscore();
void deleteline(int line);
private:
Tagblock tag[2];
Block base[20][10];
static int shape[19][4][4];
int deleteline_number;
long int score;
int level;
}ss;
/////////////////////////////////
/////////////////////////////////
void main()
{
srand((unsigned)time(NULL));
ss.gamestar();
ss.chutag();
initgraph(630,660);
ss.putwindow();
ss.basewindow();
while(1)
{
ss.putwindow();
ss.basewindow();
ss.hitmsg();
clearviewport();
}
getch();
}
/////////////////////////////////
/////////////////////////////////
/*--------------------------------------*/
void game::gamestar()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<10;j++)
base[i][j].state=0;
}
tag[1].color=(RGB(rand()%254,rand()%254,rand()%254));
tag[1].shape=rand()%19;
while(tag[1].shape==19){tag[1].shape=rand()%19;}
tag[1].x=3,tag[1].y=-3;
score=0;
level=1;
deleteline_number=0;
}
/*--------------------------------------*/
void game::chutag()
{
tag[0].color=tag[1].color;
tag[0].shape=tag[1].shape;
tag[0].x=tag[1].x;
tag[0].y=tag[1].y;
tag[1].color=(RGB(rand()%254,rand()%254,rand()%254));
tag[1].shape=rand()%19;
while(tag[1].shape==19){tag[1].shape=rand()%19;}
tag[1].x=3,tag[1].y=-3;
}
/*-----------------------------------------*/
void game::basewindow()
{
BeginBatchDraw();
setcolor(RGB(50,150,50));
for(int i=1;i<=11;i++)
line(40*i,30,40*i,630);
for(i=1;i<=21;i++)
line(40,i*30,440,i*30);
for(i=1;i<=5;i++)
line(450+30*i,50,450+30*i,170);
for(i=1;i<=5;i++)
line(480,20+i*30,600,20+i*30);
outtextxy(480,200,"版本编号:");
outtextxy(480,220,"1001110108");
char a[10]={'\0'};
gcvt(level,10,a);
outtextxy(480,250,"当前游戏等级:");
outtextxy(520,270,a);
for(i=0;i<10;i++)
a[i]='\0';
gcvt(score,10,a);
outtextxy(500,300,"当前得分:");
outtextxy(480,320,a);
outtextxy(480,360,"按键索引:");
outtextxy(480,390,"上、下、左、右:");
outtextxy(470,420,"w(8)、s(5)、a(4)、d(6)");
outtextxy(480,450,"暂停键:");
outtextxy(480,480,"space(空格键)");
outtextxy(480,510,"认输键!!");
outtextxy(480,530,"R键!(呵呵)");
outtextxy(480,560,"结束游戏!");
outtextxy(480,580,"请按t");
FlushBatchDraw();
}
/*-------------------------------------*/
void game::putwindow()
{
//BeginBatchDraw();
for(int i=0;i<20;i++)
{
for(int j=0;j<10;j++)
{
if(base[i][j].state==1)
{
setfillstyle(base[i][j].color);
bar3d(40+40*j,30+30*i,80+40*j,60+30*i,2,true);
}
}
}
setcolor(tag[0].color);
setfillstyle(tag[0].color);
for(i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[tag[0].shape][i][j]==1)
bar3d(40+40*(tag[0].x+j),30+30*(tag[0].y+i),80+40*(tag[0].x+j),60+30*(tag[0].y+i),2,true);
}
setcolor(tag[1].color);
setfillstyle(tag[1].color);
for(i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[tag[1].shape][i][j]==1)
bar3d(480+30*j,50+30*i,510+30*j,80+30*i,2,true);
}
//FlushBatchDraw();
}
/*---------------------------------------*/
int game::movecheck(key a)
{
if(a==left)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(shape[tag[0].shape][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j-1].state==1||tag[0].x+j-1<0)
return 0;
}
}
return 1;
}
else if(a==right)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(shape[tag[0].shape][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j+1].state==1||tag[0].x+j+1>9)
return 0;
}
}
return 1;
}
else if(a==down)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(shape[tag[0].shape][i][j]==1)
if(base[tag[0].y+i+1][tag[0].x+j].state==1||tag[0].y+i+1>19)
return 2;
}
}
return 1;
}
else{return 1;}
}
/*-------------------------------------------*/
void game::move(key a,int b)
{
if(b==0){}
else if(b==1)
{
if(a==left)
tag[0].x--;
else if(a==right)
tag[0].x++;
else if(a==down)
tag[0].y++;
else{}
}
else if(b==2)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(shape[tag[0].shape][i][j]==1&&tag[0].y+i>=0)
{
base[tag[0].y+i][tag[0].x+j].state=1;
base[tag[0].y+i][tag[0].x+j].color=tag[0].color;
}
}
}
getscore();
for(;deleteline_number>0;deleteline_number--)
{
score+=deleteline_number*100;
}
level=score/50000+1;
chutag();
}
else{}
}
/*-----------------------------------------------*/
void game::hitmsg()
{
key a=no;
char k='k';
for(int i=0;i<40;i++)
{
if(kbhit())
{
switch(char k=getch())
{
case 'w':
case '8':
a=up;break;
case 's':
case '5':
a=down;break;
case 'a':
case '4':
a=left;break;
case 'd':
case '6':
a=right;break;
case ' ':
a=part;break;
case 't':
a=esc;break;
case 'r':
a=again;break;
default:
a=no;break;
}
if(a==no){}
else if(a==esc){exit(0);}
else if(a==part){while(k!=' '){k=getch();}}
else if(a==again)
{
gamestar();
chutag();
clearviewport();
return;
}
else if(a==up)
{
changeblock();
clearviewport();
putwindow();
basewindow();
}
else
{
move(a,movecheck(a));
return;
}
}
Sleep(30/level);
a=no;
}
a=down;
move(a,movecheck(a));
}
/*-----------------------------------------------------*/
void game::changeblock()
{
if(tag[0].shape==0)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[1][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=1;
}
else if(tag[0].shape==1)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[0][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=0;
}
else if(tag[0].shape==2)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[3][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=3;
}
else if(tag[0].shape==3)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[4][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=4;
}
else if(tag[0].shape==4)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[5][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=5;
}
else if(tag[0].shape==5)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[2][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=2;
}
else if(tag[0].shape==6)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[7][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=7;
}
else if(tag[0].shape==7)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[8][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
tag[0].shape=8;
}
else if(tag[0].shape==8)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(shape[9][i][j]==1)
if(base[tag[0].y+i][tag[0].x+j].state==1||tag[0].x+j<0||tag[0].x+j>9)
return;
}
Snake-_-calculator-_-Tetris.zip_snake_窗口计算器
版权申诉
27 浏览量
2022-09-24
04:59:44
上传
评论
收藏 217KB ZIP 举报
林当时
- 粉丝: 95
- 资源: 1万+