Need help with game project

hello, i’m working on a little game for a uni project and i got stuck trying to move an object using keyboards. depending where i move the void keypressed part the key might get spammed or not show at all, but the object still wont move.

this is the main file

Timer t;
Enemie e;
Player p;




void setup (){
  
  //window setup
  size(800, 800);
  background(0);
  smooth();
  
  
  t= new Timer();
  e= new Enemie();
  p= new Player();
  
  
}

void draw (){
  background(0);
  
  
  t.display();
  e.run();
  p.run();
  
  
  
}

void keyPressed() {
  println(key);
  
  if (key == 'a') {
    p.x--;
  } else {
    keyReleased();
  }
}

this is the file where im creating the player (movable object)

class Player {
  
  //class variables
  float x;
  float y;
  PShape t;
  
  
  //class constructor
  Player() {
    
    x= width/2;
    y= height/1.2;
    
    t = createShape();
    t.beginShape();
    t.fill(#FFF303);
    t.stroke(255);
    t.vertex(x, y); //nose
    t.vertex(x-10, y+30);
    t.vertex(x-20, y-20); //left peak
    t.vertex(x-70, y+80); //left ass
    t.vertex(x-20, y+60); // left wing
    t.vertex(x+20, y+60); // right wing
    t.vertex(x+70, y+80); //right ass
    t.vertex(x+20, y-20); //right peak
    t.vertex(x+10, y+30);
    
    t.endShape(CLOSE);
    

    
  }
  
  
  //class functions
  void run() { //function container
    display();
    shoot();
    life();
    keyPressed();
  }
  
  
  
  void display() {
    
    
    shape(t);
    fill(#03FFDB);
    stroke(#030CFF);
    strokeWeight(2);
    
  }
  
  void shoot()  {
    
    
  }
  
  void life() {
    
    
  }
  
}

i tried having the void keypressed part inside the player file too and no luck either.

thanks for replying and this deffinately works as i want to make my shape to work but i dont really understand what is going on. I’m completely new to this and its for a final piece project so i have to be able to explain myself. I hoped there is a easier way to make it move and i was just doing something wrong myself.

OK, thank you for your question!

Also, warm welcome to the forum!
Great to have you here!

  • We can’t run your code, because we don’t have Timer and Enemy class

  • Please post the code in one go, not in 2 sections, otherwise we have to copy it twice into our IDE to run it

  • when you display the player, you don’t use x,y, so changing x in keyPressed() hasn’t any effect

  • when you define the shape x and y should be 0 so we can place the shape later with width/2 and height /2 using translate() command

  • Don’t call keyReleased(), it gets called automatically.

Warm regards,

Chrisir


//Timer t;
//Enemie e;
Player p;

void setup () {
  //window setup
  size(800, 800);
  background(0);
  smooth();
  //t= new Timer();
  //e= new Enemie();
  p= new Player();
}

void draw () {
  background(0);
  //t.display();
  //e.run();
  p.run();
}

void keyPressed() {
  println(key);

  if (key == 'a') {
    p.x--;
  } else  if (key == 'd') { 
    p.x++;
  }
}//func 

//=========================================================================

class Player {

  //class variables
  float x;
  float y;
  PShape t;

  //class constructor
  Player() {
    x= 0;
    y= 0;

    t = createShape();

    t.beginShape();

    t.fill(#FFF303);
    t.stroke(255);
    t.vertex(x, y); //nose
    t.vertex(x-10, y+30);
    t.vertex(x-20, y-20); //left peak
    t.vertex(x-70, y+80); //left ass
    t.vertex(x-20, y+60); // left wing
    t.vertex(x+20, y+60); // right wing
    t.vertex(x+70, y+80); //right ass
    t.vertex(x+20, y-20); //right peak
    t.vertex(x+10, y+30);

    t.endShape(CLOSE);

    x= width/2;
    y= height*.2;
  }

  //class functions
  void run() { //function container
    display();
    shoot();
    life();
    keyPressed();
  }

  void display() {
    pushMatrix();
    translate(x, y); 

    fill(#03FFDB);
    stroke(#030CFF);
    strokeWeight(2);

    shape(t);

    popMatrix();
  }

  void shoot() {
  }

  void life() {
  }
}//class
//

1 Like

ok so what you told me works great thanks @Chrisir . I added the whole thing i hope its fine now, i only added the main file and the players one at first because the other 2 dont really have anything to do with what was going on.

I made the p.x+ and - on the movement 15 to make it move faster but the way it looks when it moves its a bit clunky. And if i use p.x++ and – it moves way to slow but it looks smooth.

My last question before i move on to making it shoot and destroy things, is there a way to make it move fast but smooth at the same time?
edit: And maybe allow W+A and W+D / S+A / S+D work at the same time. i added under the a and d the same thing for w and s but they only work separately.

//GameMode File

//initializing classes
Timer t;
Enemy e;
Player p;




void setup (){
  
  //window setup
  size(800, 800);
  background(0);
  smooth();
  
  
  t= new Timer();
  e= new Enemy();
  p= new Player();
  
  
}

void draw (){
  background(0);
  
  
  t.display();
  e.run();
  p.run();
  
  
  
}

void keyPressed() {
  println(key);
  
  if (key == 'a') {
    p.x -= 15;
  } else if (key  == 'd') {
    p.x += 15;
  }
 
 if (key == 'w') {
    p.y -= 15;
  } else if (key  == 's') {
    p.y += 15;
  }
}

//Enemy File

class Enemy {
  
  //class variables
  float[] x  = new float[15];
  float[] y  = new float[15];
  float[] m  = new float[15]; //speed of falling
  float a; //shake speed
  float noiseScale; //shake intensity
  float sX; //shake on X axis
  
  //class constructor
  Enemy() {
    
    for (int i=0; i<15; i++){
      x[i] = random(width);
      y[i] = (TOP-200);
      m[i] = random(1,5);
      
      a=0;
      noiseScale=110;
      
    }
    
    
    
  }
  
    
  //class functions   
  void run() {
    display();
    life();
    shoot(); 
  }
  
  
  
  void display() {
    //circles color
    stroke(255);
    fill(#F70000);
    
    //drawing the shake
    float sX = noise(a)*noiseScale;
    a+= 0.1;
    
    //drawing the circles and movement
    for (int i=0; i<15; i++){
      ellipse(x[i]+ sX ,y[i], 50, 50);     
      y[i] = y[i] + m[i];
      
      // if the circle goes past the bottom it will reapear on top
      if (y[i] > height) { 
        y[i] = TOP-200;
      }
    }
  }

 
 void life() {
   
 
 }
 
 void shoot() {
   
 
 }
 
}
 
//Player File

class Player {
  
  //class variables
  float x;
  float y;
  PShape t;
  
  
  //class constructor
  Player() {
    
    x= 0;
    y= 0;
    
    t = createShape();
    t.beginShape();
    t.fill(#FFF303);
    t.stroke(255);
    t.vertex(x, y); //nose
    t.vertex(x-10, y+20);
    t.vertex(x-20, y-20); //left peak
    t.vertex(x-90, y+90); //left wing
    t.vertex(x-30, y+60); // left ass
    t.vertex(x+30, y+60); // right ass
    t.vertex(x+90, y+90); //right wing
    t.vertex(x+20, y-20); //right peak
    t.vertex(x+10, y+20);
    t.endShape(CLOSE);
    
    x= width/2;
    y= height*0.8;
    
  }
  
  
  //class functions
  void run() { //function container
    display();
    shoot();
    life();
  }
  
  
  
  void display() {
    
    pushMatrix();
    translate(x, y);
    
    fill(#03FFDB);
    stroke(#030CFF);
    strokeWeight(2);
    
    shape(t);
    
    popMatrix();
    
  }
  
  void shoot()  {
    
    
  }
  
  void life() {
    
    
  }
  
}
  

//Timer File

class Timer {
  
  //class variables
  int mstime = millis();  //giving the miliseconds a value
  int minutes;
  int seconds;
  float x; //x of the whole box
  float y; //y of the whole box
  
  //class constructor (defining)
  Timer() {
    x= width*0.003;
    y= height*0.003;
  }
  
  //class functions
  void display() {
    
    fill(0);
    stroke(255);
    strokeWeight(1);
    rect(x,y, 93, 37);
  
  
  
  if ( millis() > mstime + 1000) 
  {
    mstime= millis();
    seconds = seconds +1;
  }
  
  if (seconds > 59)
  {
    minutes++;
    seconds = 0;
  }
  
  fill(255);
  textAlign(RIGHT);
  textSize(30);
  
  if( minutes <=9) {
    text("0", x+24, y+30);
  }
  text(minutes, x+44, y+30);
  
  if( seconds <=9) {
    text("0", x+69, y+30);
  }
  text(seconds, x+89, y+30);
  
  text( ":", x+51, y+28);
    
  }
}

Easing is the effect that we slow this process down by saying dx * easing prior to adding this result to x.

see Tutorial on Easing

1 Like

you could have a boolean for each key (wkey, akey, skey, dkey) and set them true and in keyReleased to false

then eval the booleans and say x++ and x-- and y++ and y--

could work

The main part of the sketch is the byte array keysDown[]:
final byte[] keysDown = new byte[KEYS_RANGE];

Its length is big enough to store most of the keyCode values we have available on our keyboards:

And the whole magic happens inside these 3 functions:

void keyPressed() {
  setKey(keyCode, true);
}

void keyReleased() {
  setKey(keyCode, false);
}

void setKey(final int k, final boolean isDown) {
  if (k < KEYS_RANGE & k >= 0)  keysDown[k] = byte(isDown);
}
  • Each index of keysDown[] corresponds to 1 keyCode value.
  • When a key is pressed its corresponding keyCode is used as an index value for keysDown[].
  • And then the value 1 is assigned to that indexed keyCode.
  • The value 1 lasts as long as its corresponding key is held down.
  • Once it’s released its corresponding indexed keyCode is assigned back to value 0.
  • Let’s say you wanna check out whether the special key UP is being held down.
  • You can have something like this: if (keysDown[UP] == 1) {}.
  • For vanilla keys such as letters & numbers it’s almost the same.
  • Let’s say you wanna check out the key D is being held down.
  • You’d have something like this: if (keysDown[+'D'] == 1) {}.

Pretty much that’s all you’re gonna need to know in order to adapt my sketch to you project.

yes, making a boolean array and having the keys true when pressed and false when released allows 2 keys to be pressed at once. Thx for all the help, now i can move on to making things shoot and die :smiley:

this is how ive done it:

void keyPressed() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=true;
  } 
  if (key  == 'd') {
    p.keys[1]=true;
  }
  
  //move on y axis
  if (key == 'w') {
    p.keys[2]=true;
  }
  if (key  == 's') {
    p.keys[3]=true;
  }
           
}

void keyReleased() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=false;
  }
  if (key  == 'd') {
    p.keys[1]=false;
  }
  
  //move on y axis
  if (key == 'w') {
    p.keys[2]=false;
  }
  if (key  == 's') {
    p.keys[3]=false;
  }
           
}

//setting the distance value of wasd movement
void movement() {
  
  if(p.keys[0]){
    p.x -= 15;
  }
  if(p.keys[1]){
    p.x += 15;
  }
  if(p.keys[2]){
    p.y -= 15;
  }
  if(p.keys[3]){
    p.y += 15;
  }

}

i wanted to have this whole thing in the player class file but it only works in the main file. i decided to define the array in there tho to keep the main file more clean.

edit: moving only the void movement to the player file works. but keypressed and keyreleased must stay in the main file.

You can have keypressed and keyreleased in the main file
AND have a keypressedClass and keyreleasedClass in the class and call it from
keypressed and keyreleased in the main file

i went ahead adding a bullet class so i can start shooting, but i cant manage to make it fallow the player around.

This is the bullets class, it either goes up all the time but not fallwing the X of the ship, or if i add x= p.x; withing the display void the bullet will go left and right depending on how i move the ship

//Bullets File

class Bullet {
  
  //class variables
  
  float x;
  float y;
  float speed;
  
  
  
  //class constructor
  Bullet() {
    

   x= p.x;
   y= p.y-15;
   speed = 10;
    
    
  }
  
  
  //class functons
  
  void run() {  //functions container
    display();
  }
  
  
  void display() {
    
    
    //reset bullets when they reach top
    if (y <3) {
      y= p.y;
    }
    
    fill(255);
    stroke(#17FF81);
    strokeWeight(5);
    ellipse(x ,y , 20, 20);
    
    y-= speed;
  }
  
    
}

in my book the bullet leaves the ship and when you move the ship, the bullet doesn’t move with it.

The bullet stays on its initial track / vector.

When the bullet reaches the top:

  if (y <3) {
      y= p.y;
    }

could be:

  if (y <3) {
      x= p.x;
      y= p.y;
    }

why not?

Chrisir

1 Like

that does the trink, i was looking into arrays and other things and the answer was hiding in my face lol.

now i just have to make it shoot more then 1 at a time before i can sort out the collisions.

1 Like

I always do shooting with an ArrayList of bullets

Then I can

  • add to the arraylist and
  • when it reaches the top, remove it from ArrayList

how would that work for my piece of code?

havent used arraylists before myself so im not sure how they work

Yeah,

before setup() say ArrayList<Bullet> list = new ArrayList();

upon firing, say list.add(........);

then display the flying bullets:

say :

for(Bullet b : list) 
     list.display();   // error here, can you correct it?

General information

(not to do with the error)

see https://www.processing.org/reference/ArrayList.html

Chrisir

not exactly sure where all this go to. i think i put the array list in the wright place but everything else after im lost

//GameMode File

//initializing classes
Timer t;
Enemy e;
Player p;
Bullet b;

ArrayList<Bullet> list = new ArrayList();


void setup (){
  
  //window setup
  size(800, 800);
  background(0);
  smooth();
  
  
  t= new Timer();
  e= new Enemy();
  p= new Player();
  b= new Bullet();
  
  
}

void draw (){
  background(0);
  
  
  t.run();
  e.run();
  p.run();
  b.run();
  
  
}

void keyPressed() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=true;
  } 
  if (key  == 'd') {
    p.keys[1]=true;
  }
  
  //move on y axis
  if (key == 'w') {
    p.keys[2]=true;
  }
  if (key  == 's') {
    p.keys[3]=true;
  }
           
}

void keyReleased() {

  //move on x axis
  if (key == 'a') {
    p.keys[0]=false;
  }
  if (key  == 'd') {
    p.keys[1]=false;
  }
  
  //move on y axis
  if (key == 'w') {
    p.keys[2]=false;
  }
  if (key  == 's') {
    p.keys[3]=false;
  }
           
}

//Bullets File

class Bullet {
  
  //class variables
  
  float x;
  float y;
  float speed;
  int i;
  
  
  
  //class constructor
  Bullet() {
    
     x = p.x;
     y = p.y;
     speed = 5;
    
 }
  
  
  //class functons
  
  void run() {  //functions container
    display();
  }
  
  
  void display() {
    
    
    //reset bullets when they reach top
    if (y <3) {
      x= p.x;
      y = p.y;
    }
    for (i=0; i<10; i++) {
    fill(255);
    stroke(#17FF81);
    strokeWeight(5);
    ellipse(x, y, 20, 20);
    
    i+= 0.2;
    y-= speed;
    }
  }
  
    
}

yeah, how do you plan to fire? Space bar? That’s ==’ ’

Display the flying bullets throughout (the for-loop) in draw()

i just want to have to bullets flying without having to press anything. i will eventuallly add a powerup to shoot with 2 bullets at once or 3 bullets at once.

and i will have to add colission for them to be able to kill some “asteroids” , so ill need them to keep fire even if the bullet gets destroyed on colission. so the way i have it now to return when it reaches the top wont work in the end.

in draw(): firing: say

if(frameCount%10 == 0)
    if (list.size() < 5)
         list.add(........);