# Help creating a random point that moves

• 12-10-2012
Human107
Help creating a random point that moves
I have an assignment that involves creating a simple game. My game is sort of similar to the game snake, in that the purpose is to collect objects, in this case that moves in a random direction.

using: K.I.S.S programming

My issue and what I need help with is creating the array, filling it with random points, and then implementing a way to make it move (I think)

What it does now is create a point that moves crazy fast and without a pattern.

Line 33-45 is the function I need help with.

My code:
insert
Code:

```#include <stdio.h> #include <kiss-graphics.h> #include <kiss-compat.h> #include <kiss-input.h> void drawRobot(int x0, int y0,float a, float s); void drawbackground(); void drawtarget(); int main() {     int x=400,y=400,i,a,size;//start in middle     graphics_init(800,800);         graphics_update();     while((x <= 800)&&(y > 0)&&(x > 0)&&(y <= 800)){// loop until Q key is pressed         drawbackground();         if(left_button()!=0){x=x- 4;} // if left is pressed move left a pixel         if(right_button()!=0){x=x+4;}// if right, move to the right         if(up_button()!=0){y=y-4;}// if up is pressed move up (smaller Y)         if(down_button()!=0){y=y+4;}// if down is pressed move down         drawRobot(x,y,0,0);         drawtarget();         graphics_update();// update the screen     }                 sleep(5);     return 0; } //This is the function void drawtarget(double xA[], double yA[], int aSize) {     int x,y;     int a[1000],b[1000],size=1000,i;// create an array and other int variables     //for(i=0;i<10;i++){         a[i]=rand()%700;         b[i]=rand()%700;         x=a[3];         y=a[4];         graphics_circle_fill(x,y,10,5,5,5);         graphics_update();            }     void drawbackground() {             graphics_fill(0,180,0);// fill in background     graphics_update(); } void drawRobot(int x, int y,float a, float s) {     graphics_circle_fill(x+5,y+10,10,5,5,5);//Robot     graphics_line(x+50, y+50, x-5, y-5, 0, 0, 0); }```
Thank you guys, in advance for helping. Any pointers and advice would be great.
• 12-10-2012
AndiPersti
Quote:

Originally Posted by Human107
My issue and what I need help with is creating the array, filling it with random points, and then implementing a way to make it move (I think)

What it does now is create a point that moves crazy fast and without a pattern.

If you want to move the object randomly, you need to calculate a random direction (probably either up, down, left or right) and then move the object accordingly.

Bye, Andreas
• 12-10-2012
Click_here
rand (CRT)

You are missing srand
• 12-10-2012
Click_here
Oh, and stdlib.h for the random function :)
• 12-10-2012
First a tip on some English for your KISS website:
Quote:

Were a mod_ PERL programming house. and no. we don't do POOP (Perl Object Orientated Programming). We use a object orientated style with out <correct is "without"> the OOPs it self. <wrong words, correct is "itself">

There are a number of reasons why. <replace the period with a comma> but simply put.. We don't

We do how ever <correct word is "however"> like using gagets, <correct is "gadgets"> widgets and javascript, Ajax, DHTML and CSS.
And here's a program to help with your timing of the movement. The ascii robot starts out fast, but slows as it moves along.

Code:

``` /*ASCII robot animation */ #include <stdio.h> #include <conio.h> #include <time.h> #include <windows.h> #define VMAX 3 #define HMAX 3 void printRobot(char robot[][3],int x, int y); void fillIn(int x,int y); int main(void) {   int i,x,y;   clock_t pause = 15;   //clock_t timerNew,timerOld;   char robot[VMAX][HMAX]={       {'.','o','.'},       {'-','¦','-'},       {'/',' ','\\'}       };   printf("Say I want this 'robot', to get animated, but slow down as he travels\n\n");   for(i=0;i<304;i++)       printf("%c%c%c%c%c",'.','.','.','.','.');   x=2;y=6;     for(i=0;i<108;i++) {       _gotoxy(x,y);       printRobot(robot,x,y);         Sleep(pause);         fillIn(x,y);       x+=1;        if(x>75){         x=2;         y+=VMAX;       }       if(i>95) {         ++y;         pause+=4;       }       pause+=3;   }   x+=10; y+=VMAX;   _gotoxy(x,y);   printRobot(robot,x,y);   _gotoxy(1,23);   printf("\n");   return 0; } void printRobot(char robot[][3],int x, int y) {   int r,c;     for(c=0,r=2;c<HMAX;c++)       printf("%c",robot[r][c]);   _gotoxy(x,y-1);   for(c=0,r=1;c<HMAX;c++)        printf("%c",robot[r][c]);   _gotoxy(x,y-2);   for(c=0,r=0;c<HMAX;c++)       printf("%c",robot[r][c]); } void fillIn(int x,int y) {   int i;   for(i=VMAX;i>-1;i--) {       _gotoxy(x,y-i);       printf("...");   } }```
• 12-10-2012
Human107
Thanks for the help guys, I'll give this all a try.
• 12-10-2012
Human107
Sorry guys, I still can't figure this out. All I can get it to do is come up with a 1000 dot at one time.

Can someone please show me the proper way to do this?
• 12-10-2012