Array vs. Data Bank

Blitz3D Forums/Blitz3D Beginners Area/Array vs. Data Bank

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hi Again Everybody,

Well, thank you all very much for all the suggestions so far. Unfortunately, I haven't been able to solve the problem yet. The string array I used in the example code was just a mistake. The actual code uses an integer array. I would like to try the code on a third 'slow' machine, but I don't have another one to try it on. But, like I mentioned, one of the things that makes me suspicious of this machine as a whole is that it can't run any of the demo games that came with Blitz Plus. But if it is some hardware issue, is that something I need to be concerned about when trying to distribute Blitz Plus games to the general public? I thought Blitz Plus was supposed to run on all Win95+ systems?

Anyway, I've narrowed the problem down to something that happens in one of the following functions. I apologize for posting so much code, and if there's a more appropriate way to post code to this site, please just let me know.

Although I said the game involved a "snake" that was just for simplicity. It's exactly like the "snake" game, except that mine uses Dachshunds. Below you'll find the type declaration for the dogs, the initial array I use for the starting body coordinates and the three functions that move, update and draw the dog. The rest of the game involves drawing a perimiter and maze of walls to go through, a computer opponent dog, and bowls and bones for eating. I've even tried taking away all the walls and opponent dog, but I get the same problem, when the dog eats about 10 bowls, he grows to an array of 58 blocks and he just slows down way too much to play. I think the probem may be in the "update_hund()" function below, but maybe my whole approach is flawed. Again, thanks for any help or suggestions.

-Dave

-----------------------------------------------------------
Code
-----------------------------------------------------------

Type Hunden
Field head% ; anim image index
Field head_x% ; X value of head image
Field head_y% ; Y value of head image
Field tail% ; anim image index
Field tail_x% ; X value of tail image
Field tail_y% ; Y value of tail image
Field red% ; red value of the dog
Field blue% ; blue value of the dog
Field green% ; green value of the dog
Field heading$ ; v|h which way's the dog heading
Field x_incr% ; how many x increments to go each move
Field y_incr% ; how many y increments to go each move
Field id% ; ID of dog : Black = 0, Brown = 8,
; Red = 16, Tan = 24
Field owner$ ; (h|c) human or computer
Field grow% ; how many blocks to let the dog grow by
; from eating food
Field shrink% ; how many blocks to let the dog shrink by
; from eating a bone
End Type


; Beginning body block locations for this dog
Dim my_coords%(2,8)
my_coords%(1,1) = 645
my_coords%(2,1) = 196
my_coords%(1,2) = 650
my_coords%(2,2) = 196
my_coords%(1,3) = 655
my_coords%(2,3) = 196
my_coords%(1,4) = 660
my_coords%(2,4) = 196
my_coords%(1,5) = 665
my_coords%(2,5) = 196
my_coords%(1,6) = 670
my_coords%(2,6) = 196
my_coords%(1,7) = 675
my_coords%(2,7) = 196
my_coords%(1,8) = 680
my_coords%(2,8) = 196
LAST_INDEX% = 8


; This is called when the user presses one of the arrow
; keys.
Function move_my_hund(hund.Hunden,direction$)

; Get beginning and ending body array values before we
; update so we can compare and turn the the tail the right
; direction.
old_first_x% = my_coords%(1,1)
old_first_y% = my_coords%(2,1)
last_x% = my_coords%(1,LAST_INDEX%)
last_y% = my_coords%(2,LAST_INDEX%)

; Move the direction that was passed to this function
Select direction$
Case "up"

hund\y_incr% = Abs(hund\y_incr%) * -1
next_x% = last_x%

; Check top perimiter
If last_y% < 12
lose_life()
Else
; No problem, go up one
next_y% = last_y% + hund\y_incr%
hund\heading$ = "v"
EndIf

; Animation
If hund\head% = 2 + hund\id%
hund\head% = 3 + hund\id%
Else
hund\head% = 2 + hund\id%
EndIf


Case "down"
hund\y_incr% = Abs(hund\y_incr%)
next_x% = last_x%

; Check bottom perimiter
If last_y% > 397
lose_life()
Else
; No problem, go down one
next_y% = last_y% + hund\y_incr%
hund\heading$ = "v"
EndIf

