⑴ 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;
}