This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#26 2012-04-10 18:43:23

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Processing Topic

Uh okay here's an experimental version of the game Shots I made without the game part.

Code:

ArrayList shooters = new ArrayList();
int stimer,ctimer,gtimer,osec,firedelay,shotNum;
boolean fr = false;
float maxAll;
PHist hist;
void setup(){
  size(700,500);
  background(#FFEEEE);
  text("Shots",width/2-10,height/2-50);
  smooth();
  shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
  hist = new PHist(0,height,width,(height/4)*3,8);
  osec = 2;
  firedelay = 59;
}
void draw(){
  background(#445599);
  maxAll = 0;
  for (int i = 0; i < shooters.size(); i++) {
    Shooter shooter = (Shooter) shooters.get(i);
    float maxShot = shooter.update();
    shotNum+=shooter.shots.size();
    maxAll = max( maxAll, maxShot );
    for(int j=0; j < shooter.shotSpeeds.length; j++){
      hist.dumpValue(shooter.shotSpeeds[j]);
    }
  }
  hist.onDraw();
  hist.clearBins();
  shotNum=0;
  stimer++;
  ctimer++;
  if (osec != second()){
    gtimer++;
    osec = second();
  }
  if (stimer==firedelay) stimer=0;
  if (ctimer==199){
    shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
    ctimer=0;
  }
  if (fr) println(frameRate);
  println(" ");
}
class Shooter{
  int x,y,r;
  int ofd = firedelay;
  int[] colour = new int[3];
  int tnum = int(random(0,firedelay));
  ArrayList shots = new ArrayList();
  float[] shotSpeeds;
  Shooter (int x_, int y_){
    x = x_;
    y = y_;
    r = 20; 
    colour[0] = int(random(0,255));
    colour[1] = int(random(0,255));
    colour[2] = int(random(0,255));
  }
  float update(){
    if (firedelay != ofd){if(tnum>firedelay) tnum = int(random(0,firedelay));ofd=firedelay;}
    fill(colour[0],colour[1],colour[2]);
    if (stimer==tnum) shots.add(new Shot());
    rect(x,y,r,r);
    float maxSpeed = 0;
    float sumSpeed = 0;
    shotSpeeds = new float[shots.size()+1];
    for (int i = 0; i < shots.size(); i++) {
      Shot shot = (Shot) shots.get(i);
      shot.update();
      shotSpeeds[i] = shot.vel.mag();
      maxSpeed = max( maxSpeed, shot.vel.mag() );
      sumSpeed += shot.vel.mag();
    }
    return maxSpeed;
  }
  class Shot{      
    int r = 5;
    PVector vel   = new PVector( random(-.1,.1),random(-.1,.1) );
    PVector loc   = new PVector( x+r/2, y+r/2 );
    PVector accel = new PVector(0,0);
    void update(){
      accel.x = ( random(-.5,.5) );
      accel.y = ( random(-.5,.5) );
      ellipse( loc.x, loc.y, r*2, r*2 );
      if (   loc.y + r >= height 
          || loc.y - r <= 0) {
          vel.y *=-1;
      }
      if (   loc.x + r >= width 
          || loc.x - r <= 0) {
          vel.x *=-1;
      }
      vel.add(accel);
      loc.add(vel);
    }
  }
}
class PHist{
 int x,y;
 int updateSpeed,oupdateSpeed;
 int w;
 int h;
 int nbins;
 int[] binValues;
 color[] binColours;
 PHist () {
   x = 0;
   y = height;
   w = width;
   h = height/2;
   nbins = 10;
   binValues = new int[nbins];
   binColours = new color[nbins];
   updateSpeed = 29;
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 20*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }

 };
 PHist ( int xi, int yi, int wi, int hi, int nbinsi) {
   x = xi;
   y = yi;
   w = wi;
   h = hi;
   nbins = nbinsi;
   oupdateSpeed = updateSpeed;
   binValues = new int[nbins];
   binColours = new color[nbins];
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 1*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }
 }
 void clearBins(){
   for ( int ii = 0; ii < nbins; ii++ ){
     binValues[ii] = 0;
   }
 }
 void dumpValue( float value ){
     for ( int jj = 0; jj < nbins; jj++ ){
         if ( value > jj*maxAll/nbins && value < (jj + 1)*maxAll/nbins )
             binValues[ jj ] += h/shotNum;
     }
 }
 void onDraw(){
       for ( int ii = 0; ii < nbins; ii++ ) {
         fill( binColours[ii] );
         rect( x+ii*w/nbins, y-binValues[ii], w/nbins, constrain(binValues[ii],0,h) );
       }
 }
}

Posts: 20000 - Show all posts

Offline

 

#27 2012-04-10 21:00:10

scratchisthebest
Scratcher
Registered: 2009-02-08
Posts: 500+

Re: Processing Topic

veggieman001 wrote:

Uh okay here's an experimental version of the game Shots I made without the game part.

Code:

ArrayList shooters = new ArrayList();
int stimer,ctimer,gtimer,osec,firedelay,shotNum;
boolean fr = false;
float maxAll;
PHist hist;
void setup(){
  size(700,500);
  background(#FFEEEE);
  text("Shots",width/2-10,height/2-50);
  smooth();
  shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
  hist = new PHist(0,height,width,(height/4)*3,8);
  osec = 2;
  firedelay = 59;
}
void draw(){
  background(#445599);
  maxAll = 0;
  for (int i = 0; i < shooters.size(); i++) {
    Shooter shooter = (Shooter) shooters.get(i);
    float maxShot = shooter.update();
    shotNum+=shooter.shots.size();
    maxAll = max( maxAll, maxShot );
    for(int j=0; j < shooter.shotSpeeds.length; j++){
      hist.dumpValue(shooter.shotSpeeds[j]);
    }
  }
  hist.onDraw();
  hist.clearBins();
  shotNum=0;
  stimer++;
  ctimer++;
  if (osec != second()){
    gtimer++;
    osec = second();
  }
  if (stimer==firedelay) stimer=0;
  if (ctimer==199){
    shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
    ctimer=0;
  }
  if (fr) println(frameRate);
  println(" ");
}
class Shooter{
  int x,y,r;
  int ofd = firedelay;
  int[] colour = new int[3];
  int tnum = int(random(0,firedelay));
  ArrayList shots = new ArrayList();
  float[] shotSpeeds;
  Shooter (int x_, int y_){
    x = x_;
    y = y_;
    r = 20; 
    colour[0] = int(random(0,255));
    colour[1] = int(random(0,255));
    colour[2] = int(random(0,255));
  }
  float update(){
    if (firedelay != ofd){if(tnum>firedelay) tnum = int(random(0,firedelay));ofd=firedelay;}
    fill(colour[0],colour[1],colour[2]);
    if (stimer==tnum) shots.add(new Shot());
    rect(x,y,r,r);
    float maxSpeed = 0;
    float sumSpeed = 0;
    shotSpeeds = new float[shots.size()+1];
    for (int i = 0; i < shots.size(); i++) {
      Shot shot = (Shot) shots.get(i);
      shot.update();
      shotSpeeds[i] = shot.vel.mag();
      maxSpeed = max( maxSpeed, shot.vel.mag() );
      sumSpeed += shot.vel.mag();
    }
    return maxSpeed;
  }
  class Shot{      
    int r = 5;
    PVector vel   = new PVector( random(-.1,.1),random(-.1,.1) );
    PVector loc   = new PVector( x+r/2, y+r/2 );
    PVector accel = new PVector(0,0);
    void update(){
      accel.x = ( random(-.5,.5) );
      accel.y = ( random(-.5,.5) );
      ellipse( loc.x, loc.y, r*2, r*2 );
      if (   loc.y + r >= height 
          || loc.y - r <= 0) {
          vel.y *=-1;
      }
      if (   loc.x + r >= width 
          || loc.x - r <= 0) {
          vel.x *=-1;
      }
      vel.add(accel);
      loc.add(vel);
    }
  }
}
class PHist{
 int x,y;
 int updateSpeed,oupdateSpeed;
 int w;
 int h;
 int nbins;
 int[] binValues;
 color[] binColours;
 PHist () {
   x = 0;
   y = height;
   w = width;
   h = height/2;
   nbins = 10;
   binValues = new int[nbins];
   binColours = new color[nbins];
   updateSpeed = 29;
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 20*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }

 };
 PHist ( int xi, int yi, int wi, int hi, int nbinsi) {
   x = xi;
   y = yi;
   w = wi;
   h = hi;
   nbins = nbinsi;
   oupdateSpeed = updateSpeed;
   binValues = new int[nbins];
   binColours = new color[nbins];
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 1*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }
 }
 void clearBins(){
   for ( int ii = 0; ii < nbins; ii++ ){
     binValues[ii] = 0;
   }
 }
 void dumpValue( float value ){
     for ( int jj = 0; jj < nbins; jj++ ){
         if ( value > jj*maxAll/nbins && value < (jj + 1)*maxAll/nbins )
             binValues[ jj ] += h/shotNum;
     }
 }
 void onDraw(){
       for ( int ii = 0; ii < nbins; ii++ ) {
         fill( binColours[ii] );
         rect( x+ii*w/nbins, y-binValues[ii], w/nbins, constrain(binValues[ii],0,h) );
       }
 }
}

uhhhhhhh wut

Here's some junk I'm working on:
*paste*

Code:

class Ball {
  PVector pos;
  boolean done = false;
  
  Ball(PVector p) {
    pos = p;
  }
  
  void update(float diff) {
    pos.z += diff;
    if(pos.z > 420) {
      done = true;
    }
  }
  
  void display() {
    pushMatrix();
    if(pos.z < -100) {
      fill(map(pos.z,-10000,-100,0,255));
    } else {
      fill(255);
    }
    translate(pos.x,pos.y,pos.z);
    ellipse(0,0,10,10);
    popMatrix();
  }
}

import processing.opengl.*;

int NUM = 500;
ArrayList<Ball> balls = new ArrayList();

float speed = -0.5;

float mx,my,camX,camY;

void setup() {
  size(500,500,OPENGL);
  
  fill(255);
  noStroke();
  smooth();
  
  for(int i=0; i < NUM; i++) {
    if(i%2 == 0) {
      balls.add(new Ball(new PVector(width-50,height-50,(-i*10)-20)));
    } else {
      balls.add(new Ball(new PVector(50,height-50,(-i*10)-20)));
    }
  }
  
  perspective(PI/4,width/ (float) height,0.01,5000);
}

void draw() {
  mx += (mouseX-mx)/10f;
  my += (mouseY-my)/10f;
  
  camX = mx - width/2;
  camY = my - height/2;
  
  background(0);
  translate(camX,camY);
  for(int i = (balls.size()-1); i >= 0; i--) {
    Ball b = balls.get(i);
    if(!b.done) {
      b.update(speed);
      b.display();
    } else {
      balls.remove(i);
      addRandomBall();
    }
  }
  
  speed += 0.05;
}

void addRandomBall() {
  balls.add(new Ball(new PVector(random(-width*2,width*2),random(-height*2,height*2),random(-15000,-5000))));
}

edit: lol "crap" is filtered

Last edited by scratchisthebest (2012-04-10 21:00:58)


bye 1.4, we all loved you. but we all outgrew the site. 2.0 is a welcome change.
http://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.png

Offline

 

#28 2012-04-11 17:02:07

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Processing Topic

Yay I got it today.

It's very confuzzling.


Why

Offline

 

#29 2012-04-11 18:09:14

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Processing Topic

scratchisthebest wrote:

veggieman001 wrote:

Uh okay here's an experimental version of the game Shots I made without the game part.

Code:

ArrayList shooters = new ArrayList();
int stimer,ctimer,gtimer,osec,firedelay,shotNum;
boolean fr = false;
float maxAll;
PHist hist;
void setup(){
  size(700,500);
  background(#FFEEEE);
  text("Shots",width/2-10,height/2-50);
  smooth();
  shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
  hist = new PHist(0,height,width,(height/4)*3,8);
  osec = 2;
  firedelay = 59;
}
void draw(){
  background(#445599);
  maxAll = 0;
  for (int i = 0; i < shooters.size(); i++) {
    Shooter shooter = (Shooter) shooters.get(i);
    float maxShot = shooter.update();
    shotNum+=shooter.shots.size();
    maxAll = max( maxAll, maxShot );
    for(int j=0; j < shooter.shotSpeeds.length; j++){
      hist.dumpValue(shooter.shotSpeeds[j]);
    }
  }
  hist.onDraw();
  hist.clearBins();
  shotNum=0;
  stimer++;
  ctimer++;
  if (osec != second()){
    gtimer++;
    osec = second();
  }
  if (stimer==firedelay) stimer=0;
  if (ctimer==199){
    shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
    ctimer=0;
  }
  if (fr) println(frameRate);
  println(" ");
}
class Shooter{
  int x,y,r;
  int ofd = firedelay;
  int[] colour = new int[3];
  int tnum = int(random(0,firedelay));
  ArrayList shots = new ArrayList();
  float[] shotSpeeds;
  Shooter (int x_, int y_){
    x = x_;
    y = y_;
    r = 20; 
    colour[0] = int(random(0,255));
    colour[1] = int(random(0,255));
    colour[2] = int(random(0,255));
  }
  float update(){
    if (firedelay != ofd){if(tnum>firedelay) tnum = int(random(0,firedelay));ofd=firedelay;}
    fill(colour[0],colour[1],colour[2]);
    if (stimer==tnum) shots.add(new Shot());
    rect(x,y,r,r);
    float maxSpeed = 0;
    float sumSpeed = 0;
    shotSpeeds = new float[shots.size()+1];
    for (int i = 0; i < shots.size(); i++) {
      Shot shot = (Shot) shots.get(i);
      shot.update();
      shotSpeeds[i] = shot.vel.mag();
      maxSpeed = max( maxSpeed, shot.vel.mag() );
      sumSpeed += shot.vel.mag();
    }
    return maxSpeed;
  }
  class Shot{      
    int r = 5;
    PVector vel   = new PVector( random(-.1,.1),random(-.1,.1) );
    PVector loc   = new PVector( x+r/2, y+r/2 );
    PVector accel = new PVector(0,0);
    void update(){
      accel.x = ( random(-.5,.5) );
      accel.y = ( random(-.5,.5) );
      ellipse( loc.x, loc.y, r*2, r*2 );
      if (   loc.y + r >= height 
          || loc.y - r <= 0) {
          vel.y *=-1;
      }
      if (   loc.x + r >= width 
          || loc.x - r <= 0) {
          vel.x *=-1;
      }
      vel.add(accel);
      loc.add(vel);
    }
  }
}
class PHist{
 int x,y;
 int updateSpeed,oupdateSpeed;
 int w;
 int h;
 int nbins;
 int[] binValues;
 color[] binColours;
 PHist () {
   x = 0;
   y = height;
   w = width;
   h = height/2;
   nbins = 10;
   binValues = new int[nbins];
   binColours = new color[nbins];
   updateSpeed = 29;
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 20*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }

 };
 PHist ( int xi, int yi, int wi, int hi, int nbinsi) {
   x = xi;
   y = yi;
   w = wi;
   h = hi;
   nbins = nbinsi;
   oupdateSpeed = updateSpeed;
   binValues = new int[nbins];
   binColours = new color[nbins];
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 1*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }
 }
 void clearBins(){
   for ( int ii = 0; ii < nbins; ii++ ){
     binValues[ii] = 0;
   }
 }
 void dumpValue( float value ){
     for ( int jj = 0; jj < nbins; jj++ ){
         if ( value > jj*maxAll/nbins && value < (jj + 1)*maxAll/nbins )
             binValues[ jj ] += h/shotNum;
     }
 }
 void onDraw(){
       for ( int ii = 0; ii < nbins; ii++ ) {
         fill( binColours[ii] );
         rect( x+ii*w/nbins, y-binValues[ii], w/nbins, constrain(binValues[ii],0,h) );
       }
 }
}

uhhhhhhh wut

Here's some junk I'm working on:
*paste*

Code:

class Ball {
  PVector pos;
  boolean done = false;
  
  Ball(PVector p) {
    pos = p;
  }
  
  void update(float diff) {
    pos.z += diff;
    if(pos.z > 420) {
      done = true;
    }
  }
  
  void display() {
    pushMatrix();
    if(pos.z < -100) {
      fill(map(pos.z,-10000,-100,0,255));
    } else {
      fill(255);
    }
    translate(pos.x,pos.y,pos.z);
    ellipse(0,0,10,10);
    popMatrix();
  }
}

import processing.opengl.*;

int NUM = 500;
ArrayList<Ball> balls = new ArrayList();

float speed = -0.5;

float mx,my,camX,camY;

void setup() {
  size(500,500,OPENGL);
  
  fill(255);
  noStroke();
  smooth();
  
  for(int i=0; i < NUM; i++) {
    if(i%2 == 0) {
      balls.add(new Ball(new PVector(width-50,height-50,(-i*10)-20)));
    } else {
      balls.add(new Ball(new PVector(50,height-50,(-i*10)-20)));
    }
  }
  
  perspective(PI/4,width/ (float) height,0.01,5000);
}

void draw() {
  mx += (mouseX-mx)/10f;
  my += (mouseY-my)/10f;
  
  camX = mx - width/2;
  camY = my - height/2;
  
  background(0);
  translate(camX,camY);
  for(int i = (balls.size()-1); i >= 0; i--) {
    Ball b = balls.get(i);
    if(!b.done) {
      b.update(speed);
      b.display();
    } else {
      balls.remove(i);
      addRandomBall();
    }
  }
  
  speed += 0.05;
}

void addRandomBall() {
  balls.add(new Ball(new PVector(random(-width*2,width*2),random(-height*2,height*2),random(-15000,-5000))));
}

Mine tracks all the speeds of the shots onscreen into a histogram.


Posts: 20000 - Show all posts

Offline

 

#30 2012-04-12 14:21:11

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

RedRocker227 wrote:

Yay I got it today.

It's very confuzzling.

Whoa, I got it yesterday too. Yay.

Hmm, I haven't found it too confusing yet. Are you doing some of the tutorials they have?

Offline

 

#31 2012-04-12 15:10:52

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

veggieman001 wrote:

It's really a good stepping stone, though I think it requires knowledge of programming outside Scratch. I'm going to be working on porting Scratch to Processing at some point in the next couple weeks, to make the transition better. It'll use Scratch block names with Processing parenthetical syntax, and include collision sensing built in like Scratch does. Once I have a significant portion done, I'll upload it somewhere and make it an open source project.  smile

I was just thinking the same thing. B)

Offline

 

#32 2012-04-12 16:10:10

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Processing Topic

ProgrammingFreak wrote:

RedRocker227 wrote:

Yay I got it today.

It's very confuzzling.

Whoa, I got it yesterday too. Yay.

Hmm, I haven't found it too confusing yet. Are you doing some of the tutorials they have?

Yeah, but I just don't really understand most of it. If I'm not mistaken, void setup() and void draw() are two of the most important things, and I don't have a clue what they do. XD


Why

Offline

 

#33 2012-04-12 16:54:26

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

RedRocker227 wrote:

ProgrammingFreak wrote:

RedRocker227 wrote:

Yay I got it today.

It's very confuzzling.

Whoa, I got it yesterday too. Yay.

Hmm, I haven't found it too confusing yet. Are you doing some of the tutorials they have?

Yeah, but I just don't really understand most of it. If I'm not mistaken, void setup() and void draw() are two of the most important things, and I don't have a clue what they do. XD

Lol, I kinda felt like that too.
I don't really get Setup, but Draw is pretty much where all the action is. You can have variables and classes out of them, but everything else I think have to be in there.  hmm

Idk ahhh.

Last edited by ProgrammingFreak (2012-04-12 16:54:44)

Offline

 

#34 2012-04-12 17:00:07

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Processing Topic

Can you make 3D with this?


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#35 2012-04-12 17:13:10

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

poopo wrote:

Can you make 3D with this?

Somewhat. I don't know how accurate it is.

Offline

 

#36 2012-04-12 17:18:52

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

Hmmm. Why is this not working?

Code:

player steve = new player();
PFont f;
boolean menu=true;

void setup(){
  size(500,300);
  f = loadFont("Rockwell-48.vlw");

}
void draw(){

  if(menu=true){
    background(127);
    stroke(#ffffff);
    textFont(f, 30);
    text("Items", width/5,height/2-50);
    line(width/5,height/2-45, width/2,height/2-45);
    textFont(f, 20);
    text("Play (press x to play)", width/5,height/2-25);
    if (mouseX > 100 && mouseY > 100 && mouseX < 150 && mouseY < 130){
        fill(#000000);
      }else{
        fill(#ffffff);
      }
    } else {
      steve.display();
      background(400);
    }
}
    

void mousePressed() { 
  if(menu=true){
    menu=false;
  }
}
class player { 
  float speed;
  float lives;
  float x,y,width,height;
  player() { 
    lives = 3;
    speed = 10;
    x = 30;
    y = 30;
    width= 20;
    height= 20;
    
  }

  void display() {
    fill(89,94, 180);
    ellipseMode(CENTER);
    ellipse(x,y,width, height);
    
  }

}

Offline

 

#37 2012-04-12 18:35:15

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Processing Topic

ProgrammingFreak wrote:

Hmmm. Why is this not working?

Code:

player steve = new player();
PFont f;
boolean menu=true;

void setup(){
  size(500,300);
  f = loadFont("Rockwell-48.vlw");

}
void draw(){

  if(menu=true){
    background(127);
    stroke(#ffffff);
    textFont(f, 30);
    text("Items", width/5,height/2-50);
    line(width/5,height/2-45, width/2,height/2-45);
    textFont(f, 20);
    text("Play (press x to play)", width/5,height/2-25);
    if (mouseX > 100 && mouseY > 100 && mouseX < 150 && mouseY < 130){
        fill(#000000);
      }else{
        fill(#ffffff);
      }
    } else {
      steve.display();
      background(400);
    }
}
    

void mousePressed() { 
  if(menu=true){
    menu=false;
  }
}
class player { 
  float speed;
  float lives;
  float x,y,width,height;
  player() { 
    lives = 3;
    speed = 10;
    x = 30;
    y = 30;
    width= 20;
    height= 20;
    
  }

  void display() {
    fill(89,94, 180);
    ellipseMode(CENTER);
    ellipse(x,y,width, height);
    
  }

}

What do you want it to do?


Posts: 20000 - Show all posts

Offline

 

#38 2012-04-13 08:27:22

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

veggieman001 wrote:

ProgrammingFreak wrote:

Hmmm. Why is this not working?

Code:

player steve = new player();
PFont f;
boolean menu=true;

void setup(){
  size(500,300);
  f = loadFont("Rockwell-48.vlw");

}
void draw(){

  if(menu=true){
    background(127);
    stroke(#ffffff);
    textFont(f, 30);
    text("Items", width/5,height/2-50);
    line(width/5,height/2-45, width/2,height/2-45);
    textFont(f, 20);
    text("Play (press x to play)", width/5,height/2-25);
    if (mouseX > 100 && mouseY > 100 && mouseX < 150 && mouseY < 130){
        fill(#000000);
      }else{
        fill(#ffffff);
      }
    } else {
      steve.display();
      background(400);
    }
}
    

void mousePressed() { 
  if(menu=true){
    menu=false;
  }
}
class player { 
  float speed;
  float lives;
  float x,y,width,height;
  player() { 
    lives = 3;
    speed = 10;
    x = 30;
    y = 30;
    width= 20;
    height= 20;
    
  }

  void display() {
    fill(89,94, 180);
    ellipseMode(CENTER);
    ellipse(x,y,width, height);
    
  }

}

What do you want it to do?

The mousePressed isn't working. It'll run, but it won't do anything

___________________________________________________________
While I was frustrated with this, I started on a Scratch Processing Library. How I'm doing it is with lots of classes. 1 for each block section. And also another one for Sprites. This way it can be easier to make your own....well...sprites in Processing. You can also change certain properties (eg x, y, source image).

Adding blocks should be pretty easy, at least most of them will. I'm starting with Motion first, and then probably looks or Control (though I'm not sure how many control blocks will actually be needed).

There was one concern I had. If you were going to have multiple Sprites, you would want a way to direct the certain function you want to do to that sprite. An easy way out would be to just have one of the arguments as the sprite you want to do it to (eg movesteps(Sprite1, 10);). But I don't really prefer that way.
What would make it look nicer and cleaner is if you could do something like: Sprite1.movesteps(10);.

But to do that, we would have to put all of the block voids in the Sprite class. Which in my opinion, would clutter it up A LOT. But yeah, that's what I'll probably do.

Here is what I plan for it to be like:

Code:

Sprite Sprite1 = new Sprite();
Sprite1.movesteps(10);

So yeah, opinions?

Last edited by ProgrammingFreak (2012-04-13 08:28:12)

Offline

 

#39 2012-04-13 11:40:45

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Processing Topic

Code:

void mousePressed() { 
  if(menu){
    menu=false;
  }
}

Try this.
And yeah, mine was/is pretty similar.


Posts: 20000 - Show all posts

Offline

 

#40 2012-04-13 12:30:30

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

I don't have much yet, just wanted to show my progress.
I'm trying to think how to only run the command once when called in draw().

Code:

//Scratch in Processing
//Developed by Pf
//2012
class Sprite{
  float x, y;
  Sprite(){
    x=30;
    y=30;
  }
  void show(){
    fill(252, 128, 3);
    ellipse(x, y, 55, 55);    
  }
  
  /////////////////
  //CODE FOR BLOCKS
  /////////////////
  void glidesteps(int glide){
    x +=glide;
  }
   
}
Sprite Sprite1 = new Sprite();

void draw(){
  background(#ffffff);
  //Call functions here.
  //Must have [spritename].show();
  //to have the sprite shown when 
  //the program starts
  Sprite1.show();
  //here is the code for the gliding
  Sprite1.glidesteps(10);
}
void setup(){
  size(400,400);
}

Offline

 

#41 2012-04-13 12:41:21

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Processing Topic

Here's my ancient code that I haven't worked on in forever

Code:

//---------------------------
//------CONTROL--------------
//---------------------------

void Wait(float wait_time){
  delay(round(wait_time*1000)); 
}

void Stop(){
  noLoop();
}

boolean Clicked(Sprite spr){
  if (mousePressed && mouseX>spr.x && mouseX<spr.x+spr.width && mouseY>spr.y && mouseY<spr.y+spr.height){
    return true; 
  }
  else return false;
}


//---------------------------
//------MOTION---------------
//---------------------------

int XPosition(Sprite spr){
  return spr.x;
}

int YPosition(Sprite spr){
  return spr.y;
}

void GoTo(Sprite spr,int x,int y){
  spr.x=x;
  spr.y=y;
}
void GoTo(Sprite spr,String mouse){
  if (mouse=="mouse"||mouse=="MOUSE"||mouse=="Mouse"){
    spr.x=mouseX;
    spr.y=mouseY;
  } 
}
void GoTo(Sprite spr,Sprite spr2){
  spr.x=spr2.x;
  spr.y=spr2.y;
}

void ChangeXBy(Sprite spr,int amt){
  spr.x+=amt;
}

void ChangeYBy(Sprite spr,int amt){
  spr.y+=amt;
}

void SetXTo(Sprite spr,int amt){
  spr.x=amt;
}

void SetYTo(Sprite spr,int amt){
  spr.y=amt;
}

//---------------------------
//------SENSING--------------
//---------------------------

boolean Touching(Sprite spr,Sprite spr2){
  //print coords
  //println("x: "+spr.x+" y: "+spr.y+" x+w: "+(spr.x+spr.height)+" y+h: "+(spr.y+spr.height)+" x2: "+spr2.x+" y2: "+spr2.y+" x2+w: "+(spr2.x+spr2.height)+" y2+h: "+(spr2.y+spr2.height));
  //if within area of rectangle
  if ((spr.x>=spr2.x && spr.y>=spr2.y) || (spr.x+spr.width>=spr2.x && spr.y+spr.height>=spr2.y)){
    if (spr.x<=spr2.x+spr2.width && spr.y<=spr2.y+spr2.height){
      if ((spr.x+spr.width)>=spr2.x && (spr.y+spr.height)>=spr2.y){
        return true; 
      }
      if ((spr.x+spr.width<=spr2.x+spr2.width) && (spr.y+spr.width<=spr2.y+spr2.height)){
        return true;
      }
      else return false;
    }
    else return false;
  }
  else return false;
}

float Distance(Sprite spr,Sprite spr2){
  return dist(spr.x,spr.y,spr2.x,spr2.y);
}

//---------------------------
//------OPERATORS------------
//---------------------------

float PickRandom(float low,float high){
  return random(low, high);
}
int PickRandom(int low,int high){
  return int(random(low, high));
}

//---------------------------
//------DATA_STRUCTURE-------
//---------------------------

class Sprite {
  int x,y,width,height,typ;
  ArrayList costumes = new ArrayList();
  Costume firstcostume;
  Sprite (int xx, int yy, int wid, int hei){
    x=xx;
    y=yy;
    width=wid;
    height=hei;
    typ=1;
  }
  Sprite (String im, int xx, int yy){
    x=xx;
    y=yy;
    costumes.add(new Costume(im));
    firstcostume = (Costume) costumes.get(0);
    width=firstcostume.width;
    height=firstcostume.height;
    typ=2;
  }
  void update(){
    if (typ==1){
      fill(255);
      //draw rectangle at given coordinates with set height/width
      rect(x,y,width,height);
    }
    if (typ==2){
     image(firstcostume.cImg,x,y); 
    }
  }
  class Costume{
    PImage cImg;
    int costumeNo;
    String name;
    int width,height;
    Costume (String cIm){
      cImg = loadImage(cIm);
      width = cImg.width;
      height = cImg.height;
    }
  }
}

class Broadcast {
  boolean sent = false;
  Broadcast (){
  }
  void send(){
    sent = true; 
  }
}

Posts: 20000 - Show all posts

Offline

 

#42 2012-04-13 19:57:45

scratchisthebest
Scratcher
Registered: 2009-02-08
Posts: 500+

Re: Processing Topic

ProgrammingFreak wrote:

poopo wrote:

Can you make 3D with this?

Somewhat. I don't know how accurate it is.

It's really great! The transformation functions (translate(), rotate(), scale() ) all extend wonderfully into 3d, with a few special 3d functions, like box() and sphere(). You can even get full OpenGL access with 1 line! If you try it, make sure to also install the PeasyCam library. It's a "dead-simple, mouse driven camera library" that pretty much works out of the box.  big_smile  big_smile  big_smile  big_smile  big_smile

edit: grrmrrmrrgrmrgrrm stupid oldblocks

Last edited by scratchisthebest (2012-04-13 20:02:28)


bye 1.4, we all loved you. but we all outgrew the site. 2.0 is a welcome change.
http://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.png

Offline

 

#43 2012-04-13 20:01:38

scratchisthebest
Scratcher
Registered: 2009-02-08
Posts: 500+

Re: Processing Topic

veggieman001 wrote:

scratchisthebest wrote:

veggieman001 wrote:

Uh okay here's an experimental version of the game Shots I made without the game part.

Code:

ArrayList shooters = new ArrayList();
int stimer,ctimer,gtimer,osec,firedelay,shotNum;
boolean fr = false;
float maxAll;
PHist hist;
void setup(){
  size(700,500);
  background(#FFEEEE);
  text("Shots",width/2-10,height/2-50);
  smooth();
  shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
  hist = new PHist(0,height,width,(height/4)*3,8);
  osec = 2;
  firedelay = 59;
}
void draw(){
  background(#445599);
  maxAll = 0;
  for (int i = 0; i < shooters.size(); i++) {
    Shooter shooter = (Shooter) shooters.get(i);
    float maxShot = shooter.update();
    shotNum+=shooter.shots.size();
    maxAll = max( maxAll, maxShot );
    for(int j=0; j < shooter.shotSpeeds.length; j++){
      hist.dumpValue(shooter.shotSpeeds[j]);
    }
  }
  hist.onDraw();
  hist.clearBins();
  shotNum=0;
  stimer++;
  ctimer++;
  if (osec != second()){
    gtimer++;
    osec = second();
  }
  if (stimer==firedelay) stimer=0;
  if (ctimer==199){
    shooters.add(new Shooter(int(random(30,width-30)),int(random(30,height-30))));
    ctimer=0;
  }
  if (fr) println(frameRate);
  println(" ");
}
class Shooter{
  int x,y,r;
  int ofd = firedelay;
  int[] colour = new int[3];
  int tnum = int(random(0,firedelay));
  ArrayList shots = new ArrayList();
  float[] shotSpeeds;
  Shooter (int x_, int y_){
    x = x_;
    y = y_;
    r = 20; 
    colour[0] = int(random(0,255));
    colour[1] = int(random(0,255));
    colour[2] = int(random(0,255));
  }
  float update(){
    if (firedelay != ofd){if(tnum>firedelay) tnum = int(random(0,firedelay));ofd=firedelay;}
    fill(colour[0],colour[1],colour[2]);
    if (stimer==tnum) shots.add(new Shot());
    rect(x,y,r,r);
    float maxSpeed = 0;
    float sumSpeed = 0;
    shotSpeeds = new float[shots.size()+1];
    for (int i = 0; i < shots.size(); i++) {
      Shot shot = (Shot) shots.get(i);
      shot.update();
      shotSpeeds[i] = shot.vel.mag();
      maxSpeed = max( maxSpeed, shot.vel.mag() );
      sumSpeed += shot.vel.mag();
    }
    return maxSpeed;
  }
  class Shot{      
    int r = 5;
    PVector vel   = new PVector( random(-.1,.1),random(-.1,.1) );
    PVector loc   = new PVector( x+r/2, y+r/2 );
    PVector accel = new PVector(0,0);
    void update(){
      accel.x = ( random(-.5,.5) );
      accel.y = ( random(-.5,.5) );
      ellipse( loc.x, loc.y, r*2, r*2 );
      if (   loc.y + r >= height 
          || loc.y - r <= 0) {
          vel.y *=-1;
      }
      if (   loc.x + r >= width 
          || loc.x - r <= 0) {
          vel.x *=-1;
      }
      vel.add(accel);
      loc.add(vel);
    }
  }
}
class PHist{
 int x,y;
 int updateSpeed,oupdateSpeed;
 int w;
 int h;
 int nbins;
 int[] binValues;
 color[] binColours;
 PHist () {
   x = 0;
   y = height;
   w = width;
   h = height/2;
   nbins = 10;
   binValues = new int[nbins];
   binColours = new color[nbins];
   updateSpeed = 29;
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 20*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }

 };
 PHist ( int xi, int yi, int wi, int hi, int nbinsi) {
   x = xi;
   y = yi;
   w = wi;
   h = hi;
   nbins = nbinsi;
   oupdateSpeed = updateSpeed;
   binValues = new int[nbins];
   binColours = new color[nbins];
   for ( int ii = 0; ii < nbins; ii++ ) {
     binValues[ii] = 1*ii;
     binColours[ii] = color(random(128,200),random(128,200),random(128,200),200);
   }
 }
 void clearBins(){
   for ( int ii = 0; ii < nbins; ii++ ){
     binValues[ii] = 0;
   }
 }
 void dumpValue( float value ){
     for ( int jj = 0; jj < nbins; jj++ ){
         if ( value > jj*maxAll/nbins && value < (jj + 1)*maxAll/nbins )
             binValues[ jj ] += h/shotNum;
     }
 }
 void onDraw(){
       for ( int ii = 0; ii < nbins; ii++ ) {
         fill( binColours[ii] );
         rect( x+ii*w/nbins, y-binValues[ii], w/nbins, constrain(binValues[ii],0,h) );
       }
 }
}

uhhhhhhh wut

Here's some junk I'm working on:
*paste*

Code:

class Ball {
  PVector pos;
  boolean done = false;
  
  Ball(PVector p) {
    pos = p;
  }
  
  void update(float diff) {
    pos.z += diff;
    if(pos.z > 420) {
      done = true;
    }
  }
  
  void display() {
    pushMatrix();
    if(pos.z < -100) {
      fill(map(pos.z,-10000,-100,0,255));
    } else {
      fill(255);
    }
    translate(pos.x,pos.y,pos.z);
    ellipse(0,0,10,10);
    popMatrix();
  }
}

import processing.opengl.*;

int NUM = 500;
ArrayList<Ball> balls = new ArrayList();

float speed = -0.5;

float mx,my,camX,camY;

void setup() {
  size(500,500,OPENGL);
  
  fill(255);
  noStroke();
  smooth();
  
  for(int i=0; i < NUM; i++) {
    if(i%2 == 0) {
      balls.add(new Ball(new PVector(width-50,height-50,(-i*10)-20)));
    } else {
      balls.add(new Ball(new PVector(50,height-50,(-i*10)-20)));
    }
  }
  
  perspective(PI/4,width/ (float) height,0.01,5000);
}

void draw() {
  mx += (mouseX-mx)/10f;
  my += (mouseY-my)/10f;
  
  camX = mx - width/2;
  camY = my - height/2;
  
  background(0);
  translate(camX,camY);
  for(int i = (balls.size()-1); i >= 0; i--) {
    Ball b = balls.get(i);
    if(!b.done) {
      b.update(speed);
      b.display();
    } else {
      balls.remove(i);
      addRandomBall();
    }
  }
  
  speed += 0.05;
}

void addRandomBall() {
  balls.add(new Ball(new PVector(random(-width*2,width*2),random(-height*2,height*2),random(-15000,-5000))));
}

Mine tracks all the speeds of the shots onscreen into a histogram.

Ohhhh! That's what the rects were for!  smile

hey ProgrammingFreak, have you tried putting noLoop() in setup()? It causes draw() to run just once.  big_smile


bye 1.4, we all loved you. but we all outgrew the site. 2.0 is a welcome change.
http://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.png

Offline

 

#44 2012-04-14 13:36:32

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

scratchisthebest wrote:

veggieman001 wrote:

scratchisthebest wrote:


uhhhhhhh wut

Here's some junk I'm working on:
*paste*

Code:

class Ball {
  PVector pos;
  boolean done = false;
  
  Ball(PVector p) {
    pos = p;
  }
  
  void update(float diff) {
    pos.z += diff;
    if(pos.z > 420) {
      done = true;
    }
  }
  
  void display() {
    pushMatrix();
    if(pos.z < -100) {
      fill(map(pos.z,-10000,-100,0,255));
    } else {
      fill(255);
    }
    translate(pos.x,pos.y,pos.z);
    ellipse(0,0,10,10);
    popMatrix();
  }
}

import processing.opengl.*;

int NUM = 500;
ArrayList<Ball> balls = new ArrayList();

float speed = -0.5;

float mx,my,camX,camY;

void setup() {
  size(500,500,OPENGL);
  
  fill(255);
  noStroke();
  smooth();
  
  for(int i=0; i < NUM; i++) {
    if(i%2 == 0) {
      balls.add(new Ball(new PVector(width-50,height-50,(-i*10)-20)));
    } else {
      balls.add(new Ball(new PVector(50,height-50,(-i*10)-20)));
    }
  }
  
  perspective(PI/4,width/ (float) height,0.01,5000);
}

void draw() {
  mx += (mouseX-mx)/10f;
  my += (mouseY-my)/10f;
  
  camX = mx - width/2;
  camY = my - height/2;
  
  background(0);
  translate(camX,camY);
  for(int i = (balls.size()-1); i >= 0; i--) {
    Ball b = balls.get(i);
    if(!b.done) {
      b.update(speed);
      b.display();
    } else {
      balls.remove(i);
      addRandomBall();
    }
  }
  
  speed += 0.05;
}

void addRandomBall() {
  balls.add(new Ball(new PVector(random(-width*2,width*2),random(-height*2,height*2),random(-15000,-5000))));
}

Mine tracks all the speeds of the shots onscreen into a histogram.

Ohhhh! That's what the rects were for!  smile

hey ProgrammingFreak, have you tried putting noLoop() in setup()? It causes draw() to run just once.  big_smile

Ohhhh thanks, man.  big_smile

Offline

 

#45 2012-04-22 04:14:15

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Processing Topic

:0 I found a "processing for the iOS" in the app store! Unfortunately my iPod to too outdated to use it ;-;


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#46 2012-04-22 15:32:35

scratchisthebest
Scratcher
Registered: 2009-02-08
Posts: 500+

Re: Processing Topic

slinger wrote:

:0 I found a "processing for the iOS" in the app store! Unfortunately my iPod to too outdated to use it ;-;

I AM SO MAD TOO  smile
...wait I thought programming apps weren't allowed! :0 maybe they changed it! Someone resubmit the Scratch app!  big_smile

edit: try hiperpad. It's a web app. The only problem is it's really hard to type the important things like { } ( ) [ ]  " " ' ' and stuff on the cruddy iOS keyboard.

Last edited by scratchisthebest (2012-04-22 15:34:20)


bye 1.4, we all loved you. but we all outgrew the site. 2.0 is a welcome change.
http://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.pnghttp://scratch.mit.edu/img/Pico3-med.png

Offline

 

#47 2012-04-22 16:50:03

soupoftomato
Scratcher
Registered: 2009-07-18
Posts: 1000+

Re: Processing Topic

I tried using it yesterday but uh, I suck at this whole memorizing the commands.
I don't have the dedication and I need like someone explaining what every single command does.


I'm glad to think that the community will always be kind and helpful, the language will always be a fun and easy way to be introduced into programming, the motto will always be: Imagine, Program, Share - Nomolos

Offline

 

#48 2012-04-22 17:18:53

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Processing Topic

soupoftomato wrote:

I tried using it yesterday but uh, I suck at this whole memorizing the commands.
I don't have the dedication and I need like someone explaining what every single command does.

processing.org/reference/


Posts: 20000 - Show all posts

Offline

 

#49 2012-04-22 17:36:37

soupoftomato
Scratcher
Registered: 2009-07-18
Posts: 1000+

Re: Processing Topic

veggieman001 wrote:

soupoftomato wrote:

I tried using it yesterday but uh, I suck at this whole memorizing the commands.
I don't have the dedication and I need like someone explaining what every single command does.

processing.org/reference/

Yes, I've seen it,  but you have to click the things, then you're on a different page that you have to read a bunch on, then you have to click back.

They need a list like this:
float(); - whatever it does explained simply

Though I may be the extreme example for laziness.


I'm glad to think that the community will always be kind and helpful, the language will always be a fun and easy way to be introduced into programming, the motto will always be: Imagine, Program, Share - Nomolos

Offline

 

#50 2012-04-22 17:54:40

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Processing Topic

soupoftomato wrote:

Though I may be the extreme example for laziness.

Yes you are.  tongue
The tutorials are better cause it walks you through so you can know whats going on.

Offline

 

Board footer