Skip to content

Instantly share code, notes, and snippets.

@davisonio
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davisonio/cb2abef467292354ef7e to your computer and use it in GitHub Desktop.
Save davisonio/cb2abef467292354ef7e to your computer and use it in GitHub Desktop.
Really shitty capture the flag game in Java.
import sun.audio.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
import java.io.*;
public class CaptureTheFlag extends Frame implements KeyListener
{
/* Your coordinates */
private int x;
private int y;
/* random encouragement sounds */
int encouragementCounter = 0;
int randomNumber = 0;
/* Used to play sounds only once */
public int counter = 0;
/* Used for a temporary value for hit detection */
int tempXPosition;
int tempYPosition;
Random rand = new Random();
/* The enemies */
private int[] xCoordinates = new int[10];
private int[] yCoordinates = new int[10];
private int[] xHarderEnemies = new int [7];
private int[] yHarderEnemies = new int[7];
/* The flag */
private int xFlag = 250;
private int yFlag = 50;
/* The returning box */
private int xSafeBox = 200;
private int ySafeBox = 900;
private boolean carryingFlag = false;
/* Determines if an enemy is touching you */
private boolean hit = false;
/* Sound variables */
private static AudioPlayer MGP = AudioPlayer.player;
private static AudioStream BGM;
private static AudioData MD;
private static AudioDataStream play = null;
private static ContinuousAudioDataStream loop = null;
public CaptureTheFlag()
{
setBackground(Color.gray);
x = 200;
y = 900;
/* RULES */
System.out.println("******RULES******");
System.out.println("Press WASD to move.");
System.out.println("If an enemy touches you, you reset to your starting position");
System.out.println("You can move off the left and right edge of the screen and wrap around the other side");
System.out.println("Touch the flag to carry it");
System.out.println("If an enemy touches you while you have the flag, everything resets");
System.out.println("Bring the flag back to your square to win.");
setTitle("Capture the flag!");
/* Assigning coordinates to the enemies */
for (int i = 0; i < xCoordinates.length; i++)
{
xCoordinates[i] = rand.nextInt(450);
yCoordinates[i] = rand.nextInt(700) + 100;
}
/* Assigning coordinates to the harder enemies */
for (int i = 0; i < xHarderEnemies.length; i++)
{
xHarderEnemies[i] = xCoordinates[i];
yHarderEnemies[i] = yCoordinates[i] + 150;
}
this.setSize(500,1000);
addKeyListener(this);
}
public void keyTyped(KeyEvent event)
{
if(event.getKeyChar() == 'a')
{
x-=10;
if (CarryingFlag() == true && x < 0)
{
x = 450; // Wraps around to the right side, including your flag
xFlag = x;
}
else if (x < 0)
{
x = 450; // Wraps around to the right side
}
}
else if(event.getKeyChar() == 'd')
{
x+=10;
if (CarryingFlag() == true && x > 450)
{
x = 0; // Wraps around to the left side, including your flag
xFlag = x;
}
else if (x > 450)
{
x = 0; // Wraps around to the left side
}
}
else if(event.getKeyChar() == 's')
{
if (y < 940)
y+=10; // Sets a bottom boundary
}
else if(event.getKeyChar()== 'w')
{
if (y > 50)
y-=10; // Sets a top boundary
}
/* Moves enemies towards you */
for (int i = 0; i < xCoordinates.length; i++)
{
if (x < xCoordinates[i])
xCoordinates[i] -= rand.nextInt(5)+1;
if (x > xCoordinates[i])
xCoordinates[i] += rand.nextInt(5)+1;
if (y < yCoordinates[i])
yCoordinates[i] -= rand.nextInt(5)+1;
if (y > yCoordinates[i])
yCoordinates[i] += rand.nextInt(5)+1;
}
/* Moving the harder enemies */
if (carryingFlag == true)
{
MoveHarderEnemies();
}
/*footstep sounds*/
footstep();
/* encouragement sounds */
encouragementCounter++;
if (encouragementCounter >= 50)
{
randomNumber = rand.nextInt(20);
if (randomNumber == 1)
{
closeToVictory();
encouragementCounter = 0;
}
else if (randomNumber == 2)
{
weGotThis();
encouragementCounter = 0;
}
else if (randomNumber == 3)
{
keepFighting();
encouragementCounter = 0;
}
else if (randomNumber == 4)
{
fightHarder();
encouragementCounter = 0;
}
}
repaint();
}
public void keyPressed(KeyEvent event)
{
}
public void keyReleased(KeyEvent event)
{
}
public void MoveHarderEnemies()
{
for (int i = 0; i < xHarderEnemies.length; i++)
{
if (x < xHarderEnemies[i])
xHarderEnemies[i] -= rand.nextInt(6)+1;
if (x > xHarderEnemies[i])
xHarderEnemies[i] += rand.nextInt(6)+1;
if (x - xHarderEnemies[i] > 300)
xHarderEnemies[i] -= rand.nextInt(10)+5;
else if (x - xHarderEnemies[i] < -300)
xHarderEnemies[i] += rand.nextInt(10)+5;
if (xHarderEnemies[i] < 0)
xHarderEnemies[i] += 450;
if (xHarderEnemies[i] > 450)
xHarderEnemies[i] -= 450;
if (Math.abs(x-xHarderEnemies[i]) <= 100 && Math.abs(y-yHarderEnemies[i]) <= 100)
{
if (y < yHarderEnemies[i])
{
yHarderEnemies[i] -= rand.nextInt(3)+1;
}
if (y > yHarderEnemies[i])
{
yHarderEnemies[i] += rand.nextInt(3)+1;
}
}
}
}
public static void fallBack()
{
try
{
BGM = new AudioStream(new FileInputStream("fallBack.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void missionFailed()
{
try
{
BGM = new AudioStream(new FileInputStream("missionFailed.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void lose()
{
try
{
BGM = new AudioStream(new FileInputStream("lose.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void capturedOurFlag()
{
try
{
BGM = new AudioStream(new FileInputStream("CapturedOurFlag.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void fightHarder()
{
try
{
BGM = new AudioStream(new FileInputStream("fightHarder.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void footstep()
{
try
{
BGM = new AudioStream(new FileInputStream("realFootstep.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void starting()
{
try
{
BGM = new AudioStream(new FileInputStream("backgroundMusic.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void startingSound()
{
try
{
BGM = new AudioStream(new FileInputStream("CaptureTheFlag.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void weGotThis()
{
try
{
BGM = new AudioStream(new FileInputStream("weGotThis.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void missionAccomplished()
{
try
{
BGM = new AudioStream(new FileInputStream("missionAccomplished.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void closeToVictory()
{
try
{
BGM = new AudioStream(new FileInputStream("closeToVictory.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void keepFighting()
{
try
{
BGM = new AudioStream(new FileInputStream("keepFighting.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public static void goodJobTeam()
{
try
{
BGM = new AudioStream(new FileInputStream("goodJobTeam.wav"));
MD = BGM.getData();
play = new AudioDataStream(MD);
}
catch(IOException error)
{
System.out.println(error);
}
MGP.start(play);
}
public void paint(Graphics g)
{
Won(); // Checks if you won
Hit(); // Checks if any enemies are hitting you.
g.drawString("Capture the flag!",200,40);
g.drawString("Return the flag!",180,970);
/* Carries the flag */
if (CarryingFlag() == true)
{
xFlag = x + 20;
yFlag = y;
}
/* Restart if hit */
if (hit == true)
Restart();
/* If you aren't hit, update the normal positions */
if (hit != true)
UpdatePositions(g);
if (carryingFlag == true)
HarderEnemiesHit(); // Checks if the harder enemies are hitting you
/* Spawns harder enemies */
if (CarryingFlag() == true)
SpawnEnemies(g);
g.setColor(Color.white);
g.drawRect(xSafeBox, ySafeBox, 50, 50);
}
/* Checks if you are hit */
public void Hit()
{
for (int i = 0; i < xCoordinates.length; i++)
{
tempXPosition = xCoordinates[i] - x;
tempYPosition = yCoordinates[i] - y;
if (Math.abs(tempXPosition) <= 50)
{
if (Math.abs(tempYPosition) <= 50)
{
hit = true;
randomNumber = rand.nextInt(3);
if (randomNumber == 0)
lose();
else if (randomNumber == 1)
fallBack();
else
missionFailed();
encouragementCounter = 0;
randomNumber = 0;
counter = 0;
}
}
}
}
/* Checks if you are carrying the flag */
public boolean CarryingFlag()
{
if (xFlag - x >= -50 && xFlag - x <= 50)
{
if (yFlag - y >= -50 && yFlag - y <= 50)
{
carryingFlag = true;
if (counter == 0 || counter == 1)
{
if (counter == 1)
{
capturedOurFlag();
encouragementCounter = 0;
}
counter++;
}
return true;
}
}
carryingFlag = false;
return false;
}
public void Restart()
{
x = 200;
y = 900;
xFlag = 250;
yFlag = 50;
for (int i = 0; i < xCoordinates.length; i++)
{
xCoordinates[i] = rand.nextInt(400);
yCoordinates[i] = rand.nextInt(750);
}
counter = 0;
hit = false; // Set hit to false now.
carryingFlag = false;
}
public void UpdatePositions(Graphics g)
{
g.setColor (Color.green);
g.fillRect (x, y, 50, 50);
g.setColor(Color.white);
g.fillRect(xFlag, yFlag, 10,50);
for (int i = 0; i < xCoordinates.length; i++)
{
g.setColor (Color.blue);
g.fillOval (xCoordinates[i], yCoordinates[i], 50, 50);
}
}
public void SpawnEnemies(Graphics g)
{
g.setColor(Color.orange);
for (int i = 0; i < xHarderEnemies.length; i++)
{
g.fillOval(xHarderEnemies[i], yHarderEnemies[i],50,50);
}
}
public void HarderEnemiesHit()
{
for (int i = 0; i < xHarderEnemies.length; i++)
{
tempXPosition = xHarderEnemies[i] - x;
tempYPosition = yHarderEnemies[i] - y;
if (Math.abs(tempXPosition) <= 50)
{
if (Math.abs(tempYPosition) <= 50)
{
randomNumber = rand.nextInt(3);
if (randomNumber == 0)
lose();
else if (randomNumber == 1)
fallBack();
else
missionFailed();
randomNumber = 0;
encouragementCounter = 0;
hit = true;
}
}
}
}
public void Won()
{
if (Math.abs(x-xSafeBox) <= 10 && Math.abs(y-ySafeBox) <= 10 && carryingFlag == true)
{
randomNumber = rand.nextInt(2);
if (randomNumber == 0)
missionAccomplished();
else
goodJobTeam();
encouragementCounter = 0;
randomNumber = 0;
Restart();
}
}
public static void main(String args[])
{
starting();
startingSound();
Frame frm = new CaptureTheFlag();
frm.setVisible(true);
frm.repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment