Home   Info   DevZone   Wiki  
UsersWeb  |  MainWeb  |  InfoWeb  |  DevZoneWeb  |  SupportWeb
PaddleWars.java ] [ not logged in ] [ Web: Main ] goto:  options
[ get info on or edit ] login or new user ] [ list of topics, hubs & nodes, or recent changes ]

ProblemArticles


/*
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,  
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS  
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS  
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
POSSIBILITY OF SUCH DAMAGE.

THIS SOFTWARE IS PROVIDED FOR JOS AND IS RELEASED UNDER THE GPL (GNU PUBLIC LICENSE). IF 
YOU DO NOT UNDERSTAND THE TERMS OF THE GPL, YOU ARE NOT LEGAL TO USE THIS SOFTWARE.

so much for the legal stuff, now to the important part:

High,
welcome to PaddleWars, Version 0.3 (= Release 2). Not much going on yet, but you are able
to play!

Short Release History:
R1: 0.1: Single player, pretty simple short hack
	 0.2: Multiplayer, startup sequence.
R2: 0.3: Points Counter, End of Game, Timer

future releases: 
Statistics for single player
more color!

howto Use:
- put this file in a directory xyz/jos/org/games  [where xyz is any dir]
  [Win users: xyzjosorggames, is any dir]
  The name of the file must be PaddleWars.java (capitalisation is important!)
- go to dir xyz/jos/org/games
- compile it: 
  javac PaddleWars.java
- go to the directory xyz and call
  java -classpath $CLASSPATH:. org.jos.games.PaddeWars
  [WIN: java -classpath %CLASSPATH%;. org.jos.games.PaddleWars

General Hints:
are now all explained within the game.
The Game is too easy for you? Try Setting your skill to expert :-)

This game is fully scalable. Screen to small? Resize it! The whole game is dynamically 
adjusted to the window size. Play it in 50x50 or 2000x2000 !

If you have any problems, suggestions, hints, anything, mail me at

max.berger@phobos.fachschaften.tu-muenchen.de

*/

package org.jos.games; 

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;

public class PaddleWars extends Frame implements KeyListener,Runnable {
 
  double px,py,rx,ry,p1y,p2y;

  boolean p1u,p1d,p2u,p2d,pause;

  double p1s;
  double p2s;
  static double bs=0.04;
  
  static String VERSION="0.3";

  int p1p,p2p;

  static boolean dif2 = true;
  int maxpt;
 
  int gametime;

  PaddleWars(int p1, int p2, int m) {
	 p1s = ((double)p1/20); if (p1==0) p1s=1.0;
	 p2s = ((double)p2/20); if (p2==0) p2s=1.0;
	 maxpt = m;
	 show();
	 px = 0.5; rx = 0.005;
	 py = 0.5; ry = 0.005;

	 p1p = 0;
	 p2p = 0;
 
	 p1u=p1d=p2u=p2d=pause=false;

	 p1y= 0.5-(p1s/2); p2y = 0.5-(p2s/2);
	 gametime=0;
	 setSize(640,480);
	 setTitle("PaddleWars");
	 addKeyListener(this);
	 new Thread(this).start();
	 }
 
  public static void main(String[] a) {
	 try {
		BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Welcome to PaddleWars, Version "+VERSION);
		System.out.println();
		System.out.println("0...Computer");
		System.out.println("1...Expert");
		System.out.println("2...Normal");
		System.out.println("3...Easy");
		System.out.println("4...Very Easy");
		System.out.println("5...Beginners");
		System.out.println();
		int p1=9,p2=9,m=0;
		while ((p1<0)||(p1>5)) {System.out.print ("Player 1 (0..5): "); p1 = new Integer(d.readLine()).intValue();}
		while ((p2<0)||(p2>5)) {System.out.print ("Player 2 (0..5): "); p2 = new Integer(d.readLine()).intValue();}
		while ((m<3)||(m>51)) {System.out.print ("Maximum Points (3..51): "); m = new Integer(d.readLine()).intValue();}
		System.out.println();
		System.out.println("Warning: Mouse might have to be within the window for the keys to works");
		System.out.println();
		System.out.println("w......Quit");
		System.out.println("p......Pause");
		if (p1!=0) {
		  System.out.println("q......Player 1 Up");
		  System.out.println("a......Player 1 Down");
		  }
		if (p2!=0) {
		  System.out.println("Up.....Player 2 Up");
		  System.out.println("Down...Player 2 Down");
		  }
		PaddleWars w = new PaddleWars(p1,p2,m); 
		} catch (Exception e) {
		System.out.println("Sorry, an Exception occured");
		}
	 }

  public void finish() {
	 System.out.println();
	 if (p1p==maxpt) {
		System.out.println("Player 1 won the game by getting "+maxpt+" points!");
		System.out.println("Player 2 had only "+p2p+" point(s).");
		} else if (p2p==maxpt) {
		System.out.println("Player 2 won the game by getting "+maxpt+" points!");
		System.out.println("Player 1 had only "+p1p+" point(s).");
		} else {
		System.out.print("The game was early aborted ");
		if (p1p==p2p) System.out.println("and was a draw at "+p1p+" point(s)."); else
		if (p1p>p2p) System.out.println("and was won by player 1 ("+p1p+":"+p2p+")"); else
		  System.out.println("and was won by player 2 ("+p2p+":"+p1p+")");
		}
	 System.out.println("GameTime was "+((double)gametime / 100)+" s.");
	 dispose();
	 java.lang.System.exit(0);
	 }

  public void keyTyped(KeyEvent k) {
	 Graphics g = getGraphics();
	 if (k.getKeyChar() == 'w') finish();
	 if (k.getKeyChar() == 'p') pause=!pause;
	 }

  public void keyPressed(KeyEvent k) {
	 if (k.getKeyChar()=='q') p1u = true;
	 if (k.getKeyChar()=='a') p1d = true;

	 if (k.getKeyCode()==KeyEvent.VK_UP)	p2u = true;
	 if (k.getKeyCode()==KeyEvent.VK_DOWN) p2d = true;
	 }
  public void keyReleased(KeyEvent k) {
	 if (k.getKeyChar()=='q') p1u = false;
	 if (k.getKeyChar()=='a') p1d = false;

	 if (k.getKeyCode()==KeyEvent.VK_UP)	p2u = false;
	 if (k.getKeyCode()==KeyEvent.VK_DOWN) p2d = false;
	 }
  
  void doDraw(Graphics g, boolean tobo, boolean clearBall, boolean clearPanel,boolean clearPoints) {
	 int left = getInsets().left;
	 int top = getInsets().top;
	 int right = getSize().width-getInsets().right;
	 int bottom = getSize().height-getInsets().bottom;
	 int ssize = g.getFontMetrics().getHeight();

	 bottom-=ssize;

	 ssize-=g.getFontMetrics().getDescent();

	 int factorx = (right-left);
	 int factory = (bottom-top);

	 if (clearBall) {
		g.clearRect(left+(int)((px-0.01)*factorx),top+(int)((py-0.01)*factory),(int)(0.02*factorx),(int)(0.02*factory));
		}else {
		g.fillRect(left+(int)((px-0.01)*factorx),top+(int)((py-0.01)*factory),(int)(0.02*factorx),(int)(0.02*factory));
		}

	 if (clearPoints) {
		g.clearRect(0,bottom,right,ssize);
		} else {
		g.drawString(new Integer(p1p).toString(),left+(int)(0.01*factorx),bottom+ssize);
		String s2 = new Integer(p2p).toString();
		g.drawString(s2,left+(int)(0.99*factorx)-g.getFontMetrics().stringWidth(s2),bottom+ssize);
		}


	 if (clearPanel) {
		if (p1s<0.99) g.clearRect(left,top+(int)(p1y*factory),(int)(0.02*factorx),(int)(p1s*factory));
		if (p2s<0.99)g.clearRect(left+(int)(0.98*factorx),top+(int)(p2y*factory),(int)(0.02*factorx),(int)(p2s*factory));
		} else { 
		g.fillRect(left,top+(int)(p1y*factory),(int)(0.02*factorx),(int)(p1s*factory));
		g.fillRect(left+(int)(0.98*factorx),top+(int)(p2y*factory),(int)(0.02*factorx),(int)(p2s*factory));
		}
	 
	 if (tobo||clearPanel) {
		g.fillRect(left,top,(int)(1.0*factorx),(int)(bs*factory));
		g.fillRect(left,top+(int)((1.0-bs)*factory),(int)(1.0*factorx),(int)(bs*factory));
		}

	 }

  public void paint(Graphics g) {
	 super.paint(g);
	
	 doDraw(g,true,false,false,false);
	}

  public void run() {
	 try {
		while (true) { 
		  Thread.sleep(10);
		  if (!pause) {
			 gametime++;
			 Graphics g = getGraphics(); 
			 boolean p = p1u || p1d || p2u || p2d;
			 if ((py<(bs+0.01)) || (py>(0.99-bs))) doDraw(g,true,true,p,false); else doDraw(g,false,true,p,false);
			 px+=rx;
			 py+=ry;

			 if ((p1u)&&(p1y>bs)) p1y-=0.01;
			 if ((p1d)&&(p1y<(1.0-bs-p1s))) p1y+=0.01;
 
			 if ((p2u)&&(p2y>bs)) p2y-=0.01;
			 if ((p2d)&&(p2y<(1.0-bs-p2s))) p2y+=0.01;

			 doDraw(g,false,false,false,false);
			 if ((px<0.02) || (px>0.98)) {
				rx=-rx;
				double nry = (Math.random()*0.010)+0.002;
				if (ry<0) ry=-nry; else ry = nry;
 
				if (px<0.02) {
				  if ((py<p1y)||(py>(p1y+p1s))) { 
					 doDraw(g,false,true,false,true);
					 px = 0.5;
					 p2p++;
					 if ((p1p==p2p)&&(p1p==maxpt-1)&&(dif2)) { p1p=p2p=maxpt-2; }
					 if (p2p==maxpt) {
						finish();
						}
					 doDraw(g,false,false,false,false);
					 }
				  }
				if (px>0.98) {
				  if ((py<p2y)||(py>(p2y+p2s))) {
					 doDraw(g,false,true,false,true);
					 px = 0.5;
					 p1p++;
					 if ((p1p==p2p)&&(p1p==maxpt-1)&&(dif2)) { p1p=p2p=maxpt-2; }
					 doDraw(g,false,false,false,false);
					 if (p1p==maxpt) {
						finish();
						}
					 }
				  }
				}
			 if ((py<0.05) || (py>0.95)) {
				ry=-ry;
				}
			 }
		  }
		} catch (Exception e) {}
	 }

  }


I added pre html tags so that the code could just be copied without having to edit the page.

-- RobertFitzsimons (23 February 1999)




Content of these pages are owned and copyrighted by the poster.
Hosted by: