Jump to content
IGNORED

Getting random value between 10-110


Recommended Posts

I am having trouble getting a number between 10-110. This is my code I have now:

Get_random_value
        jsr Random  ; get value between
        and #127    ; 0-127
        adc #10      ; add ten
        sta NextValueXPos   ; save value

        cpx #110   ; check if value is above 110
        bcs Lower_X_Pos  ; if so, go to the lowering part of the code
        
        rts  ; end this section
        
Lower_X_Pos
        lda NextValueXPos    ; load value
        sbc #57    ; decrease by 57
        sta NextValueXPos        ; save value
        rts  ; end this section

Sometimes a value will turn the object up at the extreme right part of the code where it is unreachable, even though I had set it to decrease the value of the x position by a whole bunch. Or am I doing something wrong?

Random:
        lda Rand
        lsr
 ifconst Rand16
        rol Rand16
 endif
        bcc noeor
        eor #$B4
noeor
        sta Rand
 ifconst Rand16
        eor Rand16
 endif
        rts   

EDIT: Argh! I am such a dummy! It should be this:

        cmp #110   ; check if value is above 110
        bcs Lower_X_Pos  ; if so, go to the lowering part of the code

Since it's saved in a, not x.

EDIT #2: Hmm. This isn't working as I thought it would, either.

Edited by atari2600land
Link to comment
Share on other sites

What is Rand initialized to, and are you using the Rand16 extension or not? Not that it should matter, as the main routine should cap the value anyway.

 

You might be interested in the carry flag that is set by Random, and will affect the ADC in the main routine. Since the addition won't cause the carry flag to be set again, you are subtracting 57 without carry later on. Sometimes it is fine to take shortcuts if you know the value of flags, but sometimes you can get bitten by them too.

Link to comment
Share on other sites

I did. I still get the same problem, though. Also, I don't need a value above 255 so the carry should be unimportant, right? That's why I asked if there was a way to add to numbers without it.

 

Get_random_value

        clc
        jsr Random
        and #127
        adc #10    
        sta NextValueXPos    

        cmp #100
        bcs Lower_X_Pos
        rts
        
Lower_X_Pos
        sec
        lda NextValueXPos
        sbc #40
        sta NextValueXPos        
        rts
Link to comment
Share on other sites

jsr Random will change the carry so you have it at the wrong spot.

 

It helps to comment the range of the accumulator values.

 

 

 

Get_random_value:
        jsr Random     ; A = 0-255
        clc
        and #127       ; A = 0-127
        adc #10        ; A = 10-137
        sta NextValueXPos    

        cmp #111       ; need to compare with 111, else the range ends up being 10-109
        bcs Lower_X_Pos ; C will be set if A >= 111
        rts
        
Lower_X_Pos:               ; A = 111-127
;        sec               ; not needed as we know C is set due to the CMP #110
;        lda NextValueXPos ; not needed as A is already NextValueXPos
        sbc #40            ;  A = 71-97
        sta NextValueXPos        
        rts
Link to comment
Share on other sites

I tested this and it doesn't work either. So something must be wrong with my sense of x positioning values. What is the farthest x value a 4-pixel wide missile can be without it going off the screen and warping to the left? Also, I have this code for a Y value and it seems to work okay.

 

        jsr Random
        and #15   ; 0-15
        adc #231  ; add 231 to make it 231-246
        sta ValueY  
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...