当前位置:首页 » 编程语言 » c语言打砖块代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言打砖块代码

发布时间: 2022-01-20 16:33:42

‘壹’ c语言掷骰子代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain(void)
{
intai[3];//甲的三个骰子的点数
intbi[3];//乙的三个骰子的点数
inti;
srand((unsigned)time(NULL));//播时间种子,产生随机数
for(i=0;i<3;i++)
{
ai[i]=rand()%6+1;//随机产生1-6的点数
bi[i]=rand()%6+1;
}
printf("甲的点数:");
for(i=0;i<3;i++)
{
printf("%d",ai[i]);
}

printf(" 乙的点数:");
for(i=0;i<3;i++)
{
printf("%d",bi[i]);
}

if(ai[0]+ai[1]+ai[2]>bi[0]+bi[1]+bi[2])
{
printf(" 甲获胜. ");
}
else{
printf(" 乙获胜. ");
}
return0;
}

‘贰’ 这个C语言打砖块的代码,砖块如何实现刷新(砖块怎么消失)

c语言游戏中实现动画靠的是1秒钟多于25次的刷新。大一用easyx做过类似的,现在有点忘了,核心思想大概是

while(判断游戏未结束)//时间没停,未触发游戏终止标志
{
for(inti=0;i<objectNum;i++)
{
//根据时间更新每个砖块的状态
//如果某砖块的flag设为被打到,清除该物品,如果是链表删节点
//未被打到,砖块.y更新
}

//画背景图

for(inti=0;i<objectNum;i++)
{
//画每个砖块
}
//获取用户命令
//一旦有命令,DispatchCommand()
//调用那个函数,检测鼠标位置停留的时候是不是按键了,检测有没有操作砖块
//sleep(100ms),延时造成视觉停留

}

‘叁’ 用C语言做打砖块

??不懂

‘肆’ 那位给一个C语言作的打砖游戏的代码!!!!简单易懂的!!!!

外加在键盘操作的基础上,用鼠标进行操作问题补充:就是用鼠标也能玩游戏,用C语言麻烦啊 C++多好,用WIN32程序或者MFC简单多了,打砖块不怎么需要资源

‘伍’ C语言编打砖块游戏的一些问题

随机数

‘陆’ 打砖块C语言项目总结与收获

摘要 1.换一本教材,不要用谭浩强的书,书里错误不少,而且会导致很恶劣的代码风格。

‘柒’ 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语言如何敲这个代码

#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d %d %d\n",
a>b?(a>c?a:c):(b>c?b:c),
a>b?(a<c?a:(b>c?b:c)):(b<c?b:(a>c?a:c)),
a<c?(a<b?a:b):(b<c?b:c)
);

return 0;
}

‘玖’ c语言编的打砖块游戏

#include "graphics.h"
#include "stdio.h"
#include "conio.h" /*所需的头文件*/
int on; /*声明具有开关作用的全局变量*/
static int score; /*声明静态的记分器变量*/

/* 定义开始界面函数*/
int open()
{
setviewport(100,100,500,380,1); /*设置图形窗口区域*/
setcolor(4); /*设置作图色*/
rectangle(0,0,399,279); /*以矩形填充所设的图形窗口区域*/
setfillstyle(SOLID_FILL,7); /*设置填充方式*/
floodfill(50,50,4); /*设置填充范围*/
setcolor(8);
settextstyle(0,0,9); /*文本字体设置*/
outtextxy(90,80,"BALL"); /*输出文本内容*/
settextstyle(0,0,1);
outtextxy(110,180,"version 1.0");
outtextxy(110,190,"made by ddt");
setcolor(128);
settextstyle(0,0,1);
outtextxy(120,240,"Press any key to continue......");
}
/*定义退出界面函数*/
int quitwindow()
{
char s[100]; /*声明用于存放字符串的数组*/
setviewport(100,150,540,420,1);
setcolor(YELLOW);
rectangle(0,0,439,279);
setfillstyle(SOLID_FILL,7);
floodfill(50,50,14);
setcolor(12);
settextstyle(0,0,8);
outtextxy(120,80,"End");
settextstyle(0,0,2);
outtextxy(120,200,"quit? Y/N");
sprintf(s,"Your score is:%d",score);/*格式化输出记分器的值*/
outtextxy(120,180,s);
on=1; /*初始化开关变量*/
}
/*主函数*/
main()
{
int gdriver,gmode;
gdriver=DETECT; /*设置图形适配器*/
gmode=VGA; /*设置图形模式*/
registerbgidriver(EGAVGA_driver); /*建立独立图形运行程序*/
initgraph(&gdriver,&gmode,""); /*图形系统初试化*/
setbkcolor(14);
open(); /*调用开始界面函数*/
getch(); /*暂停*/

while(1) /*此大循环体控制游戏的反复重新进行*/
{
int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=-2,dy=-2;/*初始化小球相关参数*/
int left[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][100];/*方砖阵列相关参数*/
static int pp;
static int phrase; /*一系列起开关作用的变量*/
int oop=15;
pp=1;
score=0;
driver=DETECT;
mode=VGA;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
setbkcolor(10);
cleardevice(); /*图形状态下清屏*/
clearviewport(); /*清除现行图形窗口内容*/
b=t+6;
r=l+60;
setcolor(1);
rectangle(0,0,639,479);
setcolor(4);
rectangle(l,t,r,b);
setfillstyle(SOLID_FILL,1);
floodfill(l+2,t+2,4);
for(i=0,k=0;i<=6;i++) /*此循环绘制方砖阵列*/
{
top[i]=k;
bottom[i]=top[i]+20;
k=k+21;
oop--;
for(j=0,m=0;j<=7;j++)
{
left[j]=m;
right[j]=left[j]+80;
m=m+81;
setcolor(4);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(SOLID_FILL,j+oop);
floodfill(left[j]+1,top[i]+1,4);
num[i][j]=pp++;
}
}
while(1) /*此循环控制整个动画*/
{
while(!kbhit())
{
x=x+dx; /*小球运动的圆心变量控制*/
y=y+dy;
if(x+r1>r||x+r1<r)
{ phrase=0;}
if((x-r1<=r||x+r1<=r)&&x+r1>=l)
{
if(y<t)
phrase=1;
if(y+r1>=t&&phrase==1)
{dy=-dy;y=t-1-r1;}
}
if(off==0)
continue;
for(i=0;i<=6;i++) /*此循环用于判断、控制方砖阵列的撞击、擦除*/
for(j=0;j<=7;j++)
{
if((x+r1<=right[j]&&x+r1>=left[j])||(x-r1<=right[j]&&x-r1>=left[j]))
{
if(( y-r1>top[i]&&y-r1<=bottom[i])||(y+r1>=top[i]&&y+r1<=bottom[i] ))
{
if(num[i][j]==0)
{continue; }
setcolor(10);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(SOLID_FILL,10);
floodfill(left[j]+1,top[i]+1,10);
dy=-dy;
num[i][j]=0;
score=score+10;
printf("%d\b\b\b",score);
}
}
if((y+r1>=top[i]&&y+r1<=bottom[i])||(y-r1>=top[i]&&y-r1<=bottom[i]))
{
if((x+r1>=left[j]&&x+r1<right[j])||(x-r1<=right[j]&&x-r1>left[j]))
{
if(num[i][j]==0)
{ continue;}
setcolor(10);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(SOLID_FILL,10);
floodfill(left[j]+1,top[i]+1,10);
dx=-dx;
num[i][j]=0;
score=score+10;
printf("%d\b\b\b",score);
}
}
}
if(x+r1>639) /*控制小球的弹射范围*/
{dx=-dx;x=638-r1;}
if(x<=r1)
{dx=-dx;x=r1+1;}
if(y+r1>=479)
{off=0;quitwindow();break;}
if(y<=r1)
{dy=-dy;y=r1+1;}
if(score==560)
{off=0;quitwindow();break;}
setcolor(6);
circle(x,y,r1);
setfillstyle(SOLID_FILL,14);
floodfill(x,y,6);
delay(1000);
setcolor(10);
circle(x,y,r1);
setfillstyle(SOLID_FILL,10);
floodfill(x,y,10);
}
a=getch();
setcolor(10);
rectangle(l,t,r,b);
setfillstyle(SOLID_FILL,10);
floodfill(l+2,t+2,10);
if(a==77&&l<=565) /*键盘控制设定*/
{dl=20;l=l+dl;}
if(a==75&&l>=15)
{dl=-20;l=l+dl;}
if(a=='y'&&on==1)
break;
if(a=='n'&&on==1)
break;
if(a==27)
{quitwindow();off=0;}
r=l+60;
setcolor(4);
rectangle(l,t,r,b);
setfillstyle(SOLID_FILL,1);
floodfill(l+5,t+5,4);
delay(100);
}
if(a=='y'&&on==1) /*是否退出游戏*/
{break;}
if(a=='n'&&on==1)
{ continue;}
}
closegraph();
}

