/* (c) 2008 Dave Gilbert, based on my JavaScript version */ /* si@treblig.org */ int gameWidth=450; int gameHeight=500; int topOfPlayArea=70; // Sizes of sprites int shipWidth=53; int shipHeight=32; int alien1Width=28; int alien1Height=23; int mothershipWidth=108; int mothershipHeight=48; int mothershipY=topOfPlayArea; int alienInitialTop=topOfPlayArea+mothershipHeight; int bombHeight=10; // I.e. 1 in .... int mothershipChance=1000; int mothershipStep=15; // Spacing between aliens int alienHspace=alien1Width+10; int alienVspace=alien1Height+10; int missileHeight=20; int missileVstep=10; int bombStep=10; int bombDamage=7; // Damage to a base // Base positons int[] baseLeftSides = {50,140,230,320}; // Steps/rates of movements int shipStep=4; int baseBottom=gameHeight-(10+shipHeight)-missileHeight; int baseHeight=30; int baseWidth=70; int alienRows=4; int alienCols=7; int alienHstep=4; int alienVstep=6; int totalAliens=alienRows*alienCols; int bombsMaxInitial=4; int bombsMax=32; int bombsPerLevel=4; // ------------------------------------------------------------- // Current game state int ships=3; int level=1; int score=0; boolean gameOver=false; int shipPos=baseLeftSides[2]+shipWidth/2; // Middle of ship int[] baseBottomArray = new int[gameWidth]; int[] baseTopArray = new int[gameWidth]; boolean[] alienLive = new boolean[totalAliens]; // Note these correspond to the top and left of alien(0,0) - even if that row/column is all dead int currentAlienArrayLeft; int currentAlienArrayTop; int currentAlienHStep; // As Hstep except for sign int alienLeftNoneEmptyCol; int alienRightNoneEmptyCol; int alienLowestNoneEmptyRow; int mothershipX; // Not running if -1 boolean loseShip; boolean missileInFlight; int missileBottom; int missileX; PFont mainFont; PImage shipSprite; PImage alienSprite; PImage mothershipSprite; boolean buttonMoveLeftIsDown=false; boolean buttonMoveRightIsDown=false; int bombsInFlight=0; int bombsMaxThisLevel; int[] bombsBottomArray=new int[bombsMax]; int[] bombsXArray=new int[bombsMax]; // -ve means not active void setup () { size (gameWidth,gameHeight); mainFont=loadFont("BitstreamVeraSansMono-Roman-48.vlw"); shipSprite=loadImage("ship.png"); alienSprite=loadImage("alien1.png"); mothershipSprite=loadImage("mothership.png"); bombsMaxThisLevel=bombsMaxInitial; missileInFlight=false; missileBottom=gameHeight-shipHeight; loseShip=false; ships=3; resetAliens(); resetBases(); resetBombs(); mothershipX=-1; frameRate(20); } void resetBombs() { for (int b=0;b=0) { image(mothershipSprite,mothershipX,mothershipY); } drawBases(); drawAliens(); drawMissile(); drawBombs(); } void drawAliens() { for(int y=0;ybaseTopArray[i]) { line(i,baseBottomArray[i],i,baseTopArray[i]); } } } void drawMissile() { int x; if (missileInFlight) { x=missileX; } else { x=shipPos; } stroke(255,0,0); line(x,missileBottom,x,missileBottom-missileHeight); } void drawBombs() { stroke(0,255,0); for(int b=0;b=0) { line(bombsXArray[b], bombsBottomArray[b] , bombsXArray[b], bombsBottomArray[b]-bombHeight); } } } // -------------------------------------------------------------- // ------------------ Update state ------------------------------ void doUpdate () { if (!gameOver) { updateAliens(); updateMothership(); updateBombs(); updateMissile(); updateShip(); if (loseShip) { loseShip=false; ships--; if (ships==0) { // Game over gameOver=true; } else { resetAliens(); resetBases(); resetBombs(); } } } } // Erode the bases and also check to see if the aliens are at missile level void erodeBases () { int alienArrayBottom=currentAlienArrayTop+(alienLowestNoneEmptyRow+1)*alienVspace-(alienVspace-alien1Height); for(int x=0;x=(gameHeight-(missileHeight+shipHeight))) { loseShip=true; } } void updateAliens () { int actualArrayLeft, actualArrayRight; // this is the left most edge of alien(0,0) even if the whole of row 0 is dead. currentAlienArrayLeft+=currentAlienHStep; actualArrayLeft=currentAlienArrayLeft+alienLeftNoneEmptyCol*alienHspace; actualArrayRight=currentAlienArrayLeft+(alienRightNoneEmptyCol+1)*alienHspace-(alienHspace-alien1Width); boolean moveDown; moveDown=((currentAlienHStep>0) && (actualArrayRight>=gameWidth)) || ((currentAlienHStep<0) && (actualArrayLeft<=0)); if (moveDown) { currentAlienHStep=-currentAlienHStep; currentAlienArrayTop+=alienVstep; erodeBases(); } } // Pick an alien to drop a bomb from // WARNING: Don't call this if there aren't any live aliens int pickAlien () { int a; a=int(random(0,totalAliens-0.001)); while (alienLive[a]==false) { a=int(random(0,totalAliens-0.001)); }; return a; } void updateMothership() { if (mothershipX>=0) { // already running mothershipX+=mothershipStep; if (mothershipX>gameWidth) { mothershipX=-1; } } else { // not running - should we start it? if (random(mothershipChance)<1) { mothershipX=0; } } } void updateBombs () { // First update ones in flight for(int b=0;b=0) { // The bomb is live bombsBottomArray[b]+=bombStep; // See if it hit a base if ((baseTopArray[x]=(gameHeight-shipHeight)) && (x>(shipPos-shipWidth/2)) && (x<(shipPos+shipWidth/2))) { // We've hit the ship loseShip=true; hitSomething=true; } if (hitSomething || (bombsBottomArray[b]>=gameHeight)) { bombsXArray[b]=-1; bombsInFlight-=1; } } } // Now consider adding a bomb if (bombsInFlightnewRightMost) { newRightMost=col; }; if (row>newLowest) { newLowest=row; }; } } alienLeftNoneEmptyCol=newLeftMost; alienRightNoneEmptyCol=newRightMost; alienLowestNoneEmptyRow=newLowest; if (!foundAny) { resetAliens(); bombsMaxThisLevel+=2; if (bombsMaxThisLevel>bombsMax) { bombsMaxThisLevel=bombsMax; } level++; } } void updateMissile () { if (missileInFlight) { boolean endFlight=false; int missileTop; missileBottom-=missileVstep; missileTop=missileBottom-missileHeight; // Hit bases? if ((baseBottomArray[missileX]>baseTopArray[missileX]) && (missileTop=currentAlienArrayLeft) && (missileX<(currentAlienArrayLeft+alienCols*alienHspace)) && (missileTop>currentAlienArrayTop) && (missileTop<(currentAlienArrayTop+alienRows*alienVspace))) { // Well somewhere horizontally within the range of the aliens int alienCol=int((missileX-currentAlienArrayLeft)/alienHspace); int alienRow=int((missileTop-currentAlienArrayTop)/alienVspace); if ((alienCol>=0) && (alienCol=0) && (alienRowmothershipY) && (missileX>=mothershipX) && (missileX<(mothershipX+mothershipWidth))) { // Disable mothership mothershipX=-1; score+=100; endFlight=true; } if (missileTop