Jump to content
IGNORED

Randomizing and "Shot" Questions


Arcade

Recommended Posts

I have a few new questions:

 

1) I've seen code for randomizing where the player/enemy would appear. How about color? Say you have an enemy that you want to use, but you don't want to create anymore enemy sprites. Everytime this enemy appears in a new random spot (or was even shot/hit), how would you also go about changing the color? Not all of the colors on the TIA Pallete, but say just a choice few (aka 5 perhaps). I've tried it but the problem was the sprites just continued to randomly change colors instead of just staying one color then changing to another when the enemy appeared elsewhere.

 

2) Look at "Outlaw" as an example. When a player is hit both players "stop" if you will. Is it possible to code to have only one character to "stop" when hit and still allow the other player to move? I know this can sound un-fair but I'm curious if this is possible? I've seen code about allowing characters to randomly appear elsewhere when shot or perform a short animation.

 

Thanks for all your help!

Link to comment
Share on other sites

For the color you could define an array of the specific colors you want to use, and use the rand function to generate an index into the array, using any of various methods (discussed in other threads) to reduce the random number to within the desired range of index values.

 

To keep the color from changing all the time, you need to assign the random index value to a variable, and don't call the rand function again until you need to respawn the enemy in a new random location.

 

If you're using the multisprite or DPC+ kernel to draw multiple enemies, you'll need to have a separate variable for each enemy's color index.

Link to comment
Share on other sites

For the color you could define an array of the specific colors you want to use, and use the rand function to generate an index into the array, using any of various methods (discussed in other threads) to reduce the random number to within the desired range of index values.

 

To keep the color from changing all the time, you need to assign the random index value to a variable, and don't call the rand function again until you need to respawn the enemy in a new random location.

 

If you're using the multisprite or DPC+ kernel to draw multiple enemies, you'll need to have a separate variable for each enemy's color index.

 

Okay it's not as easy as I had expected. Could you give me an example using just 3 or 4 colors so I can see what you mean?

 

Also another quick question...let's say for instance your character will move normally. Then, by pressing the fire button, your character does something else instead of move (say for instance he fires in all 4 directions). How would you go about coding it to where you can switch out between moving and shooting? I can make my character move, then when I press the fire button he won't move, but instead fire in all 4 directions. However, when I press the fire button again, he won't start moving (thus cancelling out the shooting). I've tried numerous things, but the only thing I can come up with is having to hold the fire button down. I don't want to hold it down, just press the fire button to switch out between the two commands.

 

Finally I'm not using the DPC+ kernel. I don't know what all extra it adds in to batari. I do have the files downloaded, just not sure where to put the files to allow batari to use DPC+ (if it's even worth using).

Link to comment
Share on other sites

I think he means something like this

 

rem rand & 3 takes rand mod 4

temp1 = rand & 3
player_color = color_table[temp1]

data color_table
color0, color1, color2, color3
end

 

Here, since the number of possible values

rand returns is a multiple of 4,

you can reduce the range to 4 values

by taking rand mod 4 whithout producing

bias towards any color(s)

(it's trickyer if you want say one of 5

colors without bias)

Since 4 is a power of 2 you can do the

mod operation by masking for the lower

2 bits by anding with 3

Link to comment
Share on other sites

Alrighty, using the above example (and doing some tweaking per my code) I was finally able to make the sprite randomly change color. Granted, it only changes when I restart Stella but hey it's not constantly randomizing now! I tried a few things to make it change to one of the random colors upon a certain collision but it was to no avail. How do I recall the random generator again to make the color change upon the collision? By the way, here is part of my code and tell me if I'm correct on this:

 

f = rand & 7 (this labels "f" as the rand...I have this outside my "main")

 

Inside my main I have this:

 

temp1 = f (since "f" is the rand, I need to label my temp1 as the rand as well to generate a random color)

COLUP1 = Color_Table_0[temp1] (I want Player 1's sprite color to change randomly, so I put Player 1's sprite as a Color Table (with the randomness above added to it))

 

data Color_Table_0

66, 114, 194, 98, 78, 240, 6, 254

end

(This is my color table and below are the colors I want my Player 1 sprite to randomly generate as...thus, the colors are labeled in my chart, Player 1 above is labeled as the Color Table, and since temp1 = f which = rand, this allows 1 random color to be chosen.)

 

I'm new to this, but I'm hoping what I said is correct? I hope what I said makes sense (as it does to me in my limited knowledge) but this is what I'm guessing after using the above code to fit my needs. If so that'll make me happy as I learned something else!

Link to comment
Share on other sites

Also another quick question...let's say for instance your character will move normally. Then, by pressing the fire button, your character does something else instead of move (say for instance he fires in all 4 directions). How would you go about coding it to where you can switch out between moving and shooting? I can make my character move, then when I press the fire button he won't move, but instead fire in all 4 directions. However, when I press the fire button again, he won't start moving (thus cancelling out the shooting). I've tried numerous things, but the only thing I can come up with is having to hold the fire button down. I don't want to hold it down, just press the fire button to switch out between the two commands.

It sounds like you got the color issue figured out, and I'm not experienced with DPC+ yet, so I'll tackle the movement question.

 

From your description of what you want to achieve, I'd suggest defining a flag for indicating whether the player is moving or is stationary/shooting. It doesn't need to be an entire variable (byte)-- just a single bit. Random Terrain's bB web page should have lots of examples of dividing a byte into multiple bit variables or flags.

 

Now, supposing that you have plenty of variables free, the simplest way to do this would be to use an entire byte for the flag, then use the exclusive-or (the ^ operator) to flip the flag when the button is pressed, something like this:

 

  dim flag = a

  flag = 0 : rem initialize the flag to "off"

loop

  if joy0fire then flag = flag ^ 1 : rem this flips the flag on if it's off, or off if it's on

  if flag = 0 then gosub player_is_moving
  if flag = 1 then gosub player_is_firing

  goto loop

 

This is obviously an incomplete example, but hopefully you can get the idea of what I mean.

 

The tricky part, though, will be reading the fire button, because you'll need to "debounce" it-- which I didn't do in the above example. Debouncing a console switch (like game reset or game select) or a fire button or a game controller refers to processing its state in a way that prevents it from being "read" too many times in a row, or too quickly. For example, in the code above it's possible that pressing the fire button will rapidly flip the flag on and off and on and off many times as the program loops, because the computer/2600 operates so much faster than you do-- so even if you try to tap the button very quickly, it may seem to the 2600 as though you're holding the button down for a really long time. There are examples of debouncing the fire button in older posts, and Random Terrain also has examples on his bB web page.

Link to comment
Share on other sites

Alrighty, using the above example (and doing some tweaking per my code) I was finally able to make the sprite randomly change color. Granted, it only changes when I restart Stella but hey it's not constantly randomizing now! I tried a few things to make it change to one of the random colors upon a certain collision but it was to no avail. How do I recall the random generator again to make the color change upon the collision? By the way, here is part of my code and tell me if I'm correct on this:

 

f = rand & 7 (this labels "f" as the rand...I have this outside my "main")

 

Inside my main I have this:

 

temp1 = f (since "f" is the rand, I need to label my temp1 as the rand as well to generate a random color)

COLUP1 = Color_Table_0[temp1] (I want Player 1's sprite color to change randomly, so I put Player 1's sprite as a Color Table (with the randomness above added to it))

 

data Color_Table_0

66, 114, 194, 98, 78, 240, 6, 254

end

(This is my color table and below are the colors I want my Player 1 sprite to randomly generate as...thus, the colors are labeled in my chart, Player 1 above is labeled as the Color Table, and since temp1 = f which = rand, this allows 1 random color to be chosen.)

 

I'm new to this, but I'm hoping what I said is correct? I hope what I said makes sense (as it does to me in my limited knowledge) but this is what I'm guessing after using the above code to fit my needs. If so that'll make me happy as I learned something else!

 

 

I only used temp1 because you can't use an

expression as an index.

 

Rand is a function. In the case of rand

it's like a variable whos value changes

every time you read it.

each time you read it, it calls a subroutine

that calculates a new value. That takes time

(the inlinerand optimization puts a copy

of the code that does the calculation wherever

you use rand, that's faster but takes more space)

 

So whenever you do f = rand & 7 you get a new f

If f is only used for the one thing eg the player1

color you may as well make f the color and not

the index so you don't have to keep looking in the

table.

if the player color is one that you have to set

after each drawscreen you'd do this.

f = rand & 7
f = color_table_0[f]

 

when you want a new color

then where ever you set COLUP1

 

COLUP1 = f

 

if you don't need to update COLUP1 untill you

change the color you'd do this

 

temp1 = rand & 7
COLUP1 = color_table_0[temp1]

 

and not waste a variable

(again, this is assumming you're not using f

for anything else)

Edited by bogax
Link to comment
Share on other sites

Thanks to both of you for your help and explanations! I try to always keep RT's page opened up so I can glance at some things when coding. I don't always understand the stuff but I do try and test out some things. I know I've been asking a lot of questions but I guess that's okay. Not only is my project coming along more and more, but I'm starting to understand things better.

Link to comment
Share on other sites

This is not really related to your opening questions but I thought I might add my experience with temp1 - temp6.

 

Don't trust 'em.

 

You might not anticipate when they'll be overwritten or cleared before you're ready to use them. So use 'em to save some variables but always factor in room for Murphy.

 

In some of my games I actually "waste" a variable as my own temporary variable to mitigate the level of unknown :)

Link to comment
Share on other sites

Yeah I found after some testing putting "f" as rand did what I wanted vs the temp.

 

I know I haven't posted any code, and I'm not trying to be a jerk, it's just I came up with this idea, and wanted to try and surprise the community with the finished product once I'm done. It's not going to be the greatest thing ever, but it's my first attempt, and I want it to be something special for everyone and hope that everyone can appreciate it for what it is.

 

I guess in a nutshell here's what I'm trying to test: (as I've been trying and it's not working and I'm getting agitated!)

 

The sprite starts as a random color (which I got down and it will do everytime I start up Stella). But now I'm trying to test the fact, how can I go one step further and every time I press up on the joystick, then I can recall the randomness and change the color of the sprite? I tested setting it as white in the main, but it stays white no matter what. So if I set it to a random color in the main then it will show. If I don't even set it in the main obviously it doesn't appear UNTIL I press up...but it's always that one random color.

 

How to make it start off as a random color...then press up and it will change...press up again and it changes again? Is this where I need to set it to temp1 instead of f then? (As far as rand). Sorry if I'm babbling or confusing...I'm just getting aggrevated!

Edited by Arcade
Link to comment
Share on other sites

The sprite starts as a random color (which I got down and it will do everytime I start up Stella). But now I'm trying to test the fact, how can I go one step further and every time I press up on the joystick, then I can recall the randomness and change the color of the sprite? I tested setting it as white in the main, but it stays white no matter what. So if I set it to a random color in the main then it will show. If I don't even set it in the main obviously it doesn't appear UNTIL I press up...but it's always that one random color.

 

How to make it start off as a random color...then press up and it will change...press up again and it changes again? Is this where I need to set it to temp1 instead of f then? (As far as rand). Sorry if I'm babbling or confusing...I'm just getting aggrevated!

Well, unless you're using a multi-sprite kernel and are using the player that gets multiplexed, you'll need to set the player's color in the loop sometime before you call drawscreen (since COLUP0 and COLUP1 get wiped out at the end of drawscreen when the score is displayed). So "all" you need to do to test the random colors is to check the joystick just before you set the player's color and-- if the joystick is pressed up-- randomize the color again before you set it.

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...