Hello, how are you, I’m new here.
I would like to learn how to use processing correctly so I can create my own games and become a good programmer.
At the moment I have not programmed any games, I am trying to create a small base so that each object inherits from it and it is easier to create games.
I have two databases, one with arraylist and the other with hashmap but I don’t know if I am doing it correctly and that is why I have registered in this forum to be able to learn everything I can.
I’m going to show the code I have to see if I’m on the right track or if something needs to be changed. I already asked this in the processing forum but the answer was not very clear.
The code that I am going to show has a parent class that is responsible for managing a list where the objects will be added and a method to execute the draw method and another to delete objects.
The player class shows a ship that moves and can fire bullets by pressing the z button and the bullet class moves up and is eliminated when leaving the screen.
//prueba de veracidad
void settings(){
size(640,480);
noSmooth();
}
void setup(){
frameRate(60);
background(85,106,206);
new Jugador();
}
void draw(){
background(85,106,206);
Padre.draw_all();
}
//clase disparo-----------------------------------
class Disparo extends Padre{
private float x,y,velocidad;
private PImage grafico = loadImage("disparo.png");
public Disparo(float x,float y){
super();
imageMode(CENTER);
this.x = x;
this.y = y;
this.velocidad = 5;
}
public void draw(){
mover();
eliminar();
image(grafico,x,y);
}
private void mover(){
y -= velocidad;
}
private void eliminar(){
if(y < 64){
live = false;
kill();
}
}
} //fin clase disparo---------------------------------
//clase jugador--------------------------------------
class Jugador extends Padre{
private float x,y,velocidad;
private PImage grafico = loadImage("jugador.png");
private int contador;
public Jugador(){
super();
imageMode(CENTER);
this.x = 320;
this.y = 400;
this.velocidad = 5;
this.contador = 0;
}
public void draw(){
mover();
disparar();
image(grafico,x,y);
}
private void mover(){
if(keyPressed && key == CODED && keyCode == LEFT){
x -= velocidad;
}else if(keyPressed && key == CODED && keyCode == RIGHT){
x += velocidad;
}
if(keyPressed && key == CODED && keyCode == UP){
y -= velocidad;
}else if(keyPressed && key == CODED && keyCode == DOWN){
y += velocidad;
}
}
private void disparar(){
contador++;
if(keyPressed && key == 'z' && contador > 5){
new Disparo(x,y);
contador = 0;
}
}
} //fin clase jugador------------------------------------
//clase padre-----------------------------------
static class Padre{
private static ArrayList<Padre> lista = new ArrayList<Padre>();
public boolean live = true;
public Padre(){
lista.add(this);
}
public void draw(){
}
public static void draw_all(){
for(int indice = lista.size()-1;indice >= 0;indice--){
Padre padre = lista.get(indice);
padre.draw();
}
}
public void kill(){
for(int indice = lista.size()-1;indice >= 0;indice--){
Padre padre = lista.get(indice);
if(padre.live == false){
lista.remove(indice);
}
}
}
} //fin clase padre------------------------------