Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Processing Language (Processing 3.3.6 IDE) : The skeletal game is provided that includes the following files: Asteroid.pde, Bullet.pde, Ship.pde, and asteroidsGame.pde. Your task will be

Processing Language (Processing 3.3.6 IDE): The skeletal game is provided that includes the following files: Asteroid.pde, Bullet.pde, Ship.pde, and asteroidsGame.pde. Your task will be to design some structure to enhance the game.

Asteroid.pde

class Asteroid { float radius; float omegaLimit = .05; PVector position; PVector velocity; PVector rotation; float spin; int col = 100; PImage pic;

public Asteroid(PVector pos, float radius_, PImage pics_) { radius = radius_;

position = pos; float angle = random(2 * PI); velocity = new PVector(cos(angle), sin(angle)); velocity.mult((50*50)/(radius*radius)); angle = random(2 * PI); rotation = new PVector(cos(angle), sin(angle)); spin = random(-5,5); pic = pics_; } void hit(ArrayList asteroids) { asteroids.remove(this); } //update the asteroid's position and make it spin void update() { position.add(velocity); rotate2D(rotation, radians(spin)); } //display the asteroid void render() { roundBack(); pushMatrix(); translate(position.x,position.y); rotate(heading2D(rotation)+PI/2); image(pic, -radius,-radius,radius*2, radius*2); popMatrix(); } void roundBack() { if (position.x < 0) position.x = width; if (position.y < 0) position.y = height; if (position.x > width) position.x = 0; if (position.y > height) position.y = 0; }

/* float heading2D(PVector pvect) { return (float)(Math.atan2(pvect.y, pvect.x)); }

void rotate2D(PVector v, float theta) { float xTemp = v.x; v.x = v.x*cos(theta) - v.y*sin(theta); v.y = xTemp*sin(theta) + v.y*cos(theta); }*/ }

Bullet.pde

class Bullet { PVector position; PVector velocity; int radius = 5; PImage img = loadImage("bullet.png");

public Bullet(PVector pos, PVector vel) { position = pos; velocity = vel; } //check whether the bullet collide with any asteroid (by examining the distance to each asteroid) boolean checkCollision(ArrayList asteroids) { for(Asteroid a : asteroids) { PVector dist = PVector.sub(position, a.position); if(dist.mag() < a.radius) { a.hit(asteroids); return true; } } return false; } //update the bullet's position void update() { position.add(velocity); } //display the bullet void render() { pushMatrix(); translate(position.x, position.y); rotate(heading2D(velocity)+PI/2); image(img, -radius/2, -2*radius, radius, radius*5); popMatrix(); } /*float heading2D(PVector pvect){ return (float)(Math.atan2(pvect.y, pvect.x)); }*/

}

Ship.pde

class Ship { PVector position; PVector velocity; PVector acceleration; PVector rotation; float drag = .9; float r = 15; PImage img = loadImage("rocket.png");

public Ship() { position = new PVector(width/2, height-50); acceleration = new PVector(0,0); velocity = new PVector(0,0); rotation = new PVector(0,1); } void update() { PVector below = new PVector(0, -2*r); rotate2D(below, heading2D(rotation)+PI/2); below.add(position);

velocity.add(acceleration); velocity.mult(drag);//adjust the speed to avoid it moving too fast velocity.limit(5);//the maximum speed is 5 position.add(velocity);

} void roundBack() { if (position.x < r) position.x = width-r; if (position.y < r) position.y = height-r; if (position.x > width-r) position.x = r; if (position.y > height-r) position.y = r; } boolean checkCollision(ArrayList asteroids) { for(Asteroid a : asteroids) { PVector dist = PVector.sub(a.position, position); if(dist.mag() < a.radius + r/2) { a.hit(asteroids); return true; } } return false; } void render() { roundBack(); float theta = heading2D(rotation) + PI/2; theta += PI; pushMatrix(); translate(position.x, position.y); rotate(theta); fill(0);

image(img,-r,-r*1.5,2*r,3*r); popMatrix(); } /*float heading2D(PVector pvect) { return (float)(Math.atan2(pvect.y, pvect.x)); }*/ /* void rotate2D(PVector v, float theta) { float xTemp = v.x; v.x = v.x*cos(theta) - v.y*sin(theta); v.y = xTemp*sin(theta) + v.y*cos(theta); }*/ }

asteroidsGame.pde

Ship ship;

boolean upPressed = false;//CHANGE LEFT AND RIGHT TO UP AND DOWN( IN SHIP TOO) boolean downPressed = false; boolean rightPressed = false; boolean leftPressed = false;

float shipSpeed = 2; float bulletSpeed = 10;

int numAsteroids = 2; //the number of asteroids int startingRadius = 50; //the size of an asteroid

PImage asteroidPic; PImage rocket;

ArrayList bullets; ArrayList asteroids;

PFont font;

// game state variables int gameState; public final int INTRO = 1; public final int PLAY = 2; public final int PAUSE = 3; public final int GAMEOVER = 4;

void setup() { background(0); size(800,500); font = createFont("Cambria", 32); frameRate(24); asteroidPic = loadImage("asteroid.png"); rocket = loadImage("rocket.png"); asteroids = new ArrayList(0); gameState = INTRO; }

void draw() { switch(gameState) { case INTRO: drawScreen("Welcome!", "Press s to start"); break; case PAUSE: drawScreen("PAUSED", "Press p to resume"); break; case GAMEOVER: drawScreen("GAME OVER", "Press s to try again"); break; case PLAY: background(0); ship.update(); ship.render(); if(ship.checkCollision(asteroids) || asteroids.size() <=0) gameState = GAMEOVER; else { for(int i = 0; i < bullets.size(); i++) { bullets.get(i).update(); bullets.get(i).render(); if(bullets.get(i).checkCollision(asteroids)) { bullets.remove(i); i--; } } for(int i=0; i

//Initialize the game settings. Create ship, bullets, and asteroids void initializeGame() { ship = new Ship(); bullets = new ArrayList(); asteroids = new ArrayList(); for(int i = 0; i

// void fireBullet() { println("fire");//this line is for debugging purpose

PVector pos = new PVector(0, ship.r*2); rotate2D(pos,heading2D(ship.rotation) + PI/2); pos.add(ship.position); PVector vel = new PVector(0, bulletSpeed); rotate2D(vel, heading2D(ship.rotation) + PI/2); bullets.add(new Bullet(pos, vel)); }

void keyPressed() { if(key== 's' && ( gameState==INTRO || gameState==GAMEOVER )) { initializeGame(); gameState=PLAY; } if(key=='p' && gameState==PLAY) gameState=PAUSE; else if(key=='p' && gameState==PAUSE) gameState=PLAY; //when space key is pressed, fire a bullet if(key == ' ' && gameState == PLAY) fireBullet(); if(key==CODED && gameState == PLAY) { if(keyCode==UP) upPressed=true; else if(keyCode==DOWN) downPressed=true; else if(keyCode == LEFT) leftPressed = true; else if(keyCode==RIGHT) rightPressed = true; }

}

void keyReleased() { if(key==CODED) { if(keyCode==UP) { upPressed=false; ship.acceleration = new PVector(0,0); } else if(keyCode==DOWN) { downPressed=false; ship.acceleration = new PVector(0,0); } else if(keyCode==LEFT) leftPressed = false; else if(keyCode==RIGHT) rightPressed = false; } }

void drawScreen(String title, String instructions) { background(0,0,0); // draw title fill(255,100,0); textSize(60); textAlign(CENTER, BOTTOM); text(title, width/2, height/2); // draw instructions fill(255,255,255); textSize(32); textAlign(CENTER, TOP); text(instructions, width/2, height/2); }

float heading2D(PVector pvect) { return (float)(Math.atan2(pvect.y, pvect.x)); }

void rotate2D(PVector v, float theta) { float xTemp = v.x; v.x = v.x*cos(theta) - v.y*sin(theta); v.y = xTemp*sin(theta) + v.y*cos(theta); }

Add the following components to this game:

1. The harm of every bullet and the health of every asteroid. For example, if a bullet hits an asteroid, the asteroid will lose 10 points health. If an asteroid loses all health, it will be destroyed.

2. Score and timer, which decide the outcome of the game. For example, only when score is greater than 100 and the elapsed time is less than 30 seconds, you win the game. You need to decide how to relate score with the number of asteroid, the health of an asteroid, and the number of bullets.

3. Add audio effects to the game, such as background music and the sound effect when a collision happens

4. Add a screen of credits. Anytime when the players press a key (e.g. c), they can see an animated credit screen.

5. Make the number of bullets limited, say, 20 bullets. Every time when an asteroid is destroyed, you get 3 bonus bullets. Involve the number of bullets in the decision of game fail or win.

6. Add a special type of asteroids and a bullet hit has 30% chance to destroy the asteroid. So, these asteroids can only have two status: healthy or destroyed (unlike the regular asteroids)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago