
-----------------------------------
ecookman
Tue Jul 14, 2009 3:36 pm

how do I make a random falshing color.
-----------------------------------
I am new to java, and am making a asteroids game.
I thought it would be cool if I could have my spaceship flash different colors, is this possible, and if it is, how would this be possible.

-----------------------------------
Zren
Tue Jul 14, 2009 4:06 pm

Re: how do I make a random falshing color.
-----------------------------------
All depends on how your rendering your Spaceship.

-----------------------------------
Tony
Tue Jul 14, 2009 4:46 pm

Re: how do I make a random falshing color.
-----------------------------------
I thought it would be cool if I could have my spaceship flash different colors...
I don't think that would be cool.

All depends on how your rendering your Spaceship.
It all depends on how you're rendering your spaceship.

-----------------------------------
saltpro15
Tue Jul 14, 2009 7:57 pm

RE:how do I make a random falshing color.
-----------------------------------
You tell him Tony! Learn yourself some grammer Zren! :lol:

-----------------------------------
Zren
Tue Jul 14, 2009 8:25 pm

Re: RE:how do I make a random falshing color.
-----------------------------------
Oh hush you two. My grammar strangely suffers during the period between June and September.

Oh and by the way salty: [url=http://www.google.ca/search?hl=en&q=define%3Agrammer&btnG=Google+Search&meta=&aq=f&oq=]grammer.

Bloody Grammar Nazis (expression alright).

-----------------------------------
ecookman
Tue Jul 14, 2009 8:50 pm

RE:how do I make a random falshing color.
-----------------------------------
@tony
lol good thing you won't be making the game :P


and I am making my spaceship using vector art...
if that helps any.
I will dig up the code for it and post it later.

-----------------------------------
ecookman
Tue Jul 14, 2009 9:00 pm

Re: how do I make a random falshing color.
-----------------------------------

//Class asteroids
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.Timer;
import java.util.*;

public class asteroids extends Applet implements KeyListener, ActionListener  {
    boolean UK,LK,RK;
        
    Timer timer;
    Image offscreen;
    Graphics offg;
    SpaceCraft ship;
    asteroid roid;
    ArrayList  Alist;

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    
    public void init() {
        this.setSize(900, 600);
        this.addKeyListener(this);
       
        ship=new SpaceCraft();
        timer=new Timer(20, this);
        offscreen=createImage(this.getWidth(), this.getHeight());
        offg= offscreen.getGraphics();
        roid = new asteroid();
        Alist= new ArrayList();
        for (int i = 0; i 25){
           ship.yspeed =25;
       }
        if (a.getKeyCode() ==KeyEvent.VK_RIGHT){
          
            RK=true;
        }
        if (a.getKeyCode() ==KeyEvent.VK_LEFT){
            LK=true;
        }
        if (a.getKeyCode() ==KeyEvent.VK_UP){
           
           UK=true;
        }

       repaint();
    }
    public void keyReleased (KeyEvent a){
        if (a.getKeyCode() ==KeyEvent.VK_RIGHT){
            RK=false;
        }
        if (a.getKeyCode() ==KeyEvent.VK_LEFT){
            LK=false;
        
        }
         if (a.getKeyCode() ==KeyEvent.VK_UP){
            UK=false;
         }
    }
    public void keyChecked (){
    if (RK==true) {
        ship.TRight();
    }
    if (LK==true) {
        ship.TLeft();
    }

    if (UK==true){
    ship.accellerate();
    }
    }
    public void keyTyped (KeyEvent a){}

    public void start(){
    timer.start();
    }
    public void stop (){
    timer.stop();
    }

    public boolean collision(VectorSprite Thing1, VectorSprite Thing2){
        int x,y;
        for (int i = 0; iColours.size) colourCounter = 0

Back & Forth:
You need another variable indicating direction. It's basically in Order with an extra if statement.
This method is very nice to achieve a glow effect if the colours are in order by how light they are.

Then have it render with the colour set to Colours[colourCounter].

You get?

-----------------------------------
TheGuardian001
Wed Jul 15, 2009 12:01 am

Re: how do I make a random falshing color.
-----------------------------------
Alternatively, if you want truly random colors, you could set the color using the Random class and the Color.getHSBColor(float h, float s, float b). Although creating an array of Colors yourself and cycling through them is probably more efficient for the program.

[code]
import java.applet.*; //Applets are good.
import java.awt.*;  //Colours and stuff
import java.util.*;  //Random numbers are fun.

public class randColor extends Applet{
    Random r = new Random(); //a new random number!

    public void start(){
        setSize(70,70); //small window.
    }
    public void paint(Graphics g){
        g.setColor( //set the color
            Color.getHSBColor(r.nextFloat(),r.nextFloat(),r.nextFloat()) 
              //to a (sort of) random HSB color value
        );
        g.fillRect(10,10,50,50); //Draw a rectangle
        try{
        	Thread.sleep(200); //wait
        }
        catch(InterruptedException e){   	
        }
        repaint(); //start again
    }
}
[/code]

-----------------------------------
ecookman
Wed Jul 15, 2009 12:09 pm

Re: how do I make a random falshing color.
-----------------------------------
//space craft 
 
public SpaceCraft(){
        r= 255;
        g= 0;
        b= 100;
        colour = new Color(r,g,b);
        active=true;
....


//i added this on the bottom of the class
public void updateposition(){
        r= (int)(Math.random()*255);
        g= (int)(Math.random()*255);
        b= (int)(Math.random()*255);
        colour=new Color(r,g,b);
        super.updateposition();


and then

// VectorSprite class
public void paint(Graphics g){
// i added this       
 g.setColor(colour);





I have no clue how to get it to transition from one color to another

-----------------------------------
TheGuardian001
Wed Jul 15, 2009 1:49 pm

Re: how do I make a random falshing color.
-----------------------------------
Following your instructions on those additions, it works for me.

-----------------------------------
ecookman
Wed Jul 15, 2009 1:50 pm

RE:how do I make a random falshing color.
-----------------------------------
i am not sure on how to implement it.

-----------------------------------
Zren
Wed Jul 15, 2009 5:44 pm

Re: how do I make a random falshing color.
-----------------------------------
Another thing that would be best is if you have it rotating/randoming on a timer that way it isn't changing EVERY single cycle.

Here's what I'd have changed from the initial code to get something working. It's not exactly how your doing it, but it should help you find where you need to go.
Set of Colours
Random

VectorSprite Class
Color colour

getColour method
returns colourspaint method
setColor( this.getColour() ); 

Spaceship Class
int colourCounter
Color colours

Init method
define colours and set counter to 0
this.nextColour()


nextColour method
increment next colour
check if counter is over array size, and if so, reset
set VectorSprite variable colour to colours
set VectorSprite variable colour new random colour

Asteroids Class

ActionPerformed method
ship.nextColour()


If you want to add a timer around it, then add a new variable in either VectorSprite or Spaceship (Depending on if you only want the ship to go flash-like), that would represent the time between colour changes. Also add a variable to represent the time at the last colour change.

-----------------------------------
ecookman
Wed Jul 15, 2009 8:07 pm

RE:how do I make a random falshing color.
-----------------------------------
what if I made a if statement around each of my " r g b" variables and have it say something like if the value of the variable is 255 then variable -1. but then how would i get it to counting back up w/o having infinite if statements.

(sorry this is my first time using java and this is kinda complicated for me... I have never worked with counting or timers.)

-----------------------------------
Zren
Wed Jul 15, 2009 9:07 pm

Re: how do I make a random falshing color.
-----------------------------------
int i = 0;
while (true) {
i++;
if (i > 255) i = 0;
}

This would loop forever going 0, 1, 2, ..., 255, 0, 1, ...

ActionPerformed is basically inside a loop, which exits when the applet closes. You following?

-----------------------------------
ecookman
Thu Jul 16, 2009 10:26 am

RE:how do I make a random falshing color.
-----------------------------------
ooh. cool so easy. thanks


and just a general question...


How come the sound effects for my game don't don't work on my computer, but other ones? (and yes the sound effects are in the same location in the class folder).
