Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am using processing for my engine. Download the file “asteriodsGame.zip” in Bl

ID: 3920286 • Letter: I

Question

I am using processing for my engine.

Download the file “asteriodsGame.zip” in Blackboard -> CourseContent -> Example Programs. Unzip it and try out the skeletal game. There is some basic activities, but the software is strictly a toy: there are no goals. Your task will be to design some structure to enhance the game.

Try to consider the following questions:

Goals: what goals does the player have in the game?

Choice: what choices does the player make? Resources: what resources are in the game? How will the player spend the resources? How will new resources be acquired?

Variety of Encounter: how can you increase the variety of encounter?

Based on the above questions, 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. (30 points)

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. (40 points)

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

4. Add a screen of credits. Anytime when the players press a key (e.g. ‘c’), they can see an animated credit screen. An example function is shown in the slides of Tutorial 6. (10 points)

Bonus parts:

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. (10 points)

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).(15 points)

--AsteroidGame--
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<Bullet> bullets;
ArrayList<Asteroid> 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<Asteroid>(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<asteroids.size(); i++)//(Asteroid a : asteroids)
{
asteroids.get(i).update();   
asteroids.get(i).render();
}
  
float theta = heading2D(ship.rotation)+PI/2;   

if(leftPressed)
rotate2D(ship.rotation,-radians(5));
  
if(rightPressed)
rotate2D(ship.rotation, radians(5));

if(upPressed)
{
ship.acceleration = new PVector(0,shipSpeed);
rotate2D(ship.acceleration, theta);
}   
  
}
break;
}
}

//Initialize the game settings. Create ship, bullets, and asteroids
void initializeGame()
{
ship = new Ship();
bullets = new ArrayList<Bullet>();
asteroids = new ArrayList<Asteroid>();

for(int i = 0; i <numAsteroids; i++)
{
PVector position = new PVector((int)(Math.random()*width), 50);   
asteroids.add(new Asteroid(position, startingRadius, asteroidPic));
}
}


//
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);
}

--Asteroid--

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<Asteroid> 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--

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<Asteroid> 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--

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<Asteroid> 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);
}*/

}

Explanation / Answer

package asteroids;

import java.awt.Point;

import java.awt.event.KeyEvent;

import java.awt.geom.Rectangle2D;

import java.util.LinkedList;

import java.util.List;

import java.util.ListIterator;

import java.util.Random;

import processing.core.PApplet;

/**

*

* This class is the logic center of the game. It controls the object list

* handling all operations such as adding/destroying objects, monitoring collisions,

* etc.

*/

class Game {

private PApplet canvas;

private List spaceThings;

private List createables;

private ListIterator li;

private ListIterator createablesLi;

private SpaceThing target;

Random rand = new Random();

private int gameState;

private int frame;

private Ship ship;

private int level;

private int score;

private int asteroidsRemaining;

private int shipsRemaining;

private int bulletsActive;

private int powerUps;

// These constants make switches much nicer

final int SHIP = 1;

final int ASTEROID = 2;

final int BULLET = 3;

final int NUKE = 4;

final int MENU = 0;

final int PLAYING = 1;

final int GAMEOVER = 2;

/**

*

* @param papp Main PApplet of game.

*

*/

public Game(PApplet papp) {

canvas = papp;

}

public void initMenu() {

gameState = MENU;

}

/**

* Resets all object lists, scores, etc and inits level 1.

*/

public void newGame() {

spaceThings = new LinkedList();

createables = new LinkedList();

shipsRemaining = 3;

score = 0;

createThing(SHIP);

initLevel(1);

gameState = PLAYING;

}

public void gameOver() {

spaceThings = null;

createables = null;

gameState = GAMEOVER;

}

/**

* Creates a level with newLeveL + 2 ships

* @param newLevel

*/

public void initLevel(int newLevel) {

level = newLevel;

for(int i=0; i<level+2; i++) {

createThing(ASTEROID);

}

}

/**

* Creates Ships, Asteroids, Bullets, etc and adds them to the main

* object list.

* @param thing type of SpaceThing. Ship, Asteroid or Bullet

*/

public void createThing(int thing) {

li = spaceThings.listIterator();

switch(thing) {

case SHIP:

li.add(new Ship(canvas));

break;

case ASTEROID:

li.add(new Asteroid(canvas, 1+level));

break;

}

}

/**

* Removes thing from object list.

* @param thing

*/

public void destroyThing(SpaceThing thing) {

li = spaceThings.listIterator();

li.remove();

}

public void draw() {

canvas.background(0);

if (gameState == PLAYING) {

drawObjects();

drawHud();

if(asteroidsRemaining <= 0) {

initLevel(level+1);

}

if(shipsRemaining < 0) {

gameOver();

}

} else if (gameState == MENU) {

drawMenu();

} else if (gameState == GAMEOVER) {

drawGameOver();

}

}

/**

* This is drawn while gamestate is MENU. Controls, enter to play, etc.

*/

public void drawMenu() {

canvas.textAlign(PApplet.CENTER);

canvas.text("BAD ASTEROIDS", canvas.width/2, 40);

canvas.text("By Zac Stewart", canvas.width/2, 60);

canvas.text("zgstewart@mail.com", canvas.width/2, 75);

canvas.text("Controls:", canvas.width/2, 105);

canvas.text("W - Accelerate", canvas.width/2, 125);

canvas.text("A - Rotate Left", canvas.width/2, 140);

canvas.text("D - Rotate Right", canvas.width/2, 155);

canvas.text("Space - Fire Primary", canvas.width/2, 170);

canvas.text("Use the mouse to target and fire nukes", canvas.width/2, 200);

canvas.text("Press enter to begin", canvas.width/2, 230);

}

/**

* :( Score, go back to main menu.

*/

public void drawGameOver() {

canvas.textAlign(PApplet.CENTER);

canvas.text("GAME OVER", canvas.width/2, 40);

canvas.text("Score: " + score, canvas.width/2, 60);

canvas.text("Insert Coins to Continue", canvas.width/2, 80);

canvas.text("Or Press Enter", canvas.width/2, 95);

}

/**

* Displays game info like score, ships remaining, etc

*/

public void drawHud() {

canvas.stroke(255);

canvas.fill(255);

canvas.textAlign(PApplet.LEFT);

canvas.text("Level: " + level, 10, 20);

canvas.text("Ships: " + shipsRemaining, 10, 40);

canvas.text("Nukes: " + ship.getNukes(), 10, 60);

canvas.textAlign(PApplet.RIGHT);

canvas.text("Score: " + score, canvas.width-10, 20);

}

/**

* Lots going on in here. Main control center for game. Sets the ship

* var to the current Ship, removes dead objects, adds items in each objects

* createable array to the object list and clears said array. Monitors

* collisions via getCollision() and then enacts the appropriate collision.

*/

public void drawObjects() {

frame += 1;

li = spaceThings.listIterator();

createablesLi = createables.listIterator();

asteroidsRemaining = 0;

powerUps = 0;

target = null;

while(li.hasNext()) {

Object x = li.next();

if(x instanceof Ship) {

ship = (Ship) x;

shipsRemaining += ship.incShips;

ship.incShips = 0;

} else if (x instanceof Asteroid) {

Point mousePos = canvas.getMousePosition();

if (mousePos != null) {

if (((Asteroid) x).getBounds().contains(mousePos.x, mousePos.y)) {

target = (SpaceThing) x;

}

}

asteroidsRemaining += 1;

} else if (x instanceof PowerUp) {

powerUps += 1;

}

if(x instanceof SpaceThing) {

SpaceThing s = (SpaceThing) x;

if(s.createable != null) {

for(SpaceThing createable : s.createable) {

if(!(createable instanceof Bullet) || bulletsActive < 3) {

if(createable instanceof Bullet) {

bulletsActive += 1;

}

createablesLi.add(createable);

}

}

s.createable = null;

}

if (s.remove) {

if (s instanceof Ship) {

shipsRemaining += ((Ship) s).incShips;

((Ship) s).incShips = 0;

bulletsActive = 0;

} else if (s instanceof Bullet) {

bulletsActive -= 1;

} else if (s instanceof Asteroid) {

score += 10;

}

li.remove();

} else {

s.draw();

SpaceThing collidingObject = getCollision(s);

if(collidingObject instanceof SpaceThing){

s.collide(collidingObject);

if(s.createable != null) {

for(SpaceThing createable : s.createable) {

createablesLi.add(createable);

}

s.createable = null;

}

}

}

}

}

// add powerups

if(frame % 100 == 0 && powerUps <= 0) {

int coinToss = rand.nextInt(2);

int type = (coinToss == 1 ? SHIP : NUKE);

li.add(new PowerUp(canvas, type));

}

insertCreateables();

}

/**

* This loops through the createables and adds them to the object list.

* if the ship's spawn place is blocked by an asteroid, it waits until

* next iteration.

*/

public void insertCreateables() {

li = spaceThings.listIterator();

createablesLi = createables.listIterator();

while(createablesLi.hasNext()) {

Object createable = createablesLi.next();

if(createable instanceof Ship) {

if(collisionAtPoint(canvas.width/2,canvas.height/2) == null) {

li.add(createable);

createablesLi.remove();

}

} else {

li.add(createable);

createablesLi.remove();

}

}

}

/**

* this is used to determine if there is an object at an given x, y pair.

* @param xPoint

* @param yPoint

* @return

*/

public SpaceThing collisionAtPoint(int xPoint, int yPoint) {

Rectangle2D ghostShip = new Rectangle2D.Float(canvas.width/2, canvas.height/2, 20, 20);

ListIterator it = spaceThings.listIterator();

while(it.hasNext()) {

Object x = it.next();

if(x instanceof Asteroid) {

Asteroid object = (Asteroid) x;

if(object.collides(ghostShip)) {

return object;

}

}

}

return null;

}

/**

* Nested ListIterator loops to check for collisions between object and

* all other items in the object list. Returns whatever SpaceThing is

* colliding with object.

* @param object

* @return SpaceThing

*/

public SpaceThing getCollision(SpaceThing object) {

ListIterator it = spaceThings.listIterator();

while(it.hasNext()) {

Object x = it.next();

if(object instanceof Asteroid) {

if(((Asteroid) object).collides(x)) {

return (SpaceThing) x;

}

}

if(object instanceof PowerUp && x instanceof Ship) {

if(((PowerUp) object).collides((SpaceThing) x)) {

return (SpaceThing) x;

}

}

}

return null;

}

/**

* Key monitor. Maps keys and space keys to corresponding ship

* methods or game methods depending on gameState.

* @param type

* @param e

*/

public void control(String type, KeyEvent e) {

// System.out.println(e);

switch(gameState) {

case PLAYING: {

if(type.equals("keyDown")) {

switch(e.getKeyCode()) {

case 87:

ship.setAccelerating(true);

break;

case 65:

ship.setRotatingLeft(true);

break;

case 68:

ship.setRotatingRight(true);

break;

case 32:

ship.firePrimary();

break;

default:

break;

}

} else if(type.equals("keyUp")) {

switch(e.getKeyCode()) {

case 87:

ship.setAccelerating(false);

break;

case 65:

ship.setRotatingLeft(false);

break;

case 68:

ship.setRotatingRight(false);

break;

default:

break;

}

}

break;

}

case MENU: {

if(type.equals("keyDown")) {

switch(e.getKeyCode()) {

case 10:

newGame();

}

}

break;

}

case GAMEOVER: {

if(type.equals("keyDown")) {

switch(e.getKeyCode()) {

case 10:

initMenu();

}

}

break;

}

}

}

/**

* Mouse click monitor. Targets and fires nukes. Protip: click an asteroid

* and it tracks the asteroid. Click space and it just blows up on that point.

*/

public void mouseControl() {

switch(gameState) {

case PLAYING: {

if(target != null) {

ship.fireNuke(target);

} else {

ship.fireNuke();

}

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote