Week 3 : Object Oriented Programming

Originally, I wanted to make the game ‘Space Invaders,’ but with my own kind of twist. I planned to get all the objects working before i look through my digital art designs to see any recurring themes i could implement into the game.

Starting with the set of ‘aliens,’ I initially created a for loop inside a foor loop to create a grid of ‘aliens’ that randomly moved in a uniform manner. The plan was that the bottom row of the ‘aliens’ would be shooting at the players’ character. The player had to miss all the bullets, and also shoot back at the ‘aliens’ to kill them.

After doing the grid, finally getting them to randomly move in a uniform manner, and also getting the player to move their character left to right, I was at a dead end. I had so many bugs, obstacles, and things that I wasn’t sure about. I believe if I had more time, I could have solved it, but I still wanted a working game, so I decided to change it.

After letting my original plan go, I decided to do the basic pong- game. Where balls are falling in random order, and the goal is to catch it with your slider. It took some time for me to transition from the original code to the new one, but I finally reached a conclusion where my game was working. Ideally I could add a time limit and a score, but I guess it would just have to wait till later.

I still faced some difficulties, especially figuring out how to get the ball to bounce back from only the slider, but managed to do it. My only obstacle now is actually playing the game – It appears I’m really bad at it, so forgive me in the video. !

// Calling the player class
Player player;
 
// calling the shooter class as an array
Shooters [] shooters ;


  void setup(){
    size (1000,1000);
    ellipseMode (CENTER);
    shooters = new Shooters [20];
    player = new Player ();
    
    // creating a for loop to create an array of shooters
    for (int i = 0; i<10; i ++){
      shooters [i] = new   Shooters (); 
  }
  }
  
  void draw (){
    background (0, 0, 0);
    player.drawPlayer ();
    player.checkEdge();
    // creating a for loop for the shooters 
    for (int i = 0; i<10; i ++){
      shooters[i].drawShooters();
      shooters[i].update ();
      shooters[i].checkEdge();
    keyPressed();
  }
  }
  
  // to be able to move the rectangle
  void keyPressed (){
    if (keyPressed && keyCode == LEFT) {
      player.playerSpeed = -3;
      player.update();
    }
    
     if (keyPressed && keyCode == RIGHT) {
      player.playerSpeed =3;
      player.update();
 
    }
 
  }
  //////////////////////////////
// Tab Player
class Player {
  float playerX, playerY, w, h; 
  float playerSpeed;
  
  Player(){
    playerX = width/2;
    playerY = height - (height/6);
    w = 100;
    h = 25;
    
  }
  
  void drawPlayer (){
    noStroke();
    fill (255, 255, 255);
    rect (playerX, playerY, w, h);
  }
  
  void update (){
    playerX += playerSpeed;
  }
  
  
  // Honestly, this didnt work. I tried to make the rectangle stop when it reached the edge. 
    void checkEdge (){
    if (playerX > width - 100){
      print("stop");
      playerSpeed = 0 ;
      update();
    }
    }
  

  }
////////////
//Tab Shooters
class Shooters {
  float shootersRadius;
  float locX, locY;
  float y, x, xspeed, yspeed;
  
  // creating the shooters and having them move at a different speed each time
  Shooters(){
    x = 0;
    y = 0; 
    xspeed = random (3,5); 
    yspeed = random (1,3);

  
  }
  
    void update (){
      x = x + xspeed; 
      y = y + yspeed; 
      }
  
  void checkEdge (){
 
    // setting the dimensions
   if (x > width || x <0 ) {
     xspeed = xspeed* -1;
   }
   if ( y < 0 ) {
     yspeed = yspeed*-1;
  }
// creating the boundaries for the rectangle (if it hits the rectangle it bounces)
  if ( y > player.playerY && y < width){
    if (x > player.playerX && x < player.playerX + player.w) {
      xspeed = xspeed* -1;
    yspeed = yspeed*-1;
    }
  }
  }
  
  void drawShooters(){
    noStroke();
    fill ( 255, 215, 0);
    circle ( x, y, 32 );
  }
    }

 

Leave a Reply