‘拾’ 做一个打砖块的游戏 用C语言

请编译前在主目录下建立一个文本文件(xia.txt)内容为:0 0 0 0 0
哈哈
#include<dos.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
union REGS regs;
unsigned int zhuan[5];

int Msinit();
void Setmouse(int left,int right,int top,int buttom);
int Msread(int *xp,int *yp,int *bup);
void Draw(int x,int y,int sizex,int sizey);

main()
{int mode=VGAHI,driver=VGA;
unsigned int l;
int i,gi,gj,j,flag=1;/*i,j是循环变量,flag是标记变量,-1:向x负半轴移动,+1:向x正半轴移动*/
double qx,qy,k,b=0.0,speech=0.4,x,y;
double r=2.0,bx=60.0,byy=270.0;
double pianx=100.0,piany=100.0,tx=20.0,ty=10.0,jx=2.0,jy=2.0;
int mx=0,my=0,mb,sum;/*sum纪录砖块的数目*/
FILE * p;
if((p = fopen("xia.txt", "r")) == NULL)
{printf("The file cannot open!\n");
exit(1);}
initgraph(&driver,&mode,"\\tc\\BGI");
Msinit();
Setmouse((int)(pianx+1+bx/2),(int)((tx+jx)*8+pianx-1-bx/2),(int)byy+piany,(int)byy+piany);

star:cleardevice();/*程序重载的介入点*/
sum=0;
qx=100.0+pianx-10;qy=180.0+pianx-10;k=0.33;
setcolor(7);
rectangle((int)(pianx-2),(int)(piany-2),(int)((tx+jx)*8+2+pianx),302+piany);
setcolor(1);
rectangle((int)(pianx-1),(int)(piany-1),(int)((tx+jx)*8+1+pianx),301+piany);
/*读取盘面情况*/
for(i=0;i<5;i++)
fscanf(p,"%x ",&zhuan[i]);
/*画砖块*/
for(i=0;i<5;i++)
{l=1;
for(j=0;j<16;j++)
{if((zhuan[i]&l)==0)
{Draw((int)((jx+tx)*((16*i+j)%8)+pianx+jx),(int)((jy+ty)*((int)((16*i+j)/8))+piany+jy),

(int)tx,(int)ty);
sum+=1;
}
l=l*2;
}
}

for(;;)
{ setfillstyle(1, 0);
bar(mx-bx/2,my,mx+bx/2,my+5);
Msread(&mx, &my, &mb);

Draw(mx-bx/2,my,bx,5);
setcolor(0);
circle(qx,qy,r);
/*判断求是否反弹*/
if(qx-r<=pianx+1 || qx+r>=(tx+jx)*8+pianx-1) {flag=-flag;k=-k;}
if(qy-r<=piany+1) k=-k;

for(gi=0;gi<5;gi++)
{l=1;
for(gj=0;gj<16;gj++)
{
if((zhuan[gi]&l)==0)
{j=(16*gi+gj)/8;
i=(16*gi+gj)%8;
x=(jx+tx)*i+jx+tx/2+pianx;
y=(jy+ty)*j+jy+ty/2+piany;
/*边判断1*/
if(qy>=y-ty/2 && qy<=y+ty/2 &&(pow(qx+r-x+tx/2,2)<1 || pow(qx-r-x-tx/2,2)<1))
{flag=-flag;k=-k;
zhuan[gi]=zhuan[gi]|l;
sum-=1;
if(sum==0) {printf("Win!\n");getch();goto star;}
setfillstyle(1, 0);
bar((jx+tx)*i+pianx+jx,(jy+ty)*j+piany+jy,(jx+tx)*i+pianx+jx+tx,(jy+ty)

*j+piany+jy+ty);
}
else
/*边判断2*/
if(qx>
;=x-tx/2 && qx<=x+tx/2 &&(pow(qy+r-y+ty/2,2)<1 || pow(qy-r-y-ty/2,2)<1))
{k=-k;
zhuan[gi]=zhuan[gi]|l;
sum-=1;
if(sum==0) {printf("Win!\n");getch();goto star;}
setfillstyle(1, 0);
bar((jx+tx)*i+pianx+jx,(jy+ty)*j+piany+jy,(jx+tx)*i+pianx+jx+tx,(jy+ty)

*j+piany+jy+ty);
}
else
/*角判断*/
if(pow(qx-x+tx/2,2)+pow(qy-y+ty/2,2)<=r*r || pow(qx-x-tx/2,2)+pow(qy-y+ty/2,2)<=r*r ||

pow(qx-x+tx/2,2)+pow(qy-y-ty/2,2)<=r*r || pow(qx-x-tx/2,2)+pow(qy-y-ty/2,2)<=r*r)
{flag=-flag;
zhuan[gi]=zhuan[gi]|l;
sum-=1;
if(sum==0) {printf("Win!\n");getch();goto star;}
setfillstyle(1, 0);
bar((jx+tx)*i+pianx+jx,(jy+ty)*j+piany+jy,(jx+tx)*i+pianx+jx+tx,(jy+ty)

*j+piany+jy+ty);
}
}l=l*2;}}
/*棍棒的反弹*/
if(qx<=mx+bx/2 && qx>=mx-bx/2 && pow(qy+r-my,2)<1) {k=-(k/pow(k*k,0.5))*(0.3*bx/(pow(pow

(qx-mx,2),0.5)+0.000001));}
if((int)(qy+r)>my+0.5) {printf("DEAD!");Setmouse(0,0,640,480);getch();exit(1);}
b=qy-qx*k;
if(flag==1) qx=qx+speech/pow(1.0+k*k,0.5);
if(flag==-1) qx=qx-speech/pow(1.0+k*k,0.5);
qy=qx*k+b;/*计算球心坐标*/
setcolor(14);
circle((int)qx,(int)qy,r);
delay(1);
if(mb==1) {Setmouse(0,0,640,480);exit(1);}
}
}

