Creating a Simple Enemy AI
in
Programming Questions
•
11 months ago
Hi guys I been working on creating a simple enemy AI for my space invader type game and running into several small issues, one is that I had to redo my functions over and over again to create different positions for the enemies and have them travel at different speed was wondering if there was a way I could make my functions simpler and second when i actually start putting enemies on screen well they kick each other out of the screen which was pretty funny but not the effect i was going for. Here my code so if anybody could give me a quick hand fairly new at this stuff and a little confuse
- float enemyX = 0; // enemies
float enemyY = 0;
float enemyX1 = 0;
float enemyY1 = 20;
float enemyX2 = 0;
float enemyY2 = 0;
float enemyX3 = 0;
float enemyY3 = 0;
float enemyX4 = 0;
float enemyY4 = 0;
float enemyX5 = 0;
float enemyY5 = 0;
float enemyX6 = 0;
float enemyY6 = 0;
float enemyX7 = 0;
float enemyY7 = 0;
float enemyX8 = 0;
float enemyY8 = 0;
float enemyX9 = 0;
float enemyY9 = 0;
float enemyX10 = 0;
float enemyY10 = 0;
float enemyX11 = 0;
float enemyY11 = 0;
float enemyX12 = 0;
float enemyY12 = 0;
float enemyX13 = 0;
float enemyY13 = 0;
float enemyX14 = 0;
float enemyY14 = 0;
float enemyX15 = 0;
float enemyY15 = 0;
float enemyX16 = 0;
float enemyY16 = 0;
float enemyX17 = 0;
float enemyY18 = 0;
float enemyX19 = 0;
float enemyY19 = 0;
float enemyX20 = 0;
float enemyY20 = 0;
float enemyX21 = 0;
float enemyY21 = 0;
float enemyX22 = 0;
float enemyY22 = 0;
float enemyX23 = 0;
float enemyY23 = 0;
float speed = 10;
float speed1 = 1; // enemies speed
float speed2 = 20;
float speed3 = 30;
float speed4 = 40;
float speed5 = 50;
//Global variables listed above
void setup(){ //basic setup for enemy 3 test
size(500,500); //skect size
smooth(); //does something i forgot
}
void draw(){ //our main program
background(0);
move(); // to move enemies
bounce(); // to make enemies move from left to right
drawEnemy(30,50,50,color(255,0,0));
move1(); // to move enemies
bounce1(); // to make enemies move from left to right
drawEnemy1(60,70,60,color(0,255,0));
}
void move(){
// Change the x location by speed
enemyX = enemyX + speed;
}
void move1(){
// Change the x location by speed
enemyX1 = enemyX1 + speed;
}
void move2(){
// Change the x location by speed
enemyX2 = enemyX2 + speed;
}
void move3(){
// Change the x location by speed
enemyX3 = enemyX3 + speed;
}
void move4(){
// Change the x location by speed
enemyX4 = enemyX4 + speed;
}
void move5(){
// Change the x location by speed
enemyX5 = enemyX5 + speed1;
}
void move6(){
// Change the x location by speed
enemyX6 = enemyX6 + speed1;
}
void move7(){
// Change the x location by speed
enemyX7 = enemyX7 + speed1;
}
void move20(){
// Change the x location by speed
enemyX20 = enemyX20 + speed1;
}
void move8(){
// Change the x location by speed
enemyX8 = enemyX8 + speed1;
}
void move9(){
// Change the x location by speed
enemyX9 = enemyX9 + speed5;
}
void move10(){
// Change the x location by speed
enemyX10 = enemyX10 + speed2;
}
void move11(){
// Change the x location by speed
enemyX11 = enemyX11 + speed4;
}
void move12(){
// Change the x location by speed
enemyX12 = enemyX12 + speed2;
}
void move13(){
// Change the x location by speed
enemyX13 = enemyX13 + speed5;
}
void move14(){
// Change the x location by speed
enemyX14 = enemyX14 + speed3;
}
void move15(){
// Change the x location by speed
enemyX15 = enemyX15 + speed3;
}
void move16(){
// Change the x location by speed
enemyX16 = enemyX16 + speed3;
}
void move17(){
// Change the x location by speed
enemyX17 = enemyX17 + speed4;
}
void bounce(){
if ((enemyX > width) || (enemyX < 0)){
speed = speed * -1;
}
}
void bounce1(){
if ((enemyX1 > width) || (enemyX1 < 0)){
speed = speed * -1;
}
}
void bounce2() {
if ((enemyX2 > width) || (enemyX2 < 0)){
speed = speed * -1;
}
}
void bounce3() {
if ((enemyX3 > width) || (enemyX3 < 0)){
speed = speed * -1;
}
}
void bounce4() {
if ((enemyX4 > width) || (enemyX4 < 0)){
speed = speed * -1;
}
}
void bounce5() {
if ((enemyX5 > width) || (enemyX5 < 0)){
speed1 = speed1 * +1;
}
}
void bounce6() {
if ((enemyX6 > width) || (enemyX6 < 0)){
speed1 = speed1 * -1;
}
}
void bounce7() {
if ((enemyX7 > width) || (enemyX7 < 0)){
speed1 = speed1 * +1;
}
}
void bounce8() {
if ((enemyX8 > width) || (enemyX8 < 0)){
speed1 = speed1 * -1;
}
}
void bounce9() {
if ((enemyX9 > width) || (enemyX9 < 0)){
speed5 = speed5 * -1;
}
}
void bounce10() {
if ((enemyX10 > width) || (enemyX10 < 0)){
speed2 = speed2 * -1;
}
}
void bounce11() {
if ((enemyX11 > width) || (enemyX12 < 0)){
speed4 = speed4 * -1;
}
}
void bounce12() {
if ((enemyX12 > width) || (enemyX12 < 0)){
speed2 = speed2 * -1;
}
}
void bounce13() {
if ((enemyX13 > width) || (enemyX13 < 0)){
speed5 = speed5 * -1;
}
}
void bounce14() {
if ((enemyX14 > width) || (enemyX14 < 0)){
speed3 = speed3 * -1;
}
}
void bounce15() {
if ((enemyX15 > width) || (enemyX15 < 0)){
speed3 = speed3 * -1;
}
}
void bounce16() {
if ((enemyX16 > width) || (enemyX16 < 0)){
speed3 = speed3 * -1;
}
}
void bounce17() {
if ((enemyX17 > width) || (enemyX17 < 0)){
speed4 = speed4 * -1;
}
}
void bounce20() {
if ((enemyX20 > width) || (enemyX20 < 0)){
speed1 = speed1 * -1;
}
}
void drawEnemy(int x, int y, int thesize, color c) {
int offset = thesize / 4;
rectMode(CENTER);
ellipseMode(CENTER);
stroke(200);
fill(c);
rect(enemyX,y,thesize,thesize/2);
fill(200);
ellipse(enemyX - offset,y - offset,offset,offset/2);
ellipse(enemyX + offset,y - offset,offset,offset/2);
ellipse(enemyX - offset,y + offset,offset,offset/2);
ellipse(enemyX + offset,y + offset,offset,offset/2);
}
void drawEnemy1(int x, int y, int thesize1, color c) {
int offset1 = thesize1 / 4;
rectMode(CENTER);
ellipseMode(CENTER);
stroke(200);
fill(c);
rect(enemyX1,enemyY1,thesize1,thesize1/2);
fill(200);
ellipse(enemyX1 - offset1,enemyY1 - offset1,offset1,offset1/2);
ellipse(enemyX1 + offset1,enemyY1 - offset1,offset1,offset1/2);
ellipse(enemyX1 - offset1,enemyY1 + offset1,offset1,offset1/2);
ellipse(enemyX1 + offset1,enemyY1 + offset1,offset1,offset1/2);
}
1