Welcome, Guest. Please login or register.

Author Topic: Got something cool for ya! (Java/Processing)  (Read 6120 times)

0 Members and 1 Guest are viewing this topic.

Offline HeartlessSora1234

Got something cool for ya! (Java/Processing)
« on: May 25, 2012, 04:14:42 AM »
TRAWN!
Basically a version of Tron I made before I graduated High School as a final project for intro to java ( I just finished AP computer sci).
The main difference between my version and Tron is in my version you're able to use the boarders like"warp tunnels" and appear on the opposite side of the grid.  frogc00l

 I used Processing to make it and I've been toying around with it for a while now adding a few things here and there. I recently added the option to choose how many players you want allowing you to choose from one player draw/survival to a 4 player chaos fest. There's a few glitches in the game like being able to suicide by turning in on yourself and being able to crisscross with other players if timed right but glitches make fun times.

Here's an executable of the game for those who just want to play(Windows, Mac, and Linux):
http://www.mediafire.com/?71bojvai59962jq


For educational purposes, it's made from two  Classes:

(Note:Use Processing http://processing.org/ and if you don't already have it but you're into coding you should really get it.)

The first is the Player Class

Code: [Select]
public class Player {
 
  //declares field variables for position, velocity,
  //and the number corresponding to the players color
 
  private int x;
  private int y;
  private int vel_x;
  private int vel_y;
  private int colorNum;

  //declares field variables so the object knows if it
  //has won and if the game is over
 
  private boolean won;
  private boolean gameOver;

  //declares field variables that store the initial
  //position and velocity of the player
 
  private int init_x;
  private int init_y;
  private int init_velx;
  private int init_vely;

  //declares integer constants that correspond to colors

  final private int RED = 0;
  final private int GREEN = 1;
  final private int BLUE = 2;
  final private int YELLOW = 3;

  Player(int start_x, int start_y, int start_velx, int start_vely, int start_color) {
    //constructor for player;
    //stores parameters to field variables
   
    x = start_x;
    y = start_y;
    vel_x = start_velx;
    vel_y = start_vely;
    colorNum = start_color;
   
    //initializes the boolean variables to be initially false
    won = false;
    gameOver = false;
   
    //stores initial position and velocity
   
    init_x = start_x;
    init_y= start_y;
    init_velx = start_velx;
    init_vely = start_vely;
  }

  void display() { //displays player on screen
    //sets fill color according to the designated player color
    if(colorNum == 0) 
      fill(255,0,0);
    else if(colorNum == 1)
      fill(0,255,0);
    else if(colorNum == 2)
      fill(0,0,255);
    else
      fill(0,255,255);
    //draws player as a small circle
    ellipseMode(CORNER);
    ellipse(x,y,5,5);
  }

  void updatePos() {
    //updates the position of the player according to the current velocity
    x += vel_x;
    y += vel_y;
   
    //moves the player to the opposite side of the screen if it hits an edge
    if(x < 0) {
      x = (w_width-grid_size);
    }
    else if(x >= w_width) {
      x =0;
    }
    if(y < 0) {
      y = (w_height-grid_size);
    }
    else if(y >= w_height) {
      y = 0;
    }
  }

  void resetPos() //resets boolean variables, player position,
  {               //and velocity to initial values
    won = false;
    gameOver = false;
    x = init_x;
    y = init_y;
    vel_x = init_velx;
    vel_y = init_vely;
  }
 
  void changeDir(int new_velx, int new_vely) {
    //method that changes the direction of movement of the player
    vel_x = new_velx;
    vel_y = new_vely;
  }

  int updateX() { //returns the current x position
    return x;
  }
  int updateY() { //returns the current y position
    return y;
  }

  int updateVelx() { //returns the current x velocity
    return vel_x;
  }
  int updateVely() { //returns the current y velocity
    return vel_y;
  }

  void endGame() { //sets the velocity to 0 at the end of the game
    vel_x=0;
    vel_y=0;
  }

  void won() { //records if the player has won
    won = true;
  }

  boolean getWon() { //returns if the player has won
    return won;
  }

  void gameOver() { //records if the round has ended
    gameOver = true;
  }

  boolean getGameOver() { //returns if the player recognizes the end of the round
    return gameOver;
  }
}

Then the Much longer Trawn Class

Code: [Select]
final int w_width = 600; //declares window dimensions as constants
final int w_height = 400;
final int grid_size = 5; //declares the size of the grid squares

int[][] grid; // declares array for the grid
color[] hit; // declares array for the grid square colors
Player player1; //declares players
Player player2;
Player player3;
Player player4;

boolean startGame = false; //declares boolean var that tells if the game has started
boolean gameEnded = false; //declares boolean var that tells if the round has ended

boolean endOne = false;
boolean endTwo = false;
boolean endThree = false;
boolean endFour = false;

boolean playOne = true;
boolean playTwo = false;
boolean playThree = false;
boolean playFour = false;


void setup() {
  size(w_width, w_height); //sets the window dimensions

  grid = new int[w_height/grid_size][w_width/grid_size]; //initializes the
  //grid array, making
  //as many values as
  //squares that will fit
  //in the window

  for (int r = 0; r < grid.length; r++) //sets all of the values of the array
    //to 0, making each square in the grid
    //initially black
  {
    for (int c = 0; c < grid[r].length; c++)
    {
      grid[r][c] = 0;
    }
  }
  hit = new color[5]; //initializes the color array and gives them three
  //values for black, blue, and red
  hit[0] = color(0, 0, 0);
  hit[1] = color(0, 0, 255);
  hit[2] = color(255, 0, 0);
  hit[3] = color(0, 255, 0);
  hit[4] = color(255, 255, 0);


  player1 = new Player((0+50), (w_height-50), 5, 0, 2); //initializes the players
  player2 = new Player((w_width-50), (0+50), -5, 0, 0);
  player3 = new Player((w_width-50), (w_height-50), -5, 0, 3);
  player4 = new Player ((0+50), (0+50), 5, 0, 4);
}

void draw() {
  drawGrid(); //draws the grid lines

  if (gameEnded == false) //prevents the color of the grid squares from being
    //changed while the round is over to make it easier
    //to tell who the winner is
    updateGrid();
  for (int r = 0; r < grid.length; r++) //draws the grid sqaures
  {
    for (int c = 0; c < grid[r].length; c++)
    {
      fill(hit[grid[r][c]]); //colors the square
      rect(c * grid_size, r * grid_size, grid_size, grid_size);
    }
  }
  if (startGame == false) { //welcome message, telling object of the game
    //and player controls
    stroke(255, 255, 255);
    rect((w_width/12), (w_height/8), (w_width-100), (w_height-100));
    PFont fontA = loadFont("OCRAExtended-48.vlw");
    textFont(fontA, 20);
    fill(255, 255, 255);
    text("Welcome to Trawn!", (w_width/12)+50, (w_height/8)+60);
    text("Select Number of Players (Press 1-4)", (w_width/12)+50, (w_height/8)+80);

    text("Player 1 Controls are the Arrows", (w_width/12)+50, (w_height/8)+120);
    text("Player 2 Controls are WASD", (w_width/12)+50, (w_height/8)+140); 
    text("Player 3 Controls are YGHJ", (w_width/12)+50, (w_height/8)+160);
    text("Player 4 Controls are 8456 (NumPad)", (w_width/12)+50, (w_height/8)+180);

    text("The Player that Collides with", (w_width/12)+50, (w_height/8)+220);
    text("the Light Path Loses.", (w_width/12)+50, (w_height/8)+240);
    text("Press Spacebar to Play! Good Luck!", (w_width/12)+50, (w_height/8)+260);

    if (keyPressed && (key == '2')) {
      playTwo = true;
    }
    if (keyPressed && (key == '3')) {

      playThree = true;
    }
    if (keyPressed && (key == '4')) {

      playFour = true;
    }
    if (keyPressed && (key == ' ')) //when Y is pressed, the game
      //begins
      startGame = true;
  }
  else if (startGame == true) { //begins game
    player1.display();  //displays players

    if (playTwo == true) {
      player2.display();
    }

    if (playThree == true) {
      player2.display();
      player3.display();
    }

    if (playFour == true) {
      player2.display();
      player3.display();
      player4.display();
    }

    player1.updatePos(); //updates position of players
    if (playTwo == true || playThree == true || playFour == true) {
      player2.updatePos();
    }
    if (playThree == true || playFour == true) {
      player3.updatePos();
    }

    if (playFour == true) {
      ;
      player4.updatePos();
    }

    hitCheck(player1);  //checks to see if a player has hit the light trail
    if (playTwo == true || playThree == true || playFour == true) {
      hitCheck(player2);
    }
    if (playThree == true || playFour == true) {
      hitCheck(player3);
    }
    if (playFour == true) {
      hitCheck(player4);
    }
  }
}


void updateGrid() { //checks player position to color the grid square
  //corresponding to each player's position in the color
  //of the player
  for (int r = 0; r < grid.length; r++)
  {
    for (int c = 0; c < grid[r].length; c++)
    {
      if ((player1.updateX()/grid_size == c) && (player1.updateY()/grid_size == r))
        grid[r][c] = 1;
    }
  }
  if (playTwo == true || playThree == true || playFour == true) {
    for (int r = 0; r < grid.length; r++)
    {
      for (int c = 0; c < grid[r].length; c++)
      {
        if ((player2.updateX()/grid_size == c) && (player2.updateY()/grid_size == r))
          grid[r][c] = 2;
      }
    }
  }
  if (playThree == true || playFour == true) {
    for (int r = 0; r < grid.length; r++)
    {
      for (int c = 0; c < grid[r].length; c++)
      {
        if ((player3.updateX()/grid_size == c) && (player3.updateY()/grid_size == r))
          grid[r][c] = 3;
      }
    }
  }
  if (playFour == true) {
    for (int r = 0; r < grid.length; r++)
    {
      for (int c = 0; c < grid[r].length; c++)
      {
        if ((player4.updateX()/grid_size == c) && (player4.updateY()/grid_size == r))
          grid[r][c] = 4;
      }
    }
  }
}

void drawGrid() //draws the grid lines
{
  background(0); //sets the background as black
  stroke(50, 50, 50); //colors the grid lines gray
  for (int i = 0; i < w_width; i += grid_size) //draws lines
  {
    line(i, 0, i, w_height);
    line(0, i, w_width, i);
  }
}

void hitCheck(Player P) //checks to see if a player has hit a light trail
{
  int borderCheck_x=P.updateX(); //instantiates two variables that will go
  int borderCheck_y=P.updateY(); //into the check, set at default to be the
  //current player position

  if (P.updateY()<0) //checks to see if the players position is outside of the
    //dimensions of the window, and sets the borderCheck
    //to be on the edge so that there is no array error
    //since the array only corresponds to positions inside
    //the dimensions of the window
    borderCheck_y=0;
  else if (P.updateY()>w_height)
    borderCheck_y=w_height-grid_size;
  else if (P.updateX()<0)
    borderCheck_x=0;
  else if (P.updateX()>w_width)
    borderCheck_x=w_width-grid_size;


  if (grid[(borderCheck_y)/grid_size][(borderCheck_x)/grid_size]!=0) {
    //checks the grid square corresponding the players current position to
    //see if it is already colored (meaning there is a light trail there)

    P.endGame(); //stops all players
    if (P == player1) { //Prevents Player from moving after hit
      endOne = true;
    }
    if (P == player2) {
      endTwo = true;
    }
    if (P == player3) {
      endThree = true;
    }
    if (P == player4) {
      endFour = true;
    }



    if (playFour == true) {
      if ( (endOne == false && endTwo == true && endThree == true  && endFour == true)
        || (endOne == true && endTwo == false && endThree == true  && endFour == true)
        || (endOne == true && endTwo == true && endThree == false  && endFour == true)
        || (endOne == true && endTwo == true && endThree == true  && endFour == false)||
        (endOne == true && endTwo == true && endThree == true  && endFour == true))
      {
        gameEnded = true;
      }
    }
    else if ( playThree == true) {
      if ( (endOne == false && endTwo == true && endThree == true)
        || (endOne == true && endTwo == false && endThree == true)
        || (endOne == true && endTwo == true && endThree == false)
        || (endOne == true && endTwo == true && endThree == true))
      {
        gameEnded = true;
      }
    }
    else if (playTwo == true) {
      if ( (endOne == false && endTwo == true)
        || (endOne == true && endTwo == false)
        || (endOne == true && endTwo == true))
      {
        gameEnded = true;
      }
    }
    else if (playOne == true) {
      if (endOne == true)
      {
        gameEnded = true;
      }
    }
    else {
      gameEnded = false;
    }
    P.gameOver(); //tells a losing player that the game has ended
  }
  else if (gameEnded == true) {
    P.endGame();
    P.won(); //the player that has not hit a light trail is told that it is
    //the winner. if both players lose simultaneously, there is a tie
    P.gameOver(); //tells a winning player that the game has ended
  }




  if ((playFour == true)
    && (gameEnded == true)&&((player1.getGameOver()==true)
    && (player2.getGameOver()==true)
    && (player3.getGameOver()==true)
    && (player4.getGameOver()==true))) {
    //when the game is over, and when both players have been told the game is over
    //(in order to keep the judging of the winner before the text reports the winner)
    endText(); //displays the winner and ending message
  }
  else if ((playThree == true)
    && (player1.getGameOver() == true)
    && (player2.getGameOver()==true)
    && (player3.getGameOver()==true)) {

    endText();
  }
  else if ((playTwo == true)
    && (player1.getGameOver() == true)
    && (player2.getGameOver()==true))
  {
    endText();
  }
  else if ((gameEnded == true) && (player1.getGameOver() == true))
  {
    endText();
  }
}


void endText() { //displays the winner and ending message

  PFont fontA = loadFont("OCRAExtended-48.vlw"); //imports font
  textFont(fontA, 20); //sets font and font size
  fill(255, 255, 255); //makes font white

  //reports winner in the form of an on-screen message

  if (playFour == true) {
    if (endOne == false && endTwo == true && endThree == true  && endFour == true)
      text("Player 1 Wins!", 10, 30);
    else if (endOne == true && endTwo == false && endThree == true  && endFour == true)
      text("Player 2 Wins!", 10, 30);
    else if (endOne == true && endTwo == true && endThree == false  && endFour == true)
      text("Player 3 Wins!", 10, 30);
    else if (endOne == true && endTwo == true && endThree == true  && endFour == false)
      text("Player 4 Wins!", 10, 30);
    else
      text("It's a tie!", 10, 30);
  }

  else if (playThree == true) {
    if (endOne == false && endTwo == true && endThree == true)
      text("Player 1 Wins!", 10, 30);
    else if (endOne == true && endTwo == false && endThree == true)
      text("Player 2 Wins!", 10, 30);
    else if (endOne == true && endTwo == true && endThree == false)
      text("Player 3 Wins!", 10, 30);
    else
      text("It's a tie!", 10, 30);
  }

  else if (playTwo == true) {
    if (endOne == false && endTwo == true)
      text("Player 1 Wins!", 10, 30);
    else if (endOne == true && endTwo == false)
      text("Player 2 Wins!", 10, 30);
    else
      text("It's a tie!", 10, 30);
  }

  else {
    text("Game Over", 10, 30);
  }

  text("Play Again? (Press the Spacebar)", 10, 50);
  //prompts user if he/she wishes to play again
  //to press Y
  if (keyPressed && (key == '1')) {
    playOne = true;
    playTwo = false;
    playThree = false;
    playFour = false;
  }

  if (keyPressed && (key == '2')) {

    playOne = false;
    playTwo = true;
    playThree = false;
    playFour = false;
  }
  if (keyPressed && (key == '3')) {

    playOne = false;
    playTwo = false;
    playThree = true;
    playFour = false;
  }
  if (keyPressed && (key == '4')) {

    playOne = false;
    playTwo = false;
    playThree = false;
    playFour = true;
  }
  if (keyPressed && (key == ' ')) {

    player1.resetPos(); //resets player positions
    if (playTwo == true || playThree == true || playFour == true)
      player2.resetPos();
    if (playThree == true || playFour == true)
      player3.resetPos();
    if (playFour == true)
      player4.resetPos();


    endOne = false;
    endTwo = false;
    endThree = false;
    endFour = false;

    for (int r = 0; r < grid.length; r++)
      //resets the colors of the grid squares
    {
      for (int c = 0; c < grid[r].length; c++)
      {
        grid[r][c] = 0;
      }
    }
    gameEnded = false; //starts the game up again
  }
}

void keyPressed() {
  //player controls;
  //checks to see if a button has been pressed, and changes the direction of
  //movement on the corresponding player. Prevents a player from moving
  //opposite the current direction of travel

  if (gameEnded == false) { //will not work if round has ended
    if (endOne == false) {
      if ((key == CODED)&&((keyCode == UP) && (player1.updateVely() <= 0)))
        player1.changeDir(0, -5);
      else if ((key == CODED)&&((keyCode == DOWN) && (player1.updateVely() >=0)))
        player1.changeDir(0, 5);
      else if ((key == CODED)&&((keyCode == LEFT) && (player1.updateVelx() <= 0)))
        player1.changeDir(-5, 0);
      else if ((key == CODED)&&((keyCode == RIGHT) && (player1.updateVelx() >= 0)))
        player1.changeDir(5, 0);
    }
    if (endTwo == false) {
      if ((key == 'w' || key == 'W') && (player2.updateVely() <= 0))
        player2.changeDir(0, -5);
      else if ((key == 's' || key == 'S') && (player2.updateVely() >= 0))
        player2.changeDir(0, 5);
      else if ((key == 'a' || key == 'A') && (player2.updateVelx() <= 0))
        player2.changeDir(-5, 0);
      else if ((key == 'd' || key == 'D') && (player2.updateVelx() >= 0))
        player2.changeDir(5, 0);
    }
    if (endThree == false) {
      if ((key == 'y' || key == 'Y') && (player3.updateVely() <= 0))
        player3.changeDir(0, -5);
      else if ((key == 'h' || key == 'H') && (player3.updateVely() >= 0))
        player3.changeDir(0, 5);
      else if ((key == 'g' || key == 'G') && (player3.updateVelx() <= 0))
        player3.changeDir(-5, 0);
      else if ((key == 'j' || key == 'J') && (player3.updateVelx() >= 0))
        player3.changeDir(5, 0);
    }

    if (endFour == false) {
      if ((key == '8') && (player4.updateVely() <= 0))
        player4.changeDir(0, -5);
      else if ((key == '5') && (player4.updateVely() >= 0))
        player4.changeDir(0, 5);
      else if ((key == '4') && (player4.updateVelx() <= 0))
        player4.changeDir(-5, 0);
      else if ((key == '6') && (player4.updateVelx() >= 0))
        player4.changeDir(5, 0);
    }
  }
}

Mess with the code on your own if you want, but if you want to post it somewhere else just get my permission.
I want to add more things to the game so let me know your ideas! I may or may not post the updates.
« Last Edit: May 25, 2012, 04:17:09 AM by HeartlessSora1234 »
"There is always Light. You just have to find it."

Offline Sejo Mino

Re: Got something cool for ya! (Java/Processing)
« Reply #1 on: May 25, 2012, 06:27:00 AM »
Yay 2d gamez...... Something i remember making when i was in high school and while i was in college also. i wish i still had the program to make them.

Conjoint Gaming [Game On]

Re: Got something cool for ya! (Java/Processing)
« Reply #1 on: May 25, 2012, 06:27:00 AM »

Offline Blackllama

Re: Got something cool for ya! (Java/Processing)
« Reply #2 on: May 25, 2012, 12:06:15 PM »
Will have to check it out, sounds nice.

Offline HeartlessSora1234

Re: Got something cool for ya! (Java/Processing)
« Reply #3 on: May 25, 2012, 02:33:50 PM »
Will have to check it out, sounds nice.
It's pretty good  smug

but seriously let me know of some ideas for things I can add to it. Making this has been a great way to practice coding but I need more ideas. I've been thinking I could add modes to it like snake and (omg) multiplayer snaketrawn.
« Last Edit: May 25, 2012, 02:38:31 PM by HeartlessSora1234 »
"There is always Light. You just have to find it."

Offline Andredem

Re: Got something cool for ya! (Java/Processing)
« Reply #4 on: May 25, 2012, 03:35:00 PM »
That is really cool, especially being a fan on Tron

Nice work - I'm sure that was a good grade
"Get busy livin' or get busy dying"

Offline Cortez (Mr. T. FOO!)

Re: Got something cool for ya! (Java/Processing)
« Reply #5 on: May 25, 2012, 04:10:07 PM »
A few friends and I play BM Tron at school all the time on our off-blocks. Similar to this but you cannot hit the walls. Nice work though.
Does this look like a ball field to you sucka? This is a sandbox. For making sandcastles.

Quote
Post Count
A Novel by Inject OH 4
Conjoint Gaming
"You thought a Human Centipede was bad, wait till you get a load of us."
Bears, beer and bitches. That's everyone's motto.
Quote from: some guy on PC gamer
First of all, books were all but dead until tablets rejuvenated the industry
Quote from: Blazyd
Cortez I'm actually on black tar heroin fyi
Only been in it once didn't really pay attention to the staff, I think their was an eatable thong... but that may have been a totally different store, ANYWAYS... lol.
The plunger could simply be out of view, the pants + it's location behind the toilet may hide it... Or it's a fraud and we need to take down the system with out golden axes while destroying the rest of the demon-spawn so that we may live in a utopia.

Offline Tendovvi

Re: Got something cool for ya! (Java/Processing)
« Reply #6 on: May 25, 2012, 05:05:57 PM »
rectangle2d :D :D buhaeaehuhuhueaaa :))))))
TGOD
Doll023: ur from finland? thats in ireland right?
--------------------------------------------------------------------
Blazyd: why do you accept random friend invites from fans you've never met
Blazyd: Your friends list is packed
Blazyd: packed as hillary clintons ass


                                                                     

Offline HeartlessSora1234

Re: Got something cool for ya! (Java/Processing)
« Reply #7 on: May 26, 2012, 09:39:03 PM »
That is really cool, especially being a fan on Tron

Nice work - I'm sure that was a good grade
Yup it was a 100%, and the only two differences between this and tron are the starting positions and the warp walls right? (besides AI and multiplayer) Or am I forgetting something?
"There is always Light. You just have to find it."

Conjoint Gaming [Game On]

Re: Got something cool for ya! (Java/Processing)
« Reply #7 on: May 26, 2012, 09:39:03 PM »

 


* ShoutBox!

Refresh History

SimplePortal 2.3.5 © 2008-2012, SimplePortal