运动与静止同时实现在这里我用圆实现运动、方块实现静止
代码如下:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
void *ball;
int x, y,maxx;
unsigned int size;
initgraph(&gdriver, &gmode, "");
maxx = getmaxx();
x = 0;
y = 200;
rectangle(x,y+11,x+20,y+31);
circle(x+10,y,10);
size = imagesize(x, y-10, x+20, y+10);
ball = malloc(size);
setfillstyle(SOLID_FILL, BLACK);
while (!kbhit())
{
cleardevice();
x += 10;
if (x >= maxx)
x = 0;
rectangle(0,211,20,231);
circle(x+10,y,10);
delay(100);
}
free(ball);
closegraph();
return 0;
}
再来说一下赛车游戏
我上面给大家可以说是从第一步:制作简单的场景和玩家的赛车
到第二步:进一步完善场景,
大家可能已经有所领会
下面我们就把它做成一个简单的游戏
代码如下:
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
static int c=1;
static int e=0;
static int u=0;
static int v=0;
static int x=1;
static int y=0;
static int j=-21;
static int i;
static int b;
void begin(void)
{
int xmax, ymax;
int startx,starty,endx,endy;
setbkcolor(WHITE);
xmax = getmaxx();
ymax = getmaxy();
endx=20;endy=20;
startx=10;starty=10;
setcolor(RED);
settextstyle(1,HORIZ_DIR,4);
outtextxy(10,240,"Bus Bus Bus");
outtextxy(300,300,"Made by PU BIN.");
setlinestyle(SOLID_LINE,0,3);
setwritemode(XOR_PUT);
setcolor(GREEN);
while(startx<xmax&&starty<ymax)
{
rectangle(startx,starty,endx,endy);
delay(4);
rectangle(startx,starty,endx,endy);
startx+=2;
starty+=startx*startx/2000;
endx=startx+10;
endy=starty+10;
sound(startx*10);
delay(10);
nosound();
}
getch();
cleardevice();
}
void road(void)
{
int h;
for(h=0;h<4;h++)
line(150+h*100,0,150+h*100,472);
for(h=0;h<3;h++)
{
setlinestyle(3,0,1);
line(200+h*100,0,200+h*100,472);
settextstyle(1,HORIZ_DIR,3);
}
settextstyle(1,HORIZ_DIR,4);
setcolor(RED);
outtextxy(40,40,"PASS");
outtextxy(40,80,"1");
setcolor(10);
setlinestyle(SOLID_LINE,0,3);
rectangle(150,400,450,460);
outtextxy(160,410,"Ready,Begin!");
}
void math(void)
{
settextstyle(1,HORIZ_DIR,4);
setcolor(RED);
if(x==2)
{
outtextxy(40,80,"1");
outtextxy(40,80,"2");
}
else if(x==3)
{
outtextxy(40,80,"2");
outtextxy(40,80,"3");
}
else if(x==4)
{
outtextxy(40,80,"3");
outtextxy(40,80,"4");
}else if(x==5){
outtextxy(40,80,"4");
outtextxy(40,80,"5");
}else if(x==6){
outtextxy(40,80,"5");
outtextxy(40,80,"6");
}else if(x==7){
outtextxy(40,80,"6");
outtextxy(40,80,"7");
}else if(x==8){
outtextxy(40,80,"7");
outtextxy(40,80,"8");
}else if(x==9){
outtextxy(40,80,"8");
outtextxy(40,80,"9");
}else if(x==10){
outtextxy(40,80,"9");
outtextxy(40,80,"10");
}else if(x==11){
outtextxy(40,80,"10");
outtextxy(40,80,"11");
}else if(x==12){
outtextxy(40,80,"11");
outtextxy(40,80,"12");
}else if(x==13){
outtextxy(40,80,"12");
outtextxy(40,80,"13");
}else if(x==14){
outtextxy(40,80,"13");
outtextxy(40,80,"14");
}else if(x==15){
outtextxy(40,80,"14");
outtextxy(40,80,"15");
}else if(x==16){
outtextxy(40,80,"15");
outtextxy(40,80,"16");
}else if(x==17){
outtextxy(40,80,"16");
outtextxy(40,80,"17");
}else if(x==18){
outtextxy(40,80,"17");
outtextxy(40,80,"18");
}else if(x==19){
outtextxy(40,80,"18");
outtextxy(40,80,"19");
}else if(x==20){
outtextxy(40,80,"19");
outtextxy(40,80,"20");
}else if(x==21){
outtextxy(40,80,"20");
outtextxy(40,80,"21");
}else if(x==22){
outtextxy(40,80,"21");
outtextxy(40,80,"22");
}else if(x==23){
outtextxy(40,80,"22");
outtextxy(40,80,"23");
}else if(x==24){
outtextxy(40,80,"23");
outtextxy(40,80,"24");
}else if(x==25){
outtextxy(40,80,"24");
outtextxy(40,80,"25");
y=1; v=1;
}
}
void bus2(void)
{
setcolor(BLUE);
rectangle(180+c*100,350,220+c*100,390);
rectangle(170+c*100,340,230+c*100,350);
rectangle(190+c*100,320,210+c*100,340);
rectangle(170+c*100,390,230+c*100,400);
setcolor(5);
line(190+c*100,350,190+c*100,390);
line(200+c*100,350,200+c*100,390);
line(210+c*100,350,210+c*100,390);
line(200+c*100,300,200+c*100,320);
line(185+c*100,300,215+c*100,300);
if((b==0x4b00)&&(c>0))
c--;
else if((b==0x4d00)&&(c<2))
c++;
else if((b==0x5000)&&(c<2))
sleep(1);
setcolor(BLUE);
rectangle(180+c*100,350,220+c*100,390);
rectangle(170+c*100,340,230+c*100,350);
rectangle(190+c*100,320,210+c*100,340);
rectangle(170+c*100,390,230+c*100,400);
setcolor(5);
line(190+c*100,350,190+c*100,390);
line(200+c*100,350,200+c*100,390);
line(210+c*100,350,210+c*100,390);
line(200+c*100,300,200+c*100,320);
line(185+c*100,300,215+c*100,300);
setcolor(BLUE);
}
void light(void)
{
rectangle(535,65,565,155);
floodfill(545,100,BLUE);
setfillstyle(1,RED);
fillellipse(550,80,10,10);
sound(650);
delay(300);
nosound();
delay(100);
setfillstyle(1,YELLOW);
fillellipse(550,110,10,10);
sound(650);
delay(300);
nosound();
delay(100);
setfillstyle(1,GREEN);
fillellipse(550,140,10,10);
sound(650);
delay(300);
nosound();
delay(100);
setcolor(10);
setlinestyle(SOLID_LINE,0,3);
rectangle(150,400,450,460);
outtextxy(160,410,"Ready,Begin!");
}
void brock(void)
{
if((c==i)&&(j>4)&&(j<38))
e=1;
}
void tree(void)
{
int w;
int poly[14];
setcolor(10);
for (w=-3;w<3;w=w+2)
{
line(85,-25+u*15+w*157,85,35+u*15+w*157);
line(95,-25+u*15+w*157,95,35+u*15+w*157);
line(105,-25+u*15+w*157,105,35+u*15+w*157);
line(115,-25+u*15+w*157,115,35+u*15+w*157);
line(75,-9+u*15+w*157,75,19+u*15+w*157);
line(125,-9+u*15+w*157,125,19+u*15+w*157);
}
for (w=-2;w<3;w=w+2)
{
poly[0] = 530;
poly[1] = u*15+w*157;
poly[2] =515;
poly[3] = 25+u*15+w*157;
poly[4] =485;
poly[5] =25+u*15+w*157 ;
poly[6] =470;
poly[7] =u*15+w*157 ;
poly[8] =485;
poly[9] =-25+u*15+w*157;
poly[10] =515;
poly[11] =-25+u*15+w*157 ;
poly[12] = poly[0];
poly[13] = poly[1];
drawpoly(7,poly);
}
}
void bus1(void)
{
randomize();
i=random(3);
setlinestyle(SOLID_LINE,0,3);
do
{
do
{
if((x!=7)&&(x!=16)&&(x!=23))
setcolor(x);
else
setcolor(2);
rectangle(170+i*100,j*10,230+i*100,60+j*10);
rectangle(160+i*100,70+j*10,240+i*100,260+j*10);
line(180+i*100,70+j*10,180+i*100,260+j*10);
line(200+i*100,70+j*10,200+i*100,260+j*10);
line(220+i*100,70+j*10,220+i*100,260+j*10);
tree();
sound(j*10);
delay(10);
nosound();
if((x!=7)&&(x!=16)&&(x!=23))
setcolor(x);
else
setcolor(2);
rectangle(170+i*100,j*10,230+i*100,60+j*10);
rectangle(160+i*100,70+j*10,240+i*100,260+j*10);
line(180+i*100,70+j*10,180+i*100,260+j*10);
line(200+i*100,70+j*10,200+i*100,260+j*10);
line(220+i*100,70+j*10,220+i*100,260+j*10);
tree();
u++;
j++;
brock();
if(e==1)
{
j=48;
y=1;
u=63;
v=0;
}
}while((bioskey(1)==0)&&(j<=47)&&(u<=62));
if((j<=47)&&(u<=62))
/* function 0 returns the key that is waiting */
{
b= bioskey(0);
bus2();
}
else if(u<=62)
{
j=-21;
randomize();
i=rand()%3;
x++;
math();
setcolor(x);
}
else
u=0;
}while(y!=1);
}
void end()
{
if (v==1)
{
for(j=0;j<94;j++)
{
rectangle(150,j*5,450,60+j*5);
settextstyle(1,HORIZ_DIR,4);
outtextxy(160,10+j*5,"STOP! You win!");
sound(500+j*10);
delay(2);
nosound();
setlinestyle(SOLID_LINE,0,3);
rectangle(150,j*5,450,60+j*5);
outtextxy(160,10+j*5,"STOP! You win!");
}
closegraph();
printf("\n\nyou win!");
}
else if(v==0 )
{
closegraph();
printf("\n\nYou lose.");
}
printf("\n\n\nCome back again.\n\n Made by YOU LIAO working house. pubingk@citiz.net");
getch();
getch();
}
void car()
{
setcolor(BLUE);
setlinestyle(SOLID_LINE,0,3);
rectangle(280,350,320,390);
rectangle(270,340,330,350);
rectangle(290,320,310,340);
rectangle(270,390,330,400);
setcolor(5);
line(290,350,290,390);
line(300,300,300,320);
line(300,350,300,390);
line(310,350,310,390);
line(285,300,315,300);
setcolor(BLUE);
}
void main(void)
{
int gdriver = DETECT , gmode;
initgraph(&gdriver, &gmode, "");
begin();
setbkcolor(7);
setcolor(WHITE);
setwritemode(XOR_PUT);
road();
car();
light();
bus1();
end();
}
代码如下:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode;
void *ball;
int x, y,maxx;
unsigned int size;
initgraph(&gdriver, &gmode, "");
maxx = getmaxx();
x = 0;
y = 200;
rectangle(x,y+11,x+20,y+31);
circle(x+10,y,10);
size = imagesize(x, y-10, x+20, y+10);
ball = malloc(size);
setfillstyle(SOLID_FILL, BLACK);
while (!kbhit())
{
cleardevice();
x += 10;
if (x >= maxx)
x = 0;
rectangle(0,211,20,231);
circle(x+10,y,10);
delay(100);
}
free(ball);
closegraph();
return 0;
}
再来说一下赛车游戏
我上面给大家可以说是从第一步:制作简单的场景和玩家的赛车
到第二步:进一步完善场景,
大家可能已经有所领会
下面我们就把它做成一个简单的游戏
代码如下:
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
static int c=1;
static int e=0;
static int u=0;
static int v=0;
static int x=1;
static int y=0;
static int j=-21;
static int i;
static int b;
void begin(void)
{
int xmax, ymax;
int startx,starty,endx,endy;
setbkcolor(WHITE);
xmax = getmaxx();
ymax = getmaxy();
endx=20;endy=20;
startx=10;starty=10;
setcolor(RED);
settextstyle(1,HORIZ_DIR,4);
outtextxy(10,240,"Bus Bus Bus");
outtextxy(300,300,"Made by PU BIN.");
setlinestyle(SOLID_LINE,0,3);
setwritemode(XOR_PUT);
setcolor(GREEN);
while(startx<xmax&&starty<ymax)
{
rectangle(startx,starty,endx,endy);
delay(4);
rectangle(startx,starty,endx,endy);
startx+=2;
starty+=startx*startx/2000;
endx=startx+10;
endy=starty+10;
sound(startx*10);
delay(10);
nosound();
}
getch();
cleardevice();
}
void road(void)
{
int h;
for(h=0;h<4;h++)
line(150+h*100,0,150+h*100,472);
for(h=0;h<3;h++)
{
setlinestyle(3,0,1);
line(200+h*100,0,200+h*100,472);
settextstyle(1,HORIZ_DIR,3);
}
settextstyle(1,HORIZ_DIR,4);
setcolor(RED);
outtextxy(40,40,"PASS");
outtextxy(40,80,"1");
setcolor(10);
setlinestyle(SOLID_LINE,0,3);
rectangle(150,400,450,460);
outtextxy(160,410,"Ready,Begin!");
}
void math(void)
{
settextstyle(1,HORIZ_DIR,4);
setcolor(RED);
if(x==2)
{
outtextxy(40,80,"1");
outtextxy(40,80,"2");
}
else if(x==3)
{
outtextxy(40,80,"2");
outtextxy(40,80,"3");
}
else if(x==4)
{
outtextxy(40,80,"3");
outtextxy(40,80,"4");
}else if(x==5){
outtextxy(40,80,"4");
outtextxy(40,80,"5");
}else if(x==6){
outtextxy(40,80,"5");
outtextxy(40,80,"6");
}else if(x==7){
outtextxy(40,80,"6");
outtextxy(40,80,"7");
}else if(x==8){
outtextxy(40,80,"7");
outtextxy(40,80,"8");
}else if(x==9){
outtextxy(40,80,"8");
outtextxy(40,80,"9");
}else if(x==10){
outtextxy(40,80,"9");
outtextxy(40,80,"10");
}else if(x==11){
outtextxy(40,80,"10");
outtextxy(40,80,"11");
}else if(x==12){
outtextxy(40,80,"11");
outtextxy(40,80,"12");
}else if(x==13){
outtextxy(40,80,"12");
outtextxy(40,80,"13");
}else if(x==14){
outtextxy(40,80,"13");
outtextxy(40,80,"14");
}else if(x==15){
outtextxy(40,80,"14");
outtextxy(40,80,"15");
}else if(x==16){
outtextxy(40,80,"15");
outtextxy(40,80,"16");
}else if(x==17){
outtextxy(40,80,"16");
outtextxy(40,80,"17");
}else if(x==18){
outtextxy(40,80,"17");
outtextxy(40,80,"18");
}else if(x==19){
outtextxy(40,80,"18");
outtextxy(40,80,"19");
}else if(x==20){
outtextxy(40,80,"19");
outtextxy(40,80,"20");
}else if(x==21){
outtextxy(40,80,"20");
outtextxy(40,80,"21");
}else if(x==22){
outtextxy(40,80,"21");
outtextxy(40,80,"22");
}else if(x==23){
outtextxy(40,80,"22");
outtextxy(40,80,"23");
}else if(x==24){
outtextxy(40,80,"23");
outtextxy(40,80,"24");
}else if(x==25){
outtextxy(40,80,"24");
outtextxy(40,80,"25");
y=1; v=1;
}
}
void bus2(void)
{
setcolor(BLUE);
rectangle(180+c*100,350,220+c*100,390);
rectangle(170+c*100,340,230+c*100,350);
rectangle(190+c*100,320,210+c*100,340);
rectangle(170+c*100,390,230+c*100,400);
setcolor(5);
line(190+c*100,350,190+c*100,390);
line(200+c*100,350,200+c*100,390);
line(210+c*100,350,210+c*100,390);
line(200+c*100,300,200+c*100,320);
line(185+c*100,300,215+c*100,300);
if((b==0x4b00)&&(c>0))
c--;
else if((b==0x4d00)&&(c<2))
c++;
else if((b==0x5000)&&(c<2))
sleep(1);
setcolor(BLUE);
rectangle(180+c*100,350,220+c*100,390);
rectangle(170+c*100,340,230+c*100,350);
rectangle(190+c*100,320,210+c*100,340);
rectangle(170+c*100,390,230+c*100,400);
setcolor(5);
line(190+c*100,350,190+c*100,390);
line(200+c*100,350,200+c*100,390);
line(210+c*100,350,210+c*100,390);
line(200+c*100,300,200+c*100,320);
line(185+c*100,300,215+c*100,300);
setcolor(BLUE);
}
void light(void)
{
rectangle(535,65,565,155);
floodfill(545,100,BLUE);
setfillstyle(1,RED);
fillellipse(550,80,10,10);
sound(650);
delay(300);
nosound();
delay(100);
setfillstyle(1,YELLOW);
fillellipse(550,110,10,10);
sound(650);
delay(300);
nosound();
delay(100);
setfillstyle(1,GREEN);
fillellipse(550,140,10,10);
sound(650);
delay(300);
nosound();
delay(100);
setcolor(10);
setlinestyle(SOLID_LINE,0,3);
rectangle(150,400,450,460);
outtextxy(160,410,"Ready,Begin!");
}
void brock(void)
{
if((c==i)&&(j>4)&&(j<38))
e=1;
}
void tree(void)
{
int w;
int poly[14];
setcolor(10);
for (w=-3;w<3;w=w+2)
{
line(85,-25+u*15+w*157,85,35+u*15+w*157);
line(95,-25+u*15+w*157,95,35+u*15+w*157);
line(105,-25+u*15+w*157,105,35+u*15+w*157);
line(115,-25+u*15+w*157,115,35+u*15+w*157);
line(75,-9+u*15+w*157,75,19+u*15+w*157);
line(125,-9+u*15+w*157,125,19+u*15+w*157);
}
for (w=-2;w<3;w=w+2)
{
poly[0] = 530;
poly[1] = u*15+w*157;
poly[2] =515;
poly[3] = 25+u*15+w*157;
poly[4] =485;
poly[5] =25+u*15+w*157 ;
poly[6] =470;
poly[7] =u*15+w*157 ;
poly[8] =485;
poly[9] =-25+u*15+w*157;
poly[10] =515;
poly[11] =-25+u*15+w*157 ;
poly[12] = poly[0];
poly[13] = poly[1];
drawpoly(7,poly);
}
}
void bus1(void)
{
randomize();
i=random(3);
setlinestyle(SOLID_LINE,0,3);
do
{
do
{
if((x!=7)&&(x!=16)&&(x!=23))
setcolor(x);
else
setcolor(2);
rectangle(170+i*100,j*10,230+i*100,60+j*10);
rectangle(160+i*100,70+j*10,240+i*100,260+j*10);
line(180+i*100,70+j*10,180+i*100,260+j*10);
line(200+i*100,70+j*10,200+i*100,260+j*10);
line(220+i*100,70+j*10,220+i*100,260+j*10);
tree();
sound(j*10);
delay(10);
nosound();
if((x!=7)&&(x!=16)&&(x!=23))
setcolor(x);
else
setcolor(2);
rectangle(170+i*100,j*10,230+i*100,60+j*10);
rectangle(160+i*100,70+j*10,240+i*100,260+j*10);
line(180+i*100,70+j*10,180+i*100,260+j*10);
line(200+i*100,70+j*10,200+i*100,260+j*10);
line(220+i*100,70+j*10,220+i*100,260+j*10);
tree();
u++;
j++;
brock();
if(e==1)
{
j=48;
y=1;
u=63;
v=0;
}
}while((bioskey(1)==0)&&(j<=47)&&(u<=62));
if((j<=47)&&(u<=62))
/* function 0 returns the key that is waiting */
{
b= bioskey(0);
bus2();
}
else if(u<=62)
{
j=-21;
randomize();
i=rand()%3;
x++;
math();
setcolor(x);
}
else
u=0;
}while(y!=1);
}
void end()
{
if (v==1)
{
for(j=0;j<94;j++)
{
rectangle(150,j*5,450,60+j*5);
settextstyle(1,HORIZ_DIR,4);
outtextxy(160,10+j*5,"STOP! You win!");
sound(500+j*10);
delay(2);
nosound();
setlinestyle(SOLID_LINE,0,3);
rectangle(150,j*5,450,60+j*5);
outtextxy(160,10+j*5,"STOP! You win!");
}
closegraph();
printf("\n\nyou win!");
}
else if(v==0 )
{
closegraph();
printf("\n\nYou lose.");
}
printf("\n\n\nCome back again.\n\n Made by YOU LIAO working house. pubingk@citiz.net");
getch();
getch();
}
void car()
{
setcolor(BLUE);
setlinestyle(SOLID_LINE,0,3);
rectangle(280,350,320,390);
rectangle(270,340,330,350);
rectangle(290,320,310,340);
rectangle(270,390,330,400);
setcolor(5);
line(290,350,290,390);
line(300,300,300,320);
line(300,350,300,390);
line(310,350,310,390);
line(285,300,315,300);
setcolor(BLUE);
}
void main(void)
{
int gdriver = DETECT , gmode;
initgraph(&gdriver, &gmode, "");
begin();
setbkcolor(7);
setcolor(WHITE);
setwritemode(XOR_PUT);
road();
car();
light();
bus1();
end();
}
评论