想用C / C ++移动球 [英] want to move ball in C/C++

查看:63
本文介绍了想用C / C ++移动球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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 <>

推荐答案

这不是问题。你使用随机,然后抱怨球随机移动。这没有道理。 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.


这篇关于想用C / C ++移动球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