Code : Tout sélectionner
;
; labyrinthe 2D par Polux en purebasic, puis modifié par beauregard afin d'avoir un autre regard.
If InitMouse ()=0 Or InitSprite ()=0 Or InitKeyboard ()=0 Or InitSprite3D ()=0
MessageRequester ( "Error" , "Can't open DirectX 7 or later" , 0)
End
EndIf
Enumeration ; ici, on donne un petit nom à nos sprites( non, chuis pas un numéro !):
#fondA
EndEnumeration
; Si votre PC ne peut pas afficher une haute résoution résolution en 32bit, Alors arrêt du programme.
If OpenScreen (1024, 768, 32, "labyrinthe 2D" )=0
MessageRequester ( "Error" , "Can't open a 1024*768 - 32 bit screen !" , 0)
End
EndIf
CreateSprite(0,64,64) ; creation d'un sprite bidon pour le mur
StartDrawing(SpriteOutput(0))
Box(0,0,64,64,RGB(255,255,255))
Box(2,2,60,60,RGB(225,225,225))
StopDrawing()
CreateSprite(1,64,64) ; creation de la bille
StartDrawing(SpriteOutput(1))
BackColor ( RGB (0,0,0))
Circle(32,32,30,RGB(255,255,255))
Box (20+4,0, 4*4, 4, RGB (158,5,5))
Box (12+4,4, 4*8, 4, RGB (162,15,15))
Box (20+4,8, 4*4, 4, RGB (164,21,21)):Box (8+4,8, 4, 4, RGB (164,21,21)):Box (44+4,8, 4, 4, RGB (164,21,21))
Box (24+4,12, 4*2, 4, RGB (168,32,32)):Box (4+4,12, 4, 4, RGB (168,32,32)):Box (48+4,12, 4, 4, RGB (168,32,32))
Box (24+4,16, 4*2, 4, RGB (172,42,42)):Box (4+4,16, 4, 4, RGB (172,42,42)):Box (48+4,16, 4, 4, RGB (172,42,42))
Box (20+4,20, 4*4, 4, RGB (176,53,53)):Box (4+4,20, 4*2, 4, RGB (176,53,53)):Box (44+4,20, 4*2, 4, RGB (176,53,53))
Box (0,24, 64, 4, RGB (179,61,61))
Box (0,28, 64, 4, RGB (185,77,77))
Box (0,32, 64, 4, RGB (190,89,89))
Box (0,36, 64, 4, RGB (195,102,102))
Box (0,40, 64, 4, RGB (200,115,115))
Box (0,44, 64, 4, RGB (204,127,127))
Box (0,48, 64, 4, RGB (204,127,127))
Box (0,52, 64, 4, RGB (204,127,127))
Box (0,48+8, 4*4, 4, RGB (209,140,140)):Box (24,48+8, 4*4, 4, RGB (209,140,140)):Box (48,48+8, 4*4, 4, RGB (209,140,140))
Box (8/2,52+8, 4*2, 4, RGB (214,152,152)):Box (28,52+8, 4*2, 4, RGB (214,152,152)):Box (52,52+8, 4*2, 4, RGB (214,152,152))
StopDrawing ()
Global temp1.w,temp2.w,temp3.w,temp4.w
Structure objet
id.l
x.w
Y.w
sensx.l
sensy.l
sr.w
attract.w
EndStructure
Global NewList bfond.objet(); bloc fond
Declare Fclonebfond()
Global colonne,dcolonne,dligne
x=8*64:Y=6*64
Global zutA,zutB.w,zutC.w ; utile pour le codeur pour tester le code.
Repeat ; ************ Boucle principale ***************************************************************************************************************************
ClearScreen ( RGB (0,0,155))
ExamineKeyboard()
; fabrication du fond ( sous forme de bloc):
If nbrefond<1:nbrefond+1:Fclonebfond():EndIf
;- Affichage clone fondbloc si existe:
ForEach bfond()
zutB+1
DisplayTransparentSprite( bfond()\id , bfond()\x, bfond()\Y)
If x+64>bfond()\x-1 And x+64<bfond()\x+16; collision à droite.
If (Y+8>bfond()\Y+4 And Y+8<bfond()\Y+60) Or (Y+32>bfond()\Y+4 And Y+32<bfond()\Y+60) Or (Y+56>bfond()\Y+4 And Y+56<bfond()\Y+60)
temp1=1:x=bfond()\x-65
EndIf
EndIf
If x>bfond()\x+48 And x<bfond()\x+65; collision à gauche.
If (Y+8>bfond()\Y+4 And Y+8<bfond()\Y+60) Or (Y+32>bfond()\Y+4 And Y+32<bfond()\Y+60) Or (Y+56>bfond()\Y+4 And Y+56<bfond()\Y+60)
temp2=1:x=bfond()\x+65
EndIf
EndIf
If (x+8>bfond()\x+4 And x+8<bfond()\x+60) Or (x+32>bfond()\x+4 And x+32<bfond()\x+60) Or (x+56>bfond()\x+4 And x+56<bfond()\x+60)
If Y>bfond()\Y+48 And Y<bfond()\Y+65; collision à haut.
temp3=1:Y=bfond()\Y+65
EndIf
EndIf
If (x+8>bfond()\x+4 And x+8<bfond()\x+60) Or (x+32>bfond()\x+4 And x+32<bfond()\x+60) Or (x+56>bfond()\x+4 And x+56<bfond()\x+60)
If Y+64>bfond()\Y-1 And Y+64<bfond()\Y+16; collision en bas
temp4=1:Y=bfond()\Y-65
EndIf
EndIf
Next
;- objet dirigé par le joueur:
DisplayTransparentSprite(1,x,Y)
If KeyboardPushed(#PB_Key_Right) And temp1=0:x+4:EndIf
If KeyboardPushed(#PB_Key_Left) And temp2=0:x-4:EndIf
If KeyboardPushed(#PB_Key_Up) And temp3=0:Y-4:EndIf
If KeyboardPushed(#PB_Key_Down) And temp4=0:Y+4:EndIf
; ligne de test afin de connaître la valeur des variables:
StartDrawing ( ScreenOutput ())
DrawingMode (1)
FrontColor ( RGB (255,0,0))
zutA$= Str (zutA)
zutB$= Str (zutB)
zutC$= Str (zutC)
nbrefond$= Str (nbrefond)
temp1$= Str (temp1)
temp2$= Str (temp2)
temp3$= Str (temp3)
temp4$= Str (temp4)
DrawText (0,1+100, "temp1: " +temp1$)
DrawText (0,1+120, "temp2: " +temp2$)
DrawText (0,1+140, "temp3: " +temp3$)
DrawText (0,1+160, "temp4: " +temp4$)
;DrawText (50,1+220, "nbrefond: " +nbrefond$)
;DrawText (0,1+200, "zutA: " +zutA$);:zutA=0
;DrawText (0,1+220, "zutB: " +zutB$):zutB=0
;DrawText (0,1+240, "zutC: " +zutC$);:zutC=0
; DrawText (350,20, "appuyez sur la touche R pour remise à 0 du code")
StopDrawing ()
temp1=0:temp2=0:temp3=0:temp4=0 ; remise à 0 indispensable des variables utilisées pour le déplacement.
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Procedure Fclonebfond()
; placement clone fond sous forme de simple bloc:
; 16 colonne de tiles ( 16*64=1024) pour 12 ligne de tiles ( 12*64=768).
For t=0 To 15+(16*11)
If t=16 Or t=32 Or t=48 Or t=64 Or t=80 Or t=96 Or t=112 Or t=128 Or t=144 Or t=160 Or t=176 Or t=192
dligne+64:dcolonne=0
EndIf
Read colonne
If colonne=1
LastElement(bfond())
AddElement(bfond())
bfond()\id=#fondA
bfond()\x=dcolonne
bfond()\Y=dligne
EndIf
dcolonne+64
Next
EndProcedure
DataSection ; section data pour le tableau
Data.l 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Data.l 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
Data.l 1,0,1,1,1,1,0,0,1,1,1,0,1,1,1,1
Data.l 1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1
Data.l 1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1
Data.l 1,0,1,0,0,1,0,1,1,1,0,0,0,0,0,1
Data.l 1,0,0,0,0,0,0,1,0,1,0,0,1,1,1,1
Data.l 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1
Data.l 1,0,1,1,1,1,0,0,0,0,0,1,0,0,0,1
Data.l 1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1
Data.l 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1
Data.l 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
EndDataSection