| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 939 人关注过本帖
标题:呵呵(今天翻别人电脑翻出来我以前开始学c写的俄罗斯方块)
只看楼主 加入收藏
小鱼儿c
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:852
专家分:1317
注 册:2011-4-1
结帖率:95.74%
收藏
 问题点数:0 回复次数:7 
呵呵(今天翻别人电脑翻出来我以前开始学c写的俄罗斯方块)
呵呵,自己玩了一下。<BR>还可以玩。<BR>由于我的朋友的是上网本,处理速度有点慢,我用sleep() 可能在不同的机子上速度不一样。。<BR>呵呵。。。<BR><BR>看这自己以前的代码,蛮好玩的啊。。。。。<BR><BR>这个貌似是学习2个月c写的。。。。<BR>记得不太清楚了。。<BR>TC 环境 还要drv 要用到DRV 然后改我的inigraph 路径。。。<BR><BR>怀旧一下,一年多就过去了啊。。。<BR>
程序代码:
/* Note:Your choice is C IDE */
#include <stdio.h> 
#include <stdlib.h> 
#include <bios.h>
#include <graphics.h>  
#include <conio.h>
#define right 19712
#define leaf 19200
#define esc 283
#define up 18432
#define down 20480
int curbox[5][5];
int newx,newy;
int key;
int score=0;
int str[10];
int n=0;
int next2;
int gamespeed=50000;
int box[7][5][5] = {
{ 
{0,0,0,0,0}, 
{0,0,0,0,0}, 
{1,1,1,1,0}, 
{0,0,0,0,0}, 
{0,0,0,0,0} 
}, 
{ 
{0,0,0,0,0}, 
{0,0,1,0,0}, 
{0,1,1,1,0}, 
{0,0,0,0,0}, 
{0,0,0,0,0} 
}, 
{ 
{0,0,0,0,0}, 
{0,1,1,0,0}, 
{0,0,1,1,0}, 
{0,0,0,0,0}, 
{0,0,0,0,0} 
}, 
{ 
{0,0,0,0,0}, 
{0,0,1,1,0}, 
{0,1,1,0,0}, 
{0,0,0,0,0}, 
{0,0,0,0,0} 
}, 
{ 
{0,0,0,0,0}, 
{0,1,1,0,0}, 
{0,0,1,0,0}, 
{0,0,1,0,0}, 
{0,0,0,0,0} 
}, 
{ 
{0,0,0,0,0}, 
{0,0,1,1,0}, 
{0,0,1,0,0}, 
{0,0,1,0,0}, 
{0,0,0,0,0} 
}, 
{ 
{0,0,0,0,0}, 
{0,0,1,1,0}, 
{0,0,1,1,0}, 
{0,0,0,0,0}, 
{0,0,0,0,0} 
} 
};
int curx=6,cury=0;
int map[25][18];
int rotatebox()
{int x,y;
    int box1[5][5];
    for(x = 0; x < 5; x++) /*这个函数可以须要死记*/ 
for(y = 4; y >= 0; y--) /*编写一下才能印像深刻*/ 
    box1[y][x]=curbox[x][4-y];
    for(y=0;y<5;y++)
    for(x=0;x<5;x++)
    if(box1[y][x]==1&&map[y+cury][x+curx]==1)
    return 0;
    for(y=0;y<5;y++)
    for(x=0;x<5;x++)
    curbox[y][x]=box1[y][x];}
int drop()
{newy=cury+1;
if(test(curx,newy)==1)
    {cury=newy;
    return 1;}
else return 0;}
int initmap()
{int x,y;
    for(y=0;y<25;y++){
    for(x=0;x<18;x++){
    if((x<2&&x>=0)||(y>22&&y<25)||(x>15&&x<18))
    map[y][x]=1;
    else map[y][x]=0;
    if(map[y][x]==1&&x>1&&x<16&&y<23)
    {setcolor(RED);
    setlinestyle(SOLID_LINE,0,THICK_WIDTH);        
    rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100);}
    }
    }
    for(y=0;y<25;y++)
    for(x=0;x<18;x++)
    if(map[y][x]==1&&((x>=0&&x<2)||(y>22&&y<25)||(x>15&&x<18)))
    {setcolor(GREEN);
        setlinestyle(SOLID_LINE,0,THICK_WIDTH);    
        rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100);
    }
}
int test(int mx,int my)
{int x,y;
        for(y=0;y<5;y++)
        for(x=0;x<5;x++)
if(map[y+my][x+mx]==1&&curbox[y][x]==1)
    return 0;
    return 1;
    }
