File: tiroriente1a.pb - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

; philippe - Toulouse - 2013 - purebasic 4.61 x86
; sprite orient� : d'un point A ï¿½ un point B 

If InitMouse ()=0 Or InitKeyboard ()=0 Or InitSprite ()=0  Or InitSprite3D ()=0 
     MessageRequester ( "Error" , "Can't open DirectX 7 or later" , 0)
     End
EndIf
If InitSound()=0 Or InitMovie()=0
  MessageRequester("Error", "Can't open DirectX 7 Or Sound Card is not present", 0)
  End
EndIf

UsePNGImageDecoder()

; ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½
;LoadFont ( 0 , "Comic Sans Ms" ,16, #PB_Font_Bold)
;LoadFont ( 1 , "Comic Sans Ms" ,30, #PB_Font_Bold)
Enumeration ; ici, on donne un petit nom ï¿½ nos sprites( non, chuis pas un num�ro !):

#raquette
#tird
       
EndEnumeration

If OpenWindow ( 0 , 0, 0, 1024, 768, "tir orient� 2" , #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar| #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_ScreenCentered ) 
 OpenWindowedScreen ( WindowID ( 0 ), 0, 0, 1024, 768, 0, 1, 1) ; un ecran graphique dans la fen�tre.

  SetWindowColor(0, RGB (236,216,234))  
EndIf
KeyboardMode(#PB_Keyboard_International)

 CreateSprite ( #raquette , 64, 16, 0 ) 
 StartDrawing ( SpriteOutput ( #raquette )) ;- image pour la raquette
  BackColor ( RGB (0,0,0))
  Box (0,0, 64, 16, RGB (255, 255, 255))
 StopDrawing ()
 
 CreateSprite ( #tird , 32, 32, 0 ) 
 StartDrawing ( SpriteOutput ( #tird )) ;- image pour le tir
  BackColor ( RGB (0,0,0))
  Circle(16, 16, 16 , RGB (255, 0, 0)) 
 StopDrawing () 
 
Structure objet ;- variables pour les clones (liste chain�es).

objetetat.w

objetx.f 
objety.f 
objetxs.f 
objetys.f 
objetangle.f

objetspeed.w

EndStructure
Global NewList trait.objet() ; -> #raquette 
Global NewList tir.objet() ; -> #tird  


Global etape.b, tempoA.b, nbrtir.w


Procedure.f GetAngle(gx,gy, tx,ty) ; g comme go( d�part de l'action) et t comme target( l'objet cible ï¿½ toucher). 
  a.f = tx-gx
  b.f = ty-gy
  c.f = Sqr(a*a+b*b) 
  angle.f = -ACos(a/c)*57.29577 

  If ty < gy : angle=360-angle : EndIf 
  If ty > gy : angle=360+angle : EndIf

  ProcedureReturn angle+90 
EndProcedure 

; ******************************************************************************************************
Repeat ; Boucle principale
; ******************************************************************************************************
 Event= WindowEvent ()
 Start3D() ; on place cette commande d�s le d�but de la boucle principale.

 ExamineMouse()
 ExamineKeyboard ()
 
If etape=0 ;- fabrication de la raquette
  LastElement(trait())
  AddElement(trait())
  trait()\objetetat=1
  trait()\objetx=512
  trait()\objety=640 

  etape=1
EndIf

If etape=1 
  
  ForEach trait()
    If trait()\objetetat=1;- raquette

      DisplayTransparentSprite ( #raquette , trait()\objetx, trait()\objety)
      
      If KeyboardPushed ( #PB_Key_Up ) And trait()\objety>10:trait()\objety-5:EndIf ; haut. 
      If KeyboardPushed ( #PB_Key_Down ) And trait()\objety<732:trait()\objety+5:EndIf ; bas.     

      If KeyboardPushed ( #PB_Key_Right ) And trait()\objetx<932:trait()\objetx+5:EndIf ; droite.   
      If KeyboardPushed ( #PB_Key_Left ) And trait()\objetx>0:trait()\objetx-5:EndIf ; gauche.        
    EndIf     
  Next  



;- fabrication tir *************
  If KeyboardPushed(#PB_Key_X)=0 And tempoA=2:tempoA=0:EndIf
  If KeyboardPushed(#PB_Key_X) And tempoA=0:tempoA=1:EndIf
  If tempoA=1:tempoA=2 
      
    LastElement(tir())
    AddElement(tir())
    tir()\objetetat=1
    tir()\objetx=512 
    tir()\objety=384    
    tir()\objetspeed=16 ;- vitesse des boulettes

    ForEach trait()
      If trait()\objetetat=1;- raquette( ici la cible, car nous avons besoin de connaitre ses coordonn�es X et Y)
        ; pense b�te: GetAngle( bouletteX,bouletteY, cibleX,cibleY)
        
        tir()\objetangle = GetAngle(tir()\objetx,tir()\objety, trait()\objetx+16,trait()\objety)
        
        ; milieu de la raquette(taille:64*16) -> 64/2=32 puis 32-16( car disque rouge 16) et donc trait()\objetx+16
      EndIf
    Next
      
    tir()\objetxs = Sin(tir()\objetangle*(3.14/180))*tir()\objetspeed
    tir()\objetys = Cos(tir()\objetangle*(3.14/180))*tir()\objetspeed 

  EndIf
; fin fabrication tir **********



nbrtir=0  
  ForEach tir()
    If tir()\objetetat=1;- clone tir
      nbrtir+1 ; connaitre le nbre de tirs
      
      DisplayTransparentSprite ( #tird , tir()\objetx, tir()\objety)
      
      tir()\objetx=tir()\objetx+tir()\objetxs
      tir()\objety=tir()\objety+tir()\objetys 

      
      If tir()\objetx>1024 Or tir()\objetx<0 Or tir()\objety>700 Or tir()\objety<0
        DeleteElement(tir()) 
        Continue
      EndIf
      
    EndIf     
  Next   
   
EndIf 
 
 
  StartDrawing ( ScreenOutput ())
    DrawingMode (1)
      ;DrawingFont ( FontID (0)) 
      FrontColor ( RGB (255,255,255))
     rezut1$= Str (rezut1)
     rezut2$= Str (rezut2)
     
     FrontColor ( RGB (154,154,154)) :DrawText (1+1,220+1, "rezut1: " +rezut1$)
     FrontColor ( RGB (234,234,234)) :DrawText (1,220, "rezut1: " +rezut1$) 
     FrontColor ( RGB (154,154,154)) :DrawText (1+1,240+1, "rezut2: " +rezut2$)
     FrontColor ( RGB (234,234,234)) :DrawText (1,240, "rezut2: " +rezut2$) 

      FrontColor ( RGB (154,154,154)) :DrawText (350+1,20+1, "push X")
      FrontColor ( RGB (234,234,234)) :DrawText (350,20, "push X")
 
      nbrtir$= Str (nbrtir) 
      FrontColor ( RGB (164,21,21)) :DrawText (42+1, 4+1, "  " +nbrtir$)
      FrontColor ( RGB (253,201,255)) :DrawText (42, 4, "  " +nbrtir$) 
 
  StopDrawing () 
 
 
 
  Stop3D(); on place cette commande ï¿½ la fin de la boucle principale.
 FlipBuffers()
 ClearScreen(RGB(0, 42, 255)) 
; ******************************************************************************************************   
 If Event= #PB_Event_CloseWindow Or KeyboardPushed ( #PB_Key_Escape ):End:EndIf; si clique coin haut droit ou touche Esc, alors ferme fen�tre.
ForEver








; IDE Options = PureBasic 4.61 (Windows - x86)
; CursorPosition = 2
; FirstLine = 96
; Folding = -