Bx游戏
# include & lttime.h & gt
# include & ltstdio.h & gt
# include & ltstring.h & gt
const int g _ width = 400
const int g _ height = 520
/*用于记录7种形状及其4种变体的表格*/
static int g _ TRS _ map[8][4][4][4];
/*更改号码表*/
static int g_map_mod[] = {1,4,4,4,2,2,2,1,0 };
/*初始化全局数据和图形显示*/
void initgr() {
initgraph(g_width,g _ height);
Setfont(12,6,“宋体”);
int Trs_map[8][4][4][4] =
{
{{{0}}},{{
{0,0,0,0},{1,1,1,0},{0,1,0,0},
},{
{0,1,0,0},{1,1,0,0},{0,1,0,0},
},{
{0,1,0,0},{1,1,1,0},
},{
{0,1,0,0},{0,1,1,0},{0,1,0,0},
},},{{
{2,2,0,0},{0,2,0,0},{0,2,0,0},
},{
{0,0,2,0},{2,2,2,0},
},{
{0,2,0,0},{0,2,0,0},{0,2,2,0},
},{
{0,0,0,0},{2,2,2,0},{2,0,0,0},
},},{{
{0,3,3,0},{0,3,0,0},{0,3,0,0},
},{
{0,0,0,0},{3,3,3,0},{0,0,3,0},
},{
{0,3,0,0},{0,3,0,0},{3,3,0,0},
},{
{3,0,0,0},{3,3,3,0},
},},{{
{4,4,0,0},{0,4,4,0},
},{
{0,0,4,0},{0,4,4,0},{0,4,0,0},
},},{{
{0,5,5,0},{5,5,0,0},
},{
{0,5,0,0},{0,5,5,0},{0,0,5,0},
},},{{
{0,0,0,0},{6,6,6,6},
},{
{0,0,6,0},{0,0,6,0},{0,0,6,0},{0,0,6,0},
},},{{
{0,0,0,0},{0,7,7,0},{0,7,7,0},
},},
};
memcpy(g_trs_map,trs_map,sizeof(Trs _ map));
}
班级游戏{
公共:
/*状态表*/
枚举{
ST_START,/*游戏重新开始*/
ST_NEXT,/*准备下一个盒子*/
ST_NORMAL,/*玩家控制阶段*/
ST_OVER /*游戏结束,F2重新开始*/
};
Game(int w,int h,int bw,int bh) {
int colormap[10] = {0,0xA0,0x50A0,0xA0A0,0xC000,
0xA0A000,0xC04040,0xA000A0,0x808080,0x ffffff };
memcpy(m_colormap,colormap,sizeof(m _ colormap));
int Keys[8] = {VK_F2,VK_LEFT,VK_RIGHT,VK_DOWN,VK_UP,VK_NUMPAD0,VK _ SPACE };
memcpy(m_Keys,Keys,sizeof(Keys));
memset(m_KeyState,0,sizeof(m _ key state));
m _ gamepool _ w = w
m _ gamepool _ h = h
m _ base _ w = bw
m _ base _ h = bh
randomize();
m _ CTL _ t =-1;
m_pcb =新图像;
for(int I = 0;我& lt10;++i) {
drawtile(bw * i,0,bw,bh,5,colormap[I]);
}
getimage(m_pcb,0,0,bw*10,BH);
m _ state = ST _ START
}
/*状态转换处理*/
int deal () {
int nRet = 0;
If (m_state == ST_START) {//初始化
m _ next 1 _ s = random(7)+1;
m _ next 2 _ s = random(7)+1;
m _ pause = 0;
memset(m_gamepool,255,sizeof(m _ game pool));
for(int y = 1;y & lt= m _ game pool _ h;++y) {
for(int x = 1;x & lt= m _ game pool _ w;++x)
m _ game pool[y][x]= 0;
}
m _禁止_下降= 0;
m _ CTL _ t =-1;
nRet = 1;
m _ state = ST _ NEXT
} else if ( m_state == ST_NEXT ) {
m _ CTL _ x =(m _ game pool _ w-4)/2+1;
m _ CTL _ y = 1;
m _ CTL _ t = 0;
m _ CTL _ s = m _ next 1 _ s;
m _ CTL _ dy = 0;
m _ next 1 _ s = m _ next 2 _ s;
m _ next 2 _ s = random(7)+1;
m _ curtime = m _ droptime
m _ curxtime = 0;
nRet = 1;
if ( isCrash() ) {
m _ gray _ y = m _ game pool _ h * 2;
m _ over _ ST = 0;
m _ state = ST _ OVER
}否则{
m _ state = ST _ NORMAL
}
} else if (m_state == ST_NORMAL) {
/*处理自由落体*/
int i,j;
if(m _ key state[3]= = 0 | | m _ body _ down){
- m_curtime,m _ cursubtime = 1;
}
if ( m_curxtime ) {
if(m _ curxtime & lt;0)
m _ curxtime++;
其他
m _ curxtime-;
}
/*键处理*/
for (i = 1,j = 1;我& lt=2;++i,j-=2) {
for(;m _ key flag[I]& gt;0;- m_KeyFlag[i]) {
m _ CTL _ x-= j;
if ( isCrash())
m _ CTL _ x+= j;
其他
m _ curxtime = m _ mov xtime * j;
}
}
m _ CTL _ dx = float(double(m _ curxtime)/m _ mov xtime);//处理X方向平滑
for (i = 4,j = 1;我& lt=5;++i,j-=2) {
for(int t;m _ key flag[I]& gt;0;- m_KeyFlag[i]) {
m _ CTL _ t =((t = m _ CTL _ t)+g _ map _ mod[m _ CTL _ s]+j)% g _ map _ mod[m _ CTL _ s];
if(is crash())m _ CTL _ t = t;
}
}
if(m _禁止_关闭= = 0 & amp& amp(m_KeyState[3] ) ) {
m _ curtime-= m _ cursubtime++;
}
if(m _ curtime & lt;0) {
++ m _ CTL _ y;
if ( isCrash() ) {
-m _ CTL _ y;
merge();
m _ CTL _ dy = 0;m _ CTL _ dx = 0;m _ CTL _ t =-1;
if ( m_KeyState[3])
m _禁止_下降= 1;
m _ state = ST _ NEXT
}否则{
m _ curtime+= m _ drop time;
}
}
if (m_state == ST_NORMAL) {
m _ CTL _ dy = float(double(m _ curtime)/m _ drop time);//处理Y方向平滑
}
} else if (m_state == ST_OVER) {
if(m _ gray _ y & gt;0 & amp& amp(m_gray_y % 2) == 0)
for(int x = 1;x & lt= m _ game pool _ w;++x)
if(m _ game pool[m _ gray _ y & gt;& gt1][x])
m _ game pool[m _ gray _ y & gt;& gt1][x]= 8;
m _ gray _ y-;
++ m _ over _ ST;
if(m _ key flag[0]& gt;0 )
m _ state = ST _ START
}
memset(m_KeyFlag,0,sizeof(m _ key flag));
返回nRet
}
/*碰撞检测*/
bool isCrash() {
for(int y = 0;y & lt4;++y) {
for(int x = 0;x & lt4;++x)
if(g _ TRS _ map[m _ CTL _ s][m _ CTL _ t][y][x]){
if(m _ CTL _ y+y & lt;0 | | m _ CTL _ x+x & lt;0
| | m _ game pool[m _ CTL _ y+y][m _ CTL _ x+x])
返回true
}
}
返回false
}
void merge() {
int y,x,cy = m _ game pool _ h;
/*合并处理*/
for(y = 0;y & lt4;++y) {
for(x = 0;x & lt4;++x)
if(g _ TRS _ map[m _ CTL _ s][m _ CTL _ t][y][x])
m _ game pool[m _ CTL _ y+y][m _ CTL _ x+x]
= g _ TRS _ map[m _ CTL _ s][m _ CTL _ t][y][x];
}
/*行消除计算*/
for(y = m _ game pool _ h;y & gt= 1;- y) {
for(x = 1;x & lt= m _ game pool _ w;++x) {
if ( m_gamepool[y][x] == 0)
打破;
}
if(x & lt;= m_gamepool_w ) {
如果(cy!= y ) {
for(x = 1;x & lt= m _ game pool _ w;++x)
m _ game pool[cy][x]= m _ game pool[y][x];
}
-cy;
}
}
for(y = cy;y & gt= 1;- y) {
for(x = 1;x & lt= m _ game pool _ w;++x)
m _ game pool[y][x]= 0;
}
}
/*逻辑更新主函数*/
void update(int k) {
而(k & amp& amp(k = getch(1)) ) {
for(int I = 0;我& lt8;++i) {
如果((k & amp0x ffff)=(m _ Keys[I]& amp;0xFFFF)) {
如果(k & ampKEYMSG_DOWN) {
m _ key flag[I]++;
m _ key state[I]= 1;
} else if(k & amp;KEYMSG_UP) {
m _ key flag[I]= 0;
m _ key state[I]= 0;
如果(i == 3)
m _禁止_下降= 0;
}
}
}
k = kb hit(1);
}
while(deal());
}
void drawedge(int x,int y,int w,int h,int color,int bdark = 1) {
setcolor(getchangcolor(color,1.5f));
line(x,y+h,x,y);
lineto(x+w,y);
if ( bdark)
setcolor(getchangcolor(color,0.7f));
lineto(x+w,y+h);
lineto(x,y+h);
}
void drawtile(int x,int y,int w,int h,int d,int color) {
w -,h-;
setfillstyle(颜色);
bar(x+1,y+1,x+w,y+h);
drawedge(x,y,w,h,color);
drawedge(x+1,y+1,w-2,h-2,颜色);
}
void drawframe(int x,int y,int w,int h,int d = 0) {
int coll[] = {0x400040,0x600060,0xA000A0,0xFF00FF,
0xA000A0,0x600060,0x 400040 };
setfillstyle(0x 010101);
bar(x,y,x + w -,y+h-);
for(int I = 0;我& lt7;++i) {
- x,- y,w += 2,h+= 2;
drawedge(x,y,w,h,coll[i],0);
}
}
void draw44(int bx,int by,int mat[4][4],
float dx=0,float dy=0,int nc=0,int deep=5) {
for(int y = 3;y & gt= 0;- y) {
for (int x = 0,c;x & lt4;++x) {
if ( c = mat[y][x] ) {
if(NC)c = NC;
draw tile(int(bx+(x+dx)* m _ base _ w+1000.5)-1000,
int(by+(y-dy)* m _ base _ h+1000.5)-1000,
m_base_w,m_base_h,深,
m _ colormap[c]);
}
}
}
}
void drawfps() {
静态字符串[100];
静态int n_time = 0,fps = 0;
set color(0x ffffff);
setbkmode(透明);
Sprintf(str,"帧率%.2f fps ",get fps());
outtextxy(0,0,str);
}
/*图形更新主功能*/
void render() {
int x,y,c,bx,by,deep = 5;
/*绘制背景框*/
clear device();
drawframe( m_base_x + 5 * m_base_w
m_base_y,
m_gamepool_w * m_base_w
m _ game pool _ h * m _ base _ h);
drawframe(m_base_x,m_base_y,4*m_base_w,4 * m _ base _ h);
drawframe(m_base_x,m_base_y + 5*m_base_h,4*m_base_w,4 * m _ base _ h);
/*抽取主游戏池*/
bx = m _ base _ x+4 * m _ base _ w;
by = m _ base _ y-1 * m _ base _ h;
for(y = m _ game pool _ h;y & gt= 1;- y) {
for(x = 1;x & lt= m _ game pool _ w;++x) {
if ( c = m_gamepool[y][x])
putimage(bx + x * m_base_w,by + y * m_base_h,
m_base_w,m_base_h,m_pcb,
c * m_base_w,0);
}
}
/*绘制控制块*/
if(m _ CTL _ t & gt;=0 ) {
bx = m _ base _ x+(m _ CTL _ x+4)* m _ base _ w;
by = m _ base _ y+(m _ CTL _ y-1)* m _ base _ h;
draw44(bx,by,g_trs_map[m_ctl_s][m_ctl_t],m_ctl_dx,m _ CTL _ dy);
}
/*画下一个棋子和下两个棋子*/
bx = m _ base _ x;
by = m _ base _ y;
draw44(bx,by,g _ TRS _ map[m _ next 1 _ s][0]);
bx = m _ base _ x;
by = m _ base _ y+5 * m _ base _ h;
draw44(bx,by,g_trs_map[m_next2_s][0],0,0,8);
set color(0x ffffff);
If (m_state == ST_OVER) {//结束提示文字显示。
outtextxy(m_base_x+5*m_base_w,m_base_y,"按F2重启游戏");
}
draw fps();
}
静态int dealbit(int a,float dt) {
a = int(a * dt);
如果(a & gt255)a = 255;
else if(a & lt;0)a = 0;
返回a;
}
static int getchangcolor(int Color,float t) {
int r = GetRValue(Color),g = GetGValue(Color),b = getb value(Color);
r = dealbit(r,t);
g = dealbit(g,t);
b = dealbit(b,t);
返回RGB(r,g,b);
}
公共:
int m_base_x,m_base_y,m_base_w,m _ base _ h;
int m _ droptime
int m _ curtime
int m _ cursubtime
int m _ movxtime
int m _ curxtime
私人:
int m_gamepool_w,m _ game pool _ h;
int m _ game pool[30][30];//从1开始,0用于边界碰撞检测。
int m_ctl_x,m_ctl_y,m_ctl_t,m _ CTL _ s;//当前控制块属性
float m_ctl_dx,m _ ctl _ dy
int m_next1_s,m _ next 2 _ s;
int m _ body _ down;
int m _ colormap[10];
公共:
int m _ pause
int m _ state//游戏主状态
int m _ gray _ y;
int m _ over _ st
int m _ Keys[8];
int m _ key flag[8];
int m _ key state[8];
私人:
IMAGE * m _ pcb
};
int main() {
int fps = 200
setinitmode(0);
initgr();
游戏game(10,20,24,24);
game.m _ base _ x = 20
game.m _ base _ y = 20
game . m _ drop time = fps/2;
game . m _ mov xtime = 10;
BeginBatchDraw();
for(int k;(k = kbhit(1))!= -1;delay_fps(fps)) {
game . update(k);
game . render();
}
EndBatchDraw();
返回0;
}
这是C语言的俄罗斯方块源代码,希望能用上。