SPH_Snake

Programmation avancée de jeux en PureBasic
Avatar de l’utilisateur
SPH
Messages : 4723
Inscription : mer. 09/nov./2005 9:53

SPH_Snake

Message par SPH »

Le tout debut de mon snake. Je vous poste le code pour ceux que ca interesse et je répondrais aux questions.

ps : actu, il se dirige, ne meurt pas en se mordant, plante si on sort de l'ecran, et aucune option n'est codé (mais ca va venir)

Code : Tout sélectionner

InitSprite() 
InitKeyboard() 

dw=1024
dh=768
dc=32

If OpenScreen(dw,dh,dc,"SPH_Snake")=0 
End 
EndIf 
StartDrawing(ScreenOutput()) 
MemVideo = DrawingBuffer() 
dbp=DrawingBufferPitch()
dbpf=DrawingBufferPixelFormat()
;1=  #PB_PixelFormat_8Bits       ; 1 bytes per pixel, palletized
;2=  #PB_PixelFormat_15Bits      ; 2 bytes per pixel 
;3=  #PB_PixelFormat_16Bits      ; 2 bytes per pixel
;4=  #PB_PixelFormat_24Bits_RGB  ; 3 bytes per pixel (RRGGBB)
;5=  #PB_PixelFormat_24Bits_BGR  ; 3 bytes per pixel (BBGGRR)
;6=  #PB_PixelFormat_32Bits_RGB  ; 4 bytes per pixel (RRGGBB)
;7=  #PB_PixelFormat_32Bits_BGR  ; 4 bytes per pixel (BBGGRR)
StopDrawing() 
;End

;Dim p(dh-1,dw-1) 
Dim p(dh-1,dbp/4-1) 

;Points blancs aux 4 coins
p(0,0)=RGB(255,255,255)
p(0,dw-1)=RGB(255,255,255)
p(dh-1,dw-1)=RGB(255,255,255)
p(dh-1,0)=RGB(255,255,255)
;Points de couleur en retrait
p(5,5)=RGB(255,0,255)
p(5,dw-6)=RGB(255,0,255)
p(dh-6,dw-6)=RGB(255,0,255)
p(dh-6,5)=RGB(255,0,255)

;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;; 
xx=dw/2
yy=dh/2
longueur=50
Dim sx(2000)
Dim sy(2000)
Dim sc(2000)
For i=0 To longueur
sx(i)=xx
sy(i)=yy
r=Random(100)+50
sc(i)=RGB(r,r,r) ; le corps du snake gris
Next
sc(0)=RGB(255,255,255) ; la tete du snake blanche
sens=0
xxx=0
yyy=0
vitesse=3
vitesse_actu=0

;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; GOOOOOO
Repeat 
ExamineKeyboard() 

If KeyboardPushed(#PB_Key_Up) And sens<>1
sens=1
xxx=0
yyy=-1
EndIf
If KeyboardPushed(#PB_Key_Left) And sens<>2
sens=2
xxx=-1
yyy=0
EndIf
If KeyboardPushed(#PB_Key_Down) And sens<>1
sens=1
xxx=0
yyy=1
EndIf
If KeyboardPushed(#PB_Key_Right) And sens<>2
sens=2
xxx=1
yyy=0
EndIf

vitesse_actu+1
If vitesse_actu=vitesse
vitesse_actu=0
If sens<>0 ; on bouge le snake
For i=longueur To 1 Step -1
sx(i)=sx(i-1)
sy(i)=sy(i-1)
Next
sx(0)+xxx
sy(0)+yyy
;If Random(30):End:EndIf
EndIf
EndIf

;affichage
StartDrawing(ScreenOutput()) 

MemVideo = DrawingBuffer() 
For u=0 To dh-1 
CopyMemory(@p(u,0), MemVideo+DrawingBufferPitch()*u,dw*4) 
Next 
;MemVideo = DrawingBuffer() 
;CopyMemory(@p(), MemVideo, dh*dbp) 

;;; on dessine le snake par dessus :
For i=longueur To 0 Step -1
Plot(sx(i),sy(i),sc(i))
Next
;;;
StopDrawing() 
FlipBuffers() 
;Delay(1) 

Until KeyboardPushed(#PB_Key_Escape) 
poshu
Messages : 1138
Inscription : sam. 31/juil./2004 22:32

Message par poshu »

Sans rapport direct: as tu continué (ou vas tu le faire) ta démo? Elle démarrait très bien et donnait envie de voir la suite.
Avatar de l’utilisateur
SPH
Messages : 4723
Inscription : mer. 09/nov./2005 9:53

Message par SPH »

Et voici mon snake avec une regle du jeu :
dans le mur = +40 de longueur
manger un malus = +15 de longueur
manger un bonus = -1 de longueur
but : avoir une longueur de snake de 1


Attentions aux etoiles et aux bluques blanches qui apparaissent.
Si l'on se mord, le morceau se transforme en pierre; donc attention

PS : c'est ce code que j'actualiserais regulierement

VERSION 3


Code : Tout sélectionner

InitSprite() 
InitKeyboard() 

dw=1024
dh=768
dc=32

If OpenScreen(dw,dh,dc,"SPH_Snake")=0 
End 
EndIf 
StartDrawing(ScreenOutput()) 
MemVideo = DrawingBuffer() 
dbp=DrawingBufferPitch()
dbpf=DrawingBufferPixelFormat()
;1=  #PB_PixelFormat_8Bits       ; 1 bytes per pixel, palletized
;2=  #PB_PixelFormat_15Bits      ; 2 bytes per pixel 
;3=  #PB_PixelFormat_16Bits      ; 2 bytes per pixel
;4=  #PB_PixelFormat_24Bits_RGB  ; 3 bytes per pixel (RRGGBB)
;5=  #PB_PixelFormat_24Bits_BGR  ; 3 bytes per pixel (BBGGRR)
;6=  #PB_PixelFormat_32Bits_RGB  ; 4 bytes per pixel (RRGGBB)
;7=  #PB_PixelFormat_32Bits_BGR  ; 4 bytes per pixel (BBGGRR)
StopDrawing() 
;End

;Dim p(dh-1,dw-1) 
Dim p(dh-1,dbp/4-1) 

; la regle du jeu sera :
snake_bonus=RGB(255,150,0)
snake_malus=RGB(150,0,200)
snake_mur=RGB(255,255,255)
For nb=1 To 10
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_malus
Next
Next
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_bonus
Next
Next
Next

For nb=1 To 100
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
p(lax,lay)=snake_mur
Next


; murs autour de l'ecran
For i=0 To dh-1
p(i,0)=snake_mur
p(i,1)=snake_mur
p(i,dw-1)=snake_mur
p(i,dw-2)=snake_mur
Next
For i=0 To dw-1
p(0,i)=snake_mur
p(1,i)=snake_mur
p(dh-1,i)=snake_mur
p(dh-2,i)=snake_mur
Next
;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;; 
xx=dw/2
yy=dh/2
longueur=60
lon_max=8000
Dim sx(lon_max)
Dim sy(lon_max)
Dim sc(lon_max)
For i=0 To longueur
sx(i)=xx
sy(i)=yy
r=Random(100)+50
sc(i)=RGB(r,r,r) ; le corps du snake gris
Next
sc(0)=RGB(255,255,255) ; la tete du snake blanche
sens=0
xxx=0
yyy=0
vitesse.f=10
vitesse_actu.f=0

;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; GOOOOOO
Repeat 
ExamineKeyboard() 

If KeyboardPushed(#PB_Key_Up) And sens<>1
sens=1
xxx=0
yyy=-1
Else
If KeyboardPushed(#PB_Key_Left) And sens<>2
sens=2
xxx=-1
yyy=0
Else
If KeyboardPushed(#PB_Key_Down) And sens<>1
sens=1
xxx=0
yyy=1
Else
If KeyboardPushed(#PB_Key_Right) And sens<>2
sens=2
xxx=1
yyy=0
EndIf
EndIf
EndIf
EndIf

;If KeyboardPushed(#PB_Key_LeftShift) And vitesse<5
;vitesse +0.1
;Else
;If KeyboardPushed(#PB_Key_LeftControl) And vitesse>1
;vitesse -0.1
;EndIf
;EndIf


vitesse_actu+vitesse
If vitesse_actu>=5
vitesse_actu=0
If sens<>0 ; on bouge le snake
For i=longueur To 1 Step -1
sx(i)=sx(i-1)
sy(i)=sy(i-1)
Next
sx(0)+xxx
sy(0)+yyy

For i=longueur To 1 Step -1
If sx(0)=sx(i) And sy(0)=sy(i)
For u=longueur To i Step -1
p(sy(u),sx(u))=snake_mur
Next
Goto fin
EndIf
Next
fin:



;;;;;;; bonus
If p(sy(0),sx(0))=snake_bonus
p(sy(0),sx(0))=0
longueur-1
longueur%lon_max
If longueur<=1
Debug ("gagné")
End
EndIf
sx(longueur)=sx(longueur-1)
sy(longueur)=sy(longueur-1)
r=Random(100)+50
sc(longueur)=RGB(r,r,r)
For nb=1 To 2
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_bonus
Next
Next
Next



EndIf

;;;;;;; malus
If p(sy(0),sx(0))=snake_malus
p(sy(0),sx(0))=0
For i=1 To 15
longueur+1
longueur%lon_max
sx(longueur)=sx(longueur-1)
sy(longueur)=sy(longueur-1)
r=Random(100)+50
sc(longueur)=RGB(r,r,r)
Next
For nb=1 To 20
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_malus
Next
Next
Next
EndIf

;;;;;;; mur
If p(sy(0),sx(0))=snake_mur
;p(sy(0),sx(0))=0
For i=1 To 40
longueur+1
longueur%lon_max
sx(longueur)=sx(longueur-1)
sy(longueur)=sy(longueur-1)
r=Random(100)+50
sc(longueur)=RGB(r,r,r)
Next
For nb=1 To 3
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_malus
Next
Next
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_mur
Next
Next
Next

; dans le mur; donc, on replace la tete autre part
sx(0)=Random(dw/2)+dw/4
sy(0)=Random(dh/2)+dh/4
EndIf
;;;;;;;
EndIf
EndIf

;effacage aleatoire d'un carré
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=0
Next
Next
If Random(20)=1
lax=3+Random(dh/3-3)*3
lay=3+Random(dw/3-3)*3
For u=0 To 2
For i=0 To 2
p(lax+i,lay+u)=snake_mur
Next
Next
EndIf

;affichage
StartDrawing(ScreenOutput()) 

MemVideo = DrawingBuffer() 
For u=0 To dh-1 
CopyMemory(@p(u,0), MemVideo+DrawingBufferPitch()*u,dw*4) 
Next 
;MemVideo = DrawingBuffer() 
;CopyMemory(@p(), MemVideo, dh*dbp) 

;;; on dessine le snake par dessus :
For i=longueur To 0 Step -1
Plot(sx(i),sy(i),sc(i))
Next
;;;
StopDrawing() 
FlipBuffers() 
;Delay(1) 

Until KeyboardPushed(#PB_Key_Escape) 
Répondre