The challenge is simple. Make one game object circle another in a perfect orbit.
Does B2D code also count? Me know none B4GL :$
' Orbiting panets demo by u9 May 2007 ' u9 4T kallnet D0T fo Option explicit Randomize class Object Public x Public y Private size Public parent Private orbitSpeed Private orbitRadius Private color ' set parent to false if no parent Public Sub initialize( newX, newY, newSize, newParent ) If isObject( newParent ) Then x = newParent.x + newX y = newParent.y + newY Set parent = newParent Else x = newX y = newY parent = newParent End If size = newSize orbitSpeed = 0 orbitRadius = 0 ' A random color color = argb( 150 + rnd * 105, rnd * 255, rnd * 255, rnd * 255 ) End Sub ' Set to 0 to stop orbit Public Sub setSpeed( newSpeed ) orbitSpeed = newSpeed End Sub ' Orbiting distance from parent Public Sub setOrbit( newOrbit ) orbitRadius = newOrbit End Sub Public Sub setPos( newX, newY ) x = newX y = newY End Sub Public Sub update() If orbitSpeed <> 0 Then x = parent.x + cos( system.gettime() / 1000 * orbitSpeed ) * orbitRadius y = parent.y - sin( system.gettime() / 1000 * orbitSpeed ) * orbitRadius End If End Sub Public Sub draw() graphics.setrect x - size / 2, y - size / 2, size, size, color graphics.setcircle x, y, size*3/5, color End Sub End class graphics.initialize 800, 600, True graphics.settitle "Square Following Planets in Perfect Orbit" key.initialize mouse.initialize mouse.hide Dim font font = graphics.createfont( "Courier New", 11 ) Dim player Set player = New Object Call player.initialize( mouse.x, mouse.y, 40, False ) Dim lastOrbiter Set lastOrbiter = player Dim buttonLeft, buttonRight buttonLeft = False buttonRight = False Dim numOrbiters numOrbiters = 0 Do ' Check mouse If mouse.leftButton Then buttonLeft = True ElseIf buttonLeft Then ' Mouse click so add orbiter numOrbiters = numOrbiters + 1 Dim tmp Set tmp = New Object Call tmp.initialize( 0, 0, 18 / numOrbiters, lastOrbiter ) tmp.setSpeed( numOrbiters * numOrbiters / 2 + rnd * 2 ) tmp.setOrbit( 150 / numOrbiters - rnd * 10 ) Set lastOrbiter = tmp Set tmp = nothing buttonLeft = False End If If mouse.rightButton Then buttonRight = True ElseIf buttonRight Then ' Mouse click so remove orbiter If isObject( lastOrbiter.parent ) Then Set lastOrbiter = lastOrbiter.parent numOrbiters = numOrbiters - 1 End If buttonRight = False End If ' Do orbit and render graphics.clear Call player.setPos( mouse.x, mouse.y ) Dim curOrbiter Set curOrbiter = lastOrbiter Do While isObject( curOrbiter ) Call curOrbiter.update() Call curOrbiter.draw() If isObject( curOrbiter.parent ) Then Set curOrbiter = curOrbiter.parent Else curOrbiter = curOrbiter.parent End If Loop graphics.settext "FPS: " & graphics.FPS, 10, 10, font, argb(255,255,255,255) graphics.settext "Number of planets and other orbiting stuff: " & numOrbiters + 1, 10, 24, font, argb(255,255,255,255) graphics.settext "Press mouse buttons to add or remove orbiting objects", 10, 38, font, argb(255,255,255,255) graphics.display system.processMessages Loop Until key.pressed( vk_escape ) Or key.pressed( vk_windowx ) mouse.show mouse.terminate key.terminate graphics.terminate
textMode(TEXT_BUFFERED) dim tex,tex2 dim spr,spr2 dim x,y x = 320 y = 240 dim x2,y2 dim degrees dim radius degrees = 0 radius = 100 tex = loadTexture("Data/asteroid.png") tex2 = LoadTexture("Data/Star.bmp") spr = NewSprite(tex) spr2 = NewSprite(tex2) SprSetPos(x,y) SprSetSize(100,100) do cls printr radius printr degrees BindSprite(spr) degrees = degrees + 2 x2 = x+(radius*cos(degrees*3.14159265/180)) y2 = y+(radius*sin(degrees*3.14159265/180)) SprSetPos(x2,y2) DrawText() loop
Save that in the Basic4GL/Programs folder and it will work. It uses the art that comes with Bagel.
That's cool. :) I've come up with rules for the challenges. Yes, of course, there is a badge involved. :) I'll post them later. I'm just waking up. —hartnell
Thank you. But you see, in my demo you can just press the left mouse button to add another orbiting square around the last added orbiting square so that should be one point per square you click :D I recommend clicking many times hehehe
(in theory there can be infinitly many, but due to brutus2d bug, only 22)
Well, Brutus2D wants to crash if i try to draw a circle with radius 0, and i didn't want to check for this. As you might have noticed the planets get smaller and smaller :)
I told guilect about it but he's opinion was that one should make sure one does not call the function with radius 0. Negative values funnily enough work hehe…
@Woad: I see that for the Sin and Cos bits you had to convert from radians to degrees. Well, that's what SinD and CosD are for. :)
And for the record, I wouldn't have known how to do this. :P I only know some implementations of trigonometry, I haven't learnt it at school yet.