We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › creating random ellipses
Page Index Toggle Pages: 1
creating random ellipses (Read 474 times)
creating random ellipses
Feb 17th, 2006, 6:40pm
 
Hi,
I'm pretty new to this and i need help if anyone can.....
I have written the code below.
I want to draw 5 coins (ellipses) on the screen at random positions. I have done one.
I also want it so that once there are less than 5 coins, it draws another one, any help?

Cheers,
Nick

PImage balloon;

float xpos; // x position of balloon
float ypos; //y position of balloon
float x=0;
float y=1;
float coins;
float r = random(100, 400);
float r2 = random(100, 400);
float yCoin, xCoin;


void setup()
{
 size(500,500); // size of applet
 balloon = loadImage("balloon.gif"); // load balloon sprite
 xpos = width/2; // start position
 ypos = height/2; // start position
 framerate(60); // set framerate to 60
}

void draw()
{
 xpos = xpos + x;
 ypos = ypos + y;
 background(102, 0, 0);  // back colour
 if(coins <5)
 {
   drawCoins(xCoin, yCoin); // draw coins
   coins--; // decrement coin count
 }
 drawSprite(xpos, ypos); // draw the sprite to the screen
}

void drawSprite(float xpos, float ypos)
{
 image(balloon, xpos, ypos);  // Display the sprite at the position xpos, ypos
   // bounce if it hits the edges
 if (xpos < 0 || xpos > height-balloon.height)
 {
   x = x * -1;
 }
 if (ypos < 0 || ypos > width-balloon.width)
 {
   y = y * -1;
 }
}

void drawCoins(float xCoin, float yCoin)
{
 xCoin = r;
 yCoin = r2;
 ellipse(xCoin, yCoin, 20, 20);
 coins++;
}

void collision()
{
}

void keyPressed()
{
 if (keyCode == LEFT) // if left key pressed
 {
   x = -1;
   y = 0;
 }
 else if (keyCode == RIGHT) // if right key pressed
 {
   x = 1;
   y = 0;
 }
 else if (keyCode == UP) // if up key pressed
 {
   y = -1;
   x = 0;
 }
 else if (keyCode == DOWN) // if down key pressed
 {
   y = 1;
   x = 0;
 }
}
Page Index Toggle Pages: 1