We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Attempting Project 5 from Learning Processing Book
Page Index Toggle Pages: 1
Attempting Project 5 from Learning Processing Book (Read 152 times)
Attempting Project 5 from Learning Processing Book
Dec 8th, 2009, 4:58pm
 
Hi Everyone,

I have been trying in vain to make a very basic 'game' for the Project 5 task. My aim is to have 2 basic objects moving up and down (so stationary in the x-axis). On the left is a 'gun' and on the right is a stick man, my aim is that the gun with shout out a 'bullet' everytime the mouse is clicked. If it hits the stick man, the face changes to acknowledge it.
..my problem lies in that I cannot figure out how to write a script for a 'bullet' object that appears and moves out of the gun barrel from left to right when the mouse is clicked. 1) I do not know how to write the scipt for the bullet to leave the same point as the moving 'gun' as it is set on random y-axial movement and 2 how to set the programme that more bullets can occur in the same fashion by clicking the mouse. Do i use Array?? How?!?!

Please could someone help me out!!! This is really getting me stuck and I've gotten no where over the last stretch of time!!

Many Thanks,

Tom


Here is the script so far;

Gun gun;
Stickman stickman;


void setup() {
 size(600,400);
 gun = new Gun(100,0,50,40,30,20,10,5);
 stickman = new Stickman(500,0,50,40,30,20,10,5);
 smooth();
 }


void draw() {
 background(255);
 
 gun.move();
 gun.display();
 

 stickman.move();
 stickman.display();
 
 
}

class Gun {

 float x,y; // location
 float a;
 float b;
 float c;
 float d;
 float e;
 float f;
 float xspeed,yspeed; // speed
 // Constructor
 Gun(float tempX, float tempY, float tempA, float tempB, float tempC, float tempD, float tempE, float tempF) {
   x = tempX;
   y = tempY;
   a = tempA;
   b = tempB;
   c = tempC;
   d = tempD;
   e = tempE;
   f = tempF;
 

   yspeed = random(0,15);
 }
   
 void move() {
    y += yspeed; // Increment y
   
    // Check vertical edges
   if (y > height || y < 0) {
     yspeed *= - 1;
   }

 }
 
 // Draw the Gun
 void display() {
   stroke(0);
   fill(0,0,0);
   rect(x,y,e+f,a);       // handle
   rect(x,y-(e),a+d,e+f); // barrel
   rect(x+e,y-(d),f,e);   // trigger
   rect(x+d+f,y+f,f,e);   // saftey thing
   rect(x+e,y+e,d,f);   // saftey thing
 }
}

class Stickman {

 float x,y; // location
 float a;
 float b;
 float c;
 float d;
 float e;
 float f;
 float xspeed,yspeed; // speed
 // Constructor
 Stickman(float tempX, float tempY, float tempA, float tempB, float tempC, float tempD, float tempE, float tempF) {
   x = tempX;
   y = tempY;
   a = tempA;
   b = tempB;
   c = tempC;
   d = tempD;
   e = tempE;
   f = tempF;
 

   yspeed = random(5,10);
 }
 
 void move() {
   y += yspeed; // Increment y
   
   // Check vertical edges
   if (y > height-60 || y < 0) {
     yspeed *= - 1;
   }
   
   }
 
 // Draw the Stickman
 void display() {
   stroke(0);
   fill(100,100);
   ellipse(x,y,c,c);        // head
   ellipse(x-f,y-f/2,f/2,f/2);        // left eye
   ellipse(x+f,y-f/2,f/2,f/2);        // right eye
   line(x,y+d-f,x,y+a+e);   // body
   line(x-d,y+c,x+d,y+c);   // arms
   line(x-d,y+(c*5/2),x,y+(c*2));   // left leg
   line(x,y+(c*2),x+d,y+(c*5/2));   // right leg
   line(x+f,y+3/2*f,x-f,y+3/2*f);   // mouth
   
 }
}




THEN I HAVE ATTEMPTED THE BULLET AND THAT A NEW ONE OCCURS WHEN MOUSE IS CLICKED.....


Bullet[] bullets;
int totalBullets = 0; // totalBullets

void setup() {
 size(600,400);
 smooth();

 bullets = new Bullet[1];
 }


void draw() {
 background(255);
}
{
   if (mousePressed()) {
     // A new Bullet
   bullets[totalBullets] = new Bullet();
   totalBullets ++ ;
   }

for (int i = 0; i < totalBullets; i++ ) {
   bullets[i].move();
   bullets[i].display();
   
}
}

class Bullet {
 float x,y;  
 float a;
 float xspeed;
 color c;
 
 Bullet (float tempX, float tempY, float tempA) {
   
   x = 100;
   y = random(height);
   a = 16;
   
   xspeed = 2;
   c = color(50,100,150);
 }
   void move() {
    x += xspeed; // Increment y
   }
   void display() {
   // Display the bullets
   rectMode(CENTER);
   fill(100,100,100);
   noStroke();
   ellipse(x,y,a,a);
   rect(x-5,y,a,a);
   }
   }




Cheers for any help you can over me!!

I am really stuck and would very much appreciate help.

Thanks again,

Tom
 

 
Page Index Toggle Pages: 1