; Animation
If hund\head% = hund\id%
hund\head% = 1 + hund\id%
Else
hund\head% = hund\id%
EndIf


Case "left"

next_y% = last_y%
; Check left perimiter
If last_x% < 11 And gate_state$ = "closed"
lose_life()
Else
; No problem, go right one
hund\x_incr% = Abs(hund\x_incr%) * -1
next_x% = last_x% + hund\x_incr%
hund\heading$ = "h"
EndIf

; Animation
If hund\head% = 6 + hund\id%
hund\head% = 7 + hund\id%
Else
hund\head% = 6 + hund\id%
EndIf


Case "right"

next_y% = last_y%
; Check right perimiter
If last_x% > 621 And gate_state$ = "closed"
lose_life()
Else
; No problem, go right one
hund\x_incr% = Abs(hund\x_incr%)
next_x% = last_x% + hund\x_incr%
hund\heading$ = "h"
EndIf

; Animation
If hund\head% = 4 + hund\id%
hund\head% = 5 + hund\id%
Else
hund\head% = 4 + hund\id%
EndIf

End Select

; Update the dog's body coords
update_hund(hund.Hunden, next_x%,next_y%)

; Compare new first x,y to old first x,y and see what way
; the last block moved and display the
; appropriate dog butt.
new_first_x% = my_coords%(1,1)
new_first_y% = my_coords%(2,1)

If new_first_y% < old_first_y% ; UP
hund\tail_x% = my_coords%(1,1) - 5
hund\tail_y% = my_coords%(2,1) + 8

; Animation
If hund\tail% = 4 + (hund\id% * 2)
hund\tail% = 5 + (hund\id% * 2)
ElseIf hund\tail% = 5 + (hund\id% * 2)
hund\tail% = 6 + (hund\id% * 2)
ElseIf hund\tail% = 6 + (hund\id% * 2)
hund\tail% = 4 + (hund\id% * 2)
Else
hund\tail% = 4 + (hund\id% * 2)
EndIf


ElseIf new_first_y% > old_first_y% ; DOWN

hund\tail_x% = my_coords%(1,1) - 5
hund\tail_y% = my_coords%(2,1) - 16

; Animation
If hund\tail% = hund\id% * 2
hund\tail% = 1 + (hund\id% * 2)
ElseIf hund\tail% = 1 + (hund\id% * 2)
hund\tail% = 2 + (hund\id% * 2)
ElseIf hund\tail% = 2 + (hund\id% * 2)
hund\tail% = 3 + (hund\id% * 2)
Else
hund\tail% = hund\id% * 2
EndIf

ElseIf new_first_x% < old_first_x% ; LEFT

hund\tail_x% = my_coords%(1,1) + 5
hund\tail_y% = my_coords%(2,1) - 5

; Animation
If hund\tail% = 12 + (hund\id% * 2)
hund\tail% = 13 + (hund\id% * 2)
ElseIf hund\tail% = 13 + (hund\id% * 2)
hund\tail% = 14 + (hund\id% * 2)
ElseIf hund\tail% = 14 + (hund\id% * 2)
hund\tail% = 15 + (hund\id% * 2)
Else
hund\tail% = 12 + (hund\id% * 2)
EndIf

ElseIf new_first_x% > old_first_x% ; RIGHT

hund\tail_x% = my_coords%(1,1) - 17
hund\tail_y% = my_coords%(2,1) - 5

; Animation
If hund\tail% = 8 + (hund\id% * 2)
hund\tail% = 9 + (hund\id% * 2)
ElseIf hund\tail% = 9 + (hund\id% * 2)
hund\tail% = 10 + (hund\id% * 2)
ElseIf hund\tail% = 10 + (hund\id% * 2)
hund\tail% = 11 + (hund\id% * 2)
Else
hund\tail% = 8 + (hund\id% * 2)
EndIf
EndIf
End Function



; Updates the body coordinates of the requested dog
Function update_hund(hund.Hunden, next_x%,next_y%)

; First figure out if we're growing, shrinking or neither
If hund\grow% > 0 ; Growing
start% = 1
end_point% = LAST_INDEX% + 1
hund\grow% = hund\grow% - 1

