Difference between revisions of "Thing2"

From CodeStuff
Jump to: navigation, search
(Created page with "<edcode> print("draw with the arrow keys"); var cx=320; var cy=240; function move() { if (keyIsDown(38)) cy-=1; if (keyIsDown(40)) cy+=1; if (keyIsDown(37)) cx-=1; if...")
 
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
<edcode>
 
<edcode>
−
print("draw with the arrow keys");
+
 
−
var cx=320;
+
 
−
var cy=240;
+
var gunFireRate=8;
 +
var gunTemperature=0;
 +
 
 +
 
 +
var player = {x:320,y:450};
 +
 
 +
var speed=5;
 +
var bulletSpeed=8;
 +
 
 +
var bullets=[];
 +
var aliens=[];
 +
 
 +
var ship=loadImage("Purzen_rocket.png");
 +
ship.handleY=22;
 +
ship.handleX=14;
 +
 
 +
var alienShip=loadImage("Spaceship Skorpio.png");
 +
alienShip.handleX=20;
 +
alienShip.handleY=20;
 +
 
 +
var explosionImage=loadImage("Explosion1.png",10);
 +
explosionImage.handleX=46;
 +
explosionImage.handleY=46;
 +
 
 +
 
 +
 
 +
var explosion = {image:explosionImage, frame : 10};
 +
 
 +
setColour("black");
 +
fillRectangle(0,0,640,480);
 +
 
 +
var ty=0;
 +
while (ty < 480) {
 +
  var shade = (255/480) * ty;
 +
 
 +
  setColour("rgb(0,0,"+shade+")");
 +
 
 +
 
 +
  fillRectangle(0,ty,  640,1);
 +
  ty+=1;
 +
}
 +
 
 +
makeBackground();
 +
 
 +
 
 +
function makeAlien(initialX,initialY,kind) {
 +
  return {x:initialX, y:initialY,dx:0,dy:  0, dead:false, behaviour:kind};
 +
}
 +
function drawAlien(alien) {
 +
  setColour("Green");
 +
 
 +
  drawImage(alienShip,alien.x,alien.y);
 +
 
 +
   
 +
}
 +
 
 +
function moveAlien(alien) {
 +
  alien.x+=alien.dx;
 +
  alien.y+=alien.dy;
 +
 
 +
  alien.dx+=(0.5-Math.random());
 +
 
 +
  if (alien.x <50) alien.dx*=0.5;
 +
  if (alien.x >590) alien.dx*=0.5;
 +
 
 +
  if ( Math.random() < 0.01) alien.behaviour=attackAlien;
 +
}
 +
 
 +
function attackAlien(alien) {
 +
  alien.x+=alien.dx;
 +
  alien.y+=alien.dy;
 +
  alien.dy+=0.05;
 +
  alien.dx+=(0.5-Math.random());
 +
 
 +
  if (alien.x <50) alien.dx*=0.5;
 +
  if (alien.x >590) alien.dx*=0.5;
 +
 
 +
    if (alien.y> 500) {
 +
      alien.dy=0;
 +
      alien.x=320;
 +
      alien.y=80;
 +
      alien.behaviour=moveAlien;
 +
    }
 +
}
 +
 
 +
 
 +
function isAlienOK(alien) {
 +
  return (alien.dead==false)
 +
}
 +
 
 +
 
 +
aliens.push( makeAlien(300,50,attackAlien) );
 +
 
 +
var i = 0;
 +
while (i < 10) {
 +
  aliens.push( makeAlien(30 + i*30,20+i*10, moveAlien) );
 +
  i=i+1;
 +
}
 +
 
 +
function makeBullet(initialX,initialY) {
 +
  return {x:initialX, y:initialY,dx:0,dy:  -bulletSpeed, dead:false};
 +
}
 +
 
 +
function drawBullet(bullet) {
 +
  setColour("orange");
 +
  fillCircle(bullet.x,bullet.y,6);
 +
   
 +
}
 +
 
 +
function distance (a,b) {
 +
  var dx=a.x-b.x;
 +
  var dy=a.y-b.y;
 +
 
 +
  return Math.sqrt(dx*dx+dy*dy);
 +
}
 +
 
 +
function moveBullet(bullet) {
 +
  bullet.x+=bullet.dx;
 +
  bullet.y+=bullet.dy;
 +
 
 +
  var b=0;
 +
  while (b<aliens.length) {
 +
    var alien = aliens[b];
 +
    b=b+1;
 +
    var d = distance(bullet,alien);
 +
   
 +
    if (d < 15) {
 +
      bullet.dead=true;
 +
      alien.dead=true;
 +
      explosion.x=alien.x;
 +
      explosion.y=alien.y;
 +
      explosion.frame=0;
 +
    }
 +
  }
 +
 
 +
}
 +
 
 +
function isBulletOK(bullet) {
 +
  return (bullet.y>0) & (bullet.dead==false)
 +
}
  
 
function move() {
 
function move() {
−
   if (keyIsDown(38)) cy-=1;
+
−
   if (keyIsDown(40)) cy+=1;
+
    explosion.frame+=1;
−
   if (keyIsDown(37)) cx-=1;
+
 
−
   if (keyIsDown(39)) cx+=1;
+
    var b=0;
 +
  while (b<bullets.length) {
 +
    var bullet = bullets[b];
 +
    moveBullet(bullet);
 +
    b=b+1;
 +
  }
 +
 
 +
    var b=0;
 +
  while (b<aliens.length) {
 +
    var alien = aliens[b];
 +
    alien.behaviour(alien)
 +
    b=b+1;
 +
   
 +
    if ( distance(player,alien) < 20) {
 +
        explosion.x=player.x;
 +
        explosion.y=player.y;
 +
        explosion.frame=0;
 +
        player.y=-5000;
 +
    }
 +
  }
 +
 
 +
 
 +
 
 +
   //if (keyIsDown(38)) cy-=speed;
 +
   //if (keyIsDown(40)) cy+=speed;
 +
   if (keyIsDown(37)) player.x-=speed;
 +
   if (keyIsDown(39)) player.x+=speed;
 +
 
 +
  if (gunTemperature >0) gunTemperature -=1;  
 +
  if (keyIsDown(32)) {
 +
    if (gunTemperature==0) {
 +
      bullets.push(makeBullet(player.x,player.y));
 +
      gunTemperature=gunFireRate;
 +
    }
 +
  }
 +
 
 +
 
 +
 
 +
  bullets = bullets.filter(isBulletOK);
 +
 
 +
  aliens = aliens.filter(isAlienOK);
 +
 
 +
 
 +
 
 
}
 
}
  
 
function draw() {
 
function draw() {
−
   fillCircle(cx,cy,6);
+
  clear();
 +
 
 +
  var b=0;
 +
  while (b<bullets.length) {
 +
    var bullet = bullets[b];
 +
    drawBullet(bullet);
 +
    b=b+1;
 +
  }
 +
 
 +
  var b=0;
 +
  while (b<aliens.length) {
 +
    var alien = aliens[b];
 +
    drawAlien(alien);
 +
    b=b+1;
 +
  }
 +
 
 +
 
 +
  drawImage(ship,player.x,player.y);
 +
   fillCircle(player.x,player.y,6);
 +
 
 +
  setColour("white");
 +
  print(aliens.length,20,20);
 +
  if (explosion.frame < 10) {
 +
    drawImage(explosionImage,explosion.x,explosion.y,explosion.frame);
 +
  }
 
}
 
}
  
 
run(move,draw);
 
run(move,draw);
 +
 +
 +
 +
 
</edcode>
 
</edcode>

Latest revision as of 02:28, 28 June 2012