VC++ 6.0 C语言实现俄罗斯方块详细教程

2020-01-06 19:35:36王振洲

还有这个,画一个圆,圆心是(320,240),半径r=200,根据角度的公式画一个圆:


# include <graphics.h> 
# include <conio.h> 
# include <math.h> 
# define PI 3.1415926 
 void main() 
 { 
   initgraph(640, 480); 
   int x,y,r=200,c; 
   double a; 
   for(a=0; a<PI*2; a+=0.0001) 
   { 
     x=(int)(r*cos(a)+320+0.5); 
     y=(int)(r*sin(a)+240+0.5); 
     c=(int)(a*255/(2*PI)); 
     setcolor(RGB(c,200-c/2,100+c/2)); 
     line(320,240,x,y); 
   } 
   getch(); 
   closegraph(); 
 } 

9.停顿:Sleep(x);停顿x/1000秒。

有了Sleep();函数,就能让线动起来了,原理就是:先画一条线,然后再画一条黑色的线覆盖原来的那条,然后再画一条线,这样不断的画线能行了。


# include <graphics.h> 
# include <conio.h> 
 void main() 
 { 
   int i,y; 
   initgraph(640,480); 
   for(y=0;y<480-2;y++) 
   { 
     setcolor(RGB(y,125,y/2%256)); 
     line(0,y,639,y); 
     line(0,y+2,639,y+2); 
     Sleep(10); 
     setcolor(BLACK); 
     line(0,y,639,y); 
   } 
   getch(); 
   closegraph(); 
 } 

10.随机数发生器:srand();使用方法:srand(time(NULL));使用时要有头文件time.h。

11.随机函数:rand();随机生成一个数,头文件是:stdlib.h,比如:


# include <stdio.h> 
# include <time.h> 
# include <graphics.h> 
 void main() 
 { 
   int t=10; 
   while(t--) 
   { 
     srand(time(NULL)); 
     printf("%dn",rand()); 
     Sleep(1000); 
   } 
} 

12.判断键盘是否有输入:kbhit();如果有的话返回1,否则返回0.

13.方向键:方健健的ASCII值我们不知道,普通的getchar();也不能输入,但是getch();通过运行程序可以发现方向比较特殊,分别是:上 224+72、下 224+80、左 224+75、右 224+77,就是说他们是由两个字符组成的,所以判断上下左右时就先判断if(kbhit()),然后判断if(getch()==224),如果是的话在判断if(getch()==72),是的话就是上,下左右同理。