10 Replies - 13442 Views - Last Post: 20 August 2006 - 03:13 AM Rate Topic: -----

#1 jorijo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

random movement in C

Posted 16 August 2006 - 07:21 AM

can anyone explain me how to generate random movement of a character horizontaly using rand function?

only can generate movement but its not random... :huh:
Is This A Good Question/Topic? 0
  • +

Replies To: random movement in C

#2 Nova Dragoon   User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 38
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: random movement in C

Posted 16 August 2006 - 07:56 AM

the rand functions works like this:

you need to seed it first:
I use this:
#include <time.h>
#include <stdlib.h>
srand(time(0));



then to use rand:
value = rand()%range  +  offset;



Where range is the number of random numbers to go between, and offset is where they should start from 0.
Was This Post Helpful? 0
  • +
  • -

#3 jorijo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

Re: random movement in C

Posted 16 August 2006 - 09:50 AM

Ive understand how it works but for example how do I print the movement of a char?!
sory if its a dummy question but Im a newbie... :D

This post has been edited by jorijo: 16 August 2006 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 frog   User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: random movement in C

Posted 16 August 2006 - 11:20 AM

how are you planning to generate any movement at all?? are you using graphics mode of text mode??
Was This Post Helpful? 0
  • +
  • -

#5 jorijo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

Re: random movement in C

Posted 16 August 2006 - 11:33 AM

	c.fcolor(0);						/* atribui a cor azul ao Carro2*/
				gotoxy(23,80-i);printf("%c",219);	/* desenha o caracter do Carro2*/
				gotoxy(22,80-i);printf("%c",219);
				for(f=1;f<10000000;f++);			/* movimento do Carro2*/
				
				if (carro2==10)						/* dimensão do Carro 2 - 10 caracateres*/
				{
					gotoxy(22,80-i+carro2);
					printf(" ");					/* apaga a posicao anterior à dimensao do Carro2*/
					gotoxy(23,80-i+carro2);
					printf(" ");
				}
				else
					carro2=carro2+1;

this how Im making the movemement

This post has been edited by jorijo: 16 August 2006 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 frog   User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: random movement in C

Posted 16 August 2006 - 11:44 AM

ohk that would be text mode. im not sure if im getting the co ordinates correct but the y axis is from 0-25 and x axis 0-80(correct me if im wrong) bow generate using rand() different values for coordinates in the loop and use them with the gotoxy() function. try that. i guess it should work
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: random movement in C

Posted 16 August 2006 - 11:40 PM

Aah...the matrix effect.
Was This Post Helpful? 0
  • +
  • -

#8 jorijo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

Re: random movement in C

Posted 17 August 2006 - 03:04 AM

yes the co ordinates are right but I dont understand what you mean with this --> "... bow generate using rand() different values for coordinates in the loop and use them with the gotoxy() function."

born2code what do you mean with matrix effect?
Was This Post Helpful? 0
  • +
  • -

#9 frog   User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: random movement in C

Posted 18 August 2006 - 09:27 PM

oops :P silly typo it was "now" and not "bow" . a little bit of explanation again. i hope you got the use of rand() . now you can generate different combintaion of coordinates from this .so you can use the found set of coordinates in the gotoxy() function and therefore you would get your random motion
Was This Post Helpful? 0
  • +
  • -

#10 jorijo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-August 06

Re: random movement in C

Posted 19 August 2006 - 08:25 AM

now I can generate random using rand() but cant control the speed do you know someway to control it?
Was This Post Helpful? 0
  • +
  • -

#11 frog   User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: random movement in C

Posted 20 August 2006 - 03:13 AM

well yes you can always use the delay() function defined in dos.h . the argument passed to it is taken in milliseconds. it suspends the execution of the program for that much time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1