rpw029
YaBB Newbies
Offline
Posts: 42
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!