Advertisement
PizzaDelivery

Thingy

Dec 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. package gametest.java;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10.  
  11.  
  12. @SuppressWarnings("serial")
  13. public class GameTest extends JPanel {
  14.  
  15. static boolean stopp = true;
  16. static String key = "";
  17. static int x = 0;
  18. static int y = 0;
  19. static boolean directionX = false; // false = left true = right;
  20. static boolean directionY = false; // false = upp true = down;
  21. static char direction = ' ';
  22. static final int ballSize = 40; // CHANGABLE VARIABLE only *5 tho, so like 5, 10, 15, 20, 25, 30, 35 etc.
  23. static int frames = 0;
  24. static int touches = 0;
  25. static int pixelsMoved = 5;
  26. static int pixels = 0;
  27. static int xLast = 0;
  28. static int xCurrent = 0;
  29. static int yLast = 0;
  30. static int yCurrent = 0;
  31. static int[] pixel;
  32. static final int WIDTH = 1280;
  33. static final int HEIGHT = WIDTH / 16 * 9; // * (16/9) for higher HEIGHT || * (9/16) for longer WIDTH
  34. static int ballWIDTH = WIDTH - (16 + ballSize); // Constant and so the ball is hit on the upper left corner
  35. static int ballHEIGHT = HEIGHT - (38 + ballSize); // Constant and so the ball is hit on the upper left corner
  36. static final int[] xMonster = {400, 70, 510, 50}; // Only ending in a 0 or 5*
  37. static final int[] yMonster = {230, 120, 620, 170}; // Only ending in a 0 or 5*
  38. static final int monsterSize = ballSize;
  39. static final int monsters = 4;
  40. boolean touched = false;
  41. boolean xMAXED = false;
  42. boolean yMAXED = false;
  43. static boolean alive = true;
  44. static boolean monsterStatus = true; // FALSE = DEAD TRUE = ALIVE
  45.  
  46. static boolean xCollisionN = false;
  47. static boolean xCollisionP = false;
  48. static boolean yCollisionN = false;
  49. static boolean yCollisionP = false;
  50.  
  51. private void moveBall() {
  52.  
  53. switch (key) {
  54. case "Upp":
  55. directionY = false;
  56. direction = 'Y';
  57. break;
  58.  
  59. case "Nedpil":
  60.  
  61. directionY = true;
  62. direction = 'Y';
  63. break;
  64. case "Vänster":
  65. directionX = false;
  66. direction = 'X';
  67. break;
  68. case "Höger":
  69. directionX = true;
  70. direction = 'X';
  71. break;
  72. }
  73. if(key.equals("Upp") || key.equals("Nedpil") || key.equals("Vänster") || key.equals("Höger")) {
  74. if(direction == 'X' ) {
  75. if(directionX == false) {
  76. if(xCollisionP == false) {
  77. x -= pixelsMoved;
  78. } else {
  79. xCollisionN = false;
  80. }
  81. } else if(directionX == true) {
  82. if(xCollisionN == false) {
  83. x += pixelsMoved;
  84. } else {
  85. xCollisionP = false;
  86. }
  87. }
  88. }
  89. if(direction == 'Y') {
  90. if(directionY == false) {
  91. if(yCollisionP == false) {
  92. y -= pixelsMoved;
  93. } else {
  94. yCollisionN = false;
  95. }
  96. } else if(directionY == true) {
  97. if(yCollisionN == false) {
  98. y += pixelsMoved;
  99. } else {
  100. yCollisionP = false;
  101. }
  102. }
  103. }
  104. }
  105.  
  106. if(x + ballSize < xMonster[0]) {
  107. System.out.println(x);
  108. xCollisionN = false;
  109. } else if(x + ballSize > xMonster[0] - 5 && x + ballSize <= xMonster[0] - 5 + monsterSize){
  110. System.out.println(x);
  111. xCollisionN = true;
  112. }
  113. if(xMonster[0] + monsterSize < x - 5) {
  114. System.out.println(x);
  115. xCollisionP = false;
  116. } else if(xMonster[0] + monsterSize > x - 5 && xMonster[0] + monsterSize <= x - 5 + ballSize){
  117. System.out.println(x);
  118. xCollisionP = true;
  119. }
  120.  
  121.  
  122. if(y + ballSize < yMonster[0]) {
  123. System.out.println(y);
  124. yCollisionN = false;
  125. } else if(y + ballSize > yMonster[0] - 5 && y + ballSize <= yMonster[0] - 5 + monsterSize){
  126. System.out.println(y);
  127. yCollisionN = true;
  128. }
  129. if(yMonster[0] + monsterSize < y - 5) {
  130. System.out.println(y);
  131. yCollisionP = false;
  132. } else if(yMonster[0] + monsterSize > y - 5 && yMonster[0] + monsterSize <= y - 5 + ballSize){
  133. System.out.println(y);
  134. yCollisionP = true;
  135. }
  136. }
  137.  
  138. @Override
  139. public void paint(Graphics g) {
  140. super.paint(g);
  141. Graphics2D g2d = (Graphics2D) g;
  142. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  143. RenderingHints.VALUE_ANTIALIAS_ON);
  144. // BACKGROUND
  145. g2d.setColor(java.awt.Color.LIGHT_GRAY);
  146. g2d.fillRect(0,0, WIDTH, HEIGHT);
  147. // PLAYER
  148. g2d.setColor(java.awt.Color.RED);
  149. g2d.fillRect(x, y, ballSize, ballSize);
  150. // MONSTER
  151. if(monsterStatus == true) {
  152. g2d.setColor(java.awt.Color.BLACK);
  153. g2d.fillRect(xMonster[0],yMonster[0], monsterSize, monsterSize);
  154. }
  155. }
  156. public GameTest() {
  157. KeyListener listener = new MyKeyListener();
  158. addKeyListener(listener);
  159. setFocusable(true);
  160. }
  161.  
  162. public class MyKeyListener implements KeyListener {
  163.  
  164. @Override
  165. public void keyTyped(KeyEvent e) {
  166. }
  167.  
  168. @Override
  169. public void keyPressed(KeyEvent e) {
  170. key = KeyEvent.getKeyText(e.getKeyCode());
  171. stopp = false;
  172. }
  173.  
  174. @Override
  175. public void keyReleased(KeyEvent e) {
  176. stopp = true;
  177. }
  178. }
  179.  
  180. public static void start() throws InterruptedException {
  181. //Creating JFrame and the frame
  182. JFrame frame = new JFrame("Sample Frame");
  183. GameTest game = new GameTest();
  184. frame.add(game);
  185. frame.setSize(WIDTH, HEIGHT);
  186. frame.setVisible(true);
  187. frame.setResizable(true);
  188. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  189.  
  190.  
  191. //Update loop
  192. while (true) {
  193. if(stopp == false) {
  194. game.moveBall();
  195. }
  196.  
  197. game.repaint();
  198.  
  199. Thread.sleep(8);
  200. }
  201. }
  202.  
  203. public static void main(String[] args) throws InterruptedException {
  204. start();
  205. } // END OF MAIN
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement