Code : Tout sélectionner
;snake créé par Kayne - http://tripamort.free.fr
;constantes
Enumeration
#tete
#corps
#queue
#serpent
EndEnumeration
;initialisation des composants
InitSprite()
InitKeyboard()
InitSound()
InitSprite3D()
Structure donnees_serpent
x.w
y.w
dx.b
dy.b
spr_colonne.w
spr_ligne.w
EndStructure
Structure decision_serpent
x.w
y.w
dx.b
dy.b
EndStructure
Dim serpent.donnees_serpent(40)
NewList decision.decision_serpent()
;variable (par defaut au debut)
tete.donnees_serpent
;tête
tete_dl=4
tete\x=320
tete\y=320
tete\dx=4
tete\dy=0
tete_spr_colonne=0
tete_spr_ligne=0
;corps
taille_serpent=10
For i=0 To taille_serpent
serpent(i)\x=tete\x-16*taille_serpent+16*i+corps_decalage_x
serpent(i)\y=320+corps_decalage_y
serpent(i)\dx=tete_dl
serpent(i)\dy=0
serpent(i)\spr_colonne=0
serpent(i)\spr_ligne=32
Next i
corps_decalage_x=-32
corps_decalage_y=0
sens.s="droite"
Macro affiche_serpent()
For i=0 To taille_serpent
ClipSprite(#serpent,serpent(i)\spr_colonne,serpent(i)\spr_ligne,32,32)
DisplayTransparentSprite(#serpent,serpent(i)\x+corps_decalage_x,serpent(i)\y+corps_decalage_y)
Next i
ClipSprite(#serpent,tete_spr_colonne,tete_spr_ligne,32,32)
DisplayTransparentSprite(#serpent,tete\x,tete\y)
EndMacro
Macro gestion_deplacement()
;déplacement
If tete\x<>0 And tete\y<>0
;prise de décision pour aller à droite
;{
If sens="droite"
If tete\dx<>tete_dl
tete\dx=tete_dl
tete\dy=0
tete_spr_colonne=0
tete_spr_ligne=0
corps_decalage_x=-32
corps_decalage_y=0
AddElement(decision())
decision()\x=tete\x
decision()\y=tete\y
decision()\dx=tete\dx
decision()\dy=tete\dy
EndIf
EndIf
;}
;prise de décision pour aller à gauche
;{
If sens="gauche"
If tete\dx<>-tete_dl
tete\dx=-tete_dl
tete\dy=0
tete_spr_colonne=64
tete_spr_ligne=0
corps_decalage_x=32
corps_decalage_y=0
AddElement(decision())
decision()\x=tete\x
decision()\y=tete\y
decision()\dx=tete\dx
decision()\dy=tete\dy
EndIf
EndIf
;}
;prise de décision pour aller en bas
;{
If sens="bas"
If tete\dy<>tete_dl
tete\dx=0
tete\dy=tete_dl
tete_spr_colonne=32
tete_spr_ligne=0
corps_decalage_x=0
corps_decalage_y=-32
AddElement(decision())
decision()\x=tete\x
decision()\y=tete\y
decision()\dx=tete\dx
decision()\dy=tete\dy
EndIf
EndIf
;}
;prise de décision pour aller en haut
;{
If sens="haut"
If tete\dy<>-tete_dl
tete\dx=0
tete\dy=-tete_dl
tete_spr_colonne=96
tete_spr_ligne=0
corps_decalage_x=0
corps_decalage_y=0
AddElement(decision())
decision()\x=tete\x
decision()\y=tete\y
decision()\dx=tete\dx
decision()\dy=tete\dy
EndIf
EndIf
;}
EndIf
EndMacro
Macro controle_serpent()
If KeyboardReleased(#PB_Key_Up )
sens="haut"
EndIf
If KeyboardReleased(#PB_Key_Down )
sens="bas"
EndIf
If KeyboardReleased(#PB_Key_Left )
sens="gauche"
EndIf
If KeyboardReleased(#PB_Key_Right )
sens="droite"
EndIf
EndMacro
;ouverture de l'ecran on commence le jeu
OpenScreen(1024,768,32,"Snake - Kayne - http://tripamort.free.fr")
;chargement des sprites // creation des sprites 3D
;planche d'images
LoadSprite(#serpent,"sprites/serpent.bmp")
Repeat
FlipBuffers()
ClearScreen(RGB(0,0,0))
ExamineKeyboard()
controle_serpent()
gestion_deplacement()
;deplacement du corps
For i=0 To taille_serpent
ForEach decision()
If serpent(i)\x=decision()\x And serpent(i)\y=decision()\y
serpent(i)\dx=decision()\dx
serpent(i)\dy=decision()\dy
EndIf
Next
Next i
;mise en mouvemennt
If ElapsedMilliseconds()-chrono>50
chrono=ElapsedMilliseconds()
tete\x=tete\x+tete\dx
tete\y=tete\y+tete\dy
If cible_atteinte=0
For i=0 To taille_serpent
serpent(i)\x=serpent(i)\x+serpent(i)\dx
serpent(i)\y=serpent(i)\y+serpent(i)\dy
Next i
EndIf
EndIf
affiche_serpent()
Until KeyboardPushed(#PB_Key_Escape )
