Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
hka403
hka403's Profile
1
Posts
2
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Please Help I need to figure out this code Due in 12 hours! Space Invaders! Lives not working.
[4 Replies]
22-May-2012 06:42 PM
Forum:
Programming Questions
//My Program
//Thank you for hndrsn6 on the processing forum for giving me some of the program.
//this helps with classifying the sound
import ddf.minim.*;
Minim minim;
//AudioSample bg;
//AudioSample shoot;
//Classifies the spaceships shooting
Spaceship mySpaceship;
ArrayList bullets;
ArrayList bombs;
//Classifies the variables that the aliens use for positioning. (from hndrsn6)
float distance_enemy;
int lives;
int cols= 10;
int rows =5;
int cols_select;
int rows_select;
Alien[][] aliens = new Alien[cols][rows];
//Timer used to control how often the enemies shoot.
Timer timer;
//(from hndrsn6)
boolean gameOn=true;
int deadAliens=0;
int lastGuy=0;
void setup() {
size(1200, 700);
smooth();
lives = 5;
//initialize the Spaceship object, call for constructor; (from hndrsn6)
mySpaceship = new Spaceship(200, 200, 200, 500, 700);
//initialize the bulletArrayList (from hndrsn6)
bullets = new ArrayList();
bombs = new ArrayList();
//initialize the AlienArray (from hndrsn6)
for (int i=0; i< cols; i++) {
for (int j=0; j<rows; j++) {
aliens[i][j] = new Alien(140, 300, 300, 360, i*100, j*80, 1);
}
}
//starts the Timer
timer = new Timer(350);
timer.start();
//finds the sound in the game.
// minim = new Minim(this);
// bg = minim.loadSample("3.mp3", 2000);
// bg.trigger();
}
void draw() {
//creates background with slight opacity for cooler effect.
fill(0, 50);
rect(0, 0, width, height);
fill(255);
textSize(15);
text("Instructions:", 150, 90);
text("Control the spaceship's movement along the x axis by moving your mouse", 150, 110);
text("Click to fire", 150, 130);
text("Don't get hit by the bullets!", 150, 150);
textSize(50);
fill (0, 255, 0);
text("Shape Invaders", width/2 - 175, 50);
float c = (random(4));
if (c > 3) {
fill(#FCFC00);
}
else {
fill(#FFFFFF);
}
ellipse (random(width), random(height), 5, 5);
// mySpaceship.die();
mySpaceship.display();
mySpaceship.move();
mySpaceship.limit_x();
mySpaceship.lastGuy();
// from hndrsn6
for (int i=0; i< cols; i++) {
for (int j=0; j<rows; j++) {
aliens[i][j].display();
aliens[i][j].move();
aliens[i][j].shift();
aliens[i][j].exitStageLeft();
}
}
for (int i=0; i < bullets.size(); i++) {
Bullet b = (Bullet) bullets.get(i);
b.display();
b.fire();
if (b.finished()) {
bullets.remove(b);
}
for (int k=0; k< cols; k++) {
for (int j=0; j<rows; j++) {
if (b.intersect(aliens[k][j])) {
bullets.remove(b);
aliens[k][j].destroyed();
deadAliens=deadAliens+1;
}
}
}
}
for (int i=0; i < bombs.size(); i++) {
Bomb b = (Bomb) bombs.get(i);
b.display();
b.fire();
if (b.finished()) {
bombs.remove(b);
}
}
for (int i=0; i< cols; i++) {
for (int j=0; j<rows; j++) {
int cols_select=int(random(10));
int rows_select=int(random(5));
if (timer.isFinished() && aliens[cols_select][rows_select].imaDeadAlien==false) {
bombs.add(new Bomb(140, 300, 300, aliens[cols_select][rows_select].true_xpos, aliens[cols_select][rows_select].true_ypos, -10));
timer.start();
}
}
}
fill(255, 0, 0);
textSize(22.5);
text ("Lives = " + lives, 1060, 30);
float y = 700;
float distance = dist(xpos, ypos, mouseX, y);
if (distance < 25) {
lives = lives - 1;
}
else {
lives = lives - 0;
}
if (lives< 1) {
fill(0);
rect (0, 0, width, height);
fill(255);
textSize(200);
text("Game Over", 50, width / 2 - 200);
}
}
//end draw
void mousePressed() {
bullets.add(new Bullet(int(random(180, 220)), 200, 200, mySpaceship.xpos, 880, 10));
shoot = minim.loadSample("LASER6.mp3", 2048);
shoot.trigger();
}
//end mousePressed
class Alien {
color h;
color s;
color b;
float a;
float xpos;
float true_xpos;
float true_ypos;
float ypos;
float xspeed;
float d;
float r;
boolean imaDeadAlien=false;
Alien(color h_, color s_, color b_, float a_, float xpos_, float ypos_, float xspeed_) {
h= h_;
s= s_;
b= b_;
a=a_;
xpos = xpos_;
ypos = ypos_;
xspeed = xspeed_;
}
void display() {
noStroke();
fill(#03FF6D);
r=40;
true_xpos=xpos+200;
true_ypos=ypos+200;
ellipse(true_xpos, true_ypos, r, r);
fill(#FF0000);
ellipse(true_xpos - 6.5, true_ypos - 4, 5, 5);
ellipse(true_xpos + 6.5, true_ypos - 4, 5, 5);
}
void move() {
xpos = xpos + xspeed;
}
void shift() {
if (xpos+200 >= width-100) {
ypos=ypos+40;
xspeed=(xspeed*(-1));
}
if (xpos+200<= 100) {
ypos=ypos+40;
xspeed=xspeed*(-1);
}
}
void destroyed() {
imaDeadAlien=true;
}
void exitStageLeft() {
if (imaDeadAlien==true) {
xpos=-2000;
xspeed=0;
}
}
}
class Bomb {
int h;
int s;
int b;
float xpos;
float ypos;
float yspeed;
Bomb(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos, float temp_yspeed) {
h= temp_h;
s= temp_s;
b= temp_b;
xpos = temp_xpos;
ypos = temp_ypos;
yspeed = temp_yspeed;
}
void display() {
fill(h, s, b);
rect(xpos, ypos, 4, 10);
}
void fire() {
ypos=ypos-yspeed;
}
boolean finished() {
if (ypos-yspeed < 50 || ypos-yspeed > 950) return true;
else return false;
}
boolean intersect(Alien a) {
float distance = dist(xpos, ypos, a.true_xpos, a.true_ypos);
if (distance < 20) {
return true;
}
else {
return false;
}
}
}
class Bullet {
int h;
int s;
int b;
float xpos;
float ypos;
float yspeed;
Bullet(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos, float temp_yspeed) {
h= temp_h;
s= temp_s;
b= temp_b;
xpos = temp_xpos;
ypos = temp_ypos;
yspeed = temp_yspeed;
}
void display() {
fill(h, s, b);
rect(xpos, ypos, 4, 10);
}
void fire() {
ypos=ypos-yspeed;
}
boolean finished() {
if (ypos-yspeed < 50 || ypos-yspeed > 950) return true;
else return false;
}
boolean intersect(Alien a) {
float distance = dist(xpos, ypos, a.true_xpos, a.true_ypos);
if (distance < 20) {
return true;
}
else {
return false;
}
}
}
class Spaceship {
//declare variables
int h;
int s;
int b;
float xpos;
float ypos;
//constructor
Spaceship(int temp_h, int temp_s, int temp_b, float temp_xpos, float temp_ypos) {
h=temp_h;
s=temp_s;
b=temp_b;
xpos=temp_xpos;
ypos=temp_ypos;
}
void display() {
noStroke();
fill(h, s, b);
ellipse(xpos, ypos, 50, 50);
}
void move() {
xpos=mouseX;
noCursor();
}
void limit_x() {
if (mouseX<100) {
xpos=100;
}
if (mouseX>(width-100)) {
xpos=(width-100);
}
}
void lastGuy() {
println(deadAliens);
if (deadAliens==49) {
lastGuy=1000;
}
else {
lastGuy=0;
}
}
}
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime) {
totalTime=tempTotalTime;
}
void start() {
savedTime= millis();
}
boolean isFinished() {
int passedTime = millis()-savedTime-(lastGuy);
if (passedTime > totalTime) {
return true;
}
else {
return false;
}
}
}
float xpos;
float ypos;
float temp_xpos = xpos;
float temp_ypos = ypos ;
void stop() {
//bg.close();
}
«Prev
Next »
Moderate user : hka403
Forum