I don't have experience in making games but did you ever made a class?
I suggest check them out, also check out how to use a arraylist.
here i changed some things to give you an idea.
the bullets don't die atm which is a issue.
I suggest to make a class for the main player aswell.
And check out tutorials (flash actionscript) there is a lot about making games, althought it's different coding techniques for making games are the same.
- float px, py;
- float vx, vy;
- float minX, maxX;
- int val;
- // store all the bullets
- ArrayList<Bullet> bullets = new ArrayList<Bullet>();
- // store all enemys
- ArrayList<Enemy> enemys = new ArrayList<Enemy>();
- void setup(){
- size( 600, 600, P3D );
-
- // add some enemys
- enemys.add(new Enemy(200, 100));
- enemys.add(new Enemy(250, 140));
-
- smooth();
- }
- void draw(){
- background(255);
-
- rect(300,550,20,20);
-
- for(Enemy enemy : enemys){
- enemy.update();
- enemy.display();
- }
-
- for(Bullet bullet : bullets){
- bullet.update();
- bullet.display();
- }
-
-
- }
- void keyPressed(){
- if(key == ' '){
- shoot();
- }
- }
- void shoot(){
- bullets.add(new Bullet(300, 550));
- }
- class Bullet{
- int x, y;
- int speed;
-
- Bullet(int x, int y){
- this.x = x;
- this.y = y;
- speed = 10;
- }
-
-
- void update(){
- // check for outside screen
- // hit detection etc.
-
- y -= speed; // wrong!!
-
- if(y < 0){
- die();
- }
- }
-
- void display(){
- pushMatrix();
- translate(x, y);
- fill(0);
- ellipse(0, 0, 2, 2);
- popMatrix();
- }
-
- void die(){
- // remove the bullet from the arrayList
- // will throw a ConcurrentModificationException
- // don't know why
- //bullets.remove(this);
- }
-
- }
- class Enemy{
- int x, y;
- int dir = 1;
- int speed = 5;
- int health = 100;
-
- Enemy(int x, int y){
- this.x = x;
- this.y = y;
- }
- void update(){
- this.x += dir*speed;
- if(x < 100 || x > width - 100){
- dir = -dir;
- }
- }
-
- void display(){
- pushMatrix();
- translate(x, y);
- ellipse( 0, 0, 50, 30 );
- popMatrix();
- }
-
- }