Skip to content
Permalink
Browse files

Added Multiplayer

-add by changing the numberOfPlayers Variable in Game.java to 2,
-if it is 2 it will spawn another player
Changed the controls to allow for player 2 to have room
-player1 shoot is controlled by 'L'
-player 2 uses wasd and space to shoot
  • Loading branch information
jdm13003 committed Apr 20, 2016
1 parent fffc0c6 commit 60bb473c1bacda07459a3e913326c3219ed86339
Showing with 129 additions and 6 deletions.
  1. +44 −6 src/main/Game.java
  2. +70 −0 src/main/Player2.java
  3. +15 −0 src/main/Skins.java
@@ -32,6 +32,7 @@
private BufferedImage background = null;

private int is_shooting = 0;
private int is_shooting2 = 0;

private int enemy_count = 1;
private int kills = 0;
@@ -44,8 +45,10 @@

//private BufferedImage player;
private Player p;
private Player2 p2;
private Controller c;
private Skins tp;
private int numberOfPlayers = 2;

public void init()
{
@@ -62,6 +65,9 @@ public void init()
addKeyListener(new KeyboardInput(this));
tp = new Skins(this);
p = new Player(200, 200, tp, this);
if(numberOfPlayers == 2){
p2 = new Player2(300, 300, tp, this);
}
c = new Controller(tp, this);
ea = c.getEntityA();
eb = c.getEntityB();
@@ -144,9 +150,11 @@ public void run() {
private void tick() {
p.tick();
c.tick();

if(numberOfPlayers == 2){
p2.tick();
}
if(kills >= enemy_count){
enemy_count +=2;
enemy_count +=1;
kills = 0;
c.createEnemy(enemy_count);
}
@@ -164,7 +172,9 @@ private void render(){
//g.drawImage(player, 100, 100, this);
p.render(g);
c.render(g);

if(numberOfPlayers == 2){
p2.render(g);
}
/////////////////
g.dispose();
bufferstrat.show();
@@ -181,13 +191,28 @@ public void keyPressed(KeyEvent e){
p.setVelY(5);
} else if(key == KeyEvent.VK_UP){
p.setVelY(-5);
} else if(key == KeyEvent.VK_SPACE ){
} else if(key == KeyEvent.VK_L ){
if(is_shooting == 3){
is_shooting = 0;
}
if(is_shooting == 0){
c.addEntity(new Laser(p.getX(),p.getY(), tp, this));}
is_shooting++;
} else if(key == KeyEvent.VK_A){
p2.setVelX(-5);
} else if(key == KeyEvent.VK_D){
p2.setVelX(5);
}else if(key == KeyEvent.VK_S){
p2.setVelY(5);
}else if(key == KeyEvent.VK_W){
p2.setVelY(-5);
}else if(key == KeyEvent.VK_SPACE ){
if(is_shooting2 == 3){
is_shooting2 = 0;
}
if(is_shooting2 == 0){
c.addEntity(new Laser(p2.getX(),p2.getY(), tp, this));}
is_shooting2++;
}

}
@@ -202,8 +227,21 @@ public void keyReleased(KeyEvent e){
p.setVelY(0);
} else if(key == KeyEvent.VK_UP){
p.setVelY(0);
} else if(key == KeyEvent.VK_SPACE ){
is_shooting =0;
} else if(key == KeyEvent.VK_L){
is_shooting = 0;
} else if(key == KeyEvent.VK_A){
p2.setVelX(0);
} else if(key == KeyEvent.VK_D){
p2.setVelX(0);
}else if(key == KeyEvent.VK_S){
p2.setVelY(0);
}else if(key == KeyEvent.VK_W){
p2.setVelX(0);
}else if(key == KeyEvent.VK_SPACE ){

is_shooting2 = 0;


}

}
@@ -0,0 +1,70 @@
package main;
import java.awt.Graphics;
import java.awt.Rectangle;

import entity.EntityTypeA;

public class Player2 extends GameObject implements EntityTypeA{

private double velX = 0;
private double velY = 0;


private Skins skin;
private Game game;

public Player2(double x, double y, Skins skin, Game game){
super(x,y);
this.skin = skin;
this.game = game;
}

public void tick(){
x += velX;
y += velY;

if (x <= 0 ){
x = 0;
}
if (x >= 736){
x = 736;
}
if (y <= 0){
y = 0;
}
if (y >= 536){
y = 536;
}
if(GamePhysics.Collision(this, game.eb)){
System.out.println("LOSE");
}
}

public void render(Graphics g){
g.drawImage(skin.player2, (int)x, (int)y, null);
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;

}
public void setVelX(double velX){
this.velX = velX;
}
public void setVelY(double velY){
this.velY = velY;
}
public Rectangle getBounds(){
return new Rectangle((int)x, (int) y, 64, 64);
}

}

@@ -6,10 +6,13 @@
public class Skins {
private int playerx = 1;
private int playery = 1;
private int player2x = 1;
private int player2y = 1;
public BufferedImage player;
public BufferedImage laser;
public BufferedImage enemy1;
public BufferedImage padraic;
public BufferedImage player2;

private SpriteSheet ss = null;

@@ -23,6 +26,7 @@ private void getTextures(){
laser = ss.grabimage(2, 3, 64, 64);
enemy1 = ss.grabimage(2, 1, 64, 64);
padraic = ss.grabimage(2, 2, 64, 64);
player2 = ss.grabimage(3, 1, 64, 64);

}

@@ -37,4 +41,15 @@ public int getPlayerX(){
public int getPlayerY(){
return playery;
}
public void setSelection2(int x, int y){
player2x = x;
player2y = y;
}

public int getPlayer2X(){
return player2x;
}
public int getPlayer2Y(){
return player2y;
}
}

0 comments on commit 60bb473

Please sign in to comment.
You can’t perform that action at this time.