How can I simplify my code and how can i get the balls to spawn in different locations

Discussion in 'Java' started by newbie73, Jan 3, 2020.

  1. newbie73


    Dec 9, 2019
    Likes Received:
    The balls currently follow each other while slowly distancing away. Is there a way I can get them to spawn in random locations.

    How can I also simplify my code?

    My code:
    float x=600;
    float y=300;
    float x1=600;
    float y1=300;

    // controls size
    float size= 50;

    //controls speed
    float xSpeed =10;
    float ySpeed= 10;
    float x1Speed =10;
    float y1Speed= 10;

    //line variables
    float l= 10;
    float lSpeed=5;

    void setup () {
    size(1600, 900);

    void draw () {

    // repeatedly clears background

    //changes colour of the ball

    // draws circle
    ellipse(x, y, size , size);
    ellipse(x1, y1, size , size);

    //makes ball 1 bounce
    x+= xSpeed;
    if(x> width|| x< 0) {
    xSpeed= xSpeed *-1;
    y+= ySpeed;
    if(y> height|| y< 0) {
    ySpeed= ySpeed *-1;

    //makes ball 2 bounce
    x1+= xSpeed;
    if(x1> width|| x1< 0) {
    x1Speed= x1Speed *-1;
    y1+= ySpeed;
    if(y1> height|| y1< 0) {
    y1Speed= y1Speed *-1;

    //moving line

    rect(0, l, 1600, 3);
    l=l+ lSpeed;
    if(l> height) {
    newbie73, Jan 3, 2020
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.