Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 <>
Posted 24-Dec-12 8:45am
Edited 24-Dec-12 8:51am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :)
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 404
1 Nirav Prabtani 268
2 _Amy 230
3 CHill60 170
4 Sergey Alexandrovich Kryukov 166
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100