| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 749 人关注过本帖
标题:游戏第一弹--pong游戏
只看楼主 加入收藏
此少年少轻狂
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-3-24
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
游戏第一弹--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);
}
搜索更多相关主题的帖子: Welcome include 游戏 
2014-03-24 18:38
aiyameili
Rank: 2
等 级:论坛游民
帖 子:16
专家分:14
注 册:2013-8-25
收藏
得分:3 
楼主厉害,佩服,佩服!
2014-03-24 19:20
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:3 
楼主是来秀代码的么?


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-03-27 15:01
神机军师
Rank: 7Rank: 7Rank: 7
来 自:游鱼潜水
等 级:黑侠
威 望:2
帖 子:202
专家分:542
注 册:2013-12-21
收藏
得分:3 
偷偷拷贝走了代码,结果:
fatal error C1083: Cannot open include file: 'graphics.h': No such file or directory

未知令人期待!
2014-03-28 12:50
hlpssd1230
Rank: 1
等 级:新手上路
帖 子:4
专家分:3
注 册:2014-3-25
收藏
得分:3 
我也遇到了楼上的问题,,,为什么啊???
2014-03-28 22:30
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:3 
楼主的代码估计是Tc的吧

DO IT YOURSELF !
2014-03-28 22:32
tompobing
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:260
专家分:809
注 册:2012-12-9
收藏
得分:3 
回复 4楼 神机军师
少头文件graphics.h,你如果用的VC++的话去网上下个头文件
2014-03-28 22:43
神机军师
Rank: 7Rank: 7Rank: 7
来 自:游鱼潜水
等 级:黑侠
威 望:2
帖 子:202
专家分:542
注 册:2013-12-21
收藏
得分:0 
回复 7楼 tompobing
没错,就是这样。。 我这个vc 少好多头文件。。

未知令人期待!
2014-03-28 22:50
ying8501
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:1092
专家分:1446
注 册:2008-11-24
收藏
得分:3 
vc作图完全是另一套,像这种tc下的图形程序vc下是运行不了的。
2014-03-29 08:11
快速回复:游戏第一弹--pong游戏
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021692 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved