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

001: ; philippe - Toulouse - 2013 - purebasic 4.61 x86
002: ; sprite orient� : d'un point A � un point B
003: 
004: If InitMouse ()=0 Or InitKeyboard ()=0 Or InitSprite ()=0  Or InitSprite3D ()=0
005:      MessageRequester ( "Error" , "Can't open DirectX 7 or later" , 0)
006:      End
007: EndIf
008: If InitSound()=0 Or InitMovie()=0
009:   MessageRequester("Error", "Can't open DirectX 7 Or Sound Card is not present", 0)
010:   End
011: EndIf
012: 
013: UsePNGImageDecoder()
014: 
015: ; ������������������������������������������������������
016: ;LoadFont ( 0 , "Comic Sans Ms" ,16, #PB_Font_Bold)
017: ;LoadFont ( 1 , "Comic Sans Ms" ,30, #PB_Font_Bold)
018: Enumeration ; ici, on donne un petit nom � nos sprites( non, chuis pas un num�ro !):
019: 
020: #raquette
021: #tird
022:        
023: EndEnumeration
024: 
025: 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 )
026:  OpenWindowedScreen ( WindowID ( 0 ), 0, 0, 1024, 768, 0, 1, 1) ; un ecran graphique dans la fen�tre.
027: 
028:   SetWindowColor(0, RGB (236,216,234))  
029: EndIf
030: KeyboardMode(#PB_Keyboard_International)
031: 
032:  CreateSprite ( #raquette , 64, 16, 0 )
033:  StartDrawing ( SpriteOutput ( #raquette )) ;- image pour la raquette
034:   BackColor ( RGB (0,0,0))
035:   Box (0,0, 64, 16, RGB (255, 255, 255))
036:  StopDrawing ()
037:  
038:  CreateSprite ( #tird , 32, 32, 0 )
039:  StartDrawing ( SpriteOutput ( #tird )) ;- image pour le tir
040:   BackColor ( RGB (0,0,0))
041:   Circle(16, 16, 16 , RGB (255, 0, 0))
042:  StopDrawing ()
043:  
044: Structure objet ;- variables pour les clones (liste chain�es).
045: 
046: objetetat.w
047: 
048: objetx.f
049: objety.f
050: objetxs.f
051: objetys.f
052: objetangle.f
053: 
054: objetspeed.w
055: 
056: EndStructure
057: Global NewList trait.objet() ; -> #raquette
058: Global NewList tir.objet() ; -> #tird  
059: 
060: 
061: Global etape.b, tempoA.b, nbrtir.w
062: 
063: 
064: Procedure.f GetAngle(gx,gy, tx,ty) ; g comme go( d�part de l'action) et t comme target( l'objet cible � toucher).
065:   a.f = tx-gx
066:   b.f = ty-gy
067:   c.f = Sqr(a*a+b*b)
068:   angle.f = -ACos(a/c)*57.29577
069: 
070:   If ty < gy : angle=360-angle : EndIf
071:   If ty > gy : angle=360+angle : EndIf
072: 
073:   ProcedureReturn angle+90
074: EndProcedure
075: 
076: ; ******************************************************************************************************
077: Repeat ; Boucle principale
078: ; ******************************************************************************************************
079:  Event= WindowEvent ()
080:  Start3D() ; on place cette commande d�s le d�but de la boucle principale.
081: 
082:  ExamineMouse()
083:  ExamineKeyboard ()
084:  
085: If etape=0 ;- fabrication de la raquette
086:   LastElement(trait())
087:   AddElement(trait())
088:   trait()\objetetat=1
089:   trait()\objetx=512
090:   trait()\objety=640
091: 
092:   etape=1
093: EndIf
094: 
095: If etape=1
096:  
097:   ForEach trait()
098:     If trait()\objetetat=1;- raquette
099: 
100:       DisplayTransparentSprite ( #raquette , trait()\objetx, trait()\objety)
101:      
102:       If KeyboardPushed ( #PB_Key_Up ) And trait()\objety>10:trait()\objety-5:EndIf ; haut.
103:       If KeyboardPushed ( #PB_Key_Down ) And trait()\objety<732:trait()\objety+5:EndIf ; bas.    
104: 
105:       If KeyboardPushed ( #PB_Key_Right ) And trait()\objetx<932:trait()\objetx+5:EndIf ; droite.  
106:       If KeyboardPushed ( #PB_Key_Left ) And trait()\objetx>0:trait()\objetx-5:EndIf ; gauche.        
107:     EndIf    
108:   Next  
109: 
110: 
111: 
112: ;- fabrication tir *************
113:   If KeyboardPushed(#PB_Key_X)=0 And tempoA=2:tempoA=0:EndIf
114:   If KeyboardPushed(#PB_Key_X) And tempoA=0:tempoA=1:EndIf
115:   If tempoA=1:tempoA=2
116:      
117:     LastElement(tir())
118:     AddElement(tir())
119:     tir()\objetetat=1
120:     tir()\objetx=512
121:     tir()\objety=384    
122:     tir()\objetspeed=16 ;- vitesse des boulettes
123: 
124:     ForEach trait()
125:       If trait()\objetetat=1;- raquette( ici la cible, car nous avons besoin de connaitre ses coordonn�es X et Y)
126:         ; pense b�te: GetAngle( bouletteX,bouletteY, cibleX,cibleY)
127:        
128:         tir()\objetangle = GetAngle(tir()\objetx,tir()\objety, trait()\objetx+16,trait()\objety)
129:        
130:         ; milieu de la raquette(taille:64*16) -> 64/2=32 puis 32-16( car disque rouge 16) et donc trait()\objetx+16
131:       EndIf
132:     Next
133:      
134:     tir()\objetxs = Sin(tir()\objetangle*(3.14/180))*tir()\objetspeed
135:     tir()\objetys = Cos(tir()\objetangle*(3.14/180))*tir()\objetspeed
136: 
137:   EndIf
138: ; fin fabrication tir **********
139: 
140: 
141: 
142: nbrtir=0  
143:   ForEach tir()
144:     If tir()\objetetat=1;- clone tir
145:       nbrtir+1 ; connaitre le nbre de tirs
146:      
147:       DisplayTransparentSprite ( #tird , tir()\objetx, tir()\objety)
148:      
149:       tir()\objetx=tir()\objetx+tir()\objetxs
150:       tir()\objety=tir()\objety+tir()\objetys
151: 
152:      
153:       If tir()\objetx>1024 Or tir()\objetx<0 Or tir()\objety>700 Or tir()\objety<0
154:         DeleteElement(tir())
155:         Continue
156:       EndIf
157:      
158:     EndIf    
159:   Next  
160:    
161: EndIf
162:  
163:  
164:   StartDrawing ( ScreenOutput ())
165:     DrawingMode (1)
166:       ;DrawingFont ( FontID (0))
167:       FrontColor ( RGB (255,255,255))
168:      rezut1$= Str (rezut1)
169:      rezut2$= Str (rezut2)
170:      
171:      FrontColor ( RGB (154,154,154)) :DrawText (1+1,220+1, "rezut1: " +rezut1$)
172:      FrontColor ( RGB (234,234,234)) :DrawText (1,220, "rezut1: " +rezut1$)
173:      FrontColor ( RGB (154,154,154)) :DrawText (1+1,240+1, "rezut2: " +rezut2$)
174:      FrontColor ( RGB (234,234,234)) :DrawText (1,240, "rezut2: " +rezut2$)
175: 
176:       FrontColor ( RGB (154,154,154)) :DrawText (350+1,20+1, "push X")
177:       FrontColor ( RGB (234,234,234)) :DrawText (350,20, "push X")
178:  
179:       nbrtir$= Str (nbrtir)
180:       FrontColor ( RGB (164,21,21)) :DrawText (42+1, 4+1, "  " +nbrtir$)
181:       FrontColor ( RGB (253,201,255)) :DrawText (42, 4, "  " +nbrtir$)
182:  
183:   StopDrawing ()
184:  
185:  
186:  
187:   Stop3D(); on place cette commande � la fin de la boucle principale.
188:  FlipBuffers()
189:  ClearScreen(RGB(0, 42, 255))
190: ; ******************************************************************************************************  
191:  If Event= #PB_Event_CloseWindow Or KeyboardPushed ( #PB_Key_Escape ):End:EndIf; si clique coin haut droit ou touche Esc, alors ferme fen�tre.
192: ForEver
193: 
194: 
195: 
196: 
197: 
198: 
199: 
200: 
201: ; IDE Options = PureBasic 4.61 (Windows - x86)
202: ; CursorPosition = 2
203: ; FirstLine = 96
204: ; Folding = -