; This is so that when we return to normal, we don't have a
; LAST_INDEX% 1 too high
If hund\grow% > 1
new_last_index% = LAST_INDEX% + 1
Else
new_last_index% = LAST_INDEX%
EndIf

ElseIf hund\shrink% > 0 ; Shrinking
start% = 3
end_point% = LAST_INDEX% + 1
hund\shrink% = hund\shrink% - 1
new_last_index% = LAST_INDEX% - 1

If start% > end_point%
start% = end_point% - 1
new_last_index% = LAST_INDEX%
EndIf

Else ; Not shrinking or growing
start% = 2
end_point% = LAST_INDEX%
new_last_index% = LAST_INDEX%
EndIf

; Now, if we're growing or shrinking copy current
; my_coords%() to loc_coords%()
If hund\grow% > 0 Or hund\shrink% > 0
Dim loc_coords%(2,end_point%)
For i% = 1 To LAST_INDEX%
loc_coords%(1,i%) = my_coords%(1,i%)
loc_coords%(2,i%) = my_coords%(2,i%)
Next

; Re-dimension my_coords and re-populate
Dim my_coords%(2,end_point%)
For i% = 1 To LAST_INDEX%
my_coords%(1,i%) = loc_coords%(1,i%)
my_coords%(2,i%) = loc_coords%(2,i%)
Next
EndIf

; Cut out or add to the appropriate places for the new body
For i% = start% To LAST_INDEX%
n% = i% - 1
my_coords%(1,n%) = my_coords%(1,i%)
my_coords%(2,n%) = my_coords%(2,i%)
Next

; Add the new next x,y
my_coords%(1,end_point%) = next_x%
my_coords%(2,end_point%) = next_y%

; Update LAST_INDEX% for new DIM
LAST_INDEX% = new_last_index%

End Function


; Draw a dog
Function draw_hund(hund.Hunden)

; Whose dog (and coords) is this?
If hund\owner$ = "h" ; Human's

; Prepare loc_coords%() for human data and load
Dim loc_coords%(2,LAST_INDEX%)
For i% = 1 To LAST_INDEX%
loc_coords%(1,i%) = my_coords%(1,i%)
loc_coords%(2,i%) = my_coords%(2,i%)
Next

Else ; Computer's

; Prepare loc_coords%() for computer data and load
Dim loc_coords%(2,LAST_INDEXC%)
For i% = 1 To LAST_INDEXC%
loc_coords%(1,i%) = your_coords%(1,i%)
loc_coords%(2,i%) = your_coords%(2,i%)
Next

EndIf

; Set drawing color to dog color and draw body
Color hund\red%, hund\green%, hund\blue%
If hund\owner$ = "h"
end_body% = LAST_INDEX%
Else
end_body% = LAST_INDEXC%
EndIf

For i% = 1 To end_body%
x% = loc_coords%(1,i%)
y% = loc_coords%(2,i%)
Rect(x%, y%, 8, 8, 1)
Next

; Draw head in right location
If hund\heading$ = "h"
If hund\x_incr% > 0 ; Moving horizontally right
hund\head_x% = loc_coords%(1,end_body%) + 8
hund\head_y% = loc_coords%(2,end_body%) - 6
ElseIf hund\x_incr% < 0 ; Moving horizontally left
hund\head_x% = loc_coords%(1,end_body%) - 16
hund\head_y% = loc_coords%(2,end_body%) - 6
EndIf
EndIf

If hund\heading$ = "v"
If hund\y_incr% < 0 ; Moving vertically up
hund\head_x% = loc_coords%(1,end_body%) - 5
hund\head_y% = loc_coords%(2,end_body%) - 16
ElseIf hund\y_incr% > 0 ; Moving vertically down
hund\head_x% = loc_coords%(1,end_body%) - 5
hund\head_y% = loc_coords%(2,end_body%) + 8
EndIf
EndIf


; Draw head and tail for this dog at the right locations
DrawImage heads, hund\head_x%, hund\head_y%, hund\head%
DrawImage tails, hund\tail_x%, hund\tail_y%, hund\tail%

End Function