// Planetoid // // Code for JDK1.1 implementation is marked *** 1.1 // likewise for JDK 1.0 = *** 1.0 // // Revisions // // 970606 Lander behaviour changed // // This code (c) Jonathan Watmough // 1997-2000 //------------------------------------------------------------ // Imports //------------------------------------------------------------ import java.applet.*; import java.awt.*; //import java.awt.event.*; // *** 1.1 import java.awt.image.ImageObserver; import java.util.*; //------------------------------------------------------------ // CLASS jPlanetoid // // Implements a fast action game in Java. //------------------------------------------------------------ public final class jPlanetoid extends Canvas implements ImageObserver { // 'Constants' final static int lsize = 80; // No of landscape sections (mountains) // 20sections x 128pixels = 2560 final static int nrandom = 1024; // Array of random numbers -1,0,-1 final static int nrandom50 = 100; // Array of random numbers 0->50 // final static int nhumans = 100; // Keep checking fully loaded!!! (100) // final static int naliens = 100; // Keep checking fully loaded!!! (100) // final static int nbombers = 20 ; // Keep checking fully loaded!!! (20 ) // final static int nlasers = 30 ; // Keep checking fully loaded!!! (30 ) // final static int nbombs = 100; // final static int nswarm = 8; // // final static int nfligh = 15; // // final static int nexplod = 10; // final static int nstars = 20; // Starfield - number of stars final static int nhumans = 10 ; // Keep checking fully loaded!!! (100) final static int naliens = 10 ; // Keep checking fully loaded!!! (100) final static int nbombers = 2 ; // Keep checking fully loaded!!! (20 ) final static int nlasers = 30 ; // Keep checking fully loaded!!! (30 ) final static int nbombs = 100; final static int nswarm = 4 ; // final static int nfligh = 15; // final static int nexplod = 10; final static int nstars = 10; // Starfield - number of stars // Variables int player_x = 1600 ; // Start position of player int player_p = 0 ; // Various uses in player motion int player_pp = 0 ; // Variable to smooth out drag motion int x_start = player_x-160; // Where the screen starts int x_finsh = x_start + 302; // Screen end pos boolean fWrap = false; // Screen is wrapping int x_finsh_shadow = 0; // Used when screen is wrapping int player_y = 128 ; // Player y pos int player_e = 64 ; // Player explosion cycle - trip to 0 for explosion int player_dx = 0 ; // Player x speed int player_dy = 0 ; // Player y speed - not used ?? int player_cyc = 0 ; // ?? int game_state = 0 ; // Boot up - do speed test int game_clock = 0 ; // Clock int gc2 = 0 ; // ==game_clock % 2 optimisation int gc3 = 0 ; // ditto int gc4 = 0 ; // ditto int score = 0 ; // Score int lives = 3 ; // Lives for game int smart_t = 0 ; // Smart bomb clock int humans = 30; // Number of humans boolean fAliens = true; // Set false each iteration // Stays false if all aliens dead boolean fTDown = false; // true when user presses 'T' boolean fTiming = false; // Timing flag int timingCount =0; long t2c = 0; // Redraw timing var long t3c = 0; // Redraw timing var long t4c = 0; // Redraw timing var long t5c = 0; // Redraw timing var long t6c = 0; // Redraw timing var long t7c = 0; // Redraw timing var long t8c = 0; // Redraw timing var long t9c = 0; // Redraw timing var long t10c = 0; // Redraw timing var boolean fNoKeyRelease = true; // Default is for fire to release other keys // Useful if browser doesn't return key events Applet parent = null; // Handle to parent applet - graphics/sound Image buffer = null; // Off-screen buffer Graphics bg = null; // Graphics context boolean keyLeft = false; // Key status - updated by event routines boolean keyRight = false; boolean keyUp = false; boolean keyDown = false; boolean keyTab = false; boolean keyFire = false; boolean keyS = false; int[] random50 = new int[nrandom50]; int crandom50 = 0; // Random number array and current pointer int[] random = new int[nrandom]; int crandom = 0; // Random number array and current pointer int[] land_y = new int[lsize+16]; // Mountains int[] land_h = new int[2560]; // Limit on how high humans go int[] stars_x = new int[nstars]; // Star x pos int[] stars_y = new int[nstars]; // Star y pos int[] alien_x = new int[naliens]; // Aliens' x,y position int[] alien_y = new int[naliens]; int[] alien_s = new int[naliens]; // State of alien int[] alien_h = new int[naliens]; // Which human we're after! int[] alien_dx= new int[naliens]; // Attract mode bouncing aliens int[] alien_dy= new int[naliens]; int[] swarm_x = new int[nswarm]; // Swarmer x offset int[] swarm_y = new int[nswarm]; // Swarmer y offset int[] swarm_s = new int[nswarm]; // Swarmer state - 0=dead 1=alive int[] swarm_o = new int[nswarm]; // Swarmer flightpath offset int swarm_c = 0; // Swarmer clock int[] fligh_x = new int[nfligh]; // Swarmer flight path int[] fligh_y = new int[nfligh]; // ditto int pod_x = 2000; // Pod x pos int pod_y = 128 ; // Pod y pos int pod_dx = 0 ; // Pod x speed int pod_dy = 0 ; // Pod y speed - zig zag course int pod_s = 0 ; // Pod state - 0=pod 1=swarmers int[] bomber_y = new int[2560]; // Bomber flight path int[] bomber_o = new int[nbombers]; // Offset of given bomber int[] bomber_s = new int[nbombers]; // Bomber state 0=dead 1=alive int[] bomber_b = new int[nbombers]; // >0 if dropping bombs int bomber_c = 0; // Bomber clock int[] explod_x = new int[nexplod]; // x Centre of explosion int[] explod_y = new int[nexplod]; // y int[] explod_t = new int[nexplod]; // 64=done 0=start explosion int cexplod = 0; // next explosion to use - explosions are recycled int[] bomb_x = new int[nbombs]; // Bombs left by bombers int[] bomb_y = new int[nbombs]; // int[] bomb_t = new int[nbombs]; // int cbomb = 0 ; // Next bomb to use int[] human_x = new int[nhumans]; // Humans' x,y position int[] human_y = new int[nhumans]; // int[] human_s = new int[nhumans]; // Human current state int[] laser_x = new int[nlasers]; // Laser x start int[] laser_e = new int[nlasers]; // Laser x end point int[] laser_y = new int[nlasers]; // Laser y int[] laser_t = new int[nlasers]; // Laser game cycles to live int laser_f = 0; // First laser - circular buffer int laser_l = 0; // Last laser // Collision detection box round player // px-9,py-8 to px+9,py+8 // If coord of alien is inside that box - collision! int cpx1 = 0 ; int cpy1 = 0 ; int cpx2 = 0 ; int cpy2 = 0 ; // Player images Hashtable hash_g = new Hashtable(); // Used for checking loaded status public Image player0 = null; // Player going right images public Image player1 = null; public Image player2 = null; public Image lplayer0 = null; // Player going left images public Image lplayer1 = null; public Image lplayer2 = null; public Image human1_im = null; // Human images public Image human2_im = null; public Image human3_im = null; public Image lander1_im = null; // Lander images public Image lander2_im = null; public Image lander3_im = null; public Image mutant_im = null; // Mutant image public Image bomber_im = null; // Bomber public Image bomb_im = null; // Bomb public Image pod_im = null; // Pod public Image swarmer_im = null; // Bomb //------------------------------------------------------------ // checkGraphicsLoaded // // Check if all graphics are loaded, and if so kick into game // state 1. //------------------------------------------------------------ private void checkGraphicsLoaded() { if( hash_g.containsKey(player0) && hash_g.containsKey(player1) && hash_g.containsKey(player2) && hash_g.containsKey(lplayer0) && hash_g.containsKey(lplayer1) && hash_g.containsKey(lplayer2) && hash_g.containsKey(human1_im) && hash_g.containsKey(human2_im) && hash_g.containsKey(human3_im) && hash_g.containsKey(lander1_im) && hash_g.containsKey(lander2_im) && hash_g.containsKey(lander3_im) && hash_g.containsKey(mutant_im) && hash_g.containsKey(bomber_im) && hash_g.containsKey(bomb_im) && hash_g.containsKey(pod_im) && hash_g.containsKey(swarmer_im) ) { // All graphics loaded - Go into standard game mode game_state++; } } public AudioClip auStart = null; // Audio Samples public AudioClip auLaser = null; public AudioClip auExplode = null; public AudioClip auMutant = null; public AudioClip auMaterialise = null; public AudioClip auLanderPickup = null; public Image scanner = null; // Scanner image // Just has landscape //------------------------------------------------------------ // imageUpdate - Callback used when loading graphics // // When an image is completely loaded get it tiled over screen. //------------------------------------------------------------ public boolean imageUpdate( Image im,int flags,int x,int y,int w,int h ) { //System.out.println( "imageUpdate: " + flags + " w,h " + w + "," + h ); // Complete ? if( (flags & ALLBITS)!=ALLBITS ) { // Image not yet loaded return true; } // Add image to hash table hash_g.put( im,"Image" ); // All done return false; } //------------------------------------------------------------ // paintLoadingScreen // // //------------------------------------------------------------ public void paintLoadingScreen( Graphics g ) { // Explanation g.drawString( "Loading graphics and sound...",0,10 ); // Draw all available images g.drawString( "Images...",0,20 ); int x = 0; int y = 24; boolean keepGoing=true; for( ;keepGoing; ) { for( Enumeration e = hash_g.keys();e.hasMoreElements(); ) { Image im = (Image)(e.nextElement()); g.drawImage( im,x,y,Color.black,null ); int w = im.getWidth( null ); x += w; if( x>=302 ) { x=0; y+=10; } if( y>=255 ) keepGoing=false; } // for enumeration } // for fill screen } //------------------------------------------------------------ // initEvents // // Under JDK1.1 this would do some thing. //------------------------------------------------------------ public void initEvents() { // // Add listener for KeyEvents ***JDK1.1.1*** // addKeyListener( new jpControls() ); } //------------------------------------------------------------ // loadSoundsGraphics // // Load sound and graphics. When this routine is called we are // in game_state=0 (Booting). We have redraw routines that // do something with the screen whilst loading. // // When everything is loaded, game should start. //------------------------------------------------------------ public void loadSoundsGraphics( Graphics g ) { /* - Local version player0 = parent.getImage( parent.getDocumentBase(),"images/player1.gif"); player1 = parent.getImage( parent.getDocumentBase(),"images/player2.gif"); player2 = parent.getImage( parent.getDocumentBase(),"images/player3.gif"); lplayer0 = parent.getImage( parent.getDocumentBase(),"images/lplayer1.gif"); lplayer1 = parent.getImage( parent.getDocumentBase(),"images/lplayer2.gif"); lplayer2 = parent.getImage( parent.getDocumentBase(),"images/lplayer3.gif"); human1_im = parent.getImage( parent.getDocumentBase(),"images/human1.gif"); human2_im = parent.getImage( parent.getDocumentBase(),"images/human2.gif"); human3_im = parent.getImage( parent.getDocumentBase(),"images/human3.gif"); lander1_im = parent.getImage( parent.getDocumentBase(),"images/lander1.gif"); lander2_im = parent.getImage( parent.getDocumentBase(),"images/lander2.gif"); lander3_im = parent.getImage( parent.getDocumentBase(),"images/lander3.gif"); mutant_im = parent.getImage( parent.getDocumentBase(),"images/mutant.gif"); bomber_im = parent.getImage( parent.getDocumentBase(),"images/bomber.gif"); bomb_im = parent.getImage( parent.getDocumentBase(),"images/bomb.gif"); pod_im = parent.getImage( parent.getDocumentBase(),"images/pod.gif"); swarmer_im = parent.getImage( parent.getDocumentBase(),"images/swarmer1.gif"); */ // Load WEB Graphics try { player0 = parent.getImage( parent.getDocumentBase(),"images/player1.gif"); player1 = parent.getImage( parent.getDocumentBase(),"images/player2.gif"); player2 = parent.getImage( parent.getDocumentBase(),"images/player3.gif"); lplayer0 = parent.getImage( parent.getDocumentBase(),"images/lplayer1.gif"); lplayer1 = parent.getImage( parent.getDocumentBase(),"images/lplayer2.gif"); lplayer2 = parent.getImage( parent.getDocumentBase(),"images/lplayer3.gif"); human1_im = parent.getImage( parent.getDocumentBase(),"images/human1.gif"); human2_im = parent.getImage( parent.getDocumentBase(),"images/human2.gif"); human3_im = parent.getImage( parent.getDocumentBase(),"images/human3.gif"); lander1_im = parent.getImage( parent.getDocumentBase(),"images/lander1.gif"); lander2_im = parent.getImage( parent.getDocumentBase(),"images/lander2.gif"); lander3_im = parent.getImage( parent.getDocumentBase(),"images/lander3.gif"); mutant_im = parent.getImage( parent.getDocumentBase(),"images/mutant.gif"); bomber_im = parent.getImage( parent.getDocumentBase(),"images/bomber.gif"); bomb_im = parent.getImage( parent.getDocumentBase(),"images/bomb.gif"); pod_im = parent.getImage( parent.getDocumentBase(),"images/pod.gif"); swarmer_im = parent.getImage( parent.getDocumentBase(),"images/swarmer1.gif"); auStart = parent.getAudioClip( parent.getDocumentBase(),"sounds/defstart.au"); auLaser = parent.getAudioClip( parent.getDocumentBase(),"sounds/laser.au"); auExplode = parent.getAudioClip( parent.getDocumentBase(),"sounds/explode.au"); auMutant = parent.getAudioClip( parent.getDocumentBase(),"sounds/mutant.au"); auMaterialise = parent.getAudioClip( parent.getDocumentBase(),"sounds/defmaterialize.au"); auLanderPickup = parent.getAudioClip( parent.getDocumentBase(),"sounds/deflanderpickup.au"); } catch( SecurityException e1 ) { System.out.println("Security exception loading images from URL: " + e1); } // Get sounds in auStart.play(); auMaterialise.play(); auLaser.play(); auLaser.play(); auLanderPickup.play(); auMutant.play(); auExplode.play(); // Start image observers on each graphics g.drawImage( player0 ,0,0,Color.black,this ); g.drawImage( player1 ,0,0,Color.black,this ); g.drawImage( player2 ,0,0,Color.black,this ); g.drawImage( lplayer0 ,0,0,Color.black,this ); g.drawImage( lplayer1 ,0,0,Color.black,this ); g.drawImage( lplayer2 ,0,0,Color.black,this ); g.drawImage( human1_im ,0,0,Color.black,this ); g.drawImage( human2_im ,0,0,Color.black,this ); g.drawImage( human3_im ,0,0,Color.black,this ); g.drawImage( lander1_im,0,0,Color.black,this ); g.drawImage( lander2_im,0,0,Color.black,this ); g.drawImage( lander3_im,0,0,Color.black,this ); g.drawImage( mutant_im ,0,0,Color.black,this ); g.drawImage( bomber_im ,0,0,Color.black,this ); g.drawImage( bomb_im ,0,0,Color.black,this ); g.drawImage( pod_im ,0,0,Color.black,this ); g.drawImage( swarmer_im,0,0,Color.black,this ); } //------------------------------------------------------------ // Default constructor // // Load images. // Set up arrays //------------------------------------------------------------ jPlanetoid( Applet parent ) { // Need applet for sounds etc this.parent = parent; // Resize ***JDK1.02*** - necessary ?? resize( 302,255 ); // Colors //------- setForeground( Color.white ); setBackground( Color.black ); // Initialise random numbers to [-1,0,1] //-------------------------------------- for( int i=0;i0.8 ) { land_y[i % lsize]-=32; land_y[(i+lsize-1) % lsize]-=16; land_y[(i+lsize+1) % lsize]-=16; } } for( int i=0;i<16;i++ ) { land_y[lsize+i]=land_y[i]; } // Initialise array containing landscape height at each point //----------------------------------------------------------- // Lookup tables R'Us //------------------- for( int i=0;i295 ) alien_dx[i]=-alien_dx[i]; if( alien_y[i]<0 || alien_y[i]>245 ) alien_dy[i]=-alien_dy[i]; } } //------------------------------------------------------------ // paintAttractMode //------------------------------------------------------------ private void paintAttractMode( Graphics g ) { // Clear background bg.setColor( Color.black ); bg.clearRect( 0,0,302,255 ); // Attract mode g.setColor( Color.white ); g.drawString( "Attract Mode !",100,128 ); // Draw bouncing aliens for( int i=0;i0 ) --smart_t; // Counter used for indexing random numbers, image cycling etc game_clock = (++game_clock) % 1024; gc2 = game_clock % 2; gc3 = game_clock % 3; gc4 = game_clock % 4; // Check keys - Handle blitz mode when all humans dead // Modified to handle slowdown if( (keyLeft && humans>0 ) && player_dx>=-15 ) player_dx--; // Drag when not thrusting if( !keyLeft && gc4==0 && player_dx<-1 ) player_dx++; // Accelerate right if( (keyRight || humans==0 ) && player_dx<= 15 ) player_dx++; // Drag when not thrusting if( !keyRight && gc4==0 && player_dx>0 ) player_dx--; if( keyUp && player_y > 42 ) player_y-=3; if( keyDown && player_y < 250 ) player_y+=3; if( keyTab && smart_t==0 ) smart_t =10; // Attract screen side to player depending on speed //------------------------------------------------- // Move player player_x = (2560 + player_x + player_dx) % 2560; // Handle lag - this is offset of start of screen from player player_p = 151 - player_dx*9; // Smooth out by using a variable that catches up slower with player position if( player_pp > player_p ) player_pp = player_pp - 2; if( player_pp < player_p ) player_pp = player_pp + 2; x_start = (2560 + player_x - player_pp) % 2560; x_finsh = x_start + 302; // Check for wrapping across end/start of landscape // In this case, we will remap objects to after end of landscape // Simplifies visibility checking/collisions etc etc // *** INCOMPLETE - doesn't check for other case - ouch! BUG BUG BUG if( x_finsh>=2560 ) { fWrap = true; if( player_x1;sz-=3 ) { // Clear graphics cg.setColor( Color.black ); cg.clearRect( 0,0,302,255 ); // Draw in landscape and nasties paintScanner( cg ); paintStarfield( cg ); paintLandscape( cg ); // paintAliens2( cg ); paintHumans( cg ); // paintLasers( cg ); // paintBombers( cg ); // paintBombs( cg ); // draw some boxes cg.setColor( Color.white ); cg.fillRect( sz +151,128-sz,3,3); cg.fillRect( sz/2+151,128-sz,3,3); cg.fillRect(-sz +151,128-sz,3,3); cg.fillRect(-sz/2+151,128-sz,3,3); cg.fillRect( 151,128-sz,3,3); cg.fillRect( sz +151,128+sz,3,3); cg.fillRect( sz/2+151,128+sz,3,3); cg.fillRect(-sz +151,128+sz,3,3); cg.fillRect(-sz/2+151,128+sz,3,3); cg.fillRect( 151,128+sz,3,3); g.drawImage(cloud,0,0,Color.white,null); } cg.dispose(); // Draw scanner scanner = createImage(160,20); Graphics sg = scanner.getGraphics(); sg.setColor( Color.green ); for( int i=0;i0 ) paintLandscape( bg ); paintLasers( bg ); paintAliens2( bg ); paintHumans( bg ); paintBombers( bg ); paintBombs( bg ); paintSwarmers( bg ); paintExplosions(bg ); if( player_e<64 ) paintPlayerExplosion( bg ); else paintPlayer( bg ); // Render screen g.drawImage( buffer,0,0,Color.white,null ); } } //------------------------------------------------------------ // paintLandscape //------------------------------------------------------------ public void paintLandscape( Graphics g ) { // Set color g.setColor( Color.green ); // Get array position to start - lsize sections of 32 pixels int ax = x_start/32 ; // Get scroll adjust int sa = x_start % 32 ; // Landscape x pos rel to screen int ls = 0; // Draw from ax to ax + 4 - allows one extra section of screen for( int i = 0;i<12; i++ ) { int xs = (ax+i) % lsize; int xf = (ax+i+1) % lsize; // Could unroll this for a few extra cycles g.drawLine( ls-sa,land_y[xs],ls+32-sa,land_y[xf] ); // Move to next section ls += 32 ; } } //------------------------------------------------------------ // paintStarfield //------------------------------------------------------------ public void paintStarfield( Graphics g ) { // Paint each star g.setColor( Color.white ); // Start offset int so = (x_start % 302) - 302; for( int i=game_clock;i<(game_clock+nstars-1);i++ ) { int xs = (stars_x[i % nstars] - so) % 302; int ys = stars_y[i % nstars]; g.drawLine( xs,ys,xs,ys ); } } //------------------------------------------------------------ // paintAliens2 //------------------------------------------------------------ public void paintAliens2( Graphics g ) { // Colour for scanner g.setColor( Color.magenta ); // For each alien for( int i=0;i0 && (ay<200 || Math.abs(dx)<100) ) ay += 4; if( dy<0 ) ay -= 4; // Got a human ? if( dx*dx+dy*dy<25 ) { // Now controlling human s = 3; // Got human human_s[h] = 3; // Caught // Play lander pickup sound if(auLanderPickup!=null) auLanderPickup.play(); } } break; // Regular alien (default case) case 1: if( gc2==0 ) { ax += random[crandom]; ax = ax % 2560; crandom = ++crandom % nrandom; ay += random[crandom]; crandom = ++crandom % nrandom; } break; // Mutant - Chase the player case 4: if( gc2==0 ) { ax += 2*random[crandom]; ax = ax % 2560; crandom = ++crandom % 1024; ay += 2*random[crandom]; crandom = ++crandom % 1024; } // Go after player now we are a mutant //int dx = (player_x /* -25+random50[game_clock % nrandom50]*/ -ax)/10; //int dy = (player_y /* -25+random50[game_clock % nrandom50]*/ -ay)/10; int dx=0; int dy=0; if( player_x>ax ) ax+=4; else ax-=4; if( player_y>ay ) ay+=4; else ay-=4; break; // Lander with human case 3: if( gc3==2 ) { ay -= 2; int hu = alien_h[i]; human_x[hu] = ax+5; human_y[hu] = ay+10; // Have we become a mutant if( ay<=40 ) { // Mark alien as a mutant s = 4; // Mark this alien's human as dead and decrement human count human_s[hu] = 0; humans--; } } break; } // Bounds check coords if( ay<40 ) ay=40 ; if( ay>190) ay=190; if( ax<0 ) ax=0; // Update array vars alien_x[i] = ax % 2560; alien_y[i] = ay; alien_s[i] = s ; // Paint alien onto scanner int sy = ay/8 + 7; int sx = ((ax-x_start+2560+1130) % 2560)/20 + 96; g.drawRect( sx,sy,1,1 ); // If partially wrapped screen, remap alien x position if( fWrap ) { // Map alien at start of landscape to after end if( ax=x_start && ax<=x_finsh ) { // Collision detect first if( ax>cpx1 && axcpy1 && ay0;j=++j % nlasers ) { int ly = laser_y[j]; if( (ax>laser_x[j] && ax=ay && ly<=(ay+6)) || smart_t>0 ) { // Increment score score+=150; // Stop laser laser_t[j] = 0; laser_f = ++laser_f % nlasers; // Set human free if( alien_s[i]==3 ) { human_s[alien_h[i]]=1; // Assign another lander to chasing human for( int a=0;a=x_start && ax<=x_finsh ) { // Visible - draw a lander if( s<4 ) { // Draw appropriate lander switch( gc3 ) { case 0: g.drawImage( lander1_im,ax-x_start, alien_y[i],Color.black,null ); break; case 1: g.drawImage( lander2_im,ax-x_start, alien_y[i],Color.black,null ); break; default: g.drawImage( lander3_im,ax-x_start, alien_y[i],Color.black,null ); } } // Visible - draw a mutant if( s==4 ) { // Make sound if( (game_clock % 20)==0 ) { if( auMutant!=null ) auMutant.play(); } // Draw mutant g.drawImage( mutant_im,ax-x_start, alien_y[i],Color.black,null ); } } // if (alien is visible) } } //------------------------------------------------------------ // paintHumans //------------------------------------------------------------ public void paintHumans( Graphics g ) { g.setColor( Color.white ); for( int i=0;i240 ) hy=240; // Update position human_x[i] = hx; human_y[i] = hy; // If wrapping, map human at start to after end if( fWrap ) { if( hxx_start && hx0 && gc3==0 ) { // Leave a bomb bomb_x[cbomb] = xb; bomb_y[cbomb] = yb; bomb_t[cbomb] = 400; cbomb = ++cbomb % nbombs; // Dropped a bomb bomber_b[i]--; } // If screen is wrapped, remap x pos if( fWrap ) { if( xbx_start && xbcpx1 && xbcpy1 && yb0 && xb>laser_x[j] && xb=yb && ly<=(yb+3) ) { // Kill bomber bomber_s[i] = 0; // Increment score score += 250; // Stop laser laser_t[j] = 0; laser_f = ++laser_f % nlasers; explod_x[cexplod]=xb % 2560; // Do an explosion explod_y[cexplod]=yb ; explod_t[cexplod]=0 ; cexplod = ++cexplod % nexplod; break; } } } } } //------------------------------------------------------------ // paintBombs //------------------------------------------------------------ public void paintBombs( Graphics g ) { g.setColor( Color.magenta ); for( int i=0;ix_start && xb=yb && ly<=(yb+3) && xb>laser_x[j] && xb0 ) { // Kill bomb bomb_t[i] = 0; // Stop laser laser_t[j] = 0; laser_f = ++laser_f % nlasers; explod_x[cexplod]=xb % 2560; // Do an explosion explod_y[cexplod]=yb ; explod_t[cexplod]=0 ; cexplod = ++cexplod % nexplod; break; } } } } } //------------------------------------------------------------ // paintExplosions //------------------------------------------------------------ public void paintExplosions( Graphics g ) { g.setColor( Color.magenta ); for( int i=0;i=64 ) continue; // If _t=0 Make sound if( explod_t[i]==0 ) if( auExplode!=null ) auExplode.play(); // Increment explosion cycle explod_t[i] += 4; int sz = explod_t[i]; // sz should be properly named // Get explosion coordinates int xb = explod_x[i]; int yb = explod_y[i]; // Remap if screen is wrapping if( fWrap ) { if( xbx_start && xbx_start && xbx_start && pod_y=pod_y && ly<=(pod_y+5) && laser_t[j]>0 && pod_x>laser_x[j] && pod_x190 ) pod_dy = -pod_dy; for( int i=0;ix_start && xb0 && xb>laser_x[j] && xb=yb && ly<=(yb+3) ) { // Stop laser laser_t[j] = 0; laser_f = ++laser_f % nlasers; // Score score += 150; // Kill swarmer swarm_s[i]=0; // Explosion explod_x[cexplod]=xb % 2560; explod_y[cexplod]=yb ; explod_t[cexplod]=0 ; cexplod = ++cexplod % nexplod; break; // Done checking this alien } } } } } } //------------------------------------------------------------ // paintLasers //------------------------------------------------------------ public void paintLasers( Graphics g ) { for( int i=laser_f;i!=laser_l;i=++i % nlasers ) { int t = laser_t[i]; if( t > 0 ) { // Kick random numbers crandom50++; crandom50 = crandom50 % nrandom50; int rand = random50[crandom50]; // Move laser laser_x[i] += player_dx; laser_e[i] += player_dx; // Choose bolt colour switch( (int)rand/10 ) { case 0: g.setColor( Color.yellow ); break; case 1: g.setColor( Color.white ); break; case 2: g.setColor( Color.magenta ); break; case 3: g.setColor( Color.lightGray ); break; case 4: g.setColor( Color.darkGray ); break; } // Draw from laser_x to laser_e int p1 = rand; int p2 = (30-t)/2*player_dx; g.drawLine( laser_x[i]-x_start+p1 ,laser_y[i], laser_x[i]-x_start+p1+40,laser_y[i] ); g.drawLine( laser_e[i]-x_start-p1-80,laser_y[i], laser_e[i]-x_start-p1 ,laser_y[i] ); // if( laser_y[i]>0 ) { // // Draw laser with gap in it // int p1 = rand; // int p2 = (30-t)/2*player_dx; // g.drawLine( laser_x[i]-x_start+p2,laser_y[i], // laser_x[i]-x_start+p1,laser_y[i] ); // g.drawLine( laser_x[i]-x_start+p1+30+p2,laser_y[i], // laser_x[i]-x_start+p1+200,laser_y[i] ); // } // else // { // // Draw laser with gap in it // int p1 = rand; // int p2 = (30-t)/2*player_dx; // g.drawLine( laser_x[i]-x_start+p2 ,-laser_y[i], // laser_x[i]-x_start-p1 ,-laser_y[i] ); // g.drawLine( laser_x[i]-x_start-p1-30+p2,-laser_y[i], // laser_x[i]-x_start-p1-200 ,-laser_y[i] ); // } // Remove laser from buffer if run out if( --laser_t[i]<=0 ) { laser_t[i]=0; laser_f=(++laser_f) % nlasers; } } } } //------------------------------------------------------------ // paintPlayer //------------------------------------------------------------ public void paintPlayer( Graphics g ) { // Get player location int px = (player_x-x_start+2560) % 2560; // Paint player onto scanner g.setColor( Color.cyan ); int sy = player_y/8 + 7; int sx = ((player_x-x_start+2560+1130) % 2560)/20 + 96; g.drawRect( sx,sy,1,1 ); // Paint score g.drawString( String.valueOf(score),10,30 ); // Check for player getting blown up if( player_e<64 ) lives--; // Paint lives g.drawImage( player0,10,10,Color.black,null ); g.drawString( " x " + String.valueOf(lives),25,18 ); // Cycle and draw player //player_cyc = (player_cyc+1) % 3; if( player_dx>=0 ) { switch( gc3 ) { case 0: g.drawImage( player0,px,player_y,Color.black,null ); break; case 1: g.drawImage( player1,px,player_y,Color.black,null ); break; case 2: g.drawImage( player2,px,player_y,Color.black,null ); break; } } else { switch( gc3 ) { case 0: g.drawImage( lplayer0,px,player_y,Color.black,null ); break; case 1: g.drawImage( lplayer1,px,player_y,Color.black,null ); break; case 2: g.drawImage( lplayer2,px,player_y,Color.black,null ); break; } } } //------------------------------------------------------------ // update - overridden to remove background clear. //------------------------------------------------------------ public void update( Graphics g ) { paint( g ); } //------------------------------------------------------------ // paintScanner //------------------------------------------------------------ public void paintScanner( Graphics g ) { // Work out the two bits to copy int st = (x_start + 2000) % 2560; int s1 = st/20; int w1 = (2560 - st)/20; // g.drawImage( scanner,80,0,80+w1,20, // s1,0,s1+w1,20,Color.black,null ); // g.drawImage( scanner,80+w1,0,240,20, // 0 ,0,s1 ,20,Color.black,null ); // Draw blue border g.setColor( Color.blue ); g.drawLine( 0,40,301,40 ); g.drawLine( 0,41,301,41 ); g.drawRect( 95,7,128,33 ); // Draw screen highlight g.setColor( Color.white ); g.drawLine( 96+64-8, 7,96+64+8, 7 ); g.drawLine( 96+64-8,40,96+64+8,40 ); // g.drawLine( 144,0 ,160,0 ); // g.drawLine( 144,19,160,19 ); } //------------------------------------------------------------ // fireLaser // // Lasers are handled using a circular buffer. // laser_l is next laser to fire // laser_f is first in buffer // // We recycle lasers if necessary. // // laser_l==laser_f means there are no lasers //------------------------------------------------------------ public void fireLaser() { // Next laser to use is laser_l // Make sound if( auLaser!=null ) auLaser.play(); // Kick random numbers crandom50++; crandom50 = crandom50 % nrandom50; int rand = random50[crandom50]; // Set up laser bolt if( player_dx>=0 ) { //System.out.println( "fireLaser: player_x " + player_x ); // Firing forwards laser_x[laser_l] = player_x+16; laser_e[laser_l] = player_x+180; } else { // Firing backwards - note reversal of firing // May need to be taken into account in laser drawing // Makes collision detection easier. laser_x[laser_l] = player_x-164; laser_e[laser_l] = player_x; } laser_y[laser_l] = player_y+4; laser_t[laser_l] = 10; //(int)(5+rand/10); // Handle laser buffer laser_l = (laser_l+1) % nlasers; if( laser_l==laser_f ) laser_f=++laser_f % nlasers; // // Use this laser // if( player_dx>=0 ) { // laser_x[i] = player_x+16; //+ 5*player_dx; // laser_y[i] = player_y+4; // laser_t[i] = (int)(10+rand/4); // break; // } // else { // laser_x[i] = player_x;// + 5*player_dx; // laser_y[i] = -(player_y+4); // -ve == Flag for fire left // laser_t[i] = (int)(10+rand/4); // break; // } } // *** fireLaser *** /*** JDK1.1 Code for handling events =============================== // CLASS jpControls //----------------- public class jpControls implements KeyListener { public void keyPressed( KeyEvent k ) { int key = k.getKeyCode(); switch( key ) { case KeyEvent.VK_D: System.out.println( "player_x: " + player_x ); System.out.println( "player_y: " + player_y ); System.out.println( "x_start : " + x_start ); // System.out.println( ": " + ); break; case KeyEvent.VK_N: keyLeft = true ; keyRight = false; break; case KeyEvent.VK_M: keyLeft = false; keyRight = true ; break; case KeyEvent.VK_A: keyUp = true ; keyDown = false; break; case KeyEvent.VK_Z: keyUp = false; keyDown = true ; break; case KeyEvent.VK_SPACE: fireLaser(); break; } } public void keyReleased( KeyEvent k ) { int key = k.getKeyCode(); switch( key ) { case KeyEvent.VK_N: keyLeft = false; break; case KeyEvent.VK_M: keyRight = false; break; case KeyEvent.VK_A: keyUp = false; break; case KeyEvent.VK_Z: keyDown = false; break; } } public void keyTyped( KeyEvent k ) { } } // *** CLASS jpControls *** ***/ // END OF JDK1.1 Event Handling /*** JDK 1.0 Event Handling Code =========================== */ // Grab input focus on mouse click public boolean mouseDown( Event e,int x,int y ) { requestFocus(); return true; } // Key pressed public boolean keyDown( Event e,int key ) { switch( key ) { case 110: // N keyLeft = true ; keyRight = false; return true; case 109: // M keyLeft = false; keyRight = true ; return true; case 97: // A keyUp = true ; keyDown = false; return true; case 122: // Z keyUp = false; keyDown = true ; return true; case 32: // SPC // if( fNoKeyRelease ) { // this.keyLeft = false; // this.keyRight = false; // this.keyDown = false; // this.keyUp = false; // } keyFire = true ; fireLaser(); return true; case 116: // T (Print timing info) fTDown = true; return true; case 9: keyTab = true; return true; case 115: keyS = true; return true; default: System.out.println("KeyDown: " + key); } return false; } // Key released public boolean keyUp( Event e,int key ) { // System.out.println("KeyUp: " + key); // fNoKeyRelease = false; switch( key ) { case 110: // N keyLeft = false; return true; case 109: // M keyRight = false; return true; case 97: // A keyUp = false; return true; case 122: // Z keyDown = false; return true; case 116: // T fTDown = false; return true; case 9: keyTab = false; return true; case 32: keyFire = false; return true; case 115: keyS = false; return true; } return false; } // END OF JDK1.1 Event Handling Code } // *** CLASS jPlanetoid ***