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 › video game errors
Page Index Toggle Pages: 1
video game errors (Read 682 times)
video game errors
Dec 23rd, 2009, 9:21am
 
Hello,

I am currently working on a video game using Processing, yet I've encountered a few errors which seem minor but I can't really figure out. I made my game like a 2D scroll screen where the character (a panda) moves through the screen and collects things, and avoids things.
I made each thing it collects its own Class, and i implemented this into my code. Yet for some reason the panda won't pick up items if it's positioning is ideal (like so the panda runs straight through the item).

Here is the code I have so far:

float page = 1; //title page
float x = 65;
float y = 260; // position of panda
float Jspeed = .9; //jumping variable
float backgroundx = 0; //background speed
float a;
float shake; //movement

int lives = 3;
int score = 0;

PImage panda;
PImage heart;
PImage fullbackground;
PImage fullbackground2;
PImage titlepage;
PImage title2;
//PImage platform;
//PImage platform2;
PImage duckimg;
PImage onigri;
PImage strawberryimg;
PImage failimg;
PImage winimg;
PImage beartrapimg;
PFont Herculanum;
int timer = 100;

Duck d1;
Duck d2;
Duck d3;
Duck d4;



boolean jump = false;
boolean run = false;
boolean loselife = false;
boolean incscore = false;

void setup(){
 size(600, 400);
 smooth();
 
 panda = loadImage("panda.png");
 titlepage = loadImage("titlepage.png");
 title2 = loadImage("title2.png");
 fullbackground = loadImage("fullbackground.png");
 fullbackground2 = loadImage("fullbackground2.png");
 failimg = loadImage("failimg.png");
 winimg = loadImage("winimg.png");
 duckimg = loadImage("duck.png");
 onigri = loadImage("onigri.png");
 strawberryimg = loadImage("strawberry.png");
 heart = loadImage("heart.png");
 //platform = loadImage("platform.png");
 //platform2 = loadImage("platform2.png");
 beartrapimg = loadImage("beartrapimg.png");
 Herculanum = loadFont ("Herculanum.vlw");
 textFont (Herculanum);
 
 d1 = new Duck (600, 300, 40, 50, 5);
}

void draw(){
 if (page == 1){
   image (titlepage, 0, 0);
   backgroundx = 600;
   lives = 3;
 }
 else if (page == 1.5){
   image (title2, 0, 0);
   backgroundx = 600;
   lives = 3;
 }
 else if (page == 2){
   backgroundx = backgroundx - 5;
   backgroundx = constrain(backgroundx, width-fullbackground.width+10, 0);
   image (fullbackground, backgroundx, 0, 3600, 400); //total background size
   
   d1.show ();

   d1.move ();
   
   d1.gather ();
   
   fill(0);
   textSize(24);
   text ("LIVES: " + lives, 450, 40);
   text ("SCORE: " + score, 50, 40);
   text ("TIME: " + timer, 250, 40);
   
   if (run == true){
     shake = cos(a)/60;
     a += 0.5;
     rotate(shake);
   }
   run = true;
   if (run = true){
     timer -= 1;
     image (panda, x, y, 150, 150);
   } else {
     if (timer < 100){
       run = true;
       jump = false;
     }
   }
 }
 else if (page == 3){
   image (winimg, 0, 0);
   fill (255);
   jump = false;
   run = false;
 }
 else if (page == 4){
   image (failimg, 0, 0);
   jump = false;
   run = false;
 }
 
 if (jump == true){
   y += Jspeed;
   Jspeed += 0.1;
   run = false;
   if (y > 260){
     jump = false;
     run = true;
     y = 260;
   }
 }
 
 if ((page == 2)&&(backgroundx == width-fullbackground.width+10)){
   page = 3;
 }
 else if ((page == 2)&&(lives == 0)){
   page = 4;
 }
}

void keyPressed(){
 if (key == ' '){
   if (page == 1){
     page = 1.5;
   }
   else if (page == 2){
     jump = true;
     Jspeed= -3;
   }
   else if (page == 2.5){
     jump = true;
     Jspeed = -3.5;
   }
   else if (page == 3){
     page = 1;
   }
   else if (page == 4){
     page = 1;
   }
 }
}
void mousePressed(){
 if (page == 1.5){
   if ((mouseX > 200)&&(mouseX < 400)&&(mouseY > 150)&&(mouseY < 250)){
     page = 2;
   }
   if ((mouseX > 200)&&(mouseX < 400)&&(mouseY > 275)&&(mouseY < 350)){
     page = 3.5;
   }
 }
}
class Duck{
 float Sx;
 float Sy;
 float Sxstart;    
 float Systart;
 float Swidth;
 float Sheight;
 float speed;
 boolean collect = false;
 int direction = -1;
 
class Duck{
 float Sx;
 float Sy;
 float Sxstart;    
 float Systart;
 float Swidth;
 float Sheight;
 float speed;
 boolean collect = false;
 int direction = -1;
 
 Duck (float xpos, float ypos, float Swidth1, float Sheight1, float sp){
   Sx = xpos;
   Sy = ypos;
   Sxstart = xpos;
   Systart = ypos;
   Swidth = Swidth1;
   Sheight = Sheight1;
   speed = sp;
 }
 void show(){
   if (collect == false){
     image(duckimg, Sx, Sy);
   }
   if (collect == true){
     image(heart, Sx, Sy);
   }
 }
 
 void move(){
   Sx += (speed*direction + 1);
   if (Sx < -duckimg.width){
     collect = false;
     Sx = Sxstart;
   }
 }
 
 void gather(){
   if (collect == false){
     if (dist (x, y, Sx, Sy) < Swidth){
       collect = true;
       loselife = false;
       incscore = true;
       score += 30;
     }
   }
 }
}

       
   
 
 
I took out some of the other classes and stuff just now and left only the one Duck.. since the answer to this question being applied to just this one class will help me for everything else as well.

Thanks for any help!
     
Re: video game errors
Reply #1 - Dec 23rd, 2009, 10:33am
 
So you have a problem with your panda, but you post code for the duck?  Roll Eyes
Re: video game errors
Reply #2 - Dec 23rd, 2009, 10:45am
 
Well the panda moves throughout the window fine, given its position, but it doesn't collect the duck. So i thought something was wrong with the duck class, or any other class that i would declare as an item.
Re: video game errors
Reply #3 - Dec 23rd, 2009, 12:53pm
 
I don't see any fundamental flaws in your duck's gather method.

But some tips to write better, readable code:
* Avoid long methods: try to create subsets of logic instead of one great blob of code
* if( collect == true ) can be written as : if ( collect )
* if( collect == false ) can be written as : if( !collect )
* Learn about subclassing, object oriented programming, I have not seen the rest of your code but I suspect there will be quite some code duplication (correct me if I'm wrong)
* Use arrays: I see Ducks d1, d2, d3 and d4, it looks like those should be in an array

Re: video game errors
Reply #4 - Dec 23rd, 2009, 1:16pm
 
Yeah, you're right, JR, and thanks. So far, I've just been teaching this to myself, and it's kind of different than what I'm used to (java, not javascript), but I started going over my code again, and I'm implementing arrays for each set of items that I've added in.
Page Index Toggle Pages: 1