Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. import javax.swing.JFrame;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. JFrame frame = new JFrame();
  7. View view = new View();
  8. Model model = new Model(view);
  9.  
  10. frame.setSize(750, 750);
  11. frame.getContentPane().add(view);
  12. frame.setVisible(true);
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. }
  15. }
  16.  
  17. import java.awt.Color;
  18. import java.awt.Font;
  19. import java.awt.Graphics;
  20. import java.awt.Rectangle;
  21.  
  22. import javax.swing.AbstractAction;
  23. import javax.swing.ActionMap;
  24. import javax.swing.InputMap;
  25. import javax.swing.JPanel;
  26. import javax.swing.KeyStroke;
  27.  
  28. public class View extends JPanel {
  29.  
  30. Model model;
  31. Rectangle bounds;
  32.  
  33. public View() {
  34. setBackground(Color.BLACK);
  35. bounds = new Rectangle(0, 0, 705, 670);
  36. }
  37.  
  38. public void setModel(Model model) {
  39. this.model = model;
  40. }
  41.  
  42. public void addKeyBinding(String name, int keyEvent, boolean pressed, AbstractAction action) {
  43. InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
  44. ActionMap actionMap = getActionMap();
  45.  
  46. inputMap.put(KeyStroke.getKeyStroke(keyEvent, 0, !pressed), name);
  47. actionMap.put(name, action);
  48. }
  49.  
  50. @Override
  51. public void paintComponent(Graphics g) {
  52. super.paintComponent(g);
  53. if (model.getEntities() != null) {
  54. for (Entity entity : model.getEntities()) {
  55. entity.paint(g);
  56. }
  57. g.setColor(Color.BLUE);
  58. g.setFont(new Font("Arial", 1, 20));
  59. g.drawString(model.getPaddleScore(1) + " : " + model.getPaddleScore(2), 350, 20);
  60. } else {
  61. System.out.println("Something is wrong with the entities...");
  62. }
  63. }
  64.  
  65. @Override
  66. public Rectangle getBounds() {
  67. return bounds;
  68.  
  69. }
  70. }
  71.  
  72. import java.awt.Dimension;
  73. import java.awt.Graphics;
  74. import java.awt.Point;
  75.  
  76. public interface Entity {
  77. public Dimension getSize();
  78.  
  79. public Point getLocation();
  80.  
  81. public void setLocation(Point p);
  82.  
  83. public void paint(Graphics g);
  84. }
  85.  
  86. import java.awt.Color;
  87. import java.awt.Dimension;
  88. import java.awt.Graphics;
  89. import java.awt.Point;
  90. import java.awt.event.KeyEvent;
  91.  
  92. public class Paddle implements Entity {
  93. int score = 0;
  94. int paddleNum;
  95. int paddleX = 0, paddleY = 0;
  96. View view;
  97.  
  98. Point location = new Point(0, 0);
  99.  
  100. public Paddle(int paddleNum) {
  101. this.paddleNum = paddleNum;
  102. }
  103.  
  104. public void createBindings() {
  105. if (paddleNum == 1) {
  106. view.addKeyBinding("leftup.pressed", KeyEvent.VK_W, true, new LeftAction(Direction.LEFT_UP, true));
  107. view.addKeyBinding("leftup.released", KeyEvent.VK_W, false, new LeftAction(Direction.LEFT_UP, false));
  108. view.addKeyBinding("leftdown.pressed", KeyEvent.VK_S, true, new LeftAction(Direction.LEFT_DOWN, true));
  109. view.addKeyBinding("leftdown.released", KeyEvent.VK_S, false, new LeftAction(Direction.LEFT_DOWN, false));
  110. } else {
  111. view.addKeyBinding("rightup.pressed", KeyEvent.VK_UP, true, new RightAction(Direction.RIGHT_UP, true));
  112. view.addKeyBinding("rightup.released", KeyEvent.VK_UP, false, new RightAction(Direction.RIGHT_UP, false));
  113. view.addKeyBinding("rightdown.pressed", KeyEvent.VK_DOWN, true,
  114. new RightAction(Direction.RIGHT_DOWN, true));
  115. view.addKeyBinding("rightdown.released", KeyEvent.VK_DOWN, false,
  116. new RightAction(Direction.RIGHT_DOWN, false));
  117. }
  118. }
  119.  
  120. @Override
  121. public Dimension getSize() {
  122. return new Dimension(25, 100);
  123. }
  124.  
  125. @Override
  126. public Point getLocation() {
  127. return new Point(location);
  128. }
  129.  
  130. @Override
  131. public void setLocation(Point p) {
  132. location = p;
  133. }
  134.  
  135. public void setView(View view) {
  136. this.view = view;
  137. }
  138.  
  139. public void resetScore() {
  140. score = 0;
  141. }
  142.  
  143. public void increaseScore() {
  144. score++;
  145. }
  146.  
  147. public int getScore() {
  148. return score;
  149. }
  150.  
  151. @Override
  152. public void paint(Graphics g) {
  153. g.setColor(Color.WHITE);
  154. g.fillRect(getLocation().x, getLocation().y, getSize().width, getSize().height);
  155. }
  156. }
  157.  
  158. import java.awt.Color;
  159. import java.awt.Dimension;
  160. import java.awt.Graphics;
  161. import java.awt.Point;
  162.  
  163. public class Ball implements Entity {
  164. Point location = new Point(0, 0);
  165. int x = 0, y = 0;
  166.  
  167. public Ball() {
  168. }
  169.  
  170. @Override
  171. public Dimension getSize() {
  172. return new Dimension(20, 20);
  173. }
  174.  
  175. @Override
  176. public Point getLocation() {
  177. return new Point(location);
  178.  
  179. }
  180.  
  181. @Override
  182. public void setLocation(Point p) {
  183. location = p;
  184. }
  185.  
  186. public void setX(int x) {
  187. this.x = x;
  188. }
  189.  
  190. public int getX() {
  191. return x;
  192. }
  193.  
  194. public void setY(int y) {
  195. this.y = y;
  196. }
  197.  
  198. public int getY() {
  199. return y;
  200. }
  201.  
  202. @Override
  203. public void paint(Graphics g) {
  204. g.setColor(Color.RED);
  205. g.fillOval(getLocation().x, getLocation().y, getSize().width, getSize().height);
  206. }
  207. }
  208.  
  209. import java.awt.Point;
  210. import java.awt.Rectangle;
  211. import java.awt.event.ActionEvent;
  212. import java.awt.event.ActionListener;
  213. import java.util.ArrayList;
  214. import java.util.HashSet;
  215. import java.util.List;
  216. import java.util.Set;
  217.  
  218. import javax.swing.JOptionPane;
  219. import javax.swing.Timer;
  220.  
  221. public class Model {
  222. Paddle paddle1;
  223. Paddle paddle2;
  224. Ball ball;
  225.  
  226. static Set<Direction> keys = new HashSet<Direction>(25);
  227. Timer timer;
  228. boolean first = false;
  229. boolean direction = false, axis = false;
  230. double ballX = 0, ballY = 0;
  231. double p1X = 0, p1Y = 0;
  232. double p2X = 0, p2Y = 0;
  233. double incline = -0.5;
  234. List<Entity> entities = new ArrayList<Entity>(20);
  235. View view;
  236.  
  237. public Model(View view) {
  238. this.view = view;
  239. startTimer();
  240. view.setModel(this);
  241. }
  242.  
  243. public void startTimer() {
  244. timer = new Timer(2, new ActionListener() {
  245.  
  246. @Override
  247. public void actionPerformed(ActionEvent arg0) {
  248. update(view.getBounds());
  249. view.repaint();
  250. }
  251. });
  252. timer.start();
  253. }
  254.  
  255. public void update(Rectangle bounds) {
  256. if (paddle1 == null || paddle2 == null || ball == null) {
  257. paddle1 = new Paddle(1);
  258. paddle2 = new Paddle(2);
  259. ball = new Ball();
  260. ballX = 300;
  261. ballY = 300;
  262. p1X = 30;
  263. p2X = 650;
  264. p1Y = 350;
  265. p2Y = 350;
  266. paddle1.setView(view);
  267. paddle2.setView(view);
  268. paddle1.createBindings();
  269. paddle2.createBindings();
  270. entities.add(paddle1);
  271. entities.add(paddle2);
  272. entities.add(ball);
  273. }
  274.  
  275. if (paddle1.getScore() > 7) {
  276. JOptionPane.showMessageDialog(view, "Player 1 has won!");
  277. paddle1.resetScore();
  278. paddle2.resetScore();
  279. } else if (paddle2.getScore() > 7) {
  280. JOptionPane.showMessageDialog(view, "Player 2 has won!");
  281. paddle1.resetScore();
  282. paddle2.resetScore();
  283. }
  284.  
  285. setDirection(direction, incline);
  286. bounce();
  287.  
  288. // TODO Add functionality for changing ball location...
  289. // TODO Don't forget to use collision detection!!!
  290.  
  291. if (keys.contains(Direction.LEFT_UP)) {
  292. p1Y -= 2;
  293. } else if (keys.contains(Direction.LEFT_DOWN)) {
  294. p1Y += 2;
  295. }
  296. if (keys.contains(Direction.RIGHT_UP)) {
  297. p2Y -= 2;
  298. } else if (keys.contains(Direction.RIGHT_DOWN)) {
  299. p2Y += 2;
  300. }
  301.  
  302. paddle1.setLocation(new Point((int) p1X, (int) p1Y));
  303. paddle2.setLocation(new Point((int) p2X, (int) p2Y));
  304. ball.setLocation(new Point((int) ballX, (int) ballY));
  305.  
  306. }
  307.  
  308. public Entity[] getEntities() {
  309. return entities.toArray(new Entity[0]);
  310.  
  311. }
  312.  
  313. public void bounce() {
  314. // TODO Paddle collision detection
  315.  
  316. if (ballX < p1X + paddle1.getSize().width && ballY > p1Y && ballY < p1Y + paddle2.getSize().height) {
  317. direction = true;
  318. }
  319.  
  320. if (ballX + ball.getSize().width > p2X && ballY > p2Y && ballY < p2Y + paddle1.getSize().height) {
  321. direction = false;
  322. }
  323.  
  324. if (ballX < view.getBounds().x) {
  325. paddle2.increaseScore();
  326.  
  327. direction = !direction;
  328.  
  329. ballX = 300;
  330. ballY = 300;
  331. // direction = true;
  332. }
  333. if (ball.getLocation().x > view.getBounds().x + view.getBounds().width) {
  334. paddle1.increaseScore();
  335.  
  336. direction = !direction;
  337.  
  338. ballX = 300;
  339. ballY = 300;
  340. // direction = false;
  341. }
  342. if (ball.getLocation().y < view.getBounds().y) {
  343. ballY++;
  344. incline *= -1;
  345.  
  346. }
  347. if (ball.getLocation().y > view.getBounds().height) {
  348. ballY--;
  349. incline *= -1;
  350. }
  351.  
  352. /////////////
  353. if (paddle1.getLocation().y < view.getBounds().y) {
  354. p1Y = view.getBounds().x - 1;
  355. }
  356. if (paddle1.getLocation().y + paddle1.getSize().height > view.getBounds().height + 22) {
  357. p1Y = view.getBounds().height - paddle1.getSize().height + 22;
  358. }
  359.  
  360. if (paddle2.getLocation().y < view.getBounds().y) {
  361. p2Y = view.getBounds().x - 1;
  362. }
  363. if (paddle2.getLocation().y + paddle2.getSize().height > view.getBounds().height + 22) {
  364. p2Y = view.getBounds().height - paddle2.getSize().height + 22;
  365. }
  366.  
  367. }
  368.  
  369. public void setDirection(boolean Xdir, double inc) {
  370. ballY += inc;
  371. if (Xdir) {
  372. ballX++;
  373. } else if (!Xdir) {
  374. ballX--;
  375. }
  376. }
  377.  
  378. public int getPaddleScore(int paddleNum) {
  379. if (paddleNum == 1)
  380. return paddle1.getScore();
  381. else {
  382. return paddle2.getScore();
  383. }
  384. }
  385. }
  386.  
  387. import java.awt.event.ActionEvent;
  388.  
  389. import javax.swing.AbstractAction;
  390.  
  391. public class LeftAction extends AbstractAction {
  392. Direction dir;
  393. boolean pressed;
  394.  
  395. public LeftAction(Direction dir, boolean pressed) {
  396. this.dir = dir;
  397. this.pressed = pressed;
  398. }
  399.  
  400. @Override
  401. public void actionPerformed(ActionEvent arg0) {
  402. if (pressed) {
  403. Model.keys.add(dir);
  404. } else {
  405. Model.keys.remove(dir);
  406. }
  407. }
  408.  
  409. }
  410.  
  411. import java.awt.event.ActionEvent;
  412.  
  413. import javax.swing.AbstractAction;
  414.  
  415. public class RightAction extends AbstractAction {
  416. Direction dir;
  417. boolean pressed;
  418.  
  419. public RightAction(Direction dir, boolean pressed) {
  420. this.dir = dir;
  421. this.pressed = pressed;
  422. }
  423.  
  424. @Override
  425. public void actionPerformed(ActionEvent e) {
  426. if (pressed) {
  427. Model.keys.add(dir);
  428. } else {
  429. Model.keys.remove(dir);
  430. }
  431. }
  432.  
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement