newbie question

Just starting out? Need help? Post your questions and find answers here.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

newbie question

Post by t57042 »

Hello,

I am new to PB and I am trying out some stuff.
The code below displays a small block wich advances every second .
Meanwhile with the buttons the direction of the block can be changed.
Why does it take 2 seconds for the box to react and change direction??

Code: Select all

; ------------------------------------------------------------
;
;   sim3
; ------------------------------------------------------------
Global ydirection,xdirection
;;==================================================
Procedure timer(t)
StartTime = ElapsedMilliseconds()             
  Repeat                                  
    ElapsedTime = ElapsedMilliseconds()-StartTime 
    Event.l = WindowEvent()    
  
  If Event = #PB_Event_Gadget
    ; do the normal application stuff here...
    Gadget  = EventGadget()  

    Select Gadget
      Case 1
        SetGadgetText(4,"north")
        ydirection = -10
        xdirection=0
      Case 2
        SetGadgetText(4,"south")
        ydirection = 10
        xdirection=0
      Case 3
        SetGadgetText(4,"east")
        ydirection = 0
        xdirection=10   
      Case 5
        SetGadgetText(4,"west")
        ydirection = 0
        xdirection=-10     
    EndSelect
   EndIf
   If Event = #PB_Event_CloseWindow 
    End
   EndIf     
  Until ElapsedTime>t  
EndProcedure
;==================================================

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Can't open DirectX 7 or later", 0)
  End
EndIf

If OpenWindow(0,0,0,610,410,"Sim 3",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
  If CreateGadgetList(WindowID(0))
    ButtonGadget(1,540, 10,60,20,"north")
    ButtonGadget(2,540, 40,60,20,"south")
    ButtonGadget(3,540, 70,60,20,"east")
    ButtonGadget(5,540, 100,60,20,"west")
    TextGadget  (4,540,140,60,30,"")
  EndIf

  If OpenWindowedScreen(WindowID(0),5,5,520,400,0,0,0)
    CreateSprite(0,10,10)
    
    If StartDrawing(SpriteOutput(0))
      
      
      FrontColor(RGB(255,0,100))    
      Box(0,0,10,10)
      StopDrawing()
    EndIf
 
  Else
    MessageRequester("Error", "Can't open windowed screen!", 0)
    End
  EndIf

EndIf
y=200
xdirection = 10
ydirection = 0

Repeat
  
  
    ; do the sprite & screen stuff here...
    
    FlipBuffers()       
     
    ; Clear the screen and draw our sprites
    ClearScreen(RGB(0,0,0))  
    DisplaySprite(0, x, y)

    x + xdirection   
    y + ydirection  
    timer(1000)
  

ForEver

End
[edit by Rings, setting Codetags ]
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Post by eJan »

Edit - removed.
Last edited by eJan on Sun Mar 02, 2008 9:05 pm, edited 1 time in total.
hellhound66
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Feb 21, 2006 12:37 pm

Post by hellhound66 »

Removed.
Last edited by hellhound66 on Wed Mar 19, 2008 11:31 pm, edited 1 time in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

> Your main loop has got a wrong command order.

exactly.
to be more precise:

your old order:
1) FlipBuffers()
2) DisplaySprite(0, x, y)
3) x + xdirection : y + ydirection
4) timer(1000)

4) you change direction
1) then you show the sprite you have displayed one frame earlier that was displayed at the position calculated two frames earlier
2) then you display the position you have calculated one frame earlier
3 then you calculate the next position with the new direction-vars
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Post by t57042 »

Thanks very much guys. Next question.
As you will see in attached code there i a wide white band over the screen (made with BOX function).
I want to get an irregular shape there, how can I achieve this?
I want to be able to check wether the small boxes are within the white area, so I think the white area has to be one big sprite?

Richard
--------------------------------------------------------------------------

; ------------------------------------------------------------
;
; sim6
; ------------------------------------------------------------
Global ydirection,xdirection
scanrate=1000
;;==================================================
Procedure timer(t)
StartTime = ElapsedMilliseconds()
Repeat
ElapsedTime = ElapsedMilliseconds()-StartTime
Event.l = WindowEvent()

If Event = #PB_Event_Gadget
; do the normal application stuff here...
Gadget = EventGadget()

Select Gadget
Case 1
SetGadgetText(4,"north")
ydirection = -10
xdirection=0
Case 2
SetGadgetText(4,"south")
ydirection = 10
xdirection=0
Case 3
SetGadgetText(4,"east")
ydirection = 0
xdirection=10
Case 5
SetGadgetText(4,"west")
ydirection = 0
xdirection=-10
EndSelect
EndIf
If Event = #PB_Event_CloseWindow
End
EndIf
Until ElapsedTime>t
EndProcedure
;==================================================

If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Error", "Can't open DirectX 7 or later", 0)
End
EndIf

OpenWindow(0,0,0,610,410,"Alfama - ATC aptitude testing ",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_Maximize )

CreateGadgetList(WindowID(0))

ButtonGadget(1,840, 10,60,20,"north")
ButtonGadget(2,840, 40,60,20,"south")
ButtonGadget(3,840, 70,60,20,"east")
ButtonGadget(5,840, 100,60,20,"west")
TextGadget (4,840,140,60,30,"")


OpenWindowedScreen(WindowID(0),5,5,820,650,0,0,0)

CreateSprite(0,10,10)
CreateSprite(1,10,10)
CreateSprite(2,800,400)


StartDrawing(SpriteOutput(2))

FrontColor(RGB(243,240,220))
Box(10,0,900,400)
StopDrawing()

StartDrawing(SpriteOutput(0))
FrontColor(RGB(255,0,100))
Box(0,0,10,10)
StopDrawing()


StartDrawing(SpriteOutput(1))
FrontColor(RGB(100,100,100))
Box(0,0,10,10)
StopDrawing()



y=200
xdirection = 10
ydirection = 0

Repeat
timer(scanrate)
x + xdirection
y + ydirection

ClearScreen(RGB(0,0,0))
DisplaySprite(2, 0, 150)
DisplaySprite(0, x, y)
DisplaySprite(1, x, y+100)
FlipBuffers()
ForEver
End
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

edit the code-tags, please. it's not readable that way.
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Post by t57042 »

What do you mean by edit the code tags?
In my browser everything is readable.
Richard
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

please insert the code into [code] [/code]
(you did it in your first post, so why not this time?)
to make it shown in a box with correct tabs and all.
without the codebox it's a p.i.t.a. to read it...
oh... and have a nice day.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Kaeru Gaman wrote:you did it in your first post, so why not this time?
Rings wrote:[edit by Rings, setting Codetags ]
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

oops... so he probably doesn't know what I'm talking about... shame
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Post by t57042 »

Another try

Code: Select all

; ------------------------------------------------------------
;
;   sim6
; ------------------------------------------------------------
Global ydirection,xdirection
scanrate=1000
;;==================================================
Procedure timer(t)
    StartTime = ElapsedMilliseconds()             
    Repeat                                 
        ElapsedTime = ElapsedMilliseconds()-StartTime
        Event.l = WindowEvent()   
       
        If Event = #PB_Event_Gadget
            ; do the normal application stuff here...
            Gadget  = EventGadget()
           
            Select Gadget
                Case 1
                    SetGadgetText(4,"north")
                    ydirection = -10
                    xdirection=0
                Case 2
                    SetGadgetText(4,"south")
                    ydirection = 10
                    xdirection=0
                Case 3
                    SetGadgetText(4,"east")
                    ydirection = 0
                    xdirection=10   
                Case 5
                    SetGadgetText(4,"west")
                    ydirection = 0
                    xdirection=-10     
            EndSelect
        EndIf
        If Event = #PB_Event_CloseWindow
            End
        EndIf     
    Until ElapsedTime>t
EndProcedure
;==================================================

If InitSprite() = 0 Or InitKeyboard() = 0
    MessageRequester("Error", "Can't open DirectX 7 or later", 0)
    End
EndIf

OpenWindow(0,0,0,610,410,"Alfama - ATC aptitude testing ",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_Maximize      )
    
    CreateGadgetList(WindowID(0))
    
        ButtonGadget(1,840, 10,60,20,"north")
        ButtonGadget(2,840, 40,60,20,"south")
        ButtonGadget(3,840, 70,60,20,"east")
        ButtonGadget(5,840, 100,60,20,"west")
        TextGadget  (4,840,140,60,30,"")
    
   
    OpenWindowedScreen(WindowID(0),5,5,820,650,0,0,0)
    
        CreateSprite(0,10,10)
        CreateSprite(1,10,10)
        CreateSprite(2,800,400)
                
        StartDrawing(SpriteOutput(2)) 
            
            FrontColor(RGB(243,240,220))   
            Box(10,0,900,300)            
            StopDrawing()   
                
                            
        StartDrawing(SpriteOutput(0))       
            FrontColor(RGB(255,0,100))   
            Box(0,0,10,10)
            StopDrawing()
        
        
        StartDrawing(SpriteOutput(1))       
            FrontColor(RGB(100,100,100))   
            Box(0,0,10,10)
            StopDrawing()
        
            

y=200
xdirection = 10
ydirection = 0

Repeat      
    timer(scanrate)
    x + xdirection   
    y + ydirection      
    
    ClearScreen(RGB(0,0,0))
    DisplaySprite(2, 0, 150)
    DisplaySprite(0, x, y)
    DisplaySprite(1, x, y+100)    
    FlipBuffers()     
ForEver
End
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

you could use a sprite, yes, but you could also use a loop to draw a bunch of lines.
to check if the boxes are within the shape, you can test via coordinates.
thus, you have to keep the values for the shape in an array.
oh... and have a nice day.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

little demo:
your code, added two arrays for the area-limits and the check

Code: Select all

; ------------------------------------------------------------
;
;   sim6
; ------------------------------------------------------------
Global ydirection,xdirection
scanrate=1000
;;==================================================
Procedure timer(t)
    StartTime = ElapsedMilliseconds()             
    Repeat                                 
        ElapsedTime = ElapsedMilliseconds()-StartTime
        Event.l = WindowEvent()   
       
        If Event = #PB_Event_Gadget
            ; do the normal application stuff here...
            Gadget  = EventGadget()
           
            Select Gadget
                Case 1
                    SetGadgetText(4,"north")
                    ydirection = -10
                    xdirection=0
                Case 2
                    SetGadgetText(4,"south")
                    ydirection = 10
                    xdirection=0
                Case 3
                    SetGadgetText(4,"east")
                    ydirection = 0
                    xdirection=10   
                Case 5
                    SetGadgetText(4,"west")
                    ydirection = 0
                    xdirection=-10     
            EndSelect
        EndIf
        If Event = #PB_Event_CloseWindow
            End
        EndIf     
    Until ElapsedTime>t
EndProcedure
;==================================================

If InitSprite() = 0 Or InitKeyboard() = 0
    MessageRequester("Error", "Can't open DirectX 7 or later", 0)
    End
EndIf

OpenWindow(0,0,0,610,410,"Alfama - ATC aptitude testing ",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_Maximize      )
    
    CreateGadgetList(WindowID(0))
    
        ButtonGadget(1,840, 10,60,20,"north")
        ButtonGadget(2,840, 40,60,20,"south")
        ButtonGadget(3,840, 70,60,20,"east")
        ButtonGadget(5,840, 100,60,20,"west")
        TextGadget  (4,840,140,60,30,"")
    
   
    OpenWindowedScreen(WindowID(0),5,5,820,650,0,0,0)
    
        CreateSprite(0,10,10)
        CreateSprite(1,10,10)
        CreateSprite(2,800,400)

; ** presetting the values
#GraphShift = 150
Dim va(799)
Dim vb(799)
For n=0 To 799
  va(n) = 50+50*Sin(n/30)
  vb(n) = 340+60*Cos(n/40)
Next
        
        StartDrawing(SpriteOutput(2)) 
            
            FrontColor(RGB(243,240,220))   
          ; drawing the lines into the sprite
            For n=0 To 799
              LineXY(n,va(n),n,vb(n))
            Next
            StopDrawing()   
                
        StartDrawing(SpriteOutput(0))       
            FrontColor(RGB(255,0,100))   
            Box(0,0,10,10)
            StopDrawing()
        
        
        StartDrawing(SpriteOutput(1))       
            FrontColor(RGB(100,100,100))   
            Box(0,0,10,10)
            StopDrawing()
        
            

y=200
xdirection = 10
ydirection = 0

Repeat      
    timer(scanrate)
    x + xdirection   
    y + ydirection      
    
    ClearScreen(RGB(0,0,0))
    DisplaySprite(2, 0, #GraphShift)
    DisplaySprite(0, x-5, y-5) ; to display the sprite centered on the coordinates

; inbound testing
    testy = y - #GraphShift
    If testy > va(x) And testy < vb(x)
      Out$="IN BOUND"
    Else
      Out$="OUT BOUND"
    EndIf
    StartDrawing(ScreenOutput())
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText(10,10,Out$,$00FFFF)
    StopDrawing()

    FlipBuffers()     
ForEver
End
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Post by t57042 »

Thank you very much.

Richard
Post Reply