topical media & game development

talk show tell print

sample-js-dodge.htm / htm



  <head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <head>
  
  <body>
  <center>
  <div width=400 height=400>
  <script type="application/processing">
  
  int n = 10;
  block[] blocks = new block[n];
  
  void setup() {
  size(400,400);
  for( int i = 0; i < n; i++ ) {
  blocks[i] = new block(int(random(300,400)), int(random(10,390)), int (random(1,10)));
  }
  }
  void draw() {
  background(255);
  for( int i = 0; i < n; i++ ) {
  blocks[i].move();
  
  }
  }
  class block {
  int x;
  int y;
  int xspeed;
  int p;
  
  block(int _x, int _y, int _xspeed) {
  x=_x;
  y=_y;
  xspeed=_xspeed;
  p=1;
  }
  
  void move() {
  
  //move the legs in stepts
  x = x-xspeed;
  if(x<0) {
    x=400;
    y=int(random(10,390));
    xspeed=int(random(1,10));
  }
  if((mouseX>(x-30)) && (mouseX<(x+30))) {
    if(((mouseY>y-30)) && (mouseY<(y+30))) {
    background(#ff0000);
  }
  }
  rectMode(CENTER);
  rect(x,y,30,30);
  ellipse(mouseX,mouseY,30,30);
  }
  }
  
  </script>
  <canvas width="400" height="400"></canvas>
  </div>
  </center>
  <form name="changeColor"> 
  <center>
  
  <p>
  <font size="2" face="arial">Choose colour
  <input type="button" name="colr" value=" Blue " onclick="document.bgColor='#0357FA'">
  <input type="button" name="colr" value=" red " onclick="document.bgColor='#FF0000'"> 
  <input type="button" name="colr" value=" Green " onclick="document.bgColor='#26B200'">
  <input type="button" name="colr" value=" Yellow " onclick="document.bgColor='#E8D502'"> 
  <input type="button" name="colr" value=" Orange " onclick="document.bgColor='#E89C02'">
  </font>
  </p> 
  </form>
  <p align=right>author: Pieter Pelt</p>
  </body>
  
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.