Created
February 7, 2016 14:40
-
-
Save Divadi/975ad107c4fb55a40441 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Math; | |
import com.leapmotion.leap.*; | |
import com.leapmotion.leap.Frame; | |
import acm.graphics.*; | |
import acm.program.*; | |
import acm.util.*; | |
import java.awt.*; | |
import java.applet.*; | |
import java.net.*; | |
class SampleListener extends Listener { | |
public float vari = 0; // x position | |
public void onInit(Controller controller) { | |
System.out.println("Initialized"); | |
} | |
public void onConnect(Controller controller) { | |
System.out.println("Connected"); | |
controller.enableGesture(Gesture.Type.TYPE_SWIPE); | |
controller.enableGesture(Gesture.Type.TYPE_CIRCLE); | |
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP); | |
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP); | |
} | |
public void onDisconnect(Controller controller) { | |
// Note: not dispatched when running in a debugger. | |
System.out.println("Disconnected"); | |
} | |
public void onExit(Controller controller) { | |
System.out.println("Exited"); | |
} | |
public float getHandPos(Controller controller) { | |
Frame frame = controller.frame(); | |
for (Hand hand : frame.hands()) { | |
return hand.palmPosition().getX(); | |
} | |
return 0; | |
} | |
public Gesture.Type getGestureType(Controller controller) { | |
Frame frame = controller.frame(); | |
GestureList gestures = frame.gestures(); | |
Gesture gesture = gestures.get(0); | |
return gesture.type(); | |
} | |
public int fingerNum(Controller controller) { | |
Frame frame = controller.frame(); | |
int count = 0; | |
for(Hand hand : frame.hands()) { | |
for(Finger finger: hand.fingers()) { | |
if(finger.isExtended()) { | |
count++; | |
} | |
} | |
} | |
return count; | |
} | |
// onFrame, loops! | |
public void onFrame(Controller controller) { | |
// System.out.println(getHandPos(controller)); | |
} | |
} | |
public class BreakoutV4 extends GraphicsProgram { | |
public static float before = 0; | |
public static int difficulty = 0; | |
public static final int APPLICATION_WIDTH = 400; | |
public static final int APPLICATION_HEIGHT = 600; | |
private static final int WIDTH = APPLICATION_WIDTH; | |
private static final int HEIGHT = APPLICATION_HEIGHT; | |
private static final int PADDLE_WIDTH = 60; | |
private static final int PADDLE_HEIGHT = 10; | |
private static final int PADDLE_Y_OFFSET = 30; | |
private static final int NBRICKS_PER_ROW = 10; | |
private static final int NBRICK_ROWS = 10; | |
private static final int BRICK_SEP = 4; | |
private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW; | |
private static final int BRICK_HEIGHT = 8; | |
private static final int BALL_RADIUS = 10; | |
private static final int BRICK_Y_OFFSET = 70; | |
private static double delay; | |
private static final int NTURNS = 3; | |
private static GRect blocks[][] = new GRect[10][10]; | |
private static GRect paddle = new GRect(0, HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT); | |
private double vx; | |
private double vy = -0.15; | |
private static GOval ball = new GOval(WIDTH / 2 + BALL_RADIUS, | |
HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT - 2 * BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2); | |
private RandomGenerator rgen = RandomGenerator.getInstance(); | |
private int blockCounter = 0; | |
private GLabel score = new GLabel("Score: 0 Lives: 3", 5, HEIGHT - 20); | |
private static SampleListener listener = new SampleListener(); | |
private static Controller controller = new Controller(); | |
// Method: run() | |
public void run() { | |
controller.addListener(listener); | |
setup(); | |
addMouseListeners(); | |
add(score); | |
System.out.println("Display number of fingers for difficulty level! - In 3..."); | |
pause(1000); | |
System.out.println("2..."); | |
pause(1000); | |
System.out.println("1..."); | |
pause(1000); | |
System.out.println("Now!"); | |
while(true) { | |
int temp = listener.fingerNum(controller); | |
if(temp != 0) { | |
difficulty = temp; | |
System.out.println("Playing at: " + difficulty); | |
break; | |
} | |
} | |
for (int i = 0; i < NTURNS; i++) { | |
vy = -0.5; | |
ball.setFilled(true); | |
add(ball); | |
//waitForClick(); | |
while(true) { | |
if(listener.getGestureType(controller) == Gesture.Type.TYPE_KEY_TAP) { | |
break; | |
} | |
} | |
while (gameOn()) { | |
paddleMovement(); | |
moveBall(); | |
wallCollisionCheck(); | |
objectCollision(); | |
if (blockCounter == 100) { | |
break; | |
} | |
score.setLabel("Score: " + blockCounter + " You are on life: " + (3 - i)); | |
add(score); | |
pause(delay); | |
} | |
if (blockCounter == 100) { | |
break; | |
} | |
ball.setLocation(WIDTH / 2 + BALL_RADIUS, HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT - 2 * BALL_RADIUS); | |
} | |
score.setLabel("Score: " + blockCounter + " Lives: 0"); | |
add(score); | |
GLabel end = new GLabel("GAME OVER!"); | |
end.setFont(new Font("Monospaced", Font.BOLD, 40)); | |
end.setLocation(WIDTH / 2 - end.getWidth() / 2, HEIGHT / 2 - end.getHeight() / 2); | |
GLabel endScore = new GLabel("SCORE: " + blockCounter); | |
endScore.setFont(new Font("Monospaced", Font.BOLD, 20)); | |
endScore.setLocation(WIDTH / 2 - endScore.getWidth() / 2, HEIGHT / 2 + end.getHeight() / 2); | |
if (blockCounter == 100) { | |
endScore.setLabel("WINNER!!!"); | |
} | |
add(end); | |
add(endScore); | |
} | |
private void setup() { | |
setSize(WIDTH, HEIGHT); | |
vx = (1.5 + difficulty * 0.2)/7; | |
if (rgen.nextBoolean(0.5)) | |
vx = -vx; | |
for (int x = 0; x < NBRICK_ROWS; x++) { | |
for (int y = 0; y < NBRICKS_PER_ROW; y++) { | |
blocks[x][y] = new GRect(BRICK_SEP / 2 + x * (BRICK_WIDTH + BRICK_SEP), | |
BRICK_Y_OFFSET + y * (BRICK_SEP + BRICK_HEIGHT), BRICK_WIDTH, BRICK_HEIGHT); | |
if (y < 2) { | |
blocks[x][y].setColor(Color.RED); | |
} else if (y < 4) { | |
blocks[x][y].setColor(Color.ORANGE); | |
} else if (y < 6) { | |
blocks[x][y].setColor(Color.YELLOW); | |
} else if (y < 8) { | |
blocks[x][y].setColor(Color.GREEN); | |
} else if (y < 10) { | |
blocks[x][y].setColor(Color.CYAN); | |
} | |
blocks[x][y].setFilled(true); | |
add(blocks[x][y]); | |
} | |
} | |
paddle.setFilled(true); | |
add(paddle); | |
delay = 0.5; | |
} | |
private boolean gameOn() { | |
if (ball.getY() + BALL_RADIUS * 2 >= HEIGHT) { | |
return false; | |
} | |
return true; | |
} | |
public void paddleMovement() { | |
float k = listener.getHandPos(controller); | |
if(k < -170) { | |
k = -170; | |
} | |
else if(k > 170) { | |
k = 170; | |
} | |
else if(k == 0.0) { | |
k = before; | |
} | |
before = k; | |
paddle.setLocation((int)(k + 200) - PADDLE_WIDTH/2, paddle.getY()); | |
add(paddle); | |
} | |
private void moveBall() { | |
ball.move(vx, vy); | |
} | |
private void wallCollisionCheck() { | |
if (ball.getX() <= 0) { | |
if (!changeVel()[1]) { | |
vy = -vy; | |
} | |
vx = Math.abs(vx); | |
} | |
if (ball.getX() + BALL_RADIUS * 2 >= WIDTH) { | |
if (!changeVel()[1]) { | |
vy = -vy; | |
} | |
vx = -Math.abs(vx); | |
} | |
if (ball.getY() <= 0) { | |
if (!changeVel()[0]) { | |
vx = -vx; | |
} | |
} | |
if ((ball.getY() + BALL_RADIUS * 2 >= paddle.getY() - 1 && ball.getY() + BALL_RADIUS * 2 < paddle.getY() + 12) | |
&& (paddle.getX() - 8 <= ball.getX() + BALL_RADIUS | |
&& ball.getX() + BALL_RADIUS <= paddle.getX() + PADDLE_WIDTH + 8)) { | |
if (!changeVel()[0]) { | |
vx = -vx; | |
} | |
vy = -vy; | |
} | |
} | |
private boolean[] changeVel() { | |
boolean a = vx > 0; | |
boolean b = vy > 0; | |
vx = (rgen.nextDouble(1.5, 2.5) + difficulty * 0.2)/7; | |
vy = (rgen.nextDouble(1.5, 2.5) + difficulty * 0.15)/7; | |
/* | |
* if (ball.getY() > HEIGHT/2) { vx = rgen.nextDouble(3.5, 7.0); vy = | |
* rgen.nextDouble(3.5, 7.0); } | |
*/ | |
boolean[] t = { a, b }; | |
return t; | |
} | |
private void objectCollision() { | |
if (getElementAt(ball.getX(), ball.getY()) != null && getElementAt(ball.getX(), ball.getY()) != paddle | |
&& getElementAt(ball.getX(), ball.getY()) != score) { | |
//sound(); too buggy | |
remove(getElementAt(ball.getX(), ball.getY())); | |
vy = -vy; | |
blockCounter++; | |
} else if (getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()) != null | |
&& getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()) != paddle | |
&& getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()) != score) { | |
//sound(); | |
remove(getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY())); | |
vy = -vy; | |
blockCounter++; | |
} else if (getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) != null | |
&& getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) != paddle | |
&& getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) != score) { | |
//sound(); | |
remove(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2)); | |
vy = -vy; | |
blockCounter++; | |
} else if (getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) != null | |
&& getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) != paddle | |
&& getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) != score) { | |
//sound(); | |
remove(getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2)); | |
vy = -vy; | |
blockCounter++; | |
} | |
} | |
private void sound() { | |
try { | |
URL myURL = new URL("file:/Users/DavidDavidDavid_Park/Desktop/Eclipse/Assignment3/Ding.wav"); | |
AudioClip clip = Applet.newAudioClip(myURL); | |
clip.play(); | |
} catch (MalformedURLException murle) { | |
System.out.println(murle); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment