Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: gooner on July 08, 2008
-
Hi everybody our good friend Shockwave has set me a challenge of making a cube and giving me this code as a starting point
' DOUBLE BUFFERING!
' SMOOTH MOVEMENT IN FREEBASIC WITHOUT HAVING TO USE "SLEEP"
'
' THIS PROGRAM SHOWS HOW TO FIX FLICKERING GRAPHICS.
'
'-------------------------------------------------------------------------------
SCREEN 19,,2,
X1=0:' X POINT 1
Y1=10:' Y POINT 1
X2=100:' X POINT 2
Y2=100:' YPOINT 2
XF1=1:' X MOVEMENT FLAG FOR POINT 1
XF2=1:' X MOVEMENT FLAG FOR POINT 2
YF1=0:' Y MOVEMENT FLAG FOR POINT 1
YF2=1:' Y MOVEMENT FLAG FOR POINT 2
SCREENSET 1,0:' SET WORKPAGE 1, VISIBLE PAGE 0 (DONT WORRY!)
DO
' DRAW A LINE
LINE(X1,Y1)-(X2,Y2)
'-------------------------------------------------------------------------------
' MOVE POINT 1
'-------------------------------------------------------------------------------
IF XF1=1 THEN
X1=X1+1
IF X1>800 THEN XF1=0
END IF
IF XF1=0 THEN
X1=X1-1
IF X1<0 THEN XF1=1
END IF
IF YF1=1 THEN
Y1=Y1+1
IF Y1>600 THEN YF1=0
END IF
IF YF1=0 THEN
Y1=Y1-1
IF Y1<0 THEN YF1=1
END IF
'-------------------------------------------------------------------------------
' MOVE POINT 2
'-------------------------------------------------------------------------------
IF XF2=1 THEN
X2=X2+1
IF X2>800 THEN XF2=0
END IF
IF XF2=0 THEN
X2=X2-1
IF X2<0 THEN XF2=1
END IF
IF YF2=1 THEN
Y2=Y2+2
IF Y2>600 THEN YF2=0
END IF
IF YF2=0 THEN
Y2=Y2-1
IF Y2<0 THEN YF2=1
END IF
SCREENCOPY
'-------------------------------------------------------------------------------
'
'-------------------------------------------------------------------------------
SLEEP 2
CLS
LOOP UNTIL INKEY$<>""
So to be able to do this i really need to understand what each part of the code is doing which i can't quite get my head around at the moment.So i would appreciate any help to explain to me what is happening to help me solve this.I could quite easily just ring Nick up and ask him but this would defeat the object of this exercise and more importantly any help you give might assist any other novices reading this.Thanks Wayne :-[
-
this code seems just to draw a line from <x1,y1> to <x2,y2>
the code increments coordinates and checks if the coordinates (x1y1 and x2 y2) reach the borders of the screen/window. if a coordinate hits a border of the screen, the increments are changed into decrements and the coordinate goes then in the opposite direction.
With this code you should have a line that bounces when the vertices of the line hit the screen borders.. that's it.
I'm not a FB coder so I cannot give you more info are there is not enough code to help you any further ;)
-
perhaps you will see better what is happening if I were to rewrite the code like this
' DOUBLE BUFFERING!
' SMOOTH MOVEMENT IN FREEBASIC WITHOUT HAVING TO USE "SLEEP"
'
' THIS PROGRAM SHOWS HOW TO FIX FLICKERING GRAPHICS.
'
'-------------------------------------------------------------------------------
' OPEN A SCREEN 800 PIXELS WIDE 600 PIXELS HIGH
SCREEN 19,,2,
' DECLARE VARIABLES THAT WILL BE USED (REQUIRED BY MY VERSION OF FB NOTHING TO WORRY ABOUT)
dim as integer X1,Y1,X2,Y2,XF1,XF2,YF1,YF2, RIGHTEDGE,LEFTEDGE,TOPEDGE,BOTTOMEDGE
LEFTEDGE=0
RIGHTEDGE=800
TOPEDGE=0
BOTTOMEDGE=600
X1=0:' X POINT 1
Y1=10:' Y POINT 1
X2=100:' X POINT 2
Y2=100:' YPOINT 2
XF1=1:' X MOVEMENT FLAG FOR POINT 1
XF2=1:' X MOVEMENT FLAG FOR POINT 2
' YF1=0:' Y MOVEMENT FLAG FOR POINT 1
YF1=-1:' Y MOVEMENT FLAG FOR POINT 1
YF2=-1:' Y MOVEMENT FLAG FOR POINT 2
SCREENSET 1,0:' SET WORKPAGE 1, VISIBLE PAGE 0 (DONT WORRY!)
DO
' DRAW A LINE
LINE(X1,Y1)-(X2,Y2)
'-------------------------------------------------------------------------------
' MOVE POINT 1
'-------------------------------------------------------------------------------
X1 = X1 + XF1 ' MOVE POINT 1 LEFT AND RIGHT
IF (X1>=RIGHTEDGE) OR (X1<=LEFTEDGE) THEN
XF1 = -XF1' REVERSE MOVEMENT WHEN WE REACH LEFT OR RIGHT
END IF
Y1 = Y1 + YF1 ' MOVE POINT 1 UP AND DOWN
IF (Y1>=BOTTOMEDGE) OR (Y1<=TOPEDGE) THEN
YF1 = -YF1 ' REVERSE MOVEMENT WHEN WE TOP OR BOTTOM
END IF
'-------------------------------------------------------------------------------
' MOVE POINT 2
'-------------------------------------------------------------------------------
X2 = X2 + XF2 ' MOVE POINT 1 LEFT AND RIGHT
IF (X2>=RIGHTEDGE) OR (X2<=LEFTEDGE) THEN
XF2 = -XF2' REVERSE MOVEMENT WHEN WE REACH LEFT OR RIGHT
END IF
Y2 = Y2 + YF2 ' MOVE POINT 1 UP AND DOWN
IF (Y2>=BOTTOMEDGE) OR (Y2<=TOPEDGE) THEN
YF2 = -YF2 ' REVERSE MOVEMENT WHEN WE TOP OR BOTTOM
END IF
SCREENCOPY ' UPDATE SCREEN WITH NEW IMAGE
'-------------------------------------------------------------------------------
'
'-------------------------------------------------------------------------------
SLEEP 2 ' WAIT FOR A SHORT TIME FOR SCREEN UPDATE TO COMPLETE THIS AVOIDS FLICKER
CLS ' CLEAR THE SCREEN SO THAT WE CAN DRAW THE NEXT IMAGE
LOOP UNTIL INKEY$<>"" ' ETERNAL LOOP UNTIL USER PUSHES ANY KEY
As stormbringer mentioned this code draws a line between X1,Y1 and X2,Y2. these variables are co-ordinates within the screen. the movement is cotrolled by the variables XF1,YF1,XF2,YF2. the values kept in the movement variables is added to X1,Y1,X2,Y2 respectively. a test is perfomed to see if either point has reached the edge of the sceen. and if so the movement values are reversed. this means that instead of continuing to move right past the edge instead the point will begin to move left until it reaches the left edge of the screen then the movement values are reversed yet again and the whole process starts repeats
do you understand what is happening here?
SCREENCOPY
SLEEP 2
CLS
LOOP UNTIL INKEY$<>""
the screen copy updates the screen with the new image (the line that you drew from X1,Y1 to X2,Y2)
sleep waits for a short moment to allow the screen to update with the new image
cls clears the screen so that when we draw the next line the old line will be erased
just for fun try commenting out cls so that it reads like this -
'CLS
the result is intersting. Is there anything that is unclear?
-
@ Stormbringer Thanks for that Stormbringer that certainly helped.
@Rainstorm Thanks for that mate.Your code looks like its gonna be a great help.I will
have a play with it when i get home from work and let you know how i get on.
:cheers:
-
Btw, don't worry about dim as integer etc at the moment, the challenge wasn't to make a cube though mate!
It was to make a square
Clue you need 4 corners :)
-
Ok Shockie that makes it a bit easier then.Better change the name of the topic then.
:-[
-
Cubes are trickier :)
I'd be impressed with a square for now.
-
Finally the penny dropped while driving the van today.Thanks to Stormbringer,Rainstorm and Shockwave for your help and giving me the determination to succeed.K+ to all three of you.
:cheers:
-
@gooner:
Karma up for you, mate! That's awesome. Very well done.
I am impressed - looks cool!
-
Welldone Gooner :)
-
Karmic boost there for you Gooner!
It's a cool looking effect too :) Well done, I am really really pleased for you, considering that this time last week you had never programmed anything before it's even more special.
-
Another one bites the dust
Well done man :cheers:
-
That's actualy really cool, especialy when it hits a coner. I love how you've managed to make something funky even though you restricted yourself to only having one square.
-
Here's his next task...
He needs to do this.. SHould give him something to think about in work tomorrow :)
-
Nice work Gooner!
@Shockwave: is the circle supposed to move?
Also how do you give karma?
-
@Naptha: Theres an applaud button under karma.
-
@Naptha: Theres an applaud button under karma.
Hmm, do you need to have karma to give? :-[
-
Yor level is ZX81 I think you must be C64 to give karma. These levels are tied to your post count. Well its the thought that counts
-
Oh well, virtual karma for you Gooner. :D
-
I will give him karma for you :cheers:
-
Thanks everbody for the nice comments.I've already started work on the next task hopefully complete it tomorrow?
cheers wayne ;)
-
Welldone Gooner....Keep it going :)
-
Cool, new coder on the scene. Welcome, and nice job so far.