• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

想用C / C ++移动球

用户头像
it1352
帮助1

问题说明

#include<graphics.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
#include<stdlib.h>

void *ball;

void image()
{
    setcolor(RED);
    setfillstyle(SOLID_FILL,RED);
    fillellipse(10,10,10,10);
    ball=malloc(imagesize(0,0,20,20));
    getimage(0,0,20,20,ball);
    cleardevice();
}

void main(void)
{
    int gmode,gdriver=DETECT;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

    int l=getmaxx()/2,r=0,t=0,b=0;
    int x=1,y=1;
    int s=0,key=0;
    int xstep=1,ystep=1;

    image();
    setbkcolor(GREEN);

    while(key!=27)
    {
        while(!kbhit())
        {
            outtextxy(250,350,"");
            setcolor(7);

            putimage(l,t,ball,XOR_PUT);
            delay(5);
            putimage(l,t,ball,XOR_PUT);

            if(l>=getmaxx()||l<=0)
            {
                x*=-1;sound(1000);
                s=0;xstep=x*(random(4) 1);
                ystep=y*(random(3) 1);
                if(l<=0)
                    l=0;
                else
                    l=getmaxx();
            }

            if(t>=getmaxy()||t<=0)
            {
                y*=-1;sound(1000);
                s=0;ystep=y*(random(4) 1);
                xstep=x*(random(3) 1);
                if(t<=0)
                    t=0;
                else
                    t=getmaxy();
            }

            l =x xstep;
            t =y ystep;
            s  ;

            if(s==5)
            {
                nosound();
            }
        }

        key=getch();
    }

    closegraph();
}



这个球随机移动,但我想把这个球向左(中),右(中),向上(中),向下移动(中心)像钻石<>


This ball moves randomly, but I would like to move this ball just left(center), right(center), up(center), down(center) like diamond <>

正确答案

#1
这不是问题。你使用随机,然后抱怨球随机移动。这没有道理。 Obvious上尉说:如果你想以某种方式移动它,就这样移动它。 :-)



-SA
This is not a question. You use random and then complain that the ball moves randomly. It makes no sense. Captain Obvious says: if you want to move it in certain way, move it in this way. :-)

—SA
您可以使用第一个随机数来决定哪个您要移动的轴。然后根据第一个数字,执行水平或垂直移动。
You might use a first random number to decide along which axis you want to move. Then according to that first number, you execute either the horizontal or the vertical move.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiahg
系列文章
更多 icon
同类精品
更多 icon
继续加载