Jump to content



0

Newb Question: Variables


14 replies to this topic

#1 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Thu Dec 30, 2010 4:56 AM

Hi,
I'm trying to make a game called Going Dotty - where you control a titular dot as it is chased by... another dot. You must find powerups and make fake dots appear to distract your enemy.

Anyway, I haven't gotten anywhere near the gameplay yet. When I try to put in my playing field:

 pfcolors:
 $02
 $04
 $06
 $08
 $0A
 $0C
 $0A
 $08
 $06
 $04
 $02
end
 playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X.............X..X.............X
 X.............XXXX.............X
 X..............................X
 X.............XXXX.............X
 X.............X..X.............X
 X..............................X
 X..............................X
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

VbB tells me the syntax with the colours is wrong. At this point, an idea hits me: to save space and to give each round a different look, why not have the playing field change colour?

My resulting code for messing around was:
 playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X.............X..X.............X
 X.............XXXX.............X
 X..............................X
 X.............XXXX.............X
 X.............X..X.............X
 X..............................X
 X..............................X
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

level = 1

 COLUPF = level*2
 COLUBK = 0
 scorecolor = 14

draw_loop
 drawscreen
 goto draw_loop

But then I had ANOTHER idea. Why not simply make it so that each time the level advances, COLUPF = COLUPF+2?
Anyway, I'm nowhere near getting any of this. My question is how do I make variables? I want the dots not to fly around the screen at a constant pace, so I want to make playerhspeed/playervspeed variables. Is it simply a matter of doing just that or am I missing something?

Sorry for jabbering, thanks in advance.

EDIT: One more thing, when I try to make these variables, not only does vBb not like those, but suddenly it gets a syntax error from the drawscreen line?

Edited by SuperNESFreak, Thu Dec 30, 2010 5:11 AM.


#2 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Thu Dec 30, 2010 6:14 AM

Hurray for not looking at the manual... sorry for double posting, but for some reason I can't reedit my first post. Anyway, here is my code now:

playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X.............X..X.............X
 X.............XXXX.............X
 X..............................X
 X.............XXXX.............X
 X.............X..X.............X
 X..............................X
 X..............................X
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

 player0:
 011000
 100100
 %01011010
 %10100101
 %10100101
 %01011010
 100100
 011000
end

 COLUBK = 0
 COLUPF = 2
 COLUP0 = 138
 scorecolor = 14

a = 75 rem playerx
b = 75 rem playery
c = 0 rem playerhspeed   <---- The syntax error comes here
d = 0 rem playervspeed

main
a=a+c : b=b+d
 drawscreen
player0x = a : player0y = b
 if joy0up then a=a-0.2
 if joy0down then a=a+0.2
 if joy0left then b=b-0.2
 if joy0right then b=b+0.2

 goto main

Sorry for sounding like I want to be baby fed, but what am I doing wrong here?

#3 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Thu Dec 30, 2010 9:21 AM

You need to put "rem" on its own line, or at least put a : between the command and the remark. (the latter doesn't work in all cases)

[edit - also, you might want to post future bB questions in the bB forum instead of this one.]

Edited by RevEng, Thu Dec 30, 2010 9:21 AM.


#4 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Thu Dec 30, 2010 12:25 PM

View PostRevEng, on Thu Dec 30, 2010 9:21 AM, said:

You need to put "rem" on its own line, or at least put a : between the command and the remark. (the latter doesn't work in all cases)

[edit - also, you might want to post future bB questions in the bB forum instead of this one.]

Whoops!

Okay, I removed the rem lines just to be safe, but the program is still objecting to me using the d variable.

 COLUBK = 0
 COLUPF = 2
 COLUP0 = 138
 scorecolor = 14

a = 75
b = 75
c = 0
d = 0 <--- error here

main
a=a+c : b=b+d


#5 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Thu Dec 30, 2010 12:32 PM

I didn't see it before because I was browsing on my phone, but you also need to indent every single command with a space or a tab. Only labels should be directly on the left margin.

#6 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Thu Dec 30, 2010 3:16 PM

View PostRevEng, on Thu Dec 30, 2010 12:32 PM, said:

I didn't see it before because I was browsing on my phone, but you also need to indent every single command with a space or a tab. Only labels should be directly on the left margin.

HURRA- shat, it doesn't work. Now the screen just flickers, maybe the loop isn't working properly? By the way, vBb tries to unindent END commands.

 rem Going Dotty, SuperNESFreak

 score = 0
 a = 25 : rem playerx
 b = 25 : rem playery
 c = 0 : rem playerhspeed
 d = 0 : rem playervspeed
 e = 100 : rem enemyx
 f = 100 : rem enemyy
 g = 0 : rem enemyhspeed
 h = 0 : rem enemyvspeed
 i = 0 : rem animations

main
 playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X.............X..X.............X
 X.............XXXX.............X
 X..............................X
 X.............XXXX.............X
 X.............X..X.............X
 X..............................X
 X..............................X
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 end

 if i = 0 then player0:
 011000
 100100
 %01011010
 %10100101
 %10100101
 %01011010
 100100
 011000

 player1:
 %10000001
 %01011010
 100100
 011000
 011000
 100100
 %01011010
 %10000001
 end

 if i = 2 then player0:
 000000
 011000
 100100
 %01011010
 %01011010
 100100
 011000
 000000

 player1:
 000000
 %10000001
 %01100110
 011000
 011000
 %01100110
 %10000001
 000000
 end

 COLUBK = 2
 COLUPF = 4
 COLUP0 = 138
 COLUP1 = 68
 scorecolor = 14
 a=a+c
 b=b+d
 i=i+1
 player0x = a
 player0y = b
 if joy0up then a=a-0.2
 if joy0down then a=a+0.2
 if joy0left then b=b-0.2
 if joy0right then b=b+0.2
 drawscreen
 goto main


#7 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Thu Dec 30, 2010 3:55 PM

Ah sorry, the "end" statements are the exception to the rule - they don't get indented either.

Also, you were adding/subtracting 0.2 to your position variables. Adding decimals only works if you use fixed point variables.

One more suggestion - use dim to give yourself variable names with meaning.

Here's the fixed code, using 1 for the position changes instead of 0.2...

 rem Going Dotty, SuperNESFreak

 score = 0
 a = 25 : rem playerx
 b = 25 : rem playery
 c = 0 : rem playerhspeed
 d = 0 : rem playervspeed
 e = 100 : rem enemyx
 f = 100 : rem enemyy
 g = 0 : rem enemyhspeed
 h = 0 : rem enemyvspeed
 i = 0 : rem animations

main
 playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X.............X..X.............X
 X.............XXXX.............X
 X..............................X
 X.............XXXX.............X
 X.............X..X.............X
 X..............................X
 X..............................X
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

 if i = 0 then player0:
 %00011000
 %00100100
 %01011010
 %10100101
 %10100101
 %01011010
 %00100100
 %00011000
end

 player1:
 %10000001
 %01011010
 %00100100
 %00011000
 %00011000
 %00100100
 %01011010
 %10000001
end

 if i = 2 then player0:
 %00000000
 %00011000
 %00100100
 %01011010
 %01011010
 %00100100
 %00011000
 %00000000
end

 player1:
 %00000000
 %10000001
 %01100110
 %00011000
 %00011000
 %01100110
 %10000001
 %00000000
end

 COLUBK = 2
 COLUPF = 4
 COLUP0 = 138
 COLUP1 = 68
 scorecolor = 14
 a=a+c
 b=b+d
 i=i+1
 player0x = b
 player0y = a
 if joy0up then a=a-1
 if joy0down then a=a+1
 if joy0left then b=b-1
 if joy0right then b=b+1
 drawscreen
 goto main


#8 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Thu Dec 30, 2010 5:06 PM

Brilliant! Couple of things that are still not working: the speed variables don't work, aka the dot still moves around at a constant pace. Also, I put in a line of code so that the fire button will change the playfield when the fire button is pressed ( if joy0fire then COLUPF = COLUPF+2) and moved the original COLUPF line to the beggining, but the screen only changes colour once.

Also put in some code for the animations to make them work. Looking amazing!

If you could help me with these things, I'd really appreciate:

1) Fluid motion Hurrah for not looking at your own code carefully enough, fixed. Argh! Negative variables!
2) Bouncing off the walls (collision with playfield?)

Thanks alot!

EDIT: The fluid motion is proving to be tricky. I think the problem is with me telling the program to test for negative variables. How can I cap the v/h speeds to 3/-3?

Edited by SuperNESFreak, Thu Dec 30, 2010 5:16 PM.


#9 Nukey Shay OFFLINE  

Nukey Shay

    Sheik Yerbouti

  • 20,458 posts
  • Location:The land of Gorch

Posted Thu Dec 30, 2010 5:51 PM

COLUPF is the system register for storing the playfield color. Reading it will not return the value of the current color being sent to the display, but the positive/negative status of paddle 0 (INPT0). The 2600 features many such registers that deal with different things depending on if they are read from or written to.

If you want your screen color to be updated, you'd need to keep track of it yourself.

#10 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Thu Dec 30, 2010 5:52 PM

View PostSuperNESFreak, on Thu Dec 30, 2010 5:06 PM, said:

Brilliant! Couple of things that are still not working: the speed variables don't work, aka the dot still moves around at a constant pace. Also, I put in a line of code so that the fire button will change the playfield when the fire button is pressed ( if joy0fire then COLUPF = COLUPF+2) and moved the original COLUPF line to the beggining, but the screen only changes colour once.
COLUPF is a write-only hardware register - you can't read from it. Instead use your own "playcolor" variable, and do something like "playcolor=playcolor+2:COLUPF=playcolor"

Quote

Argh! Negative variables!
The 6507 uses something called two's compliment to represent negative numbers. The upshot is, negative numbers are the same thing as larger positive numbers...

+3=3
+2=2
+1=1
+0=0
-1=255
-2=254
-3=253

So to test for a range between -3 and 3, you can do something like...

if speed>252 || speed<4 then goto dosomething.

Quote

2) Bouncing off the walls (collision with playfield?)
If you save the x and y positions before your last position changes, you can do something like the following...

if collision(playfield,player0) then player0x=oldplayer0x:player0y=oldplayer0y

#11 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Thu Dec 30, 2010 5:56 PM

Awesome. Is this two's compliments always like this, (ie. I don't have to tell the variable to be two's compliments?)

For the speed things, I put in this code:

 if joy0up then c=c-1
 if joy0down then c=c+1
 if joy0left then d=d-1
 if joy0right then d=d+1
 if c>252 then c = 252
 if c>3 then c = 3
 if d>252 then d = 252
 if d>3 then d = 3

No dice. Am I being repeatedly stupid here, am I just a complete newb, or both?

Edited by SuperNESFreak, Thu Dec 30, 2010 5:59 PM.


#12 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Thu Dec 30, 2010 6:00 PM

View PostSuperNESFreak, on Thu Dec 30, 2010 5:56 PM, said:

Awesome. Is this two's compliments always like this, (ie. I don't have to tell the variable to be two's compliments?)
Yup. Nothing to do on your part, except to keep them in mind when you're writing comparisons.

[edit - answer added for your edit addition]

Try this...
 if c>127 && c<252 then c = 252
 if c>3 && c<127 then c = 3
 if d>127 && d<252 then d = 252
 if d>3 && d<127 then d = 3

Edited by RevEng, Thu Dec 30, 2010 6:04 PM.


#13 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Fri Dec 31, 2010 4:22 AM

Okay: this is my code for slowing down the dot. It works when going right or down, but not when going left or up, so I've got something wrong with the negatives again. This is the code:

 if joy0up then c=c-2
 if joy0down then c=c+2
 if joy0left then d=d-2
 if joy0right then d=d+2
 if c>127 && c<251 then c = 251
 if c>3 && c<127 then c = 4
 if d>127 && d<251 then d = 251
 if d>3 && d<127 then d = 4
 if d>0 && !joy0down then d=d-1
 if d>127 && !joy0up then d=d+1
 if c>0 && !joy0right then c=c-1
 if c>127 && !joy0left then c=c+1


#14 RevEng OFFLINE  

RevEng

    River Patroller

  • 2,010 posts
  • bit shoveler
  • Location:Canada

Posted Fri Dec 31, 2010 11:55 AM

This should fix it...

 if joy0up then c=c-2
 if joy0down then c=c+2
 if joy0left then d=d-2
 if joy0right then d=d+2

 if c>127 && c<251 then c = 251
 if c>3 && c<127 then c = 4
 if d>127 && d<251 then d = 251
 if d>3 && d<127 then d = 4

 rem *** the following is true if d is positive or negative (which is also >0)
 rem if d>0 && !joy0down then d=d-1
 rem *** the fixed version...
 if d>0 && d<127 && !joy0down then d=d-1

 if d>127 && !joy0up then d=d+1

 rem *** same fix for c...
 rem if c>0 && !joy0right then c=c-1
 if c>0 && c<127 && !joy0right then c=c-1

 if c>127 && !joy0left then c=c+1

...keep at it! You've made a fine start so far - it just takes a while to get used to the "big numbers are also negative" thing.

#15 SuperNESFreak OFFLINE  

SuperNESFreak

    Space Invader

  • 24 posts

Posted Sat Jan 1, 2011 5:57 AM

Okay. I'm away from home at the moment so I can't check this but I'll upload the code so I can get it from here later. If anyone would like to try it and see if I've made any mistakes that would be really great:

 rem Going Dotty, SuperNESFreak

 score = 0
 a = 25 : rem playerx
 b = 25 : rem playery
 c = 0 : rem playerhspeed
 d = 0 : rem playervspeed
 e = 100 : rem enemyx
 f = 100 : rem enemyy
 g = 0 : rem enemyhspeed
 h = 0 : rem enemyvspeed
 i = 0 : rem animations

main

 playfield:
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 X..............................X
 X..............................X
 X.............X..X.............X
 X.............XXXX.............X
 X..............................X
 X.............XXXX.............X
 X.............X..X.............X
 X..............................X
 X..............................X
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

 if i = 0 then player0:
 %00011000
 %00100100
 %01011010
 %10100101
 %10100101
 %01011010
 %00100100
 %00011000
end

 if i = 0 then player1:
 %10000001
 %01011010
 %00100100
 %00011000
 %00011000
 %00100100
 %01011010
 %10000001
end

 if i = 4 then player0:
 %00000000
 %00011000
 %00100100
 %01011010
 %01011010
 %00100100
 %00011000
 %00000000
end

 if i = 4 then player1:
 %00000000
 %10000001
 %01100110
 %00011000
 %00011000
 %01100110
 %10000001
 %00000000
end

 COLUBK = 0
 COLUPF = 2
 COLUP0 = 138
 COLUP1 = 68
 scorecolor = 14
 a=a+c
 b=b+d
 e=e+g
 f=f+h
 i=i+1
 player0x = a
 player0y = b
 player1x = e
 player1y = f

 if i=8 then i=0

 if joy0up then c=c-2
 if joy0down then c=c+2
 if joy0left then d=d-2
 if joy0right then d=d+2
 if c>127 && c<251 then c = 251
 if c>3 && c<127 then c = 4
 if d>127 && d<251 then d = 251
 if d>3 && d<127 then d = 4
 rem *** the following is true if d is positive or negative (which is also >0)
 rem if d>0 && !joy0down then d=d-1
 rem *** the fixed version...
 if d>0 && d<127 && !joy0down then d=d-1
 if d>127 && !joy0up then d=d+1
 rem *** same fix for c...
 rem if c>0 && !joy0right then c=c-1
 if c>0 && c<127 && !joy0right then c=c-1
 if c>127 && !joy0left then c=c+1

 if a<e then g=g-2
 if a>e then g=g+2
 if b<f then h=h-2
 if b>f then h=h+2
 if g>127 && g<251 then g = 251
 if g>3 && g<127 then g = 4
 if h>127 && h<251 then h = 251
 if h>3 && h<127 then h = 4

 if collision(playfield,player0) then a=j:b=k:c=0-c:d=0-d
 if collision(playfield,player1) then e=l:f=m:g=0-g:h=0-h

 j = a : rem oldplayerx
 k = b : rem oldplayery
 l = e : rem oldenemyx
 m = f : rem oldenemyy

 drawscreen
 goto main

By the way, how does the code know when checking the animation variable when to end the if? I don't think at the moment the code will make the enemy sprite animate. Fixed.

Edited by SuperNESFreak, Sat Jan 1, 2011 6:18 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users