uploading all files - final commit
This commit is contained in:
parent
0a64bb45d7
commit
d99d69545a
31 changed files with 1138 additions and 0 deletions
6
.classpath
Normal file
6
.classpath
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
17
.project
Normal file
17
.project
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>TerorTron</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
11
.settings/org.eclipse.jdt.core.prefs
Normal file
11
.settings/org.eclipse.jdt.core.prefs
Normal file
|
@ -0,0 +1,11 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.7
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.7
|
BIN
bin/Game/Bullet.class
Normal file
BIN
bin/Game/Bullet.class
Normal file
Binary file not shown.
BIN
bin/Game/ContolersPanel.class
Normal file
BIN
bin/Game/ContolersPanel.class
Normal file
Binary file not shown.
BIN
bin/Game/GameFrame.class
Normal file
BIN
bin/Game/GameFrame.class
Normal file
Binary file not shown.
BIN
bin/Game/GameLogic.class
Normal file
BIN
bin/Game/GameLogic.class
Normal file
Binary file not shown.
BIN
bin/Game/GamePanel.class
Normal file
BIN
bin/Game/GamePanel.class
Normal file
Binary file not shown.
BIN
bin/Game/ImagePanel.class
Normal file
BIN
bin/Game/ImagePanel.class
Normal file
Binary file not shown.
BIN
bin/Game/Invisibility.class
Normal file
BIN
bin/Game/Invisibility.class
Normal file
Binary file not shown.
BIN
bin/Game/LengthNode.class
Normal file
BIN
bin/Game/LengthNode.class
Normal file
Binary file not shown.
BIN
bin/Game/MenuPanel.class
Normal file
BIN
bin/Game/MenuPanel.class
Normal file
Binary file not shown.
BIN
bin/Game/Player.class
Normal file
BIN
bin/Game/Player.class
Normal file
Binary file not shown.
BIN
bin/Game/WellcomePanel.class
Normal file
BIN
bin/Game/WellcomePanel.class
Normal file
Binary file not shown.
BIN
bin/TESTER1.class
Normal file
BIN
bin/TESTER1.class
Normal file
Binary file not shown.
BIN
bin/ctrl.png
Normal file
BIN
bin/ctrl.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 272 KiB |
BIN
bin/wellcome.png
Normal file
BIN
bin/wellcome.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 741 KiB |
125
src/Game/Bullet.java
Normal file
125
src/Game/Bullet.java
Normal file
|
@ -0,0 +1,125 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Bullet {
|
||||
private int x, y, direction;
|
||||
private LengthNode other;
|
||||
private Color color;
|
||||
public static int SIZE = Player.THIKNES, PIX_PER_STEP = 1,
|
||||
STILL_GO = 0, HIT = 1, OUT_OF_GRID = 2;
|
||||
|
||||
|
||||
private int hitPointX, hitPointY;
|
||||
public Bullet(int x, int y, int direction, LengthNode other)
|
||||
{
|
||||
color = Color.WHITE;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.direction = direction;
|
||||
this.other = other;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public int getHitPointX() {
|
||||
return hitPointX;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getHitPointY() {
|
||||
return hitPointY;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int fireStep(int maxWidth, int maxHeight)
|
||||
{
|
||||
switch(direction)
|
||||
{
|
||||
case 1: //up
|
||||
y -= PIX_PER_STEP;
|
||||
return presentState(maxWidth, maxHeight);
|
||||
|
||||
case 2: //down
|
||||
y += PIX_PER_STEP;
|
||||
return presentState(maxWidth, maxHeight);
|
||||
|
||||
case 3: //left
|
||||
x -= PIX_PER_STEP;
|
||||
return presentState(maxWidth, maxHeight);
|
||||
|
||||
case 4: //right
|
||||
x += PIX_PER_STEP;
|
||||
return presentState(maxWidth, maxHeight);
|
||||
}
|
||||
return presentState(maxWidth, maxHeight);
|
||||
}
|
||||
|
||||
private int presentState(int maxWidth, int maxHeight)
|
||||
{
|
||||
if(didHit())
|
||||
return HIT;
|
||||
else if(outOfGrid(maxWidth, maxHeight))
|
||||
return OUT_OF_GRID;
|
||||
else
|
||||
return STILL_GO;
|
||||
}
|
||||
|
||||
|
||||
private boolean didHit()
|
||||
{
|
||||
LengthNode p = other;
|
||||
for(; p.getNext() != null; p = p.getNext()){
|
||||
if(p.getNext().getX() == this.x && this.y == p.getNext().getY()){
|
||||
System.out.println("HIT! HIT! HIT!");
|
||||
p.setNext(null);
|
||||
x = -1000;
|
||||
y = -1000;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//****** DEBUG ****
|
||||
System.out.println("not hit");
|
||||
// **** end debug ****
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean outOfGrid(int maxWidth, int maxHeight)
|
||||
{
|
||||
if((this.x < 0 - SIZE) || (this.y < 0 - SIZE) || (this.x >= maxWidth + SIZE) || (this.y >= maxHeight + SIZE)){
|
||||
// *** DEBUG
|
||||
|
||||
System.out.println("OUT!");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public void draw(Graphics g)
|
||||
{
|
||||
g.setColor(color);
|
||||
g.fillOval(x, y, SIZE, SIZE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
28
src/Game/ContolersPanel.java
Normal file
28
src/Game/ContolersPanel.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
|
||||
public class ContolersPanel extends JPanel{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
ImageIcon ctrl;
|
||||
|
||||
public ContolersPanel(){
|
||||
ClassLoader loader = ContolersPanel.class.getClassLoader();
|
||||
setBackground(Color.BLACK);
|
||||
ctrl = new ImageIcon(loader.getResource("ctrl.png"));
|
||||
|
||||
add(new JLabel(ctrl));
|
||||
}
|
||||
|
||||
|
||||
}
|
91
src/Game/GameFrame.java
Normal file
91
src/Game/GameFrame.java
Normal file
|
@ -0,0 +1,91 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.WindowConstants;
|
||||
|
||||
public class GameFrame extends JFrame implements ActionListener{
|
||||
|
||||
public static final int FRAME_WIDTH = 1197, FRAME_HEIGHT = 899;
|
||||
|
||||
private JMenuBar menuBar;
|
||||
private JMenu menu;
|
||||
private JMenuItem start, controls, exit;
|
||||
|
||||
public GamePanel gamePanel;
|
||||
private ContolersPanel controlersPanel;
|
||||
private WellcomePanel welcome;
|
||||
|
||||
public GameFrame()
|
||||
{
|
||||
setSize(FRAME_WIDTH, FRAME_HEIGHT);
|
||||
setTitle("TronTeror Version 0.5.1 beta - by SagiDayan");
|
||||
setResizable(false);
|
||||
|
||||
//*************** Menu Bar *******************
|
||||
|
||||
menuBar = new JMenuBar();
|
||||
menu = new JMenu("Menu");
|
||||
start = new JMenuItem("Start Game");
|
||||
controls = new JMenuItem("controlers");
|
||||
menu.add(start);
|
||||
menu.add(controls);
|
||||
menuBar.add(menu);
|
||||
|
||||
start.addActionListener(this);
|
||||
controls.addActionListener(this);
|
||||
//add menuBar to frame
|
||||
|
||||
|
||||
this.setJMenuBar(menuBar);
|
||||
|
||||
//End Menu.
|
||||
|
||||
|
||||
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
welcome = new WellcomePanel();
|
||||
controlersPanel = new ContolersPanel();
|
||||
gamePanel = new GamePanel();
|
||||
|
||||
|
||||
//gamePanel.addKeyListener(gamePanel);
|
||||
|
||||
add(welcome);
|
||||
|
||||
|
||||
setVisible(true);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
if(e.getSource() == controls)
|
||||
{
|
||||
System.out.println("controles");
|
||||
welcome.setVisible(false);
|
||||
gamePanel.setVisible(false);
|
||||
add(controlersPanel);
|
||||
controlersPanel.setVisible(true);
|
||||
}
|
||||
else if( e.getSource() == start)
|
||||
{
|
||||
welcome.setVisible(false);
|
||||
controlersPanel.setVisible(false);
|
||||
gamePanel.setVisible(true);
|
||||
add(gamePanel);
|
||||
gamePanel.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
199
src/Game/GameLogic.java
Normal file
199
src/Game/GameLogic.java
Normal file
|
@ -0,0 +1,199 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class GameLogic {
|
||||
private Player player1, player2;
|
||||
private int width, height;
|
||||
private Color colorP1, colorP2;
|
||||
|
||||
private Bullet bullet1, bullet2;
|
||||
|
||||
// **** Invisibility *****
|
||||
|
||||
private boolean inVisibilityHappend;
|
||||
private Invisibility invisibleMod;
|
||||
|
||||
|
||||
public GameLogic(int maxWidth, int maxHeight)
|
||||
{
|
||||
width = maxWidth;
|
||||
height = maxHeight;
|
||||
colorP1 = new Color(3,3,173);
|
||||
colorP2 = new Color(173, 3, 3 );
|
||||
|
||||
inVisibilityHappend = false;
|
||||
invisibleMod = new Invisibility(maxWidth, maxHeight);
|
||||
|
||||
player1 = new Player(getNewName("Please enter Player 1 Name"), Player.LEFT, colorP1, width - 300, 420, 0); //starts from right to left!
|
||||
player2 = new Player(getNewName("Now, Please enter Player 2 Name"), Player.RIGHT, colorP2, 0 + 300, 420, 0); //starts from left to right!
|
||||
}
|
||||
|
||||
|
||||
private String getNewName(String s){
|
||||
return JOptionPane.showInputDialog(s);
|
||||
}
|
||||
|
||||
public void setP1Direction(int direct)
|
||||
{
|
||||
player1.setDirection(direct);
|
||||
}
|
||||
|
||||
public void setP2Direction(int direct)
|
||||
{
|
||||
player2.setDirection(direct);
|
||||
}
|
||||
|
||||
public String getP1Name()
|
||||
{
|
||||
return player1.getName();
|
||||
}
|
||||
|
||||
public String getP2Name()
|
||||
{
|
||||
return player2.getName();
|
||||
}
|
||||
|
||||
public int getPlayer1Point()
|
||||
{
|
||||
return player1.getPoints();
|
||||
}
|
||||
|
||||
public int getPlayer2Point()
|
||||
{
|
||||
return player2.getPoints();
|
||||
}
|
||||
|
||||
public Player getP1(){
|
||||
return player1;
|
||||
}
|
||||
|
||||
public Player getP2(){
|
||||
return player2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Bullet getBullet1() {
|
||||
return bullet1;
|
||||
}
|
||||
|
||||
|
||||
public Bullet getBullet2() {
|
||||
return bullet2;
|
||||
}
|
||||
|
||||
|
||||
public boolean stepAndOk()
|
||||
{
|
||||
|
||||
player1.step();
|
||||
player2.step();
|
||||
inVisibilityHappend = invisibleMod.makePoint(player1, player2);
|
||||
if(inVisibilityHappend)
|
||||
{
|
||||
checkPlayersVisibility();
|
||||
}
|
||||
if(player1.loosed(player2, width, height))
|
||||
{
|
||||
player2.addPoints();
|
||||
return false;
|
||||
}
|
||||
else if(player2.loosed(player1, width, height))
|
||||
{
|
||||
player1.addPoints();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void fire(Player p)
|
||||
{
|
||||
if(!p.isCanFire())
|
||||
return;
|
||||
if(p == player1)
|
||||
{
|
||||
bullet1 = new Bullet(player1.getHead().getX(), player1.getHead().getY(), player1.getDirection(), player2.getHead());
|
||||
return;
|
||||
}
|
||||
else if(p == player2)
|
||||
{
|
||||
bullet2 = new Bullet(player2.getHead().getX(), player2.getHead().getY(), player2.getDirection(), player1.getHead());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void bulletStep(Graphics g, int bulletState)
|
||||
{
|
||||
|
||||
//Player1 Shoots
|
||||
try{ //Freezes game and shoots
|
||||
if(bulletState == Bullet.STILL_GO)
|
||||
bullet1.draw(g);
|
||||
else
|
||||
player1.setCanFire(false);
|
||||
}catch (Exception e) {
|
||||
System.out.println("no bullet to p1");
|
||||
}
|
||||
//Player2 shoots
|
||||
try{ //Freezes game and shoots
|
||||
bulletState = bullet2.fireStep(width, height);
|
||||
if(bulletState == Bullet.STILL_GO)
|
||||
bullet2.draw(g);
|
||||
else
|
||||
player2.setCanFire(false);
|
||||
}catch (Exception e) {
|
||||
System.out.println("no bullet to p2");
|
||||
}
|
||||
}
|
||||
|
||||
public void restart()
|
||||
{
|
||||
player1 = new Player(player1.getName(), Player.LEFT, colorP1,width - 300, 420, player1.getPoints());
|
||||
player2 = new Player(player2.getName(), Player.RIGHT, colorP2, 0 + 300, 420, player2.getPoints());
|
||||
inVisibilityHappend = false;
|
||||
invisibleMod = new Invisibility(width, height);
|
||||
}
|
||||
|
||||
public void checkPlayersVisibility()
|
||||
{
|
||||
int x = invisibleMod.getX();
|
||||
int y = invisibleMod.getY();
|
||||
|
||||
if(player1.getHead().getX() == x && player1.getHead().getY() == y)
|
||||
invisibleModOn(player1, true);
|
||||
else if(player2.getHead().getX() == x && player2.getHead().getY() == y)
|
||||
invisibleModOn(player2, true);
|
||||
}
|
||||
|
||||
public void invisibleModOn(Player p, boolean switcher)
|
||||
{
|
||||
if(switcher)
|
||||
{
|
||||
System.out.println("got The Shit!!!");
|
||||
p.setIsVisible(false);
|
||||
invisibleMod.setX(-200);
|
||||
invisibleMod.setY(-200);
|
||||
}
|
||||
else
|
||||
p.setIsVisible(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void draw(Graphics g)
|
||||
{
|
||||
player1.draw(g);
|
||||
player2.draw(g);
|
||||
if(inVisibilityHappend)
|
||||
invisibleMod.draw(g);
|
||||
}
|
||||
|
||||
}
|
243
src/Game/GamePanel.java
Normal file
243
src/Game/GamePanel.java
Normal file
|
@ -0,0 +1,243 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class GamePanel extends JPanel implements ActionListener, KeyListener{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private MenuPanel topPanel;
|
||||
private GameLogic logic;
|
||||
|
||||
private JLabel lblScore;
|
||||
|
||||
boolean p1Fire, p2Fire;
|
||||
|
||||
public static final int W = 1195, H = 855;
|
||||
|
||||
/*
|
||||
*
|
||||
* Setup TIMERS & LISTENERS !!!
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
Timer timerGeneral;
|
||||
Timer timerBullet;
|
||||
|
||||
public GamePanel() {
|
||||
|
||||
|
||||
setBackground(Color.BLACK);
|
||||
setLayout(new BorderLayout());
|
||||
setSize(W, H);
|
||||
|
||||
|
||||
|
||||
logic = new GameLogic(W, H);
|
||||
lblScore = new JLabel(logic.getP2Name()+": "+logic.getPlayer2Point()+" "+logic.getP1Name()+": "+logic.getPlayer1Point());
|
||||
lblScore.setForeground(Color.WHITE);
|
||||
lblScore.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
add(lblScore, BorderLayout.SOUTH);
|
||||
|
||||
//TEST!!!!
|
||||
timerGeneral = new Timer(30, this);
|
||||
timerBullet = new Timer(1, this);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void p1Fire()
|
||||
{
|
||||
|
||||
logic.fire(logic.getP1());
|
||||
p1Fire = true;
|
||||
timerBullet.start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void p2Fire()
|
||||
{
|
||||
logic.fire(logic.getP2());
|
||||
p2Fire = true;
|
||||
timerBullet.start();
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
if(timerBullet.isRunning())
|
||||
paintBullets(g);
|
||||
logic.draw(g);
|
||||
g.setColor(Color.CYAN); //paint borders
|
||||
g.drawRect(0, 0, W-1, H-1);
|
||||
}
|
||||
|
||||
|
||||
private void paintBullets(Graphics g)
|
||||
{
|
||||
if(p1Fire)
|
||||
{
|
||||
int bulletState = logic.getBullet1().fireStep(W, H);
|
||||
if(bulletState == Bullet.STILL_GO){ // CHANGE 500 TODO
|
||||
logic.draw(g);
|
||||
logic.bulletStep(g, bulletState);
|
||||
}
|
||||
else{
|
||||
logic.draw(g);
|
||||
p1Fire = false;
|
||||
timerBullet.stop();
|
||||
}
|
||||
|
||||
}
|
||||
else if(p2Fire)
|
||||
{
|
||||
int bulletState = logic.getBullet2().fireStep(W, H);
|
||||
|
||||
if(bulletState == Bullet.STILL_GO){ // CHANGE 500 TODO
|
||||
logic.draw(g);
|
||||
logic.bulletStep(g, bulletState);
|
||||
}
|
||||
else{
|
||||
logic.draw(g);
|
||||
p2Fire = false;
|
||||
timerBullet.stop();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void start(){
|
||||
|
||||
logic.restart();
|
||||
timerGeneral.start();
|
||||
System.out.println(getWidth()+" "+getHeight());
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Actions! Timers, and keyActions will be down here!
|
||||
*
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
if(e.getSource()==timerGeneral)
|
||||
{
|
||||
|
||||
if(logic.stepAndOk())
|
||||
repaint();
|
||||
else
|
||||
{
|
||||
//logic.stepAndOk();
|
||||
timerGeneral.stop();
|
||||
System.out.println("FUCK!");
|
||||
lblScore.setText(logic.getP2Name()+": "+logic.getPlayer2Point()+" "+logic.getP1Name()+": "+logic.getPlayer1Point());
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
if(e.getSource() == timerBullet)
|
||||
{
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
/*
|
||||
* Player1 Controls
|
||||
*/
|
||||
if(e.getKeyCode() == KeyEvent.VK_SPACE)
|
||||
{
|
||||
if(logic.getP1().isCanFire()){
|
||||
p1Fire();
|
||||
|
||||
logic.getP1().setCanFire(false);
|
||||
}
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
|
||||
if(logic.getP1().getDirection() != Player.UP)
|
||||
logic.setP1Direction(Player.DOWN);
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_UP){
|
||||
if(logic.getP1().getDirection() != Player.DOWN)
|
||||
logic.setP1Direction(Player.UP);
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_LEFT){
|
||||
if(logic.getP1().getDirection() != Player.RIGHT)
|
||||
logic.setP1Direction(Player.LEFT);
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
|
||||
if(logic.getP1().getDirection() != Player.LEFT)
|
||||
logic.setP1Direction(Player.RIGHT);
|
||||
}
|
||||
|
||||
/*
|
||||
* Player2 Controls
|
||||
*/
|
||||
if(e.getKeyCode() == KeyEvent.VK_CONTROL)
|
||||
{
|
||||
if(logic.getP2().isCanFire()){
|
||||
p2Fire();
|
||||
|
||||
logic.getP2().setCanFire(false);
|
||||
}
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_S){
|
||||
if(logic.getP2().getDirection() != Player.UP)
|
||||
logic.setP2Direction(Player.DOWN);
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_W){
|
||||
if(logic.getP2().getDirection() != Player.DOWN)
|
||||
logic.setP2Direction(Player.UP);
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_A){
|
||||
if(logic.getP2().getDirection() != Player.RIGHT)
|
||||
logic.setP2Direction(Player.LEFT);
|
||||
}
|
||||
else if(e.getKeyCode() == KeyEvent.VK_D){
|
||||
if(logic.getP2().getDirection() != Player.LEFT)
|
||||
logic.setP2Direction(Player.RIGHT);
|
||||
}
|
||||
|
||||
if(e.getKeyCode() == KeyEvent.VK_F2)
|
||||
{
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
20
src/Game/ImagePanel.java
Normal file
20
src/Game/ImagePanel.java
Normal file
|
@ -0,0 +1,20 @@
|
|||
package Game;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class ImagePanel extends JPanel{
|
||||
|
||||
private BufferedImage image;
|
||||
JLabel l;
|
||||
public ImagePanel() {
|
||||
setBackground(Color.DARK_GRAY);
|
||||
l = new JLabel();
|
||||
l.setIcon(new ImageIcon("Resource/images.png"));
|
||||
add(l);
|
||||
validate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
101
src/Game/Invisibility.java
Normal file
101
src/Game/Invisibility.java
Normal file
|
@ -0,0 +1,101 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Invisibility {
|
||||
|
||||
private boolean goodPoint;
|
||||
private static int SIZE = 5;
|
||||
private int width, height, x, y;
|
||||
private Color color;
|
||||
private boolean show;
|
||||
|
||||
public Invisibility(int width, int height)
|
||||
{
|
||||
goodPoint = false;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
color = Color.CYAN;
|
||||
show = false;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics g)
|
||||
{
|
||||
g.setColor(color);
|
||||
g.fillRect(x, y, SIZE, SIZE);
|
||||
}
|
||||
|
||||
public boolean makePoint(Player p1, Player p2)
|
||||
{
|
||||
if(goodPoint)
|
||||
if(showPowerUp() || show)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
|
||||
x = (int)(Math.random() * width);
|
||||
if(x % Player.THIKNES != 0)
|
||||
return false;
|
||||
y = (int)(Math.random() * height);
|
||||
if(y % Player.THIKNES != 0)
|
||||
return false;
|
||||
|
||||
for(LengthNode p = p1.getHead(); p.getNext() != null ; p = p.getNext())
|
||||
{
|
||||
if(x == p.getX() && y == p.getY())
|
||||
return false;
|
||||
}
|
||||
|
||||
for(LengthNode p = p2.getHead(); p.getNext() != null ; p = p.getNext())
|
||||
{
|
||||
if(x == p.getX() && y == p.getY())
|
||||
return false;
|
||||
}
|
||||
goodPoint = true;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public boolean isGoodPoint() {
|
||||
return goodPoint;
|
||||
}
|
||||
|
||||
public void setGoodPoint(boolean goodPoint) {
|
||||
this.goodPoint = goodPoint;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public boolean showPowerUp()
|
||||
{
|
||||
int c = (int)(Math.random() * 10000);
|
||||
if(c <= 10)
|
||||
{
|
||||
show = true;
|
||||
System.out.println("POWER UP!!!!!");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
51
src/Game/LengthNode.java
Normal file
51
src/Game/LengthNode.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
package Game;
|
||||
|
||||
public class LengthNode {
|
||||
|
||||
|
||||
private int x, y;
|
||||
private LengthNode next;
|
||||
|
||||
public LengthNode(int x, int y, LengthNode next)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public LengthNode getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(LengthNode next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public boolean sameValues(LengthNode other)
|
||||
{
|
||||
if(other.getX() == this.getX() && this.getY() == other.getY())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
51
src/Game/MenuPanel.java
Normal file
51
src/Game/MenuPanel.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
package Game;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
|
||||
|
||||
public class MenuPanel extends JPanel{
|
||||
|
||||
private JButton button_start;
|
||||
private JLabel p1, p2;
|
||||
private int score1, score2;
|
||||
String name1, name2;
|
||||
|
||||
|
||||
|
||||
public MenuPanel(String name1, String name2, int score1, int score2) {
|
||||
setBackground(Color.DARK_GRAY);
|
||||
this.score1 = score1;
|
||||
this.score2 = score2;
|
||||
this.name1 = name1;
|
||||
this.name2 = name2;
|
||||
p1 = new JLabel();
|
||||
p2 = new JLabel();
|
||||
p1.setForeground(Color.WHITE);
|
||||
p2.setForeground(Color.WHITE);
|
||||
p1.setText(""+this.name1+": "+score1);
|
||||
p2.setText(""+this.name2+": "+score2);
|
||||
|
||||
setLayout(new BorderLayout(20, 0));
|
||||
add(p1, BorderLayout.WEST);
|
||||
add(new ImagePanel(), BorderLayout.CENTER);
|
||||
add(p2, BorderLayout.EAST);
|
||||
|
||||
}
|
||||
|
||||
public void updateScore(int score1, int score2){
|
||||
this.score1 = score1;
|
||||
this.score2 = score2;
|
||||
String s = ""+this.name1+": "+this.score1;
|
||||
p1 = new JLabel(s);
|
||||
System.out.println(p1.getText());
|
||||
s = ""+this.name2+": "+this.score2;
|
||||
p2.setText(s);
|
||||
System.out.println("======");
|
||||
System.out.println(""+this.name1+": "+this.score1);
|
||||
System.out.println(""+this.name2+": "+this.score2);
|
||||
System.out.println("======");
|
||||
|
||||
}
|
||||
|
||||
}
|
151
src/Game/Player.java
Normal file
151
src/Game/Player.java
Normal file
|
@ -0,0 +1,151 @@
|
|||
package Game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Player {
|
||||
|
||||
private String name;
|
||||
private int direction, points, intvisibleStepCount;
|
||||
private LengthNode head;
|
||||
private Color color;
|
||||
private boolean canFire;
|
||||
private boolean isVisible, flicker;
|
||||
|
||||
|
||||
public static int UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4;
|
||||
|
||||
|
||||
public static int PIX_PER_STEP = 5, THIKNES = 5;
|
||||
|
||||
public Player(String name, int direction, Color color, int x, int y, int point)
|
||||
{
|
||||
this.name = name;
|
||||
this.direction = direction;
|
||||
this.color = color;
|
||||
head = new LengthNode(x, y, null);
|
||||
canFire = true;
|
||||
points = point;
|
||||
isVisible = true;
|
||||
flicker = false;
|
||||
intvisibleStepCount = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean isCanFire() {
|
||||
return canFire;
|
||||
}
|
||||
|
||||
public void setIsVisible(boolean bol)
|
||||
{
|
||||
isVisible = bol;
|
||||
}
|
||||
|
||||
public void setCanFire(boolean canFire) {
|
||||
this.canFire = canFire;
|
||||
}
|
||||
|
||||
public boolean getCanFire() {
|
||||
return canFire;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getPoints() {
|
||||
return points;
|
||||
}
|
||||
|
||||
public void addPoints() {
|
||||
this.points++;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setDirection(int direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public LengthNode getHead() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public void step()
|
||||
{
|
||||
switch(direction)
|
||||
{
|
||||
case 1: //up
|
||||
head = new LengthNode(head.getX(), head.getY() - PIX_PER_STEP, head);
|
||||
break;
|
||||
case 2: //down
|
||||
head = new LengthNode(head.getX(), head.getY() + PIX_PER_STEP, head);
|
||||
break;
|
||||
case 3: //left
|
||||
head = new LengthNode(head.getX() - PIX_PER_STEP, head.getY(), head);
|
||||
break;
|
||||
case 4: //right
|
||||
head = new LengthNode(head.getX() + PIX_PER_STEP, head.getY(), head);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!isVisible)
|
||||
intvisibleStepCount++;
|
||||
if(intvisibleStepCount == 300)
|
||||
isVisible = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean loosed(Player other, int maxWidth, int maxHeight)
|
||||
{
|
||||
//====player hit himself===
|
||||
for(LengthNode p = head.getNext(); p.getNext() != null ; p = p.getNext())
|
||||
if(head.sameValues(p))
|
||||
return true;
|
||||
//===player hit other player====
|
||||
for(LengthNode p = other.getHead(); p.getNext() != null ; p = p.getNext())
|
||||
if(head.sameValues(p))
|
||||
return true;
|
||||
//=== player hit border of grid ==
|
||||
if(head.getX() < 0 || head.getY() < 0 || head.getX() > maxWidth + THIKNES || head.getY() > maxHeight + THIKNES)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public void draw(Graphics g)
|
||||
{
|
||||
g.setColor(color);
|
||||
LengthNode p = head;
|
||||
if(isVisible)
|
||||
for(;p != null; p=p.getNext())
|
||||
g.fillRect(p.getX(), p.getY(), THIKNES, THIKNES); //All Player
|
||||
else //just head and body flicking
|
||||
if(flicker){
|
||||
for(;p != null; p=p.getNext())
|
||||
g.fillRect(p.getX(), p.getY(), THIKNES, THIKNES);
|
||||
flicker = false;
|
||||
}else{
|
||||
g.fillRect(p.getX(), p.getY(), THIKNES, THIKNES);
|
||||
int flickerOdds = (int)(Math.random() * 10);
|
||||
if(flickerOdds < 1)
|
||||
flicker = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
27
src/Game/WellcomePanel.java
Normal file
27
src/Game/WellcomePanel.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package Game;
|
||||
import java.awt.Color;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
|
||||
|
||||
public class WellcomePanel extends JPanel{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 2L;
|
||||
ImageIcon wlcm;
|
||||
|
||||
public WellcomePanel(){
|
||||
ClassLoader loader = ContolersPanel.class.getClassLoader();
|
||||
setBackground(Color.BLACK);
|
||||
wlcm = new ImageIcon(loader.getResource("wellcome.png"));
|
||||
|
||||
add(new JLabel(wlcm));
|
||||
}
|
||||
|
||||
|
||||
}
|
17
src/TESTER1.java
Normal file
17
src/TESTER1.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
import Game.GameFrame;
|
||||
import Game.GamePanel;
|
||||
|
||||
|
||||
public class TESTER1 {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
GameFrame f = new GameFrame();
|
||||
f.addKeyListener(f.gamePanel);
|
||||
|
||||
}
|
||||
|
||||
}
|
BIN
src/ctrl.png
Normal file
BIN
src/ctrl.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 272 KiB |
BIN
src/wellcome.png
Normal file
BIN
src/wellcome.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 741 KiB |
Loading…
Reference in a new issue