Joshs Tug Of Word

<< Kevin's Tower Defense Game | OldProjectsTrailIndex | Video Poker by Mark >>

Click Here to see a working version

Cool and Creative! here is a two line version of Letter.getCode() I think you will enjoy:

public int getCode()
	{
		String map="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		return 65+map.indexOf(letter.toUpperCase());
}

Final Project (rough)

Rope.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

public class Rope 
{
	private int x;
	public Rope()
	{
		x = 250;
	}
	public void draw(Graphics g)
	{
		g.setColor(new Color(139,69,19));
		g.drawRect(0, 250, 1000, 20);
		g.fillRect(0, 250, 1000, 20);
		g.setColor(Color.RED);
		int [] x1points = {x-25,x+25,x};
		int [] ypoints = {250,250,300};
		Polygon tri1 = new Polygon(x1points, ypoints,3);
		g.fillPolygon(tri1);
		g.drawPolygon(tri1);
		g.setColor(Color.BLUE);
		int [] x2points = {x+475,x+525,x+500};
		Polygon tri2 = new Polygon(x2points, ypoints, 3);
		g.fillPolygon(tri2);
		g.drawPolygon(tri2);
	}
	public int getX()
	{
		return x;
	}
	public void setX(int y)
	{
		x = y;
	}
}

Letter.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class Letter 
{
	private String [] one = {"Q","W","E","R","T","A","S","D","F","Z","X","C","V"};        
	private String [] two = {"Y","U","I","O","P","G","H","J","K","L","B","N","M"};
	private boolean side;
	private String letter;
	private int x,y;

	public Letter(boolean face)
	{
		side = face;
		y = 200;
		if(side)
		{
			letter = one[(int) (Math.random()*13)];
			x = 230;
		}
		else
		{
			letter = two[(int) (Math.random()*13)];
			x = 730;
		}
	}
	public void draw(Graphics g)
	{
		g.setColor(Color.BLACK);
		g.setFont(new Font("Helvtica", Font.BOLD, 60));
		g.drawString(letter, x, y);
	}

	public void newLetter()
	{
		if(side) letter = one[(int) (Math.random()*13)];
		else letter = two[(int) (Math.random()*13)];
	}

	public String getLetter()
	{
		return letter;
	}

	public int getCode()
	{
		if(letter.equalsIgnoreCase("A")) return 65;
		if(letter.equalsIgnoreCase("B")) return 66;
		if(letter.equalsIgnoreCase("C")) return 67;
		if(letter.equalsIgnoreCase("D")) return 68;
		if(letter.equalsIgnoreCase("E")) return 69;
		if(letter.equalsIgnoreCase("F")) return 70;
		if(letter.equalsIgnoreCase("G")) return 71;
		if(letter.equalsIgnoreCase("H")) return 72;
		if(letter.equalsIgnoreCase("I")) return 73;
		if(letter.equalsIgnoreCase("J")) return 74;
		if(letter.equalsIgnoreCase("K")) return 75;
		if(letter.equalsIgnoreCase("L")) return 76;
		if(letter.equalsIgnoreCase("M")) return 77;
		if(letter.equalsIgnoreCase("N")) return 78;
		if(letter.equalsIgnoreCase("O")) return 79;
		if(letter.equalsIgnoreCase("P")) return 80;
		if(letter.equalsIgnoreCase("Q")) return 81;
		if(letter.equalsIgnoreCase("R")) return 82;
		if(letter.equalsIgnoreCase("S")) return 83;
		if(letter.equalsIgnoreCase("T")) return 84;
		if(letter.equalsIgnoreCase("U")) return 85;
		if(letter.equalsIgnoreCase("V")) return 86;
		if(letter.equalsIgnoreCase("W")) return 87;
		if(letter.equalsIgnoreCase("X")) return 88;
		if(letter.equalsIgnoreCase("Y")) return 89;
		if(letter.equalsIgnoreCase("Z")) return 90;
		return 0;
	}
}

TugOfWar.java

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class TugOfWar extends Applet implements KeyListener
{
	/**
	 * version 1.0
	 */
	private static final long serialVersionUID = 1L;
	private Letter leftLetter;
	private Letter rightLetter;
	private Rope rope;
	private int count;
	private boolean start = true;
	private boolean done = false;
	private Image imageBuffer;
	private Graphics gBuffer;

	public void init()
	{
		imageBuffer = createImage(1000,400);
        gBuffer = imageBuffer.getGraphics();
		resize(1000,400);
		this.addKeyListener(this);
		leftLetter = new Letter(true);
		rightLetter = new Letter(false);
		rope = new Rope();
		count = 0;
	}
	public void paint(Graphics g)
	{
		if(start)
		{
			gBuffer.setColor(Color.BLACK);
			gBuffer.drawRect(0, 0, 1000, 400);
			gBuffer.fillRect(0, 0, 1000, 400);
			gBuffer.setColor(Color.WHITE);
			gBuffer.setFont(new Font("Helvtica", Font.BOLD, 30));
			gBuffer.drawString("Press Space to Start", 350, 200);
			gBuffer.drawString("Try To Type More Letters Than Your Opponent", 180, 250);
			g.drawImage(imageBuffer,0,0,this);
		}
		if(count > 4 && !start && !done)
		{
			gBuffer.setColor(Color.BLUE);
			gBuffer.drawRect(0, 0, 1000, 400);
			gBuffer.fillRect(0, 0, 1000, 400);
			gBuffer.setColor(Color.WHITE);
			gBuffer.setFont(new Font("Helvtica", Font.BOLD, 30));
			gBuffer.drawString("Player 2 Wins!", 400, 200);
			gBuffer.drawString("Press Space to Play Again", 320, 250);
			g.drawImage(imageBuffer,0,0,this);
			done = true;
		}
		if(count < -4 && !start && !done)
		{
			gBuffer.setColor(Color.RED);
			gBuffer.drawRect(0, 0, 1000, 400);
			gBuffer.fillRect(0, 0, 1000, 400);
			gBuffer.setColor(Color.WHITE);
			gBuffer.setFont(new Font("Helvtica", Font.BOLD, 30));
			gBuffer.drawString("Player 1 Wins!", 400, 200);
			gBuffer.drawString("Press Space to Play Again", 320, 250);
			g.drawImage(imageBuffer,0,0,this);
			done = true;
		}
		if(count < 5 && count > -5 && !start & !done)
		{
			gBuffer.setColor(Color.GREEN);
			gBuffer.drawRect(0, 0, 1000, 400);
			gBuffer.fillRect(0, 0, 1000, 400);
			leftLetter.draw(gBuffer);
			rightLetter.draw(gBuffer);
			gBuffer.setColor(Color.BLACK);
			gBuffer.setFont(new Font("Times ", Font.ITALIC, 70));
			gBuffer.drawString("TUG OF WORD", 235, 80);
			gBuffer.setColor(Color.BLACK);
			gBuffer.fillOval(20, 105, 100, 100);
			gBuffer.drawRect(65, 200, 10, 200);
			gBuffer.fillRect(65, 200, 10, 200);
			gBuffer.fillOval(880, 105, 100, 100);
			gBuffer.drawRect(925, 200, 10, 200);
			gBuffer.fillRect(925, 200, 10, 200);
			int [] ax1points = {75,75,200,200};
			int [] ay1points = {210,220,260,250};
			int [] ax2points = {65,25,25,65};
			int [] ay2points = {210,250,260,220};
			int [] bx1points = {925,925,800,800};
			int [] by1points = {210,220,260,250};
			int [] bx2points = {935,935,975,975};
			int [] by2points = {210,220,260,250};
			int [] cx1points = {25,25,95,95};
			int [] cy1points = {250,260,280,270};
			int [] cx2points = {975,975,905,905};
			int [] cy2points = {250,260,280,270};
			Polygon arm1 = new Polygon(ax1points,ay1points,4);
			Polygon arm2a = new Polygon(ax2points,ay2points,4);
			Polygon arm3 = new Polygon(bx1points, by1points,4);
			Polygon arm4a = new Polygon(bx2points,by2points,4);
			Polygon arm2b = new Polygon(cx1points,cy1points,4);
			Polygon arm4b = new Polygon(cx2points,cy2points,4);
			gBuffer.drawPolygon(arm1);
			gBuffer.fillPolygon(arm1);
			gBuffer.drawPolygon(arm3);
			gBuffer.fillPolygon(arm3);
			rope.draw(gBuffer);	
			gBuffer.setColor(Color.BLACK);
			gBuffer.drawPolygon(arm2a);
			gBuffer.fillPolygon(arm2a);
			gBuffer.drawPolygon(arm2b);
			gBuffer.fillPolygon(arm2b);
			gBuffer.drawPolygon(arm4a);
			gBuffer.fillPolygon(arm4a);
			gBuffer.drawPolygon(arm4b);
			gBuffer.fillPolygon(arm4b);
			g.drawImage(imageBuffer,0,0,this);
			}
		}
	public void update(Graphics g)
    {
        paint(g); 
    }
	public void keyPressed(KeyEvent e) 
	{
		int key = e.getKeyCode();
		if(start && key == 32)
		{
			start = false;
			repaint();
		}
		if(done && key == 32)
		{
			done = false;
			count = 0;
			rope.setX(250);
			repaint();
		}
		if(!done && !start && key == leftLetter.getCode())
		{
			rope.setX(rope.getX()-50);
			count--;
			leftLetter.newLetter();
			rightLetter.newLetter();
			repaint();
		}
		if(!done && !start && key == rightLetter.getCode())
		{
			rope.setX(rope.getX()+50);
			count++;
			leftLetter.newLetter();
			rightLetter.newLetter();
			repaint();
		}	
	}
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
}