⑴ c语言实现用小球消除砖块,鼠标控制挡板,求源代码
#include<graphics.h>
#include<conio.h>
#define HIGH 480
#define WIDTH 640
#define N 14//砖块的数目
int ball_x,ball_y;
int ball_vx,ball_vy;
int radius;
int bar_x,bar_y;
int bar_high,bar_width;
int bar_left,bar_right,bar_top,bar_bottom;
int isbrick[N];
int brick_high,brick_width;
void startup()//数据初始化
{
initgraph(640,480);
ball_x=WIDTH/2;
ball_y=HIGH/2;
ball_vx=4;
ball_vy=4;
radius=20;
bar_high=HIGH/14;
bar_width=WIDTH/4;
bar_x=WIDTH/2;
bar_y=HIGH-bar_high/2;
bar_left=bar_x-bar_width/2;
bar_right=bar_x+bar_width/2;
bar_top=bar_y-bar_high/2;
bar_bottom=bar_y+bar_high/2;
brick_width=WIDTH/N;
brick_high=HIGH/N;
int i;
for(i=0;i<N;i++)
isbrick[i]=1;
BeginBatchDraw();
}
void clear()//清除画面
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
bar(bar_left,bar_top,bar_right,bar_bottom);
int i,brick_left,brick_right,brick_top,brick_bottom;
for(i=0;i<N;i++)
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_top=0;
brick_bottom=brick_high;
if(!isbrick[i])
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}
void show()//显示画面
{
setcolor(RED);
setfillcolor(WHITE);
fillcircle(ball_x,ball_y,radius);
bar(bar_left,bar_top,bar_right,bar_bottom);
int i,brick_left,brick_right,brick_top,brick_bottom;
for(i=0;i<N;i++)
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_top=0;
brick_bottom=brick_high;
if(isbrick[i])
{setcolor(WHITE);
setfillcolor(BROWN);
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}
FlushBatchDraw();
Sleep(10);
}
void output()//与用户输入无关的更新
{
if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3))
||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3)))
if((ball_x>=bar_left)&&(ball_x<=bar_right))//挡板与小球碰撞
ball_vy=-ball_vy;
ball_x=ball_x+ball_vx;//小球更新坐标
ball_y=ball_y+ball_vy;
if((ball_x<=radius)||(ball_x>=WIDTH-radius))//小球碰到边框返回
ball_vx=-ball_vx;
if((ball_y<=radius)||(ball_y>=HIGH-radius))
ball_vy=-ball_vy;
int i,brick_left,brick_right,brick_top,brick_bottom;//判断小球是否和砖块碰撞
for(i=0;i<N;i++)
{
if(isbrick[i])//砖块存在
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_bottom=brick_high;
if((ball_y==brick_bottom+radius)&&(ball_x>=brick_left)&&(ball_x<=brick_right))
{
isbrick[i]=0;
ball_vy=-ball_vy;
}
}
}
}
void Input()//与用户输入有关的更新
{
{
MOUSEMSG m;//定义鼠标信息
if(MouseHit())//检测当前是否有鼠标信息
{
m=GetMouseMsg();//获取一条鼠标信息
if(m.uMsg==WM_MOUSEMOVE)
{
//挡板的值得鼠标的位置
bar_x=m.x;
bar_y=m.y;
bar_left=bar_x-bar_width/2;
bar_right=bar_x+bar_width/2;
bar_top=bar_y-bar_high/2;
bar_bottom=bar_y+bar_high/2;
}
else if(m.uMsg==WM_LBUTTONDOWN)//按下鼠标左键,初始化小球的位置为挡板上面中心
{
ball_x=bar_x;
ball_y=bar_top-radius-3;
}
}
}
}
void gameover()//游戏结束
{
EndBatchDraw();
closegraph();
}
main()
{
startup();
while(1)
{
clear();
output();
Input();
show();
}
gameover();
return 0;
}
⑵ 用c语言编写一个程序模拟小球的自由落体运动
原题:一球从100m高度自由落下,每次落地后反弹回原来高度的一半,在落下。求它在第10次落地时,共进过多少m?第10次反弹的高度是?
源代码:
main()
{float sn=100,hn=sn/2; 我觉得此处的hn恒等于sn\2,正确的写法应该把此句写到
int n; for循环里。
for (n=2;n<=10;n++)
{sn=sn+2*hn;
hn=hn/2;
}
printf("第10次落地时共经过%f米.\n",sn);
printf("第10次反弹%f米.\n",hn)
}
自己改参数
⑶ 求一个C语言小球自由落体的代码,要求超过100行
#include<stdio.h>
intmain(){
intcurve[31][37]={0};//图像保存在30*36矩阵内
doubleA,AM=30;//第一周期幅值
doublek=0.18;//幅值递减率
inti,x,y;
for(i=0;i<4;++i){//共画出4个周期
A=(1-i*k)*AM;
for(x=0;x<9;++x){//每个周期用9个点描画
y=-(int)(x*A*(x-8)/16.0);
curve[y][x+i*8]=1;
}
}
for(y=30;y>=0;--y){
for(x=0;x<37;++x){
if(curve[y][x])printf("+");
elseprintf(".");
}
printf(" ");
}
return0;
}