# Add ball and move it with cursor until a line

**URL:** https://discourse.processing.org/t/add-ball-and-move-it-with-cursor-until-a-line/30975
**Category:** Coding Questions
**Created:** [June 30, 2021, 10:49am UTC](https://discourse.processing.org/t/add-ball-and-move-it-with-cursor-until-a-line/30975 "2021-06-30T10:49:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kevin20](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kevin20](https://discourse.processing.org/u/kevin20)
#### Post date: [June 30, 2021, 10:49am UTC](https://discourse.processing.org/t/add-ball-and-move-it-with-cursor-until-a-line/30975/1 "2021-06-30T10:49:10Z")

</div>

hey guys!  
first of all i want to create the game skee ball in processing.  
i already have a ball as cursor but i want that the ball is fly after throwing line and not be fixed on the cursor.

my code atm:  
int wurfstaerke = 50;  
int gesamtpunktzahl = 0;  
boolean geworfen = false;  
int mouseXTemp;  
int mouseYTemp;  
PImage hintergrund, loch1, loch2, loch3, loch4, loch5, ball;

void setup() {  
size(400, 600);  
hintergrund = loadImage(“assets/images/background.jpg”);  
}

void draw() {  
background(hintergrund);

// zielwurfzonen  
drawCircles();  
loch1 = loadImage(“assets/images/loch.png”);  
image(loch1, 80, 10);  
loch2 = loadImage(“assets/images/loch.png”);  
image(loch2, 240, 10);  
loch3 = loadImage(“assets/images/loch.png”);  
image(loch3, 160, 70);  
loch4 = loadImage(“assets/images/loch.png”);  
image(loch4, 160, 150);  
loch5 = loadImage(“assets/images/loch.png”);  
image(loch5, 160, 225);

// beschriftung zielwurfzone  
drawText();  
textSize(11);  
text(“100”, 99, 90);  
text(“100”, 260, 90);  
text(“50”, 180, 140);  
text(“40”, 180, 220);  
text(“30”, 180, 295);

// wurfline  
line(0, 300, width, 300);

// ball zeichnen  
drawBall();  
ball = loadImage(“assets/images/ball.png”);  
cursor(ball);

// distanz berechnen  
calculateDistance();  
}

void drawCircles() {  
fill(100);  
rect(10,500,180,20);  
fill(255);  
rect(10,520,180,50);  
}

void drawText() {  
fill(0);

textSize(11);  
text(“100”, 520, 50);

textSize(14);  
text("Wurfstärke: " + wurfstaerke, 10, 590);  
}

void calculateDistance() {  
// distanz des Balls zu den Wurfzonen  
}

void drawBall() {  
}

void keyPressed() {  
if (key == ‘+’) {  
wurfstaerke++;  
}  
else  
if (key == ‘-’) {  
wurfstaerke–;  
}  
}

void mousePressed() {}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 30, 2021, 12:19pm UTC](https://discourse.processing.org/t/add-ball-and-move-it-with-cursor-until-a-line/30975/2 "2021-06-30T12:19:38Z")

</div>

one remark: please load all assets in setup (which runs only once) and not in draw (which runs 60 times per second)

to achieve what you want: do you want to release the ball in any direction? Using the mouse?

Then look

- at atan2 in the reference for the angle
- at dist() for the distance
- and at function mouseReleased() to start the flying of the ball.

---

<div class="post-metadata">

### Author: ![Syed\_Daanish](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@Syed\_Daanish](https://discourse.processing.org/u/Syed_Daanish)
#### Post date: [July 1, 2021, 1:37am UTC](https://discourse.processing.org/t/add-ball-and-move-it-with-cursor-until-a-line/30975/3 "2021-07-01T01:37:38Z")

</div>

please use `<code></code>` or  
```language-name(optional)  
//Your code  
```  
or just two backticks for inline code see [Markdown Tutorial - Code](https://commonmark.org/help/tutorial/09-code.html)