/*这个函数用于实现鼠标的初始化*/
int Msinit()
{int recored;
regs.x.ax=0;
int86 (0x33, & regs, & regs);
recored=regs.x.ax;
if(recored==0)
{printf("Mouse not foundd or Mouse driver not installed.\n");
return 0;
}
else
return recored;
}

/*下面的函数用于实现设置鼠标的移动范围*/
void Setmouse(int left,int right,int top,int buttom)
{
regs.x.ax=7;
regs.x.cx=left;
regs.x.dx=right;
int86(0x33,& regs,& regs);
regs.x.ax=8;
regs.x.cx=top;
regs.x.dx=buttom;
int86(0x33,& regs,& regs);
}

/*下面这个函数用于实现鼠标的读取*/
int Msread(int *xp,int *yp,int *bup)
{int xnew,ynew,ch;
if(kbhit()) return getch();
regs.x.ax=3;
int86(0x33,& regs,& regs);
xnew=regs.x.cx;
ynew=regs.x.dx;
*bup=regs.x.bx;
*xp=xnew;
*yp=ynew;
return -1;
}

/*下面这个子程序是完成描绘一个按钮*/
void Draw(int x,int y,int sizex,int sizey)/* x, y为左上角坐标sizex,sizey为长和宽*/
{int sizx=sizex-1;
int sizy=sizey-1;
setcolor(15);/*这里最好用白色*/
line(x,y,x+sizx-1,y);
line(x,y+1,x+sizx-2,y+1);
line(x,y,x,y+sizy-1);
line(x+1,y,x+1,y+sizy-2);
setcolor(8);/*这里最好用深灰色*/
line(x+1,y+sizy,x+sizx,y+sizy);
line(x+2,y+sizy-1,x+sizx,y+sizy-1);
line(x+sizx-1,y+1,x+sizx-1,y+sizy);
line(x+sizx,y+2,x+sizx,y+sizy);
setcolor(7);/*这里最好用灰色*/
putpixel(x,y+sizy,3);
putpixel(x+1,y+sizy-1,3);
putpixel(x+sizx,y,3);
putpixel(x+sizx-1,y+1,3);
setfillstyle(1, 7);/*这里最好用灰色,设置填充模式*/
bar(x+2,y+2,x+sizx-2,y+sizy-2);
}