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

 
Re: Attempting Project 5 from Learning Processing Book
Reply #1 - Dec 9th, 2009, 1:38am
 
First, I don't understand what you do there:
Code:
void draw() {
background(255);
}
{
if (mousePressed()) {
// A new Bullet
bullets[totalBullets] = new Bullet();
totalBullets ++ ;
}
// etc.

The if (mousePressed()) is outside of draw()!
Beside, it should be if (mousePressed) (using a boolean variable), the function form is intended to be called automatically by Processing when the event occurs, not to be called this way.

Other thing: you make a constructor with parameters for Bullet, but you don't use these parameters!
And since you don't provide them either, this cannot compile.

"how to write the scipt for the bullet to leave the same point as the moving 'gun'"
Use the 'gun' variable! Or, more exactly, its fields, holding the position of the gun.
Something like:
bullets[totalBullets] = new Bullet(gun.x, gun.y);
Adapt the given values to the top of the gun, using other fields as well.

BTW, you should use meaningful variable names, a, b, c, d, etc. are useless names, for those reading the code, including yourself in a near future... You added comments (good), include the information in the variable names, eg.:
rect(x, y, e+f, handleHeight);       // handle
(not sure about e and f, extrapolate!)

HTH.
Page Index Toggle Pages: 1