How to make attack action in game

edited November 2017 in Questions about Code

Hello I'm making simple game like rpg dungeon style

I want to my character attack enemy I created and kill it

But I have no idea how to do it now..

help me.. I'm newbee to code...

ArrayList <Flame> flames = new ArrayList<Flame>();
ArrayList <Eye> eyes = new ArrayList<Eye>();

sprite lilGuy;
PImage brick;
PImage wall;
PImage pillertop;
PImage pillerside;
PImage pillerside2;
PImage background;
PImage LOCK;
PImage pillerside3;
PImage bottomtile;
PImage pillertop3;
PImage lamp;
PImage keybox;
PImage bottomtile2;
int interval =80;
int last =0;
int GRID_TILE_WIDTH =48;
int GRID_TILE_HEIGHT = GRID_TILE_WIDTH;
int numFlames = 10;
int numEyes = 10;
int [][] worldGrid = { 
  {6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6},
  {6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6},
  {6,6,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,6,6,6},
  {6,6,10,4,4,4,4,4,11,4,4,4,4,4,4,4,4,4,4,4,4,11,4,4,4,4,4,10,6,6,6},
  {6,6,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,6,6,6},
  {6,6,10,2,2,12,2,12,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6,6},
  {6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6,6},
  {3,3,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,3,3},
  {4,7,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4},
  {5,8,5,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,5,5,5},
  {9,9,9,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,9,9,9},
  {13,13,13,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,13,13,13},
  {3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3},
  {6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6},
  {6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6},
  {6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6},
  {6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6},
  {6,6,10,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,10,6,6},
  {6,6,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,6,6},
  {6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6}
 };
void setup(){
  size (GRID_TILE_WIDTH*30, GRID_TILE_WIDTH*20);
  noSmooth();
    brick = loadImage("brick.jpg");
    wall = loadImage("wall.png");
    pillertop = loadImage("pillertop.jpg");
    pillerside = loadImage("pillerside.png");
    pillerside2 = loadImage("pillerside2.png");
    background = loadImage("background.png");
    LOCK = loadImage("LOCK.png");
    pillerside3 = loadImage("pillerside3.png");
    bottomtile = loadImage("bottomtile.png");
    pillertop3 = loadImage("pillertop3.png");
    lamp = loadImage("lamp.png");
    keybox = loadImage("keybox.png");
    bottomtile2 = loadImage("bottomtile2.png");
    lilGuy= new sprite();

  for (int i=0; i <numFlames; i++){
   flames.add(new Flame());
   } 
  for (int i=0; i <numEyes; i++){
   eyes.add(new Eye());
}
}

void draw (){
  background (#262626);
  worldDrawGrid();
  lilGuy.display();
    for (int i=0; i <numFlames; i++){
    flames.get(i).drawFlame();
  }
  for (int i=0; i <numEyes; i++){
    eyes.get(i).drawEye();
   }
}



void worldDrawGrid() {
  noStroke();
  for (int row=0; row<worldGrid.length; row++) {
    for ( int col=0; col<worldGrid[row].length; col++) {
      if (worldGrid[row][col]==1) {
        image(wall, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==2){
        image(brick, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==3){
        image(pillertop, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==4){
        image(pillerside, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==5){
        image(pillerside2, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==6){
        image(background, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==7){
        image(LOCK, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==8){
        image(pillerside3, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==9){
        image(bottomtile, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==10){
        image(pillertop3, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==11){
        image(lamp, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==12){
        image(keybox, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);
        } else if (worldGrid[row][col]==13){
        image(bottomtile2, col*GRID_TILE_WIDTH, row*GRID_TILE_HEIGHT,GRID_TILE_WIDTH,GRID_TILE_WIDTH);

        }
      }
        }
      }




void keyPressed (){
switch (keyCode ){
  case DOWN:
  lilGuy.pDown= true;
  break;
  case UP:
  lilGuy.pUp= true;
  break;
  case LEFT:
  lilGuy.pLeft= true;
  break;
  case RIGHT:
  lilGuy.pRight= true;
  break;

  }
  lilGuy.moving = true;
}

void keyReleased (){
switch (keyCode ){
  case DOWN:
  lilGuy.pDown= false;
  break;
  case UP:
  lilGuy.pUp= false ;
  break;
  case LEFT:
  lilGuy.pLeft= false;
  break;
  case RIGHT:
  lilGuy.pRight= false;
  break;

  }
}

boolean animTimer(){

  if (millis()-last>= interval ){// millis keeps on counting 
    last= millis ();
    return true;

  } else {
    return false;
  }

}

class Enemy {
  float x;
  float y=0;
  int size=10;
  float speed;
  color myColor=#FFFFFF;
  boolean counted=false;
  Enemy (float x, float speed) {
    this.x=x;
    this.speed=speed;
  }
  void display() {
    fill(myColor);
    move();
    collision();
    rect(x, y, size, size);
  }
  void move() {
    y+=speed;
    if (y>height) {
      y=0;
      x=random(width);
      counted=false;
    }
    if (x<sprite.x) {
      x=x+speed/6;
    }
    if (x>sprite.x) {
      x=x-speed/6;
    }
  }
  void collision() {
    if (x>sprite.x-sprite.size/2 && x<sprite.x+sprite.size/2 && y>sprite.y-sprite.size/2 && y<sprite.y+sprite.size/2) {
      myColor=#FF0055;
      if (!counted){
        score=score-10;
        counted=true;
      }
    } else {
      myColor=int(speed*50);
    }
  }
}

class sprite {
//-fields 
PImage spriteSheet; 
PImage mySprite [][];
int f=0;
boolean pDown, pUp, pLeft, pRight = false;
boolean moving = false; 
int posX = width /2;
int posY = height/2;
int speed= 2;
int direction=0;
boolean playerhit;
int size = 10;

//-constructor
sprite(){
  playerhit = true;
  spriteSheet = loadImage ("example2.png");
  mySprite = new PImage [4][3]; 
    for (int row =0; row <4; row ++){
      for (int col= 0; col <3; col ++){
      mySprite [row][col]= spriteSheet.get (col*32, row*28, 32,28);
      }
    }  
}
//-methods
void display (){
  if (pDown){
  direction =0 ;
  posY+=speed;

  } else if (pUp){
    direction =1 ;
  posY-=speed;

  }else if (pLeft){
    direction =2 ;
  posX-=speed;

  }else if (pRight) {
    direction =3 ;
  posX+=speed;

  } else {
    moving= false;

  }

  image (mySprite [direction][f], posX, posY, 32*3, 28*3);
  if (moving && animTimer())f++;
  if (f>=3)f=0;
  }
}


  }

``

Answers

This discussion has been closed.