SNAKEv10 - WORM DOES NOT GROW.... yet HELP!!!

Advanced game related topics
crypt0night
New User
New User
Posts: 5
Joined: Sun Aug 01, 2004 10:19 pm
Contact:

SNAKEv10 - WORM DOES NOT GROW.... yet HELP!!!

Post by crypt0night »

Hi thanks for interest,
Look at this....

Image


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

the whole thing inclusive green boxes and red circle bmps can be downloaded here
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
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

* Shameless Plug *

If you are looking for a dynamic array my VList is exactly what you wan't ... especially because your snake grows basically in one direction. Unfortunately I don't have tutorials on how to use it yet but it is fairly simple. If you have any questions feel free to ask.

viewtopic.php?t=12446

Whats great is the VList automatically resizes the array and you do not have to worry about memory management at all (except for deleting the array when you are done). Also is the array shrinks, it still retains the excess memory just in case it grows again, to help speed things up, and cause less memory fragmentation.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

hi!

how fast are your vlib?
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Advanced users can use it as a regular array, so it is very fast, as fast as a static array. For someone who uses the function commands, they are very fast, and can easily iterate through the VList with a for..next loop. I think their are also some undocumented features such as passing -1 as the index, and that will use the current index, or default index and current vlist are the last ones used etc..

When i built the commands i added array bound checking where it was needed. So for example if you pass a value greater than the amount of indices stored in the array it will by default return the first item .. so this way you never get an error for going over bounds. Also if you go to the next index after the last it will automatically bring you to the first one, effectively looping you to the begining. I added these bound checking functions to ease the programmer from having to be frustrated with handle errors.

But remember this is an array list , much faster than a linked list, if used properly, and handles memory much better for things like static octree or as the above poster is doing, a snake because the snake grows in one direction and either pushes onto the end of the snake or pops of the end of the snake. You can use insert and remove specific indices in random order but this is where is is slower than a linked list.

One good thing too is that it keeps track of the current index, the total number of indexes, manages memory for you automatically, keeps track of currently allocated memory. These are values the programmer can call up to help with say using a for ... next loop for iterating etc ..
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

thanks for the description. I will download it :)

(basically my question was if it was faster than a linked list, but i wrote it a bit wrong :P )

btw, a help file would be good, instead of reading from homepage.
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Yes a help file would be excellent .. i don't know how do a PB help file though. I have it in Word format .. so maybe i'll convert it for downloading soon. Also the online function chart might have some minor errors, possibly .. so if you see any let me know.

It's still in Beta and I'm adding more useful commands over time. But my code is usually solid and well thought out so as far as bugs go, i always try to anticpate them, but something will crop up.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Also when you create the VList you can increase the size of memory allocated by passing a high number. This will help speed up the dynamicness (is tha a word) of the array when adding information. so if you are going to have 10000 pieces of data in the VList you might create it like so

MyList = VListCreate(10000)

This will pre-allocate an array capable of holding 10000 pieces of data. This gives the programmer the flexibility to pre-allocate memory if you know the maximum or minimum size you will be using.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

ok thx.
i will test the lib tomorrow if i have time.
DriakTravo
Enthusiast
Enthusiast
Posts: 346
Joined: Fri Oct 10, 2003 12:42 am
Location: Tampa,FL,USA
Contact:

Post by DriakTravo »

Hehe, this is alot like my pixel glider game on my website. I also made it in Purebasic. I included the source if it is any help. Go to http://www.1matthew.com/ (games section > pixel glider) :D
crypt0night
New User
New User
Posts: 5
Joined: Sun Aug 01, 2004 10:19 pm
Contact:

CHEEEEEE! LATE ANSWER... better tooo late then never? ;-) Re

Post by crypt0night »

Hi guys!

thanks a lot for your replys...

Thanks for the Vlist codemonger!

The pixeliglider game is great!!!! I like the particle efx! hehe....

I let you know when my game becomes more a game... ;-)

(fuck this winter <a href="http://www.ntsearch.com/search.php?q=we ... weather</a>... I hate it.... HAPPY WEEKEND!)
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

No problem, I was hoping someone would find a use for the library. If you need help implementing it don't hesitate to start a thread, email me, or PM me. It's still in Beta, but I've just added 3 more functions recently.

VListSwapRemove (Index)
VListDeleteAll ()
VListSwap (Index1, Index2)

Basically VListSwapRemove removes an index very fast, faster than a Linked List, but doesn't keep the order of the list. So if you need to remove a piece of data, use this. Oh I forgot I should add a -1 check to it, that will default to the current index. I'll have to make note of that.

VListDeleteAll is used to free up any memory of all VLists created, so you don't have to worry about memory management for you VList .. this is done, but I think I will optimize it further than what it is.

VListSwap simply swaps the data of particular indexes.

I haven't updated the PB lib with these new functions, but I will do that very soon.

Hope the lib helps.
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Post Reply