Use ',' to move the aim represented by an asterix to the left, and '.' to the right. Press space bar to fire. This is still in beta mode. // This is the Java version of an MSX BASIC game written by somebody other than me. // I am the one who converted the BASIC game into the Java version. // DISCLAIMER : Use this program at your own risk! No warranty whatsoever is given // my email address : kyungsik@ee.mcgill.ca // my home page : http://www.ee.mcgill.ca/~kyungsik // finished ( in beta ) as of August 6, 1996 // use 3 buttons to play the game. // "," to move the asterix to the left // "." to move the asterix to the right // " " to fire a torpedo // if you score more than 1500 when time is zero, // your score is not reset to zero, and you can keep destroying ships. // if you want this game to be completed, send me a fan letter via email. import java.applet.Applet; import java.awt.*; public class seabattle extends Applet implements Runnable { int x1, x2, x3; //Ship's x position boolean e1,e2,e3; //Exploded or not int x,y; //Torpedo's position int t; // time limits int s, hs, s1; // present score, high score, previous score int ht; // aim's x position int i; // decides what to animate float a; // torpedo's slope Image ShipImg, ExplosionImg; Thread battle; int z; // zoom factor int fh; // font height boolean aim_ready; int delay; boolean is_initialized; boolean remove_torpedo; int k; public void init() { ShipImg = getImage(getCodeBase(), "ShipImg.gif"); ExplosionImg = getImage(getCodeBase(), "ExplosionImg.gif"); x1 = 123; x2 = 7; x3 = 171; e1=e2=e3=false; x = 93; y = -1; t = 100; s = 0; s1 = 0; ht = 93; i = 0; fh = 16; a = 0; aim_ready = false; is_initialized = false; remove_torpedo = false; String str = getParameter("fps"); int fps = (str != null) ? Integer.parseInt(str) : 10; delay = (fps > 0) ? (1000/fps):100; str = getParameter("z"); z = (str != null) ? Integer.parseInt(str) : 2; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { if (!is_initialized) { g.setColor(Color.yellow); g.fillRect(0,0,255*z,191*z); g.setColor(Color.blue); g.fillRect(6*z,9*z,180*z,188*z); g.setColor(Color.red); g.drawString(" SEA ",198*z,2*z+fh); g.drawString(" BATTLE ",198*z,10*z+fh); is_initialized = true; } if (!remove_torpedo) { g.setColor(Color.blue); g.fillRect( 6*z, 16*z, 180*z, 82*z); } if (y>-1) { g.setColor(Color.white); g.drawLine(93*z,193*z,x*z,y*z); } else if (remove_torpedo) { g.setColor(Color.blue); g.drawLine(93*z,183*z, ( 93+(int)( a*(float)(183-(19-k)*10) ) )*z, 10*(19-k)*z ); if (k==18) { g.setColor(Color.blue); g.fillRect( 6*z,82*z, 180*z, 184*z); } } //g.fillRect( (x1-7)*z,66*z,(x1+7+16)*z,82*z); if (e1==true) g.drawImage(ExplosionImg, x1*z, 66*z, this); else g.drawImage(ShipImg, x1*z, 66*z, this); //g.fillRect( (x2-10)*z,46*z,(x2+10+16)*z,62*z); if (e2==true) g.drawImage(ExplosionImg, x2*z, 46*z, this); else g.drawImage(ShipImg, x2*z, 46*z, this); //g.fillRect( (x3-12)*z,16*z,(x3+12+16)*z,32*z); if (e3==true) g.drawImage(ExplosionImg, x3*z, 16*z, this); else g.drawImage(ShipImg, x3*z, 16*z, this); g.setColor(Color.yellow); g.fillRect(190*z,30*z,255*z,78*z+fh); g.fillRect(0,0,190*z,1*z+fh); g.setColor(Color.black); g.drawString("*", (ht-3)*z, 1*z+fh); g.drawString("A", 91*z, 184*z+fh); g.setColor(Color.green); g.drawString("HI-SCORE "+hs, 190*z,30*z+fh); g.drawString("SCORE "+s, 198*z,54*z+fh); g.drawString("TIME "+t, 198*z,78*z+fh); } public void run() { Thread me = Thread.currentThread(); boolean is_gameOver = false; long startTime = System.currentTimeMillis(); me.setPriority(Thread.MIN_PRIORITY); getAppletContext().showStatus("The battle has begun"); while (!is_gameOver) { i++;aim_ready=true; switch (i) { case 1 : if ( (y>0) && (y<70) ) break; x1 += ( ( (Math.random()<.95| (x1>150) ) ?(-7):0) + ( (Math.random()>.9)?(7):0 ) ); x1 = (x1>8)?x1:166; getAppletContext().showStatus("Ship1 moves : "+x1); break; case 2 : if ( (y>0) && (y<50) ) break; x2 += ( ( (Math.random()<.05) ? (-10) : 0 ) + ( ( (Math.random()>.1)|(x2<18) )?10:0 ) ); x2 = (x2>166)?8:x2; getAppletContext().showStatus("Ship2 moves : "+x2); break; case 3 : if ( (y>0) && (y<20) ) break; x3 += ( ( ((Math.random()<.82)|(x3>150)) ? (-12) : 0 ) + ( (Math.random()>.7)?12:0 ) ); x3 = (x3<8)?166:x3; getAppletContext().showStatus("Ship3 moves : "+x3); break; case 10 : t--; if (t<=0) timeUp(); break; } if (y>-1) if (y<=10) RemoveTorpedo(); else { y -= 10; x = 93 + (int)( (float)(183-y)*a ); getAppletContext().showStatus("torpedo x,y"+x+" , "+y+" at "+a); // collision check , no explosion yet if ( (y==70) && (x>(x1+1)) && (x< (x1+14) ) ) { e1 = true; s += 70; getAppletContext().showStatus("Hit ship1"); RemoveTorpedo(); x1 = 167; e1=false; } if ( (y==50) && (x>(x2+1)) && ( x<(x2+14) ) ) { e2 =true; s +=60; getAppletContext().showStatus("Hit ship2"); RemoveTorpedo(); x2 = (int)(Math.random()*100) + 8; e2=false; } if ( (y==20) && (x>(x3+1)) && ( x<(x3+14) ) ) { e3 =true; s += 80; getAppletContext().showStatus("Hit ship3"); RemoveTorpedo(); x3 = -(int)(Math.random()*100) + 166; e3=false; } } if (i>=10) { i=0; } repaint(); try { startTime += delay; Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break; } } stop(); } public void RemoveTorpedo(){ y = -1;remove_torpedo=true; for (k=1;k<19;k++) { repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { break; } } remove_torpedo=false; } public boolean handleEvent(Event e) { if (aim_ready) switch(e.id) { case Event.KEY_ACTION: case Event.KEY_PRESS : { switch(e.key) { case ',' : ht = (ht<9)?9:(ht-5);break; case '.' : ht = (ht>177)?177:(ht+5);break; case ' ' : if (y==-1) { a = (float)( (float)(ht-93)/179 ); x = 93; y = 190; } break; } } } aim_ready = false; return super.handleEvent(e); } public void timeUp() { if ( (s-s1)<1500 ) { // play music hs = (s>hs)?s:hs; // stop(); // for the time being init(); } t=101; } public void start() { if (battle==null) { battle = new Thread(this); battle.start(); } } public void stop() { battle.stop(); battle=null; } }