Commit f73fbc75 authored by nargessalehi98's avatar nargessalehi98

Add Color checking feature(test).

parent 66ea0959
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import org.w3c.dom.css.RGBColor;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
...@@ -19,202 +21,314 @@ import java.awt.image.BufferedImage; ...@@ -19,202 +21,314 @@ import java.awt.image.BufferedImage;
*/ */
public class GameState { public class GameState {
public int locX, locY, diam; public int locX, locY, diam;
public boolean gameOver; public boolean gameOver;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT; private boolean mousePress;
private boolean mousePress; private int mouseX, mouseY;
private int mouseX, mouseY; private KeyHandler keyHandler;
private KeyHandler keyHandler; private MouseHandler mouseHandler;
private MouseHandler mouseHandler; public double rotateAmount;
public double rotateAmount; private boolean one = false;
private boolean two = false;
public GameState() { private boolean three = false;
locX = 100;
locY = 100;
diam = 42; public GameState(int num) {
rotateAmount = 0; locX = 100;
gameOver = false; locY = 100;
// diam = 42;
keyUP = false; rotateAmount = 0;
keyDOWN = false; gameOver = false;
keyRIGHT = false; //
keyLEFT = false; keyUP = false;
// keyDOWN = false;
mousePress = false; keyRIGHT = false;
mouseX = 0; keyLEFT = false;
mouseY = 0; //
// mousePress = false;
keyHandler = new KeyHandler(); mouseX = 0;
mouseHandler = new MouseHandler(); mouseY = 0;
} //
if (num == 1) {
/** one = true;
* The method which updates the game state. }
*/ if (num == 2) {
public void update() { two = true;
if (rotateAmount >= 360 || rotateAmount <= -360) rotateAmount = 0; }
if (mousePress) { if (num == 3) {
locY = mouseY - diam / 2; three = true;
locX = mouseX - diam / 2; }
} keyHandler = new KeyHandler();
if (keyUP) { mouseHandler = new MouseHandler();
move(+5); }
}
if (keyDOWN) { public Rectangle getBounds(int locX,int locY){
move(-5); return new Rectangle( locX, locY, 25, 25);
}
} /**
if (keyLEFT) * The method which updates the game state.
rotateAmount -= 10; */
public void update() throws AWTException {
if (keyRIGHT) if (rotateAmount >= 360 || rotateAmount <= -360) rotateAmount = 0;
rotateAmount += 10; if (mousePress) {
locY = mouseY - diam / 2;
locX = Math.max(locX, 42 ); locX = mouseX - diam / 2;
locX = Math.min(locX, GameFrame.GAME_WIDTH - diam -160); }
locY = Math.max(locY, 50);
locY = Math.min(locY, GameFrame.GAME_HEIGHT - diam-160); if (keyUP) {
} // boolean Permission=true;
// for (Wall wall : Controller.walls) {
public void move(int px) { // if(wall.intersects(getBounds(locX+5,locY+5))){
double d; // System.out.println("4");
// Permission = false;
if (rotateAmount == 0) locX += px; // break;
if (rotateAmount == 90 || rotateAmount == -270) locY += px; // }
if (rotateAmount == -90 || rotateAmount == 270) locY -= px; // }
if (rotateAmount == 180 || rotateAmount == -180) locX -= px; // if (Permission)
move(+5);
if ((rotateAmount > 0 && rotateAmount < 90) || (rotateAmount < -270 && rotateAmount > -360)) { }
if (px > 0) {
locX += px * Math.cos(rotateAmount * Math.PI / 180); if (keyDOWN) {
locY += px * Math.sin(rotateAmount * Math.PI / 180); boolean Permission=true;
} else { move(-5);
locX += px * Math.cos(rotateAmount * Math.PI / 180); }
locY += px * Math.sin(rotateAmount * Math.PI / 180);
}
} if (keyLEFT)
if ((rotateAmount > 90 && rotateAmount < 180) || (rotateAmount < -180 && rotateAmount > -270)) { rotateAmount -= 15;
if (px > 0) {
d = rotateAmount - 90; if (keyRIGHT)
locX -= px * Math.sin(d * Math.PI / 180); rotateAmount += 15;
locY += px * Math.cos(d * Math.PI / 180);
} else { locX = Math.max(locX, 42);
d = rotateAmount + 270; locX = Math.min(locX, GameFrame.GAME_WIDTH - diam - 42);
locX -= px * Math.sin(d * Math.PI / 180); locY = Math.max(locY, 70);
locY += px * Math.cos(d * Math.PI / 180); locY = Math.min(locY, ((Controller.row-1)/2)*50+60);
} }
}
if ((rotateAmount > 180 && rotateAmount < 270) || (rotateAmount < -90 && rotateAmount > -180)) { public void move(int px) {
double d;
if (px > 0) {
d = rotateAmount - 180; if (rotateAmount == 0) locX += px;
locX -= px * Math.cos(d * Math.PI / 180); if (rotateAmount == 90 || rotateAmount == -270) locY += px;
locY -= px * Math.sin(d * Math.PI / 180); if (rotateAmount == -90 || rotateAmount == 270) locY -= px;
} else { if (rotateAmount == 180 || rotateAmount == -180) locX -= px;
d = rotateAmount + 180;
locX -= px * Math.cos(d * Math.PI / 180); if ((rotateAmount > 0 && rotateAmount < 90) || (rotateAmount < -270 && rotateAmount > -360)) {
locY -= px * Math.sin(d * Math.PI / 180); if (px > 0) {
} locX += px * Math.cos(rotateAmount * Math.PI / 180);
} locY += px * Math.sin(rotateAmount * Math.PI / 180);
if ((rotateAmount > 270 && rotateAmount < 360) || (rotateAmount < 0 && rotateAmount > -90)) { } else {
locX += px * Math.cos(rotateAmount * Math.PI / 180);
if (px > 0) { locY += px * Math.sin(rotateAmount * Math.PI / 180);
d = rotateAmount - 270; }
locX += px * Math.sin(d * Math.PI / 180); }
locY -= px * Math.cos(d * Math.PI / 180); if ((rotateAmount > 90 && rotateAmount < 180) || (rotateAmount < -180 && rotateAmount > -270)) {
} else { if (px > 0) {
d = rotateAmount + 90; d = rotateAmount - 90;
locX += px * Math.sin(d * Math.PI / 180); locX -= px * Math.sin(d * Math.PI / 180);
locY -= px * Math.cos(d * Math.PI / 180); locY += px * Math.cos(d * Math.PI / 180);
} } else {
d = rotateAmount + 270;
} locX -= px * Math.sin(d * Math.PI / 180);
locY += px * Math.cos(d * Math.PI / 180);
} }
}
public KeyListener getKeyListener() { if ((rotateAmount > 180 && rotateAmount < 270) || (rotateAmount < -90 && rotateAmount > -180)) {
return keyHandler;
} if (px > 0) {
d = rotateAmount - 180;
public MouseListener getMouseListener() { locX -= px * Math.cos(d * Math.PI / 180);
return mouseHandler; locY -= px * Math.sin(d * Math.PI / 180);
} } else {
d = rotateAmount + 180;
public MouseMotionListener getMouseMotionListener() { locX -= px * Math.cos(d * Math.PI / 180);
return mouseHandler; locY -= px * Math.sin(d * Math.PI / 180);
} }
}
if ((rotateAmount > 270 && rotateAmount < 360) || (rotateAmount < 0 && rotateAmount > -90)) {
/**
* The keyboard handler. if (px > 0) {
*/ d = rotateAmount - 270;
class KeyHandler extends KeyAdapter { locX += px * Math.sin(d * Math.PI / 180);
locY -= px * Math.cos(d * Math.PI / 180);
@Override } else {
public void keyPressed(KeyEvent e) { d = rotateAmount + 90;
switch (e.getKeyCode()) { locX += px * Math.sin(d * Math.PI / 180);
case KeyEvent.VK_UP: locY -= px * Math.cos(d * Math.PI / 180);
keyUP = true; }
break;
case KeyEvent.VK_DOWN: }
keyDOWN = true;
break; }
case KeyEvent.VK_LEFT:
keyLEFT = true; public void checkWall() {
break; for (Wall wall : Controller.walls) {
case KeyEvent.VK_RIGHT: if (wall.getX() == locX + 5 || wall.getY() == locY + 5) {
keyRIGHT = true; keyDOWN = false;
break; } else if (wall.getX() == locX - 5 || wall.getY() == locY - 5) {
case KeyEvent.VK_ESCAPE: keyUP = false;
gameOver = true; }
break; }
} }
}
public KeyListener getKeyListener() {
@Override return keyHandler;
public void keyReleased(KeyEvent e) { }
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: public MouseListener getMouseListener() {
keyUP = false; return mouseHandler;
break; }
case KeyEvent.VK_DOWN:
keyDOWN = false; public MouseMotionListener getMouseMotionListener() {
break; return mouseHandler;
case KeyEvent.VK_LEFT: }
keyLEFT = false;
break;
case KeyEvent.VK_RIGHT: /**
keyRIGHT = false; * The keyboard handler.
break; */
} class KeyHandler extends KeyAdapter {
}
@Override
} public void keyPressed(KeyEvent e) {
if (one) {
/** switch (e.getKeyCode()) {
* The mouse handler. case KeyEvent.VK_UP:
*/ keyUP = true;
class MouseHandler extends MouseAdapter { break;
case KeyEvent.VK_DOWN:
@Override keyDOWN = true;
public void mousePressed(MouseEvent e) { break;
mouseX = e.getX(); case KeyEvent.VK_LEFT:
mouseY = e.getY(); keyLEFT = true;
mousePress = true; break;
} case KeyEvent.VK_RIGHT:
keyRIGHT = true;
@Override break;
public void mouseReleased(MouseEvent e) { case KeyEvent.VK_ESCAPE:
mousePress = false; gameOver = true;
} break;
}
@Override }
public void mouseDragged(MouseEvent e) { if (two) {
mouseX = e.getX(); switch (e.getKeyCode()) {
mouseY = e.getY(); case KeyEvent.VK_W:
} keyUP = true;
} break;
case KeyEvent.VK_S:
keyDOWN = true;
break;
case KeyEvent.VK_A:
keyLEFT = true;
break;
case KeyEvent.VK_D:
keyRIGHT = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
if (three) {
switch (e.getKeyCode()) {
case KeyEvent.VK_I:
keyUP = true;
break;
case KeyEvent.VK_K:
keyDOWN = true;
break;
case KeyEvent.VK_J:
keyLEFT = true;
break;
case KeyEvent.VK_L:
keyRIGHT = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if (one) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
keyUP = false;
break;
case KeyEvent.VK_DOWN:
keyDOWN = false;
break;
case KeyEvent.VK_LEFT:
keyLEFT = false;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = false;
break;
}
}
if (two) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W:
keyUP = false;
break;
case KeyEvent.VK_S:
keyDOWN = false;
break;
case KeyEvent.VK_A:
keyLEFT = false;
break;
case KeyEvent.VK_D:
keyRIGHT = false;
break;
}
}
if (three) {
switch (e.getKeyCode()) {
case KeyEvent.VK_I:
keyUP = false;
break;
case KeyEvent.VK_K:
keyDOWN = false;
break;
case KeyEvent.VK_J:
keyLEFT = false;
break;
case KeyEvent.VK_L:
keyRIGHT = false;
break;
}
}
}
}
/**
* The mouse handler.
*/
class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
mousePress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
mousePress = false;
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment