Look at this....

this is the code...
Code: Select all
;{- INITIALISE WHAT I NEED, reserve memory and load functions (for drawing, keyboard, sound), variables, constants
If InitSprite() = 0
MessageRequester("ERROR","InitSprite error",3)
EndIf
If InitMovie() = 0
MessageRequester("ERROR","Initmovie",3)
EndIf
If InitSound() = 0
MessageRequester("ERROR","InitSound",3)
EndIf
If InitKeyboard() = 0
MessageRequester("ERROR","Initkeyb",3)
EndIf
; CONSTANTS
#x = 80
#y = 200
#Width = 440
#Height = 380
#NUMBEROFGREENBOX = 10
If OpenWindow(1, #x, #y, #Width, #Height, #PB_Window_SystemMenu, "SNAKE") = 0
MessageRequester("ERROR","OpenWindow",3)
EndIf
If OpenWindowedScreen(WindowID(1),0,0,#Width,#Height,0,0,0) = 0
MessageRequester("ERROR","OpenWindowedScreen",3)
EndIf
; What is WindowID?
; WindowID = WindowID(1)
;
; Syntax
;
; WindowID = WindowID([#Window])
; Beschreibung
;
; Ermittelt die einmalige ID, welche das aktuelle Fenster im Betriebssystem (OS) identifiziert. Diese Funktion ist sehr nützlich, wenn eine andere Library einen Bezug zu einem Fenster benötigt. eine optionales '#Window' kann angegeben werden.
; If OpenScreen(1024,768,32,"SNAKE") = 0
; MessageRequester("ERROR","OpenScreen",3)
; EndIf
LoadSprite(0,"DOT.BMP",0)
For x = 1 To #NUMBEROFGREENBOX ; creates a AMOUNT of greenboxes sprites everyone with his own sprite number
LoadSprite(x,"GREENBOX.BMP",0)
Next x
; using other pic formats
; Die folgenden Befehle können zum automatischen Aktivieren weiterer Bildformate verwendet werden:
;
; UseJPEGImageDecoder()
; UsePNGImageDecoder()
; UseTIFFImageDecoder()
; UseTGAImageDecoder()
;} end of init
;- STRUCTURES And DYNAMIC LINKED LISTS
Structure WormSegment
x.w
y.w
EndStructure
Dim wormsegment.WormSegment(#NUMBEROFGREENBOX) ; creates as money copies (0...#AMOUNT) of the wormsegment var-group as there are eatable green boxes
Structure worm ; defines type of worm, what it consists of
x.w ; starting pos of player´s worm
y.w ; starting pos of player´s worm
x1.w
y1.w
direction.s ; direction of worm movement
Width.w
Height.w
speed.w
length.b ; how many segments the worm has, how long it is
EndStructure
Dim worm.worm(0) ; creates one (0) worm array in memory, reserves memory for worm1
Structure Greenbox
x.w
y.w ; pos of greenbox is stored in x1 x2 x3... y1 y2 y3.... till #NUMBEROFGREENBOX
status.s ; status of box: eaten or not!?
counted.b ; 1 = box was counted eaten and worm got longer 0 = box was not counted yet
EndStructure
Dim Greenbox.Greenbox(#NUMBEROFGREENBOX) ; Generates a lot of copies named Greenbox(0..#NUMBEROF)
;{- GLOBALS
Global collisionwithgreenbox.b ; 0 = No, 1 = yes
worm(0)\x = 120
worm(0)\y = 120
worm(0)\x1 = 130
worm(0)\y1 = 130
worm(0)\Width = 18 ; sprite width in pixels
worm(0)\Height = 18
worm(0)\direction = ""
worm(0)\speed = 2
worm(0)\length = 1
Global wormlength.b ; this var is declared only to monitor "worm(0)\length" in Variablewindow of the Debugger (F6) while program is running
wormlength = worm(0)\length
;}
Procedure.w GREENBOXPOS(); generate x,y pos of the eatable greenboxes
For x = 0 To #NUMBEROFGREENBOX
Greenbox(x)\x = Random(WindowWidth())
Greenbox(x)\y = Random(WindowHeight())
Next x
EndProcedure
GREENBOXPOS()
;{- REPEAT CORE
Repeat
EventID.l = WindowEvent() ; CloseButton - without WaitWindowEvent() this line program is FREEZES, got it from 2ddrawing.pb
;{- CONTROLS
ExamineKeyboard() ; works only for dx windows otherwise use AddKeyboardShortcut(#Window, Shortcut, EventID)
If KeyboardPushed(#PB_Key_Escape)
End ; ends the prog
EndIf
If KeyboardPushed(#PB_Key_Right) And worm(0)\direction <> "left" ; worm may not eat himself
worm(0)\direction = "right"
EndIf
If KeyboardPushed(#PB_Key_Left) And worm(0)\direction <> "right"
worm(0)\direction = "left"
EndIf
If KeyboardPushed(#PB_Key_Up) And worm(0)\direction <> "down"
worm(0)\direction = "up"
EndIf
If KeyboardPushed(#PB_Key_Down) And worm(0)\direction <> "up"
worm(0)\direction = "down"
EndIf
;}
ClearScreen(0,0,200)
; but move not out of borders!
Height = WindowHeight()
Width = WindowWidth()
;{ WORM MAY NOT TURN AROUND ON THE SPOT / EAT HIMSELF or go out of bounds
Select worm(0)\direction ; worm(0)\direction stores the last direction in which the worm moved
Case "right" ; worm may only go right if lastdirection was NOT left
If worm(0)\x < (Width - worm(0)\Width) ; right border check, worm may not go off screen
worm(0)\x+worm(0)\speed
EndIf
Case "left" ; worm may only go right if lastdirection was NOT left
If worm(0)\x > 0 ; left border check, worm may not go off screen
worm(0)\x-worm(0)\speed
EndIf
Case "up" ; worm may only go right if lastdirection was NOT left
If worm(0)\y > 0
worm(0)\y-worm(0)\speed
EndIf
Case "down" ; worm may only go right if lastdirection was NOT left
If worm(0)\y < (Height - worm(0)\Height)
worm(0)\y+worm(0)\speed
EndIf
EndSelect
;}
StartDrawing(ScreenOutput()) ; does not work with sprites
StopDrawing()
; kind a strange Sprites can ouly be outputet to WinodOutput, not to (Windowed) ScreenOutput, it gives no error but sprite does not show up, don´t know why, boxes and lines work on both good
StartDrawing(WindowOutput())
; draws worm head
DisplayTransparentSprite(0,worm(0)\x,worm(0)\y)
Box(worm(0)\x+8,worm(0)\y+8,1,1,RGB(0,255,0)) ; x+9 y+9 marks the actual center pos of the worm, because sprite is a square and sprite pos is the upper left corner of this square
; For counter = 2 To worm(0)\length
;
; lastwormsegment(0)\x = worm(0)\x - worm(0)\Width
; worm(counter)\y = worm(0)\y
; DisplayTransparentSprite(0,worm(counter)\x,worm(counter)\y)
;
; Next counter
;{- COLLISION
For counter = 0 To #NUMBEROFGREENBOX
If SpritePixelCollision(counter,Greenbox(counter)\x,Greenbox(counter)\y,0,worm(0)\x,worm(0)\y) = 1 ; check if there is a collision with any greenbox sprite
Greenbox(counter)\status = "eaten" ; asigns greenbox(1..#NUMBEROFGREENBOXES) the status "eaten" and the box will not be drawn again
EndIf
Next counter
; increase length of worm "worm(0)\length" by 1 segment
For counter = 0 To #NUMBEROFGREENBOX
If Greenbox(counter)\status = "eaten" And Greenbox(counter)\counted = 0
worm(0)\length + 1 ; same as worm1\lengt = worm1\lengt + 1
Greenbox(counter)\counted = 1 ; mark box as "counted" "used", box increased wormlength, do not count again
wormlength = worm(0)\length
EndIf
Next counter
;}
;{- PAINT EATABLE BOXES
For counter = 1 To #NUMBEROFGREENBOX ; sprite(0) is the worm´s head sprite(1..#AMOUNTOFBOXES) are greenboxes
If Greenbox(counter)\status <> "eaten" ; only if the current box has NOT the status "eaten" it will be drawn
DisplaySprite(counter,Greenbox(counter)\x, Greenbox(counter)\y)
EndIf
Next counter
StopDrawing()
FlipBuffers() ; somewhere i read, it´s best to flip buffers at the end of all drawing operations
;}
Until EventID = #PB_EventCloseWindow ; until user pressed close button
;}
End
;{ OLD STUFF
; Global worm(0).worm ; declares/inits a Global (global = from all parts of the sourcecode accesible/read/writeable) copy "worm(0)" of type "worm" ("worm(0)" containing all the x.w y.w vars from structure "worm")
; Name Erweiterung Speicherverbrauch Bereich
; Byte .b 1 Byte im Speicher -128 bis +127
; Word .w 2 Byte im Speicher -32768 bis +32767
; Long .l 4 Byte im Speicher -2147483648 bis +2147483647
; Float .f 4 Byte im Speicher unlimitiert (siehe unten)
; String .s länge des Strings + 1 bis zu 64000 Zeichen
;LineXY(worm(0)\x, worm(0)\y, worm(0)\x1, worm(0)\y1, RGB(255,0,0))
;Declare GREENBOXER() ; this is necessary because the proc is called before it is declared/come to existance
;}
; jaPBe Version=2.4.7.17
; FoldLines=00400043004700510055005A005E006F0071007700A200B900D200E600E800F5
; FoldLines=00FD010C
; Build=0
; FirstLine=125
; CursorPosition=203
; ExecutableFormat=Windows
; DontSaveDeclare
; EOF
http://upload.j-z-s.com/SNAKE1.zip
My problem now is, i donßt know how to make the worm grow.... when it eats a green box.
And also when the worm has grown, how do you calculate the turning points the wormsegments have to go to ....
It´s too high math for me, maybe someone can help!???

I guess a dynamic asigned Array would be usefull, but i messed with the dynamic lists and did not get the concept.
Normal lists (not dynamic) are ok. but dynamic... zzZZZzzz...
The coolest thing would be if the dynamic list would be outputet to a textfile that can be opened and used as MONITOR for the data processed.
Can someone help?
I love PBASIC, it is GREAT! KEEP IT UP!
crypt0