int newfall()
{
int next;
int x;
int y; 
curx=6;
cury=0;
setfillstyle(SOLID_FILL,BLUE);
bar(55,20,200,50);
sprintf(str,"score:%d",score);
settextstyle(0,0,2);
outtextxy(60,25,str);
nextbox();
while(drop())
buidbox();
return test(curx,cury);
}
int buidbox()
{int x,y;
    if(!kbhit())
    gamespeed=50000;
    while(kbhit())
{key=bioskey(0);
switch(key){
case right:move(1);break;
case leaf:move(0);break;
case esc:closegraph();exit(0);
case up:rotatebox();break;
case down:gamespeed=8000;break;
}}
for(y=0;y<5;y++){
for(x=0;x<5;x++)
{if(curbox[y][x]==1)
{setcolor(GREEN);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
rectangle((x+curx)*12.5+190,(cury+y)*12.5+90,(x+curx)*12.5+200,(cury+y)*12.5+100);}}}
delay(gamespeed);
setcolor(BLACK);
for(y=0;y<5;y++){
for(x=0;x<5;x++){
if(curbox[y][x]==1){
rectangle((x+curx)*12.5+190,(cury+y)*12.5+90,(x+curx)*12.5+200,(cury+y)*12.5+100);
}
}
}
}
int rebuidmap()
{int x,y;
    cleardevice();
    putbox();
    clear();
    setfillstyle(SOLID_FILL,BLUE);
    bar(55,20,200,30);
    sprintf(str,"score:%d",score);
    settextstyle(0,0,2);
    outtextxy(55,20,str);
    for(y=0;y<25;y++)
    for(x=0;x<18;x++)    
    if(map[y][x]==1&&x>1&&x<16&&y<23)
    {setcolor(RED);
    setlinestyle(SOLID_LINE,0,THICK_WIDTH);        
    rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100);
    }
    for(y=0;y<25;y++)
    for(x=0;x<18;x++)
    if(map[y][x]==1&&((x>=0&&x<2)||(y>22&&y<25)||(x>15&&x<18)))
    {setcolor(GREEN);
        setlinestyle(SOLID_LINE,0,THICK_WIDTH);    
        rectangle(x*12.5+190,y*12.5+90,x*12.5+200,y*12.5+100);
    }
    }
