Array bug
Hi,
I found a bug in Small Basic arrays, and wrote a program to demonstrate it. In this program, I create thirty rectangles on top of each other (at the same x and y values) and make them go in the same direction at the same speed. Whenever they hit a wall, they bounce off it going the same speed they were going when they hit the wall. What should happen is that all the user ever sees is one rectangle bouncing off the walls and going at a constant speed. However, what ends up happening on my computer is that even before they hit any walls the rectangles are sort of spread out, and as time goes on, they get more and more spread out. Certain squares even teleport to other parts of the screen for no reason. In addition, the whole thing is choppy, moving in jumps and pauses.
I hope this helps.
Thanks,
Lizziedude_num = 30 'number of "dudes" for dude = 0 to dude_num-1 dude_x[dude] = 5 dude_y[dude] = 5 'Set the dude_x and dude_y for all dudes to the same location 'Since none of these are random, all the dudes are being set to the same x and y location and thus should be on top of each other dude_dx[dude] = 5 dude_dy[dude] = 5 'Set the direction and speed for all dudes to be the same 'again, since none of these are random, all dudes should move at the same speed. dude_sprite[dude] = Shapes.AddRectangle(40,40) 'create the dudes endfor Sub draw For dude = 0 to dude_num-1 Shapes.Move(dude_sprite[dude],dude_x[dude],dude_y[dude]) 'move all the dudes to a new x and y endfor endsub Sub ai For dude = 0 to dude_num-1 dude_x[dude] = dude_x[dude] + dude_dx[dude] dude_y[dude] = dude_y[dude] + dude_dy[dude] 'Update the x and y (dude_x[dude]) of the dudes by their speed (dude_dx[dude]) If dude_x[dude] < 0 then dude_x[dude] = 0 dude_dx[dude] = 5 'if the dudes run into the left wall, make them bounce back. 'Note that since the dudes should all be on top of each other to begin with, and each time they hit a wall, their speed is exactly reversed, the user should never see more than one dude on the screen. endif If dude_x[dude] > GraphicsWindow.width-40 then dude_x[dude] = graphicswindow.width-40 dude_dx[dude] = -5 'if the dudes hit the right wall, bounce them back endif If dude_y[dude] < 0 then dude_y[dude] = 0 dude_dy[dude] = 5 'if the dudes hit the top wall, bounce them back endif If dude_y[dude] > GraphicsWindow.height-40 then dude_y[dude] = GraphicsWindow.height-40 dude_dy[dude] = -5 'if the dudes hit the bottom wall, bounce them back endif endfor endsub timer.interval = 1 Timer.tick = onTick 'Calls onTick, which runs though the program once every millisecond. Sub onTick draw() ai() endsub
Answers
- here you go, use this (this solved it for me):
While "True"
draw()
ai()
endwhile
instead of:
timer.interval = 1
Timer.tick = onTick
'Calls onTick, which runs though the program once every millisecond.
Sub onTick
draw()
ai()
endsub
i dont know if this answered your question, but its worth a try^^
but i dont get it why youre using on tick??
Live for nothing, OR CODE FOR SOMETHING!- Proposed As Answer byDudeson Sunday, September 13, 2009 12:19 PM
- Marked As Answer bylitdevAnswererSunday, September 13, 2009 9:30 PM
All Replies
- LOL! Dudes! XDD ill look trough it as soon as i can!
btw, why are you using "for dude = 0 to ..." why 0 and not 1?
Live for nothing, OR CODE FOR SOMETHING! - here you go, use this (this solved it for me):
While "True"
draw()
ai()
endwhile
instead of:
timer.interval = 1
Timer.tick = onTick
'Calls onTick, which runs though the program once every millisecond.
Sub onTick
draw()
ai()
endsub
i dont know if this answered your question, but its worth a try^^
but i dont get it why youre using on tick??
Live for nothing, OR CODE FOR SOMETHING!- Proposed As Answer byDudeson Sunday, September 13, 2009 12:19 PM
- Marked As Answer bylitdevAnswererSunday, September 13, 2009 9:30 PM
- cauz computers like zeros.
Habit, I guess. It's like making my functions start with lowercase letters and all the other words with capital letters.
Dudes rock! - Hey, thanks! I never knew you could use while "true". What exactly does that mean? Will "true" ever become false? Or is it just a nonending loop until you hit the little red x?
I'm using on tick because then the program always runs the same speed no matter what computer you are on: one cycle per millisecond. I remember this old game (but not its name) where you had a bunch of cars racing along, and the goal was to finish first, though you could also buy all these guns and things to shoot the other players down, and even these little mini cars called terminators that would drive under an enemy car and blow up. As we got nicer and faster computers, though, the game became impossibly fast, so its just nicer to make sure everything is happening at the exact same time.
So anyways, I used your little bit of code and it seemed to pretty much fix the problem, although it lost the benefits of going the same speed on all computers anywhere, but then it gave me an idea.
Really, why my program was getting so screwed up is that my computer isn't fast enough. It would be in the middle of updating the positions of all my "dudes" when the millisecond timer would go off and "BING" it would have to leave and go off to start again. So I created a variable to tell if it was ready to move on again. It's slow, but hey! What do you expect when you are trying to move 30 dudes around on the screen?
doneUpdating = "True" timer.interval = 1 Timer.tick = onTick 'Calls onTick, which runs though the program once every millisecond. Sub onTick If doneUpdating = "True" then doneUpdating = "False" draw() ai() doneUpdating = "True" endif endsub - NP! (btw, yes dudes rule^^ thats why im dudeson^^) nope, "True" never becomes false. you can also choose "while X = 5" so it would loop as long as X is 5. you can also code multiple of those.
im sorry, but thats not very right. it doesnt run at the same speed on every computer because: the computer has to calculate the time too right? so if the game or program slows down the computer that much, the time also runs slow. you know what i mean? but if you have a fast computer, it doesnt run very fast, it just runs at a speed of 1 cycle per millisecond. but it runs slow on slow computers.
"although it lost the benefits of going the same speed on all computers anywhere, but then it gave me an idea." nope, its just like i said before. BUT you can use program.delay(X) instead. that should do the trick. but small basic is ____ for making stuff run at the same speed anyway.
"Really, why my program was getting so screwed up is that my computer isn't fast enough." wrong, my computer is very fast but it also had those bugs. Thats because small basic is so ____ slow.
"It would be in the middle of updating the positions of all my "dudes" when the millisecond timer would go off and "BING" it would have to leave and go off to start again." thats right!
"It's slow, but hey! What do you expect when you are trying to move 30 dudes around on the screen?" that those bitches are getting moved around like they should XD idk if you meant that corresponding to small basic, but moving around that stuff should actually use about not even 1 % of the performance of a modern pc! (try other programming languages like C++ and stuff. there stuff runs like it should^^ but sb is for beginners anyway...) i mean, look at crysis for example. if you dont know it, here a screenshot: http://static.computergames.ro/cg/assassin/images2/crysis/crysis068.jpg
this is possible with a pc! and it uses all the performance of the pc, unlike sb... i mean, most of the games today have a physics engine. but thats no big deal for a modern pc. so dont worry, moving 30 dudes is nothing compared to what a pc can do^^
yes, your code solved it. but i would rather use While and a delay. that would optimize the whole thing IMO...
if you still have questions, or i am talking complete ____, tell me!
HAVE FUN!
Live for nothing, OR CODE FOR SOMETHING!- Proposed As Answer byDudeson Wednesday, September 09, 2009 4:27 PM
- hang on... how can that be? Isn't a millisecond a constant, like one second or one hour? I mean... my computer's clock always tells me the time the same as the faster computer downstairs. It's like a measurment of time, and doesn't that mean it should be the same on every computer?
- First of all the timer cant handle 1ms. The timer resolution starts working at somethin like 15/16ms. If I try your program the speed doesnt change, even when I change the tick to 14ms.
Doing a proper delay in Small Basic is a bit of work. You should have a look at the asteroids game featured on the blog at http://smallbasic.com/program/?ASTEROIDS .
- as i said, faster computers tell the same time as slow computers. slow ones also, but i mean related to sb. the computer doesnt really get slowed down when a sb program runs like s**t. its just the program itself, wich runs slow. this means, the timer doesnt check the system time, its built in in small basic. ok?
Live for nothing, OR CODE FOR SOMETHING! hang on... how can that be? Isn't a millisecond a constant, like one second or one hour? I mean... my computer's clock always tells me the time the same as the faster computer downstairs. It's like a measurment of time, and doesn't that mean it should be the same on every computer?
solved?
Live for nothing, OR CODE FOR SOMETHING!- Yeah. Thanks. I didn't know that. I just assumed that it got the time from the motherboard but i guess i was wrong :D
- thats what i also tought.. ____ eh?^^
Live for nothing, OR CODE FOR SOMETHING! - might my fellow companion mark this thread as solved?^^
Live for nothing, OR CODE FOR SOMETHING! - Oh yes :D
Thanks for helping out. I really apreciate it. Oh yes :D
np man! (im a noob also^^)
Thanks for helping out. I really apreciate it.
Live for nothing, OR CODE FOR SOMETHING!I'm using on tick because then the program always runs the same speed no matter what computer you are on: one cycle per millisecond.
hey! if you still need to make something run on always the same speed, then check this out:
http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/374f4d84-cafc-45d3-b67f-6b77e0b977a9
i made some really awesome but simple speed limitation, wich works wonderful!
check it out!
Live for nothing, OR CODE FOR SOMETHING!


