Page 1 of 1

Testing moving a circle

Posted: Sun Oct 15, 2017 2:42 am
by ricardo
I never do anything related to games before. I want to move the circle but something went wrong !

See and cry

Code: Select all

#ScreenWidth  = 800  ; Feel free to change this to see the pixel filling speed !
#ScreenHeight = 600

If InitSprite() = 0 Or InitKeyboard()=0
  MessageRequester("Error","DirectX 7+ is needed.",0)
EndIf




If OpenScreen(#ScreenWidth, #ScreenHeight, 32, "")
  
  Repeat
    
    wave+6
    x+2
    If wave > 320 : wave = 0 : EndIf
    
    If StartDrawing(ScreenOutput())
          Circle(x, 100, 30, RGBA(100, 255, 100, 10))
      StopDrawing()
    EndIf
    
    ExamineKeyboard()
    
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape)
  
Else
  MessageRequester("Error","Can't open the screen !",0)
EndIf

End

Any help are apprecited.
Also i do not know how to use delay or something similar. I want to move the circle according to specific times AND also move it to the position where i put the mouse and give a click.
Also, alpha do nothing :P

Can anybody can give me a simple example please?

Thanks in advance !! :)

Re: Testing moving a circle

Posted: Sun Oct 15, 2017 4:49 am
by Lunasole
Just missed ClearScreen() ^^

Code: Select all

#ScreenWidth  = 800  ; Feel free to change this to see the pixel filling speed !
#ScreenHeight = 600

If InitSprite() = 0 Or InitKeyboard()=0
  MessageRequester("Error","DirectX 7+ is needed.",0)
EndIf



If OpenScreen(#ScreenWidth, #ScreenHeight, 32, "")
  
  Repeat
    ; epilectic attack
    ClearScreen(RGB(Random(255), Random(255), Random(255)))
    
    wave+6
    x+2
    If wave > 320 : wave = 0 : EndIf
    
    If StartDrawing(ScreenOutput())
          Circle(x, 100, 30, RGBA(100, 255, 100, 10))
      StopDrawing()
    EndIf
    
    ExamineKeyboard()
    
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape)
  
Else
  MessageRequester("Error","Can't open the screen !",0)
EndIf

End

Re: Testing moving a circle

Posted: Sun Oct 15, 2017 8:30 am
by Fig
; Feel free to change this to see the pixel filling speed ! <== ???
Not sure what you want to achieve here... But this is my understanding of what you asked...

Code: Select all

EnableExplicit
#ScreenWidth  = 800 
#ScreenHeight = 600
#grey=$3C3c3c
Define x.i=0
Define y.i=100
Define timer.d=0
Define time_gap.d=200 ;every 200ms change circle coordonates
If InitSprite() = 0 Or InitKeyboard()=0 Or InitMouse()=0:MessageRequester("Error","DirectX 7+ is needed.",0):end:EndIf
If OpenScreen(#ScreenWidth, #ScreenHeight, 32, "")=0:MessageRequester("Error","Can't open the screen !",0):End:EndIf

Repeat
   FlipBuffers()
   ClearScreen(#grey)
   ExamineKeyboard()
   ExamineMouse()
   ;on left clic change x,y of the circle
   If MouseButton(#PB_MouseButton_Left):x=MouseX():y=MouseY():EndIf
   
   ;every 200ms increment circle x coordonate by 2
   If timer<ElapsedMilliseconds():x+2:timer=ElapsedMilliseconds()+time_gap:EndIf
   
   If StartDrawing(ScreenOutput())
      Circle(x, y, 30, RGBA(100, 255, 100, 10))
      Circle(MouseX(),MouseY(),2,RGBA(255,0,0,0)) ;display mouse position with a little red dot
      StopDrawing()
   EndIf

Until KeyboardPushed(#PB_Key_Escape)
End

Re: Testing moving a circle

Posted: Sun Oct 15, 2017 7:47 pm
by ricardo
Fig wrote:
; Feel free to change this to see the pixel filling speed ! <== ???
Not sure what you want to achieve here... But this is my understanding of what you asked...

Code: Select all

EnableExplicit
#ScreenWidth  = 800 
#ScreenHeight = 600
#grey=$3C3c3c
Define x.i=0
Define y.i=100
Define timer.d=0
Define time_gap.d=200 ;every 200ms change circle coordonates
If InitSprite() = 0 Or InitKeyboard()=0 Or InitMouse()=0:MessageRequester("Error","DirectX 7+ is needed.",0):end:EndIf
If OpenScreen(#ScreenWidth, #ScreenHeight, 32, "")=0:MessageRequester("Error","Can't open the screen !",0):End:EndIf

Repeat
   FlipBuffers()
   ClearScreen(#grey)
   ExamineKeyboard()
   ExamineMouse()
   ;on left clic change x,y of the circle
   If MouseButton(#PB_MouseButton_Left):x=MouseX():y=MouseY():EndIf
   
   ;every 200ms increment circle x coordonate by 2
   If timer<ElapsedMilliseconds():x+2:timer=ElapsedMilliseconds()+time_gap:EndIf
   
   If StartDrawing(ScreenOutput())
      Circle(x, y, 30, RGBA(100, 255, 100, 10))
      Circle(MouseX(),MouseY(),2,RGBA(255,0,0,0)) ;display mouse position with a little red dot
      StopDrawing()
   EndIf

Until KeyboardPushed(#PB_Key_Escape)
End
Hi,

Thanks for your explanation and examples !!

One additional question. The moving circle is not nice. Is there a way to draw it with a best definition, etc or should i need to use a bitmap?

In case i need a bitmap, how to add it to this examples?

Thanks

Re: Testing moving a circle

Posted: Sun Oct 15, 2017 8:06 pm
by ricardo
If displaying a PNG with alpha, how to achieve it?

Or if drawing with RGBA how to get reults, in the example i don't see any result with RGBA

Re: Testing moving a circle

Posted: Sun Oct 15, 2017 8:16 pm
by Mijikai
ricardo wrote:If displaying a PNG with alpha, how to achieve it?
U can use Sprites -> Helpfile "2D Games"

Code: Select all

;UsePNGImageDecoder()
;Sprite = CatchSprite(#PB_All,?BUFFER,#PB_Sprite_AlphaBlending)
;DisplayTransparentSprite(Sprite,X,Y)

Re: Testing moving a circle

Posted: Mon Oct 16, 2017 1:52 pm
by ricardo
Hi,

If i want to draw the circle with high quality? Because its not showing smooth, but with very noticeable square definition in the curves.

Re: Testing moving a circle

Posted: Mon Oct 16, 2017 3:38 pm
by Fig
I guess you are looking for antialiased circles. ̶P̶b̶ ̶d̶o̶e̶s̶n̶'̶t̶ ̶p̶r̶o̶v̶i̶d̶e̶ ̶t̶h̶a̶t̶ ̶k̶i̶n̶d̶ ̶o̶f̶ ̶c̶i̶r̶c̶l̶e̶ ̶b̶y̶ ̶d̶e̶f̶a̶u̶l̶t̶,̶ ̶s̶o̶r̶r̶y̶.̶
You can draw your own circle by using Bressenham algo but if you are a beginer, it's out of your reach for now.

If you want a better circle definition, increment your resolution to decent values. (800x600 is not enough)
1280x768 or even more if your screen can display it.

Code: Select all

EnableExplicit
#ScreenWidth  = 1280
#ScreenHeight = 768
#grey=$3C3c3c
Define x.i=0
Define y.i=100
Define timer.d=0
Define time_gap.d=200 ;every 200ms change circle coordonates
Define alpha.a=128    ;alpha chanel
Define inc_alpha.b=1
Define i.i,larg.i
If InitSprite() = 0 Or InitKeyboard()=0 Or InitMouse()=0:MessageRequester("Error","DirectX 7+ is needed.",0):End:EndIf
If OpenScreen(#ScreenWidth, #ScreenHeight, 32, "")=0:MessageRequester("Error","Can't open the screen !",0):End:EndIf
;create a new sprite BACKGROUND
CreateSprite(0,#ScreenWidth,#ScreenHeight)
StartDrawing(SpriteOutput(0))
For i=1 To 100
   larg=Random(100,40)
   Box(Random(#ScreenHeight),Random(#ScreenWidth),larg,larg,Random($ffffff))
Next i      
StopDrawing()
;create a new sprite CIRCLE
CreateSprite(1,64,64)
StartDrawing(SpriteOutput(1))
Circle(32,32,31,RGB(100,255,100))
StopDrawing()
;create a new sprite MOUSE
CreateSprite(2,16,16)
StartDrawing(SpriteOutput(2))
Circle(8,8,7,RGB(255,0,0))
StopDrawing()

Repeat
   FlipBuffers()
   ClearScreen(#grey)
   ExamineKeyboard()
   ExamineMouse()
   ;on left clic change x,y of the circle
   If MouseButton(#PB_MouseButton_Left):x=MouseX():y=MouseY():EndIf
   
   ;every 200ms increment circle x coordonate by 2
   If timer<ElapsedMilliseconds():x+2:timer=ElapsedMilliseconds()+time_gap:EndIf
   ;display background
   DisplaySprite(0,0,0)
   ;display sprite Circle with alphachanel
   DisplayTransparentSprite(1,x,y,Alpha)
   Alpha+inc_alpha
   If alpha=255 Or alpha=0:inc_alpha=-inc_alpha:EndIf
   ;display mouse
   DisplayTransparentSprite(2,MouseX(),MouseY())
Until KeyboardPushed(#PB_Key_Escape)
End

Re: Testing moving a circle

Posted: Mon Oct 16, 2017 4:17 pm
by davido
Wouldn't the VectorDrawing Library produce a satisfactory anti aliased circle?

Code: Select all

  If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 200)

    If StartVectorDrawing(CanvasVectorOutput(0))

      ; partial circle
      VectorSourceColor(RGBA(255, 0, 0, 255))
      AddPathCircle(100, 100, 75)
      FillPath()
      ; partial circle with lines to the center
      MovePathCursor(300, 100)
      AddPathCircle(300, 100, 75, 0, 235, #PB_Path_Connected)
      ClosePath()
      
      VectorSourceColor(RGBA(255, 0, 0, 255))
      StrokePath(10)
    
      StopVectorDrawing()
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf

Re: Testing moving a circle

Posted: Mon Oct 16, 2017 6:59 pm
by Fig
Nice :D

Re: Testing moving a circle

Posted: Thu Oct 19, 2017 4:56 pm
by ricardo
davido wrote:Wouldn't the VectorDrawing Library produce a satisfactory anti aliased circle?

Code: Select all

  If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 200)

    If StartVectorDrawing(CanvasVectorOutput(0))

      ; partial circle
      VectorSourceColor(RGBA(255, 0, 0, 255))
      AddPathCircle(100, 100, 75)
      FillPath()
      ; partial circle with lines to the center
      MovePathCursor(300, 100)
      AddPathCircle(300, 100, 75, 0, 235, #PB_Path_Connected)
      ClosePath()
      
      VectorSourceColor(RGBA(255, 0, 0, 255))
      StrokePath(10)
    
      StopVectorDrawing()
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf

But can be used in a game or animated 2D?

Re: Testing moving a circle

Posted: Thu Oct 19, 2017 6:07 pm
by davido
@ricardo ,
There have been some demos of 2d animation. Please take a look at the one below by eddy:
http://www.purebasic.fr/english/viewtop ... 02#p489402

I don't think that there is any hardware acceleration for the Vector Library so the animation may not be smooth enough for fast games.