游戏第一弹--pong游戏
#include <graphics.h>#include <conio.h>
#include <time.h>
#include <string>
void Button();
void Score(int s1,int s2);
typedef struct line{
int l1;
int l2;
struct line *next;
}LINE;
LINE *creat(LINE *head,int i,int x,int y);
void Huifang(LINE *head);
void Draw(int x1,int y1,int x2,int y2,int x,int y);
void Drawclear(int x1,int y1,int x2,int y2,int x,int y);
int Welcome();
void main()
{
initgraph(800, 480);
int x=200,y=200,x1=5,y1=120,x2=637,y2=120,s1=0,s2=0,dx,dy=3,i=1,j=5;
char c;
LINE *head=NULL;
MOUSEMSG m;
srand( (unsigned)time( NULL ) );
dx = rand() %3+2;
while(true){
j=Welcome();
if(j==3)
break;
}
setbkcolor(WHITE);
cleardevice();
Button();
while(c!=27)
{
BeginBatchDraw();
Drawclear(x1,y1,x2,y2,x,y);
if(x<=x1+20&&y>=y1&&y<=y1+100||x>=x2-10&&y>=y2&&y<=y2+100)
dx=-dx;
if(x>=631){
x=200;
s1++;
}
if(x<=16){
x=200;
s2++;
}
Score(s1,s2);
if(y<=50||y>=448)
dy=-dy;
x+=dx;
y+=dy;
while(MouseHit()){
m = GetMouseMsg();
if(m.uMsg==WM_LBUTTONDOWN&&m.x>=660&&m.x<=750&&m.y>=60&&m.y<=100){
j=1;
break;
}
if(m.uMsg==WM_LBUTTONDOWN&&m.x>=660&&m.x<=750&&m.y>=170&&m.y<=210){
j=0;
break;
}
if(m.uMsg==WM_LBUTTONDOWN&&m.x>=660&&m.x<=750&&m.y>=280&&m.y<=320){
Huifang(head);
break;
}
if(m.uMsg==WM_LBUTTONDOWN && m.x>=660 && m.x<=750 && m.y>=390 && m.y<=430)
closegraph();
}
if(j==1){
head=creat(head,i,x,y);
i++;
}
if (kbhit()){
c = getch();
switch(c)
{
case 'w': if(y1<=40)break;
else{
y1-=20;
break;
}
case 's': if(y1>=342)break;
else{
y1+=20;
break;
}
case 'i': if(y2<=40)break;
else{
y2-=20;
break;
}
case 'k': if(y2>=342)break;
else{
y2+=20;
break;
}
case 27: break;
}
}
Draw(x1,y1,x2,y2,x,y);
FlushBatchDraw();
Sleep(20);
}
EndBatchDraw();
getch();
closegraph();
}
int Welcome()
{
MOUSEMSG m;
IMAGE img;
loadimage(&img, "d:\\6.jpg");
putimage(0, 0, &img);
Sleep(30);
while(MouseHit()){
m = GetMouseMsg();
if(m.uMsg==WM_LBUTTONDOWN&&m.x>=390&&m.x<=460&&m.y>=410&&m.y<=440)
return 3;
}
return 5;
}
void Huifang(LINE *head)
{
while(head->next!=NULL){
setcolor(GREEN);
circle(head->l1,head->l2, 10);
Sleep(20);
setcolor(WHITE);
circle(head->l1, head->l2, 10);
FlushBatchDraw();
head=head->next;
EndBatchDraw();
}
}
LINE* creat(LINE *head,int i,int x,int y)
{
LINE *p, *s;
s = (LINE*)malloc(sizeof(LINE));
s->l1 = x;
s->l2 = y;
if (i==1)
{
s->next = head;
head=s;
return head;
}
for (p=head; p->next!=NULL; p=p->next);
s->next = p->next;
p->next = s;
return head;
}
void Button()
{
char s1[]="记录轨迹",
s2[]="停止记录",
s3[]="回放轨迹",
s4[]="结束游戏";
setcolor(BLACK);
line(3,39,3,462);
line(3,39,650,39);
line(650,39,650,462);
line(3,462,650,462);
rectangle(660,60,750,100);
rectangle(660,170,750,210);
rectangle(660,280,750,320);
rectangle(660,390,750,430);
outtextxy(670,70,s1);
outtextxy(670,180,s2);
outtextxy(670,290,s3);
outtextxy(670,400,s4);
}
void Score(int s1,int s2)
{
char s[5],
s3[]="姓名",
s4[]="分数";
setcolor(BLACK);
outtextxy(100,13,s3);
outtextxy(190,13,s4);
outtextxy(420,13,s3);
outtextxy(510,13,s4);
sprintf(s,"%d",s1);
outtextxy(225,13,s);
sprintf(s,"%d",s2);
outtextxy(545,13,s);
}
void Drawclear(int x1,int y1,int x2,int y2,int x,int y)
{
setcolor(WHITE);
setfillcolor(WHITE);
fillrectangle(x1,y1,x1+10,y1+100);
setcolor(WHITE);
setfillcolor(WHITE);
fillrectangle(x2,y2,x2+10,y2+100);
setcolor(WHITE);
circle(x, y,10);
}
void Draw(int x1,int y1,int x2,int y2,int x,int y)
{
setcolor(YELLOW);
setfillcolor(YELLOW);
fillrectangle(x1,y1,x1+10,y1+100);
setcolor(YELLOW);
setfillcolor(YELLOW);
fillrectangle(x2,y2,x2+10,y2+100);
setcolor(GREEN);
circle(x, y, 10);
}