Author |
Topic: Help with arrays and X and Y (Read 435 times) |
|
Megaman27
New Member
member is offline


Posts: 6
|
 |
Help with arrays and X and Y
« Thread started on: Apr 12th, 2013, 5:25pm » |
|
Hi, I need help with a program I'm making in Basic. It is a game where the user must "eat" the "prey" by moving the character around the screen. I've used two arrays to store the X and Y positions of the 10 Prey: dim Prey_X%(10) dim Prey_Y%(10) rem repeating the cycle
numPrey% = 10 rem set up prey for n% = 1 to numPrey% read Prey_X%( n% ) read Prey_Y%( n% ) next
data 11,5,6,3,14,20,20,12,8,10,15,15,12,6,5,2…
And now I need help with making the character eat the "Prey". I want for the X and Y positions of the character to be compared to the X and Y positions of the different prey; and if they match, the prey would disappear and the score would increase. I have already done the scoring part, but what I'm stuck on is what I need to write so that the X and Y positions of each are compared. I have put: " if x%=Prey_X%( n%) but it says that it is bad subscript. Can any1 help? p.s. I'm a beginner programmer so I not looking for anything too complex. Is it another array I need to use or are the values wrong?
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Help with arrays and X and Y
« Reply #1 on: Apr 12th, 2013, 9:30pm » |
|
on Apr 12th, 2013, 5:25pm, Megaman27 wrote:I have put: if x%=Prey_X%( n%) but it says that it is bad subscript. Can any1 help? |
|
If it reports Bad subscript then the value of n% must either be negative or greater than 10 (in the case when you have DIMensioned the array with a maximum subscript of 10). When the error occurs check the value of n%.
There's nothing particularly wrong with the method you are using. It won't be terribly fast but that may not matter. An alternative approach might be to use a 2-dimensional array corresponding to the grid of positions that the 'prey' can occupy; then each element of the array can hold a value corresponding to what is present at that location. Then the test is very easy:
Code: IF grid(x%,y%) <> 0 THEN.... If your prey are positioned at character coordinates then you could simply test the character at the specified coordinates:
Code: IF GET$(x%,y%) <> " " THEN .... Richard.
|
|
Logged
|
|
|
|
Megaman27
New Member
member is offline


Posts: 6
|
 |
Re: Help with arrays and X and Y
« Reply #2 on: Apr 13th, 2013, 2:56pm » |
|
ok, but I'm still not entirely sure how the n% could be more than 10 (I put for n%=1 to 10 )? And I'm not sure about how to make the co-ords of each get compared. I tried:
if (x%,y%) = (Prey_X%(n%), Prey_Y%(N%))
But it either says I'm missing a bracket or, again, it's bad subscript. I know that "tab" is inapplicable, so is there another command that I need to use, or is the above code incorrect?
|
|
Logged
|
|
|
|
Megaman27
New Member
member is offline


Posts: 6
|
 |
Re: Help with arrays and X and Y
« Reply #3 on: Apr 13th, 2013, 3:46pm » |
|
oh and can you explain how I would apply the 2-d Array please?
I've gotten so far as:
dim PreyPos (2,10) PreyX% (10) = 11,6,14,20,8,15,12,5,13,9 PreyY% (10) = 5,3,20,12,10,15,6,20,13,7
But I think I've made an error
|
|
Logged
|
|
|
|
Megaman27
New Member
member is offline


Posts: 6
|
 |
Re: Help with arrays and X and Y
« Reply #4 on: Apr 13th, 2013, 3:49pm » |
|
or is it something like:
dim PreyPos (2,10) PreyPos (0,1)=
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Help with arrays and X and Y
« Reply #5 on: Apr 13th, 2013, 4:33pm » |
|
on Apr 13th, 2013, 2:56pm, Megaman27 wrote:I'm still not entirely sure how the n% could be more than 10 (I put for n%=1 to 10 )? |
|
Maybe you've accidentally accessed the array after the FOR..NEXT loop has finished (on exit from the loop n% will be 11).
Quote:And I'm not sure about how to make the co-ords of each get compared. I tried: if (x%,y%) = (Prey_X%(n%), Prey_Y%(N%)) |
|
Do it like this:
Code:IF x% = Prey_X%(n%) AND y% = Prey_Y%(n%) THEN... Quote:can you explain how I would apply the 2-d Array please? |
|
Suppose the grid of possible locations has 40 columns and 25 rows, then you would define the array as follows:
Code: Or if you're short of memory (e.g. you're using the trial version) use a byte array:
Code: To initialise the array with the prey positions:
Code:FOR prey% = 1 TO 10
READ x%,y%
grid%(x%,y%) = prey%
NEXT prey% To test if the character has landed on a prey:
Code:IF grid%(x%,y%) <> 0 THEN.... To 'eat' the prey (so it isn't there any more):
Code: Richard.
|
|
Logged
|
|
|
|
Megaman27
New Member
member is offline


Posts: 6
|
 |
Re: Help with arrays and X and Y
« Reply #6 on: Apr 13th, 2013, 4:56pm » |
|
Ok, sorry to bother you but I have one final Question I promise! I have almost finished the program I was making as a project, but the last problem I have is that the program says that it isn't in a Repeat Loop. Could you please look at my program and identify my fault, and I promise I'll leave you alone from now on!!
x%=20 y%=15 sprite$ =chr$(240) prey$ =chr$(241) score%=0 samexy%=false print score% dim Prey_X%(10) dim Prey_Y%(10) rem repeating the cycle numPrey% = 10 rem set up prey for n% = 1 to numPrey% read Prey_X%( n% ) read Prey_Y%( n% ) next data 4,5,6,3,14,20,20,12,8,10,15,15,12,6,5,20,10,10,9,7 rem display the prey for A%=1 to 10 print tab(Prey_X%( A% ),Prey_Y%( A% ))chr$(241) next repeat rem get the user's key g%= get rem Find out the key if g%=136 then rem if it is left if x%>5 then x%=x% - 1 endif endif print tab(x%+1,y%)chr$(32) if g%=137 then rem if it is right if x%<35 then x%=x% + 1 endif endif print tab(x%-1,y%)chr$(32) if g%=138 then rem if it is down if y%<23 then y%=y%+1 endif endif print tab(x%,y%-1)chr$(32) if g%=139 then rem if it is Up if y%>2 then y%=y%-1 endif endif print tab(x%,y%+1)chr$(32) print tab(x%,y%)chr$(240) rem Scoring system for index% = 1 to 10 if Prey_X%(index%) = x% and Prey_Y%(index%) = y% then samexy$ = true endif if samexy%=true then score%=score%+10 endif until score%=100 rem The place highlighted "Not in a REPEAT loop" if score%=100 then print "Well Done, You Have Killed All The Prey! " stop endif
|
|
Logged
|
|
|
|
Megaman27
New Member
member is offline


Posts: 6
|
 |
Re: Help with arrays and X and Y
« Reply #7 on: Apr 13th, 2013, 5:21pm » |
|
Oh sorry I sent it wrong. Thanks to your help I've finished the program. Thank you Richard  You are welcome to try it out if you want:
rem Turn off typing cursor mode 6 off rem The Sprite vdu 23,240,36,62,106,255,195,126,36,36 rem The Prey vdu 23,241,96,32,32,60,60,60,20,20 x%=20 y%=15 sprite$ =chr$(240) prey$ =chr$(241) score%=0 samexy%=false print tab(0,0) score% dim Prey_X%(10) dim Prey_Y%(10) numPrey% = 10 rem set up prey for n% = 1 to numPrey% read Prey_X%( n% ) read Prey_Y%( n% ) next data 16,5,6,7,14,20,20,12,8,10,15,15,12,6,5,20,10,10,9,7 rem display the prey for A%=1 to 10 print tab(Prey_X%( A% ),Prey_Y%( A% ))chr$(241) next rem repeating the cycle repeat rem get the user's key g%= get rem Find out the key if g%=136 then rem if it is left if x%>5 then x%=x% - 1 endif endif print tab(x%+1,y%)chr$(32) if g%=137 then rem if it is right if x%<35 then x%=x% + 1 endif endif print tab(x%-1,y%)chr$(32) if g%=138 then rem if it is down if y%<23 then y%=y%+1 endif endif print tab(x%,y%-1)chr$(32) if g%=139 then rem if it is Up if y%>2 then y%=y%-1 endif endif print tab(x%,y%+1)chr$(32) print tab(x%,y%)chr$(240) rem Scoring system for index% = 1 to 10 if Prey_X%(index%) = x% and Prey_Y%(index%) = y% then samexy% = true print tab(Prey_X%(index%),Prey_Y%(index%)) chr$(240) endif next if samexy%=true then score%=score%+10 print tab(0,0) score% endif samexy%=false rem until score%=100 rem The place highlighted "Not in a REPEAT loop" if score%=100 then print "Well Done, You Have Killed All The Prey! " stop endif until false rem turn cursor on on
I know it isn't exactly complex, but I plan to add new features later on. Anyway, thanks again Richard and best of luck in future programming!
|
|
Logged
|
|
|
|
DDRM
Administrator
member is offline


Gender: 
Posts: 321
|
 |
Re: Help with arrays and X and Y
« Reply #8 on: Apr 16th, 2013, 08:34am » |
|
Hi Megaman,
I love the llamas/camels/prey!
There are a few minor problems with your code, still.
1) If you go near the prey, sometimes it disappears before you have eaten it: I suspect it's to do with the writing of spaces when you move the monster.
2) You can eat the same prey several times, if you move back and forth over it's place!
It's not a fault, but an alternative way to handle the key input would be to use a CASE statement: this could be a lot more compact.
I hope you'll forgive the presumption, but I've modified your code a little (see below) to include this, and to reset the position of eaten prey to (-1,-1) (off the board) so they can't be eaten again! I've also made the monster visible at the beginning, so you can see where you start. I've also got rid of samexy%, and moved the score write into the block dealing with catching the prey.
Now how about making the prey move, so that you have to plan a route to intercept them? :-)
Best wishes,
David
Code:
rem Turn off typing cursor
mode 6
off
rem The Sprite
vdu 23,240,36,62,106,255,195,126,36,36
rem The Prey
vdu 23,241,96,32,32,60,60,60,20,20
x%=20
y%=15
rem show the monster
print tab(x%,y%)chr$(240)
sprite$ =chr$(240)
prey$ =chr$(241)
score%=0
samexy%=false
print tab(0,0) score%
dim Prey_X%(10)
dim Prey_Y%(10)
numPrey% = 10
rem set up prey
for n% = 1 to numPrey%
read Prey_X%( n% )
read Prey_Y%( n% )
next
data 16,5,6,7,14,20,20,12,8,10,15,15,12,6,5,20,10,10,9,7
rem display the prey
for A%=1 to 10
print tab(Prey_X%( A% ),Prey_Y%( A% ))chr$(241)
next
rem repeating the cycle
repeat
rem get the user's key
g%= get
print tab(x%,y%)chr$(32)
rem Find out the key
case g% of
when 136: if x%>5 then x%-=1
when 137: if x%<35 then x%+=1
when 139: if y%>2 then y%-=1
when 138: if y%<23 then y%+=1
endcase
print tab(x%,y%)chr$(240)
rem Scoring system
for index% = 1 to 10
if Prey_X%(index%) = x% and Prey_Y%(index%) = y% then
rem samexy% = true This is really redundant, isn't it? We can simply move the score bit into this IF
print tab(Prey_X%(index%),Prey_Y%(index%)) chr$(240)
Prey_X%(index%)=-1
Prey_Y%(index%)=-1
score%+=10
print tab(0,0) score%
endif
next
rem until score%=100 rem The place highlighted "Not in a REPEAT loop"
if score%=100 then
print "Well Done, You Have Killed All The Prey! "
stop
endif
until false
rem turn cursor on
on
|
« Last Edit: Apr 16th, 2013, 08:37am by DDRM » |
Logged
|
|
|
|
|