int putbox() { /*将curbox填充到地图上*/ 
int x, y; 
for(y = 0; y < 5; y++) /*这个也简单,主要是要根*/ 
for(x = 0; x < 5; x++) /*据curx,cury指出位置 */ 
if(curbox[y][x]) 
map[y + cury][x + curx] = curbox[y][x]; 
} 
int clear() { /*清除掉满行*/ 
/*这个函数实际上效率也很低的,为了简便 
它从头到尾作了测试*/ 
/*具体的算法为: 
从第0行开始到最后一行,测试地图点阵是否为满,如果是的话 
从当前行算起,之上的地图向下掉一行*/ 
int x, y; 
int dx, dy; 
int fullflag; 
for(y = 0; y < 25 - 2; y++) { /*最后两行保留行*/ 
fullflag = 1; /*假设为满*/ 
for(x = 2; x < 18 - 2; x++) { /*保留列~*/ 
if(!map[y][x]) { 
fullflag = 0; 
break; 
} 
} 
if(fullflag) { /*向下移动一行*/ 
for(dy = y; dy > 0; dy--) 
for(dx = 2; dx < 18 - 2; dx++) 
map[dy][dx] = map[dy - 1][dx]; 
for(dx = 2; dx < 18 - 2; dx++)  
map[0][dx] = 0; 
score+=10;
/*并清除掉第一行*/ 
} 
} 
} 
int nextbox()
{int next1,x,y;
    randomize();
    next1=rand()%7;
    for(y=0;y<5;y++)
    for(x=0;x<5;x++)
    if(n==0)
    curbox[y][x]=box[next1][y][x];
    if(n==1)
    {for(y=0;y<5;y++)
    for(x=0;x<5;x++)
    curbox[y][x]=box[next2][y][x];}
    next2=rand()%7;
    n=1;
    for(y=0;y<5;y++)
    for(x=0;x<5;x++)
    if(box[next2][y][x])
    rectangle(x*12.5+500,y*12.5+50,x*12.5+510,y*12.5+60);
}
int move(int dir)
{int x,y,newx;
    if(dir) 
    {newx=curx+1;
        if(test(newx,cury))
        curx=newx;}
    else newx=curx-1;
    if(test(newx,cury))
    curx=newx;
}
int main()
{int x,y,gm=DETECT,gr,h;
initgraph(&gm,&gr,"C:\\JMSOFT\\DRV");
//"C:\\JMSOFT\\DRV" 改成在你电脑上的地址
initmap();
newfall();
while(1){
    if(!drop())
{rebuidmap();
    for(x=3;x<16;x++)
    if(map[2][x])
    {setcolor(RED);
    settextstyle(0,0,2);
    cleardevice();
    outtextxy(260,240,"GAME OVER");
    sleep(2);
    closegraph();
        exit(0);
    }        
newfall();
}
while(kbhit())
{key=bioskey(0);
switch(key){
case right:move(1);break;
case leaf:move(0);break;
case esc:closegraph();exit(0);
case up:rotatebox();break;
}}
}}
搜索更多相关主题的帖子: 俄罗斯方块 上网本 我的朋友 sleep color 
2012-02-19 20:52
小鱼儿c
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:852
专家分:1317
注 册:2011-4-1
收藏
得分:0 
DRV.zip (48.93 KB)
那个tc图形附件

[ 本帖最后由 小鱼儿c 于 2012-2-20 02:19 编辑 ]

用心做一件事情就这么简单
2012-02-19 20:54
卡卡罗特wang
Rank: 2
来 自:湖北武汉
等 级:论坛游民
帖 子:63
专家分:42
注 册:2012-2-17
收藏
得分:0 
回复 2楼 小鱼儿c
请问楼主,怎么运行它啊,我也想玩下,我初学c,vc可以吗,该怎么做呢,求指教
2012-02-19 21:38
a451446626
Rank: 1
等 级:新手上路
帖 子:17
专家分:9
注 册:2012-2-20
收藏
得分:0 
附件在哪呀。。。楼主
2012-02-20 01:44
小鱼儿c
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:852
专家分:1317
注 册:2011-4-1
收藏
得分:0 
回复 3楼 卡卡罗特wang
TC 环境下。
发下只是好玩。
这个是我开始学c写的。
写的很一般。
没有什么好学的。。。。
我以前的那篇教程没有了。
不然你看那个还好些。

用心做一件事情就这么简单
2012-02-20 02:28
转角有梦在等
Rank: 2
来 自:黑龙江
等 级:论坛游民
帖 子:31
专家分:95
注 册:2012-2-4
收藏
得分:0 
要学习一下  2个月 有成果 好羡慕你

一起努力,,,  QQ:7325231
2012-02-20 11:08
C_596322153
Rank: 6Rank: 6
来 自:徽州
等 级:侠之大者
帖 子:182
专家分:466
注 册:2012-1-10
收藏
得分:0 
错误信息:不能打开 bios.h    能发下 嘛
2012-02-20 12:04
有容就大
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:东土大唐
等 级:版主
威 望:74
帖 子:9048
专家分:14309
注 册:2011-11-11
收藏
得分:0 
TC图形库就是好用。

梅尚程荀
马谭杨奚







                                                       
2012-02-20 19:33
快速回复:呵呵(今天翻别人电脑翻出来我以前开始学c写的俄罗斯方块)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023740 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved