# Adding automatic enemy behaviour to static sprite

**URL:** https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398
**Category:** Coding Questions
**Tags:** homework
**Created:** [February 23, 2022, 6:51pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398 "2022-02-23T18:51:17Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)
#### Post date: [February 23, 2022, 6:51pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/1 "2022-02-23T18:51:17Z")

</div>

I have included an enemy sprite in my game and I was wondering what I should do to make it behave as an enemy and bounce offensively towards the player sprite. My wondering is about the structure of the code and not only the code itself. I placed the enemy sprite in the right side of the screen, whereas the player sprite is in the left side. My sprite player bounces throughout the screen.

I googled this specific topic and, once again, I haven’t found proper readings for the case I mentioned. I consider myself to be very beginner.

My code is a bit long and I have read the community guidelines and some programmers saying that entire codes should not be posted. Should I post my full code here, given that I don’t know what parts of them are important for the topic?

---

<div class="post-metadata">

### Author: ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)
#### Post date: [February 23, 2022, 8:06pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/2 "2022-02-23T20:06:12Z")

</div>

Forgot to mention that I would like to see the sprite player dying if the enemy sprite touches it.

Should I code the enemy sprite to bounce throughout the screen and make the player sprite deviate from it as the main goal of my game? Is it easier than what I wrote in my question?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 23, 2022, 9:09pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/3 "2022-02-23T21:09:02Z")

</div>

> [@RafaDuarte](#):
>
> My code is a bit long and I have read the community guidelines and some programmers saying that entire codes should not be posted.

In this forum the original poster (OP) can freely post their entire code.

It’s only frowned upon when we post a full solution as an answer too soon w/o attempting to help the OP 1st.

Of course if the code is too big, especially if it’s got multiple files or needs to load assets, it’s advisable to host it somewhere else (like GitHub) and just link to it.

> [@RafaDuarte](#):
>
> My wondering is about the structure of the code and not only the code itself.

You should highly consider using classes to describe the characteristics (data) & actions (methods) of your “bouncing” objects:

> **[Objects](https://processing.org/tutorials/objects/)**
>
> The basics of object-oriented programming.

---

<div class="post-metadata">

### Author: ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)
#### Post date: [February 23, 2022, 9:37pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/4 "2022-02-23T21:37:34Z")

</div>

I added a ball to the screen and when the player jumps, the ball disappears. The sprite player jumps with gravity and it bounces.

I followed this:

[https://processing.org/examples/bounce.html](https://processing.org/examples/bounce.html)

The ball can move throughout the screen alongside with the player sprite or I should write a collision? I still do not understand the collision bit in Processing when it comes to sprites.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 23, 2022, 9:38pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/5 "2022-02-23T21:38:58Z")

</div>

> [@RafaDuarte](#):
>
> I still do not understand the collision bit…

> **[Collision Detection](http://jeffreythompson.org/collision-detection/table_of_contents.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

### Author: ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)
#### Post date: [February 23, 2022, 10:53pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/6 "2022-02-23T22:53:30Z")

</div>

I understood the concepts, but is it possible to visualise a ball bouncing throughout the screen alongside the player sprite and code it to act as an enemy or will I only visualise things properly if I write the collision between the sprite and the drawn ball. I see the drawn ball, but it only moves when the player jumps.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 23, 2022, 11:24pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/7 "2022-02-23T23:24:03Z")

</div>

Collision is a constant check to see if a ‘sprite’ has reached its target or if it’s free to walk on some direction.

But you’re also asking for some kind of AI ( ~~IA~~ ) algorithm so the enemy ‘sprite’ knows where to go to based on the current position of the player ‘sprite’.

While collision is relatively an easy task, direction decision is more about vector calculation.

I’ve got these 2 online examples, although I’m not good at it:

1. “Spit Fire Sample”: `http://studio.ProcessingTogether.com/sp/pad/export/ro.91nVQMnL$v06L`
2. [“Project Drone Wing”](http://studio.processingtogether.com/sp/pad/export/ro.9D4K1FIWJ7Kxc)

---

<div class="post-metadata">

### Author: ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)
#### Post date: [February 24, 2022, 12:05am UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/8 "2022-02-24T00:05:50Z")

</div>

Thank you! Your examples are fascinating. I will read more towards the AI events that I can apply to the player and enemy of my game ish code. There are only a few beginners friendly examples out there in the internet realm and yours are included. I always forget that I should be more precise whilst speaking about AI and utilise the term AI to refer to automatic behaviours. I don’t know why my brain tells me to write the word automatic over AI lol.

(Random comment: I immediately thought that you were Brazilian once you wrote ‘IA’ instead of ‘AI’, and I clicked upon your profile to check if I was just being silly, however you are Brazilian)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 24, 2022, 12:29am UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/9 "2022-02-24T00:29:42Z")

</div>

> [@RafaDuarte](#):
>
> I immediately thought that you were Brazilian once you wrote ‘IA’ instead of ‘AI’,

You’ve got me there! 🥸

---

<div class="post-metadata">

### Author: ![RafaDuarte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafaduarte/32/15787_2.png) [@RafaDuarte](https://discourse.processing.org/u/RafaDuarte)
#### Post date: [February 24, 2022, 4:55pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/10 "2022-02-24T16:55:05Z")

</div>

I coded a ball that moves throughout the screen fastly. What do you recommend me to do in order to make the ball become murderous towards the bee?

The ball is the tiny orange dot next to the honeycomb.

The bee moves with gravity as I said previously, and I utilised this: [PVector / Processing.org](https://processing.org/tutorials/pvector) to code the ball.

 ![Captura de Tela (367)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/37915725e88b651770b1d5c30721694182fc025f.png)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 24, 2022, 11:53pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/11 "2022-02-24T23:53:44Z")

</div>

> [@RafaDuarte](#):
>
> What do you recommend me to do in order to make the ball become murderous towards the bee?

As I stated before, I’m not good at vector or trigonometry, so I can’t help you out much about it.

Basically you want the ball to follow the bee, which has a similar behavior to the [“Project Drone Wing”](http://studio.processingtogether.com/sp/pad/export/ro.9D4K1FIWJ7Kxc) code I linked to.

Given you code has picture assets you should consider hosting it at GitHub and link to it so others can take a look to help you.

---

<div class="post-metadata">

### Author: ![aksaks](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@aksaks](https://discourse.processing.org/u/aksaks)
#### Post date: [March 1, 2022, 9:41pm UTC](https://discourse.processing.org/t/adding-automatic-enemy-behaviour-to-static-sprite/35398/12 "2022-03-01T21:41:21Z")

</div>

You might try applying some PID control code that seeks to bring the enemy’s position to be the same position as the player’s position. Then fool with the PID’s function parameters to get an effect you like. The types of effect you can see can very from slowing drifting towards the player’s position to zeroing right in to missing its mark and flying off somewhere but then returning and so on. The game play could even vary the control parameters according to circumstances.
