Computer Science Canada help wit random placement of astroids |
Author: | vagyb [ 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? |
Author: | SuperGenius [ Mon May 24, 2004 1:14 pm ] |
Post 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 |
Author: | vagyb [ Mon May 24, 2004 1:20 pm ] |
Post 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? |
Author: | guruguru [ Mon May 24, 2004 1:39 pm ] | ||||
Post 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:
to...
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). |
Author: | SuperGenius [ Mon May 24, 2004 1:41 pm ] | ||
Post 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
is called the var 'num' is assigned a new value within the specified range. |
Author: | vagyb [ Mon May 24, 2004 1:45 pm ] | ||
Post 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
|
Author: | guruguru [ Mon May 24, 2004 1:51 pm ] |
Post 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 ! And I'm not going to do the rest of the variables for you because I don't have that much spare time . |
Author: | vagyb [ Mon May 24, 2004 1:54 pm ] |
Post subject: | |
lol alrite i think i got it now, thx alot |
Author: | vagyb [ Mon May 24, 2004 2:00 pm ] | ||
Post subject: | |||
k i thnk i did wat u said, but problem is the rocks r still flashing, here is the code
is this wat u meant i should do? |
Author: | vagyb [ Mon May 24, 2004 2:08 pm ] |
Post subject: | |
omg wow i think i just solved the problem, ignore what i rote above. wow i'm so happy lol |
Author: | guruguru [ Mon May 24, 2004 2:13 pm ] |
Post subject: | |
Umm... no that's not at all what I said ! 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 ! EDIT: Glad you solved it! Ask away for other problems ! |
Author: | vagyb [ Mon May 24, 2004 2:49 pm ] | ||
Post 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
|
Author: | guruguru [ Mon May 24, 2004 3:20 pm ] |
Post subject: | |
Please post boulder4Pic picture. |
Author: | vagyb [ Mon May 24, 2004 3:39 pm ] |
Post subject: | |
oops my bad lol its the same as all except name is boulder4, but here it is |
Author: | guruguru [ Mon May 24, 2004 4:30 pm ] | ||||
Post 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:
to
Little things like that can be really annoying and hard to find . Game looks good! Are you planning to make it so you have to avoid the rocks? That'd be better ! |
Author: | vagyb [ Mon May 24, 2004 4:59 pm ] |
Post subject: | |
yep i am lol, once i'm done this part i'm gonna use whatdotcolor to do the detection hehe |
Author: | guruguru [ Mon May 24, 2004 6:23 pm ] |
Post subject: | |
Good idea! Just some ideas to make your code shorter and more comprehensive. 1. Take out all the number1 etc variables, you dont need them. 2. Since all your boulder pictures are the same, you dont have to make 4 picture variables. Just make one. The width and height will stay the same. However, you have to keep all the variables that determeine the position of a boulder. Then when you draw the boulders, just refer to the single picture. E.g. Pic.Draw (boulderPic, boulder1/2/3/4X, boulder1/2/3/4Y, picMerge). 3. setscreen() should always be called at the very top of the program only. You have an extra setscreen() on line 26 for some odd reason. 4. This is hard: Take out every and any reference to space, spacePic, spacHeight etc. Delete all those variables. It makes the program way too messy. Yes, you will have to change around some things. But the space variables are absoluely unncesary in this program. You do not need them. Period. 5. Idea: Convert all the info for your boulders into arrays. This will shorten your program, make it easier to program, and make it easier to read. If you want to, try these things out. If you need any help with any of thsese, feel free to ask away. |