How to finish this Ender's Game script in Processing? -


   /* draw laser beam start point in direction of end point  have stop if bumps rock.*/  int lasercolor = color(255, 0, 0); int rockcolor = color(0, 255, 0) ; int startx = (1); int starty = (1); int laserx = (10); int lasery = (20); int laserdx = (30); int laserdy = (40);  float r = 100; float g = 150; float b = 200; float = 200; float f = 100;  float diam = 20; float dim = 70;  float x = 100; float y = 100; float z = 20; int t = 100; int s = 100; int w = 60; int h = 60; int eyesize = 16;  void setup() {   size(400, 400);   background(239, 276, 238); }  void draw() {   drawplayer();   drawrocks();   drawwall();   drawbeam();   background(255);   if (mousepressed) {     a--;     fill(0);     text("score" + a, 325, 10);   }   {   void drawplayer(){       // draw player's head   fill(255);   ellipse(x,y,w,h);    // draw player's eyes   fill(0);   ellipse(x-w/3+1,y,eyesize,eyesize*2);   ellipse(x+w/3-1,y,eyesize,eyesize*2);   }    void drawrocks(){     //draw rocks     fill(255);     ellipse(x, y+(x/2), diam, diam);      fill(255);     ellipse(x+(3*x), y+(.5*x), diam, diam);      fill (255);     ellipse(x+(2*x), y+y, diam, diam);      fill(255);     ellipse(x+y, y+y, diam, diam);   }    void drawwall(){     // draw wall    stroke(0);     fill(f, w, f, a);   rect(x+(1.5*x), (y-(.05*z)), dim, diam);   }    void drawbeam(int startx, int starty, int endx, int endy) {     float laserx, lasery, laserdx, laserdy;     stroke(156, 255, 0);     strokeweight(5);     float distance = dist(endx, endy, startx, starty);   }   } } {  // expand reach of beam 1 pixel per loop iteration // doesn't skip on  laserdx = (endx-startx)/distance; laserdy = (endy-starty)/distance;  // make sure both laserdx , laserdy aren't 0 or loop never end if (laserdx == 0 && laserdy == 0) {   return; // don't draw }  // lx,ly track beam end float lx = startx;  float ly = starty;  // move along in direction of beam until hit edge or object while (get((int)lx, (int)ly) != rockcolor && lx > 0 && lx < width && ly > 0 && ly < height) {   lx = lx + laserdx;   ly = ly + laserdy; } // found end of laser beam - draw line(startx, starty, lx, ly); } 

for reason comes error. don't know why functions aren't working , don't know how declare laser , whatnot. have more code right i'm trying clean have because know basic outline of have do.

stack overflow isn't designed general "how do this" type questions. it's designed more specific "i tried x, expected y, got z instead" type questions. thing code there million ways 1 thing, , approach take depends more on over us. can hard if don't have specific question. being said, i'll try in general sense:

first off, code doesn't compile. drawrocks() , drawbeam() functions defined inside draw() function, not valid syntax. can't have functions inside other functions that. in other words, this wrong:

void draw(){     void drawrocks(){       //draw rocks    }     void drawbeam(){       //draw beam    } } 

you need define of functions @ same level, this:

void draw(){    drawrocks();    drawbeam(); }  void drawrocks(){    //draw rocks }  void drawbeam(){    //draw beam } 

notice how i'm defining functions @ same level, , calling drawrocks() , drawbeam() functions draw() function.

but moving drawbeam() function outside draw() function won't work, since have variables inside function you're trying use outside of it. doesn't make ton of sense.

the best advice can give start smaller. seem biting off more should chewing right now, common mistake novices (and non-novices) make.

start on more basic sketch one thing. single circle moving around screen. working before move on. make circle bounces off edges of screen. make circle bounces off square in middle of screen.

work way in small steps that. start small. smaller think interesting. don't try take on whole game @ once. work in small example programs, way if stuck you'll able post mcve along specific question. luck.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -