Code : Tout sélectionner
#Window = 0
#Width = 300
#Height = 300
Enumeration
#brique
#balle
EndEnumeration
Global xbrique,ybrique,xpas,ypas,xb,yb,flag
dep = 1
Procedure Sprites()
; Procedure de création des sprites
If CreateSprite(#brique,128,24,0) ; Sprite Fixe
StartDrawing(SpriteOutput(#brique))
Box(0,0,128,24,RGB($FF,$0,$0))
StopDrawing()
EndIf
If CreateSprite(#balle,20,20,0) ; Sprite Mobile
StartDrawing(SpriteOutput(#balle))
Circle(10,10,10,RGB($0,$0,$FF))
StopDrawing()
EndIf
EndProcedure
;- Initialisation de DirectX
If InitSprite() = 0 Or InitKeyboard() = 0
End
EndIf
;- Ouverture de la fenêtre et de l'écran
hwnd = OpenWindow(#Window, 0, 0, #Width, #Height, #PB_Window_TitleBar | #PB_Window_ScreenCentered, "")
OpenWindowedScreen(hwnd, 0, 0, #Width, #Height, 0, 0, 0)
Sprites()
;- Initialisation des sprites
xbrique = 86 : ybrique = 138
xb= 0 : yb= 0
xpas = 5 : ypas = 5
;- Boucle des évènements
Repeat
ClearScreen(0, 0, 0) : ExamineKeyboard()
; Affichage des sprites
DisplayTransparentSprite(#brique, xbrique, ybrique)
DisplayTransparentSprite(#balle, xb, yb)
FlipBuffers()
xb=xb + xpas
yb=yb + ypas
If SpritePixelCollision(#balle, xb, yb, #brique, xbrique-5, ybrique)<>0 And flag=0
flag=1
SetWindowTitle(#Window,"Collision à gauche")
xpas=-xpas
Goto su ; sort du test !!!
EndIf
If SpritePixelCollision(#balle, xb, yb, #brique, xbrique+5, ybrique)<>0 And flag=0
flag=1
SetWindowTitle(#Window,"Collision à droite")
xpas=-xpas
Goto su ; sort du test !!!
EndIf
If SpritePixelCollision(#balle, xb, yb, #brique, xbrique, ybrique-5)<>0 And flag=0
flag=1
SetWindowTitle(#Window,"Collision au dessus")
ypas=-ypas
Goto su ; sort du test !!!
EndIf
If SpritePixelCollision(#balle, xb, yb, #brique, xbrique, ybrique+5)<>0 And flag=0
flag=1
SetWindowTitle(#Window,"Collision au dessous")
ypas=-ypas
Goto su ; sort du test !!!
EndIf
su:
If flag=1
attente+1
EndIf
If attente>4
attente=0
flag=0
EndIf
;Delay(2)
If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf
If KeyboardPushed(#PB_Key_Left) : xbrique - dep : EndIf
If KeyboardPushed(#PB_Key_Right) : xbrique + dep : EndIf
If KeyboardPushed(#PB_Key_Up) : ybrique - dep : EndIf
If KeyboardPushed(#PB_Key_Down) : ybrique + dep : EndIf
If xb <=0 : xpas= -xpas : EndIf
If yb <=0 : ypas= -ypas : EndIf
If xb >=280 : xpas= -xpas : EndIf
If yb >=280 : ypas= -ypas : EndIf
While WindowEvent() : Wend
Until quit = 1