Adding automatic enemy behaviour to static sprite

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?

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?

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.

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

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:

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.

1 Like

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.

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”:$v06L
  2. “Project Drone Wing”
1 Like

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)

You’ve got me there! :disguised_face:

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 / to code the ball.

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” 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.

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.