Afra Binjerais – Midterm Progress

So far I got my game running but it’s still in process. I still have to imbed sound and change a couple of things, but you get the point of the game.

I found it hard to make the sprite and the ball move within the same function, but with the help of Pi, thanks Pi, I was able to do that.

Overall, I still have to add a couple of things, as I have a lot of ideas that I will try to put into action. But so far, I’m happy with my progress, even though my game is simple.

This is some of my code that’s related to the game:

var ball_diameter = 30;
var bomb_diameter = 10; 
var xpoint;
var ypoint;
var zapperwidth = 6; 
var numofbombs = 20;
var bombposX = [];
var bombposY = [];
var bombacceleration = [];
var bombvelocity = [];
var time = 0;
var timeperiod = 0;
var score = 0; 
var posX;

function setup() {
  createCanvas(640, 480);

  var temp00 = 0, temp01 = -20;
  while(temp01 < height){
    temp00 += 0.02;
    temp01 += temp00;
    timeperiod++; 
  }

  posX = zapperwidth + 0.5*ball_diameter - 2;
  xpoint = 0.5 * width;
  ypoint = height - 0.5*ball_diameter + 1;

  initbombpos();
}

function draw() {
  background(137, 209, 245);
  
  fill(239, 58, 38);
  rect(0,0, zapperwidth, height);
  scoreUpdate();

  fill(255);
  noStroke();
  for(var i=0; i<numofbombs; i++){
    ellipse(bombposX[i], bombposY[i], bomb_diameter, bomb_diameter);
  }

  updatebombpos(); 

  fill(31, 160, 224);
  ellipse(xpoint, ypoint, ball_diameter, ball_diameter);
  xpoint -= 3;

  if(mouseIsPressed && (xpoint + 0.5 * ball_diameter) < width) {
    xpoint += 6; 
  }

  if(xpoint <= posX || bombCollistonTest()) {
    gameover();
  }

  time += 1;
}

function updatebombpos(){
  for(var i=0; i<numofbombs; i++){
    bombvelocity[i] += bombacceleration[i];
    bombposY[i] += bombvelocity[i];
  }

  if( time > timeperiod){
    initbombpos();
    time = 0;
  }
}

function initbombpos (){
  for(var i=0; i<numofbombs; i++){
    bombacceleration[i] = random(0.02, 0.03);
    bombvelocity[i] = random(0,5);
    bombposX[i] = random(zapperwidth+(0.5*ball_diameter),width);
    bombposY[i] = random(height/4, height/2); // Adjust Y position to be between one-fourth and half of the canvas height
  } 
}

function bombCollistonTest(){
  var temp = 0.5*(ball_diameter+bomb_diameter)-2;
  var distance; 

  for(var i=0; i<numofbombs; i++){
    distance = dist(xpoint, ypoint, bombposX[i], bombposY[i])
    if(distance < temp) {
      return true; 
    }
  }
  return false; 
}

function gameover(){
  fill(255);
  textSize(32); 
  textAlign(CENTER, CENTER); 
  text("GAME OVER", width/2, height/2 - 20); 
  textSize(15); 
  text("Press space to restart", width/2, height/2 + 20); 
  noLoop();
}

function scoreUpdate(){
  score += 10;
  fill(255);
  text("SCORE: " + int(score/timeperiod), width - 65, 15);
}

function keyPressed() {
  if (keyCode === 32) { 
    restartGame(); 
  }
}

function restartGame() {
  time = 0;
  score = 0;
  posX = zapperwidth + 0.5 * ball_diameter - 2;
  xpoint = 0.5 * width;
  ypoint = height - 0.5 * ball_diameter + 1;
  initbombpos();
  loop();
}

 

 

Leave a Reply