Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Random integers and their properties
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DJ




PostPosted: Sun Jun 29, 2003 9:33 pm   Post subject: Random integers and their properties

Yes, i now need help creating random integers that i can then assign properties to.

i want to make obstacles in random spots in my game that have properties, like u die if u run into them. the thing is, these guys move with the rest of the game and each time the screen moves, their position re-randomizes

i can't understand the help manual's thing on rand.set and rand.next and the suchlike, and the example it gives doesnt work

DJ
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Sun Jun 29, 2003 11:27 pm   Post subject: (No subject)

well to randomize and things location in a 2d spcae you need to randomly genreat intagres that are betwen 0 and maxy as well as one that is betwen 0 and maxx. then make the guys loaction equal to thess.

Ex.

code:

randomize %makes shure there are difrent random numbers

loop
    var x : int := Rand.Int (0, maxx)
    var y : int := Rand.Int (0, maxy)

    Draw.Dot(x,y,1)
   
    delay(20)
end loop
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Tony




PostPosted: Sun Jun 29, 2003 11:58 pm   Post subject: (No subject)

the way I see DJ's problem, you dont assign properties to random integers, you randomly assign location to objects Wink

since the map moves, you first create obsticles thoughout the map at random locations using Rand.Int

then as the map moves, you also change the location of each of the obsticles with the map to keep them aligned.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Andy




PostPosted: Mon Jun 30, 2003 8:22 am   Post subject: (No subject)

what i would do is have a type containing each of the properties u need using variables then just have an array of the type and just randomly pick a slot in the array
DJ




PostPosted: Sat Jul 12, 2003 3:16 pm   Post subject: (No subject)

i like tony's bit about random positions to objects, but i need about 50 of these objects, and each needs properties like dying if you run into them.
is there an easier way to make this than copying, pasting and changing variable numbers?
Tony




PostPosted: Sat Jul 12, 2003 3:21 pm   Post subject: (No subject)

yeah... write a class for the object and then declear an array 1..50 and point to the class... You'll have an array of independant objects. At that point you to assign a random position to each of them.

As for running into them, include a collision detection function into the class and have something like
code:

for i:1..50
if object(i)->collide(currentX,currentY) then
put "game over"
return
end if
end for
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
AsianSensation




PostPosted: Sat Jul 12, 2003 3:21 pm   Post subject: (No subject)

yep, array and type

have a type for location, and declare an array of that type
make sure you have some way of assigning random values to it though,
like in a loop.

example:
code:

type Location :
    record
        x, y : int

        /*or, depends on what kind of math
         you are doing on those positions*/
        x, y : real
    end record

var Pos : array 1..50 of Location

for rep: 1..50
     Location.x := Rand.Int(1, 100)
end for


Edit: Oops, posted the same time as tony, oh well, classes are better if you desire speed and organization, but types are, IMO, easier to understand than classes
Tony




PostPosted: Sat Jul 12, 2003 3:30 pm   Post subject: (No subject)

types are probably easier if all you need to do is just store the location. Then you global collision function will take (array(i).x,array(i).y,currentX,currentY) as its arguments
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
DJ




PostPosted: Sat Jul 12, 2003 3:39 pm   Post subject: (No subject)

can someone give me a working example of an array?
i fiddled around with th turing help and it doesnt help.
AsianSensation




PostPosted: Sat Jul 12, 2003 3:41 pm   Post subject: (No subject)

an array? there are two currently excellent tutorials in the tutorial section, one by tony, and another by dodge_tomahawk...

so check those out, ask questions if you don't understand though
DJ




PostPosted: Sat Jul 12, 2003 5:08 pm   Post subject: (No subject)

good tutorial, but i'll be back
DJ




PostPosted: Tue Jul 15, 2003 11:21 pm   Post subject: (No subject)

As promised:

How can i turn this errored peice of crap
code:
var m : array 400 .. 5400 by 200 of int

into good stuff?
arrays dont work like for loops, now do they

i read the array tut but no help.
Tony




PostPosted: Tue Jul 15, 2003 11:25 pm   Post subject: (No subject)

wtf? you cant have by whatever when declearing an array. Its not a range, its a series of numbers... If you really wanna do it, use

code:

for i:400 .. 5400 by 200
var m:array i..i of int
end for


but thats still stupid and I donno if its gonna work or not.

Just declear a range you need... like 1..26
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: