Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. import java.lang.Math;
  2. import com.leapmotion.leap.*;
  3. import com.leapmotion.leap.Frame;
  4.  
  5. import acm.graphics.*;
  6. import acm.program.*;
  7. import acm.util.*;
  8.  
  9. import java.awt.*;
  10. import java.applet.*;
  11. import java.net.*;
  12.  
  13. class SampleListener extends Listener {
  14. public float vari = 0; // x position
  15.  
  16. public void onInit(Controller controller) {
  17. System.out.println("Initialized");
  18. }
  19.  
  20. public void onConnect(Controller controller) {
  21. System.out.println("Connected");
  22. controller.enableGesture(Gesture.Type.TYPE_SWIPE);
  23. controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
  24. controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
  25. controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
  26. }
  27.  
  28. public void onDisconnect(Controller controller) {
  29. // Note: not dispatched when running in a debugger.
  30. System.out.println("Disconnected");
  31. }
  32.  
  33. public void onExit(Controller controller) {
  34. System.out.println("Exited");
  35. }
  36.  
  37. public float getHandPos(Controller controller) {
  38. Frame frame = controller.frame();
  39. for (Hand hand : frame.hands()) {
  40. return hand.palmPosition().getX();
  41. }
  42. return 0;
  43. }
  44.  
  45. public Gesture.Type getGestureType(Controller controller) {
  46. Frame frame = controller.frame();
  47. GestureList gestures = frame.gestures();
  48. Gesture gesture = gestures.get(0);
  49. return gesture.type();
  50. }
  51.  
  52. public int fingerNum(Controller controller) {
  53. Frame frame = controller.frame();
  54. int count = 0;
  55. for(Hand hand : frame.hands()) {
  56. for(Finger finger: hand.fingers()) {
  57. if(finger.isExtended()) {
  58. count++;
  59. }
  60. }
  61. }
  62. return count;
  63. }
  64.  
  65. // onFrame, loops!
  66. public void onFrame(Controller controller) {
  67. // System.out.println(getHandPos(controller));
  68. }
  69.  
  70. }
  71.  
  72. public class BreakoutV4 extends GraphicsProgram {
  73.  
  74. public static float before = 0;
  75. public static int difficulty = 0;
  76.  
  77. public static final int APPLICATION_WIDTH = 400;
  78. public static final int APPLICATION_HEIGHT = 600;
  79.  
  80. private static final int WIDTH = APPLICATION_WIDTH;
  81. private static final int HEIGHT = APPLICATION_HEIGHT;
  82.  
  83. private static final int PADDLE_WIDTH = 60;
  84. private static final int PADDLE_HEIGHT = 10;
  85.  
  86. private static final int PADDLE_Y_OFFSET = 30;
  87.  
  88. private static final int NBRICKS_PER_ROW = 10;
  89.  
  90. private static final int NBRICK_ROWS = 10;
  91.  
  92. private static final int BRICK_SEP = 4;
  93.  
  94. private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
  95.  
  96. private static final int BRICK_HEIGHT = 8;
  97.  
  98. private static final int BALL_RADIUS = 10;
  99.  
  100. private static final int BRICK_Y_OFFSET = 70;
  101.  
  102. private static double delay;
  103.  
  104. private static final int NTURNS = 3;
  105.  
  106. private static GRect blocks[][] = new GRect[10][10];
  107. private static GRect paddle = new GRect(0, HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
  108.  
  109. private double vx;
  110. private double vy = -0.15;
  111. private static GOval ball = new GOval(WIDTH / 2 + BALL_RADIUS,
  112. HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT - 2 * BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2);
  113.  
  114. private RandomGenerator rgen = RandomGenerator.getInstance();
  115.  
  116. private int blockCounter = 0;
  117.  
  118. private GLabel score = new GLabel("Score: 0 Lives: 3", 5, HEIGHT - 20);
  119.  
  120. private static SampleListener listener = new SampleListener();
  121. private static Controller controller = new Controller();
  122.  
  123. // Method: run()
  124. public void run() {
  125.  
  126. controller.addListener(listener);
  127.  
  128. setup();
  129. addMouseListeners();
  130. add(score);
  131. System.out.println("Display number of fingers for difficulty level! - In 3...");
  132. pause(1000);
  133. System.out.println("2...");
  134. pause(1000);
  135. System.out.println("1...");
  136. pause(1000);
  137. System.out.println("Now!");
  138. while(true) {
  139. int temp = listener.fingerNum(controller);
  140. if(temp != 0) {
  141. difficulty = temp;
  142. System.out.println("Playing at: " + difficulty);
  143. break;
  144. }
  145. }
  146. for (int i = 0; i < NTURNS; i++) {
  147. vy = -0.5;
  148. ball.setFilled(true);
  149. add(ball);
  150. //waitForClick();
  151. while(true) {
  152. if(listener.getGestureType(controller) == Gesture.Type.TYPE_KEY_TAP) {
  153. break;
  154. }
  155. }
  156. while (gameOn()) {
  157. paddleMovement();
  158. moveBall();
  159. wallCollisionCheck();
  160. objectCollision();
  161. if (blockCounter == 100) {
  162. break;
  163. }
  164. score.setLabel("Score: " + blockCounter + " You are on life: " + (3 - i));
  165. add(score);
  166. pause(delay);
  167. }
  168. if (blockCounter == 100) {
  169. break;
  170. }
  171. ball.setLocation(WIDTH / 2 + BALL_RADIUS, HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT - 2 * BALL_RADIUS);
  172. }
  173. score.setLabel("Score: " + blockCounter + " Lives: 0");
  174. add(score);
  175. GLabel end = new GLabel("GAME OVER!");
  176. end.setFont(new Font("Monospaced", Font.BOLD, 40));
  177. end.setLocation(WIDTH / 2 - end.getWidth() / 2, HEIGHT / 2 - end.getHeight() / 2);
  178. GLabel endScore = new GLabel("SCORE: " + blockCounter);
  179. endScore.setFont(new Font("Monospaced", Font.BOLD, 20));
  180. endScore.setLocation(WIDTH / 2 - endScore.getWidth() / 2, HEIGHT / 2 + end.getHeight() / 2);
  181. if (blockCounter == 100) {
  182. endScore.setLabel("WINNER!!!");
  183. }
  184. add(end);
  185. add(endScore);
  186. }
  187.  
  188. private void setup() {
  189. setSize(WIDTH, HEIGHT);
  190. vx = (1.5 + difficulty * 0.2)/7;
  191. if (rgen.nextBoolean(0.5))
  192. vx = -vx;
  193. for (int x = 0; x < NBRICK_ROWS; x++) {
  194. for (int y = 0; y < NBRICKS_PER_ROW; y++) {
  195. blocks[x][y] = new GRect(BRICK_SEP / 2 + x * (BRICK_WIDTH + BRICK_SEP),
  196. BRICK_Y_OFFSET + y * (BRICK_SEP + BRICK_HEIGHT), BRICK_WIDTH, BRICK_HEIGHT);
  197. if (y < 2) {
  198. blocks[x][y].setColor(Color.RED);
  199. } else if (y < 4) {
  200. blocks[x][y].setColor(Color.ORANGE);
  201. } else if (y < 6) {
  202. blocks[x][y].setColor(Color.YELLOW);
  203. } else if (y < 8) {
  204. blocks[x][y].setColor(Color.GREEN);
  205. } else if (y < 10) {
  206. blocks[x][y].setColor(Color.CYAN);
  207. }
  208. blocks[x][y].setFilled(true);
  209. add(blocks[x][y]);
  210. }
  211. }
  212. paddle.setFilled(true);
  213. add(paddle);
  214. delay = 0.5;
  215. }
  216.  
  217. private boolean gameOn() {
  218. if (ball.getY() + BALL_RADIUS * 2 >= HEIGHT) {
  219. return false;
  220. }
  221. return true;
  222. }
  223.  
  224. public void paddleMovement() {
  225. float k = listener.getHandPos(controller);
  226. if(k < -170) {
  227. k = -170;
  228. }
  229. else if(k > 170) {
  230. k = 170;
  231. }
  232. else if(k == 0.0) {
  233. k = before;
  234. }
  235. before = k;
  236.  
  237. paddle.setLocation((int)(k + 200) - PADDLE_WIDTH/2, paddle.getY());
  238. add(paddle);
  239. }
  240.  
  241. private void moveBall() {
  242. ball.move(vx, vy);
  243. }
  244.  
  245. private void wallCollisionCheck() {
  246. if (ball.getX() <= 0) {
  247. if (!changeVel()[1]) {
  248. vy = -vy;
  249. }
  250. vx = Math.abs(vx);
  251. }
  252. if (ball.getX() + BALL_RADIUS * 2 >= WIDTH) {
  253. if (!changeVel()[1]) {
  254. vy = -vy;
  255. }
  256. vx = -Math.abs(vx);
  257. }
  258. if (ball.getY() <= 0) {
  259. if (!changeVel()[0]) {
  260. vx = -vx;
  261. }
  262. }
  263. if ((ball.getY() + BALL_RADIUS * 2 >= paddle.getY() - 1 && ball.getY() + BALL_RADIUS * 2 < paddle.getY() + 12)
  264. && (paddle.getX() - 8 <= ball.getX() + BALL_RADIUS
  265. && ball.getX() + BALL_RADIUS <= paddle.getX() + PADDLE_WIDTH + 8)) {
  266. if (!changeVel()[0]) {
  267. vx = -vx;
  268. }
  269. vy = -vy;
  270. }
  271. }
  272.  
  273. private boolean[] changeVel() {
  274. boolean a = vx > 0;
  275. boolean b = vy > 0;
  276. vx = (rgen.nextDouble(1.5, 2.5) + difficulty * 0.2)/7;
  277. vy = (rgen.nextDouble(1.5, 2.5) + difficulty * 0.15)/7;
  278. /*
  279. * if (ball.getY() > HEIGHT/2) { vx = rgen.nextDouble(3.5, 7.0); vy =
  280. * rgen.nextDouble(3.5, 7.0); }
  281. */
  282. boolean[] t = { a, b };
  283. return t;
  284. }
  285.  
  286. private void objectCollision() {
  287. if (getElementAt(ball.getX(), ball.getY()) != null && getElementAt(ball.getX(), ball.getY()) != paddle
  288. && getElementAt(ball.getX(), ball.getY()) != score) {
  289. //sound(); too buggy
  290. remove(getElementAt(ball.getX(), ball.getY()));
  291. vy = -vy;
  292. blockCounter++;
  293.  
  294. } else if (getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()) != null
  295. && getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()) != paddle
  296. && getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()) != score) {
  297. //sound();
  298. remove(getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY()));
  299. vy = -vy;
  300. blockCounter++;
  301. } else if (getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) != null
  302. && getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) != paddle
  303. && getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2) != score) {
  304. //sound();
  305. remove(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS * 2));
  306. vy = -vy;
  307. blockCounter++;
  308. } else if (getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) != null
  309. && getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) != paddle
  310. && getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2) != score) {
  311. //sound();
  312. remove(getElementAt(ball.getX() + BALL_RADIUS * 2, ball.getY() + BALL_RADIUS * 2));
  313. vy = -vy;
  314. blockCounter++;
  315. }
  316. }
  317.  
  318. private void sound() {
  319. try {
  320. URL myURL = new URL("file:/Users/DavidDavidDavid_Park/Desktop/Eclipse/Assignment3/Ding.wav");
  321. AudioClip clip = Applet.newAudioClip(myURL);
  322. clip.play();
  323. } catch (MalformedURLException murle) {
  324. System.out.println(murle);
  325. }
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement