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

Username:   Password: 
 RegisterRegister   
 help wit random placement of astroids
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vagyb




PostPosted: Mon May 24, 2004 12:52 pm   Post subject: help wit random placement of astroids

here is my code and when i try to put the x and y values of the astroids to random they get messed up and keep repeating in same place, could someone take a look at my code and tell me how i can put the astroids in a different location every time they come?
Sponsor
Sponsor
Sponsor
sponsor
SuperGenius




PostPosted: Mon May 24, 2004 1:14 pm   Post subject: (No subject)

your problem is that you have defined your variables as random numbers and the start of your program, and then not called randint again to change them. To fix this you need to call randint for your variables inside your main loop so they will have different values each time the loop executes
vagyb




PostPosted: Mon May 24, 2004 1:20 pm   Post subject: (No subject)

i think i understand wat u mean but once i do that the rocks start flashing on the screen :S, if any1 is kind enough could u try to fix it or do w/e SuperGenius is tryin to say and post the code here again? or explain to me in other simplier way?
guruguru




PostPosted: Mon May 24, 2004 1:39 pm   Post subject: (No subject)

Ok. You have variable boulder1Y. Then you make a random number and assign it to variable number4. Lets say that the random number outputed 150. number4 is equal to 150. Then you makeboulder1Y equal to number4- so now boudler1Y is equal to 150.

Then you begin your loop. The first time through the loop, the boulder is drawn at boulder1Y- which is 150. The second time it is draw and boulder1Y, which again is 150 because you have never changed the value of it.

To solve your problem, you have assign a random number to boulder1Y every time it reaches the end of the screen. What you could do is change this code:

code:
   
if boulder1X + boulderWidth <= 0 then                     
    boulder1X := maxx 
else                                               
    boulder1X -= 1                       
end if


to...

code:

if boulder1X + boulderWidth <= 0 then                     
    boulder1X := maxx 
    randint (boulder1Y, 120, 170)
else                                               
    boulder1X -= 1                       
end if


The same principle applies to all the other Y values. For the x values, instead of resetting the X value to maxx when it reaches the end of the screen, make it equal to a random number. (e.g. randint (boulder1X, 0, maxx).
SuperGenius




PostPosted: Mon May 24, 2004 1:41 pm   Post subject: (No subject)

try to run this example, and press both option. Now look at the code. You will see that each of the two options is the same 5 lines of code, but in a different order. What it means is whenever the line
code:

randint (num, 0, 10)

is called the var 'num' is assigned a new value within the specified range.
vagyb




PostPosted: Mon May 24, 2004 1:45 pm   Post subject: (No subject)

i think i kinda understood wat u mean, but what about this code? where do i put it? if you are nice enough could u plz put the stuff u said in the code and post it here, i will be VERY VERY grateful Sad

code:

randint (number1, 10, 40)
    randint (number2, 250, 350)
    randint (number3, 300, maxx)
    randint (number4, 120, 170)
    randint (number5, 450, 550)
    randint (number6, 240, 300)
    var boulder1X := number3                       
    var boulder1Y := number4                         
    var boulder2X := number1                                           
    var boulder2Y := number2                                           
    var boulder3X := number5
    var boulder3Y := number6
guruguru




PostPosted: Mon May 24, 2004 1:51 pm   Post subject: (No subject)

That code is fine. However you can make it more efficient. Simply plug in boulder1X and the rest into the randint function instead of number1 etc...

Change:

var boulder1X := number3
to
var boulder1X
randint(boulder1X, 300, maxx)

And do that for the rest of the boulder variables, then you can get rid of the number1's.

And no I will not post the code... the exact code to replace is right there Confused ! And I'm not going to do the rest of the variables for you because I don't have that much spare time Rolling Eyes .
vagyb




PostPosted: Mon May 24, 2004 1:54 pm   Post subject: (No subject)

lol alrite i think i got it now, thx alot
Sponsor
Sponsor
Sponsor
sponsor
vagyb




PostPosted: Mon May 24, 2004 2:00 pm   Post subject: (No subject)

k i thnk i did wat u said, but problem is the rocks r still flashing, here is the code

code:

setscreen ("graphics:400;400,position:middle;centre,nobuttonbar,title:Space")
const margin : int := 10
var number1 : int
var number2 : int
var number3 : int
var number4 : int
var number5 : int
var number6 : int
var spacePic : int
var shipPic : int
var boulderPic : int
var boulder2Pic : int
var boulder3Pic : int                       
var spaceWidth, spaceHeight, boulderWidth, boulder2Width, boulder3Width : int       
var spaceX, spaceY : int
var shipX, shipY : int
var shipWidth, shipHeight : int
var chars : array char of boolean
shipPic := Pic.FileNew ("ship.jpg")
spacePic := Pic.FileNew ("spacepic.bmp")
boulderPic := Pic.FileNew ("boulder.bmp")
boulder2Pic := Pic.FileNew ("boulder2.bmp")
boulder3Pic := Pic.FileNew ("boulder3.bmp")          setscreen ("graphics:400;400,position:middle;centre,nobuttonbar,title:Space")
shipWidth := Pic.Width (shipPic)
shipHeight := Pic.Width (shipPic)
spaceWidth := Pic.Width (spacePic)
spaceHeight := Pic.Height (spacePic)
boulderWidth := Pic.Width (boulderPic)
boulder2Width := Pic.Width (boulder2Pic)
boulder3Width := Pic.Width (boulder2Pic)         
spaceX := (spaceWidth - maxx) div 2
spaceY := (spaceHeight - maxy) div 2
shipX := (spaceWidth - shipWidth) div 2
shipY := (spaceHeight - shipHeight) div 2

setscreen ("graphics:450;350,offscreenonly")
loop

    var boulder1X : int                     
    var boulder1Y : int                       
    var boulder2X : int                                         
    var boulder2Y : int                                         
    var boulder3X : int
    var boulder3Y : int
    randint (boulder2X, 10, 40)
    randint (boulder2Y, 250, 350)
    randint (boulder1X, 300, maxx)
    randint (boulder1Y, 120, 170)
    randint (boulder3X, 450, 550)
    randint (boulder3Y, 240, 300)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then               
        shipY += 2                       
    elsif chars (KEY_DOWN_ARROW) then         
        shipY -= 2
    end if

    if boulder1X + boulderWidth <= 0 then           
        boulder1X := maxx       
        randint (boulder1X, 300, maxx)         
    else                                       
        boulder1X -= 1                   
    end if

    if boulder2X + boulder2Width <= 0 then
        boulder2X := maxx
        randint (boulder2X, 250, 350)
    else
        boulder2X -= 1
    end if

    if boulder3X + boulder2Width <= 0 then
        boulder3X := maxx
        randint (boulder3X, 450, 500)
    else
        boulder3X -= 1
    end if

    if shipX - margin < 0 then
        shipX := margin
    elsif shipX + shipWidth > spaceWidth - margin then
        shipX := spaceWidth - margin - shipWidth
    end if

    if shipY - margin < 0 then
        shipY := margin
    elsif shipY + shipWidth > spaceWidth - margin then
        shipY := spaceWidth - margin - shipWidth
    end if

    if shipY - margin < margin then
        shipY := margin
    elsif shipY + shipWidth > spaceWidth - margin then
        shipY := spaceWidth - margin - shipWidth
    end if

    if shipX - shipX < margin then
        spaceX := shipX - margin
    elsif (shipX + shipWidth) - spaceX > maxx - margin then
        spaceX := shipX + shipWidth - maxx + margin
    end if

    if shipY - spaceY < margin then
        spaceY := shipY - margin
    elsif (shipY + shipHeight) - spaceY > maxy - margin then
        spaceY := shipY + shipHeight - maxy + margin
    end if

    colorback (black)                                     
    cls
    Pic.Draw (boulderPic, boulder1X, boulder1Y, picMerge)
    Pic.Draw (boulder2Pic, boulder2X, boulder2Y, picMerge)
    Pic.Draw (boulder3Pic, boulder3X, boulder3Y, picMerge) 
    Pic.Draw (shipPic, shipX - spaceX, shipY - spaceY, picMerge)
    View.Update
end loop


is this wat u meant i should do?
vagyb




PostPosted: Mon May 24, 2004 2:08 pm   Post subject: (No subject)

omg wow i think i just solved the problem, ignore what i rote above. wow i'm so happy lol
guruguru




PostPosted: Mon May 24, 2004 2:13 pm   Post subject: (No subject)

Umm... no that's not at all what I said Crying or Very sad ! Read the above posts... first I never said anything about putting variable stuff in the loop except for regenerated a value for the y values. And then you made it regenerate it x values.. AHHHHHHHHHH!!!! Well... read my above posts and good luck Razz !

EDIT: Glad you solved it! Ask away for other problems Very Happy !
vagyb




PostPosted: Mon May 24, 2004 2:49 pm   Post subject: (No subject)

k this is the final code that works hehe, except i have 1 problem. when i move the spaceship up, it does not go till the end of the screen but stops bout 75% before reaching the screen. so basically when i press up arrow the spaceship goes up to a certain point but does not reach the top of the border

code:

setscreen ("graphics:400;400,position:middle;centre,nobuttonbar,title:Space")
const margin : int := 10
var number1 : int
var number2 : int
var number3 : int
var number4 : int
var number5 : int
var number6 : int
var spacePic : int
var shipPic : int
var boulderPic : int
var boulder2Pic : int
var boulder3Pic : int
var boulder4Pic : int
var spaceWidth, spaceHeight, boulderWidth, boulder2Width, boulder3Width, boulder4Width : int       % We need to know the width of the boulder picture
var spaceX, spaceY : int
var shipX, shipY : int
var shipWidth, shipHeight : int
var chars : array char of boolean
shipPic := Pic.FileNew ("ship.jpg")
spacePic := Pic.FileNew ("spacepic.bmp")
boulderPic := Pic.FileNew ("boulder.bmp")
boulder2Pic := Pic.FileNew ("boulder2.bmp")
boulder3Pic := Pic.FileNew ("boulder3.bmp")
boulder4Pic := Pic.FileNew ("boulder4.bmp")
setscreen ("graphics:400;400,position:middle;centre,nobuttonbar,title:Space")
shipWidth := Pic.Width (shipPic)
shipHeight := Pic.Width (shipPic)
spaceWidth := Pic.Width (spacePic)
spaceHeight := Pic.Height (spacePic)
boulderWidth := Pic.Width (boulderPic)
boulder2Width := Pic.Width (boulder2Pic)
boulder3Width := Pic.Width (boulder3Pic)
boulder4Width := Pic.Width (boulder4Pic)
spaceX := (spaceWidth - maxx) div 2
spaceY := (spaceHeight - maxy) div 2
shipX := (spaceWidth - shipWidth) div 2
shipY := (spaceHeight - shipHeight) div 2
var boulder1X : int := 10
var boulder1Y : int := 200
var boulder2X : int := 200
var boulder2Y : int := 244
var boulder3X : int := 300
var boulder3Y : int := 111
var boulder4X : int := 400
var boulder4Y : int := 222
setscreen ("graphics:450;350,offscreenonly")
loop


    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        shipY += 3
    elsif chars (KEY_DOWN_ARROW) then
        shipY -= 3
    end if

    if boulder1X + boulderWidth <= 0 then
        boulder1X := maxx

        randint (boulder1Y, 1, maxy)
    else
        boulder1X -= 2
    end if

    if boulder2X + boulder2Width <= 0 then
        boulder2X := maxx

        randint (boulder2Y, 1, maxy)
    else
        boulder2X -= 2
    end if

    if boulder3X + boulder3Width <= 0 then
        boulder3X := maxx
        randint (boulder3Y, 1, maxy)
    else
        boulder3X -= 2
    end if

    if boulder4X + boulder4Width <= 0 then
        boulder4X := maxx
        randint (boulder4Y, 1, maxy)
    else
        boulder4X -= 2
    end if

    if shipX - margin < 0 then
        shipX := margin
    elsif shipX + shipWidth > spaceWidth - margin then
        shipX := spaceWidth - margin - shipWidth
    end if

    if shipY - margin < 0 then
        shipY := margin
    elsif shipY + shipWidth > spaceWidth - margin then
        shipY := spaceWidth - margin - shipWidth
    end if

    if shipY - margin < margin then
        shipY := margin
    elsif shipY + shipWidth > spaceWidth - margin then
        shipY := spaceWidth - margin - shipWidth
    end if

    if shipX - shipX < margin then
        spaceX := shipX - margin
    elsif (shipX + shipWidth) - spaceX > maxx - margin then
        spaceX := shipX + shipWidth - maxx + margin
    end if

    if shipY - spaceY < margin then
        spaceY := shipY - margin
    elsif (shipY + shipHeight) - spaceY > maxy - margin then
        spaceY := shipY + shipHeight - maxy + margin
    end if

    colorback (black)
    cls
    Pic.Draw (boulderPic, boulder1X, boulder1Y, picMerge)
    Pic.Draw (boulder2Pic, boulder2X, boulder2Y, picMerge)
    Pic.Draw (boulder3Pic, boulder3X, boulder3Y, picMerge)
    Pic.Draw (boulder4Pic, boulder4X, boulder4Y, picMerge)
    Pic.Draw (shipPic, shipX - spaceX, shipY - spaceY, picMerge)
    View.Update
end loop

guruguru




PostPosted: Mon May 24, 2004 3:20 pm   Post subject: (No subject)

Please post boulder4Pic picture.
vagyb




PostPosted: Mon May 24, 2004 3:39 pm   Post subject: (No subject)

oops my bad lol its the same as all except name is boulder4, but here it is


boulder4.bmp
 Description:
 Filesize:  12.73 KB
 Viewed:  2428 Time(s)

boulder4.bmp


guruguru




PostPosted: Mon May 24, 2004 4:30 pm   Post subject: (No subject)

When testing to see if shipY is greater than maxy, you add the ships width to shipY, when you should be using the ships height. In your tests, you use a varibale called shipHeight, however check its initialization. You actually make shipHeight equal to shipWidth. Change:

code:

shipHeight := Pic.Width (shipPic)


to

code:

shipHeight := Pic.Height (shipPic)


Little things like that can be really annoying and hard to find Surprised . Game looks good! Are you planning to make it so you have to avoid the rocks? That'd be better Razz !
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 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: