Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.applet.Applet;
  3. import java.applet.AudioClip;
  4. import java.awt.*;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7.  
  8. /**
  9. * Created by Nivesh Varma on 07 Mar 17.
  10. */
  11. public class NekoTheCat implements MouseListener, Runnable{
  12.  
  13. Image catRight1 = new ImageIcon(getClass().getResource("Neko1.gif")).getImage();
  14. Image catRight2 = new ImageIcon(getClass().getResource("Neko2.gif")).getImage();
  15. Image catLeft1 = new ImageIcon(getClass().getResource("Neko3.gif")).getImage();
  16. Image catLeft2 = new ImageIcon(getClass().getResource("Neko4.gif")).getImage();
  17. Image redBall = new ImageIcon(getClass().getResource("red-ball.gif")).getImage();
  18.  
  19. Image cat1 = catRight1;
  20. Image cat2 = catRight2;
  21. Image currentImage = catRight1;
  22.  
  23. JFrame gameWindow = new JFrame("Neko The Cat!");
  24. JPanel gamePanel = new JPanel();
  25.  
  26. int catxPosition = 1;
  27. int catyPosition = 50;
  28. int catWidth = catRight1.getWidth(gamePanel);
  29. int catHeight = catRight1.getHeight(gamePanel);
  30. int ballxPosition = 0;
  31. int ballyPosition = 0;
  32. int ballSize = redBall.getWidth(gamePanel);
  33. int sleepTime = 100; // pause time between image repaints (in ms)
  34. int xBump = 10; // amount cat image is moved each repaint.
  35. int yBump = 10; // y shift change
  36. boolean catIsRunningToTheRight = true; // initially
  37. boolean catIsRunningToTheLeft = false;// initially
  38. boolean ballHasBeenPlaced = false;// initially
  39.  
  40. Graphics g;
  41.  
  42. AudioClip soundFile = Applet.newAudioClip(getClass().getResource("spacemusic.au"));
  43.  
  44. //constructor
  45. public NekoTheCat() {
  46. //get ready
  47. gameWindow.getContentPane().add(gamePanel, "Center");
  48. gamePanel.setBackground(Color.white);
  49. gameWindow.setSize(960,540);
  50. gameWindow.setVisible(true);
  51. gameWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  52.  
  53. g = gamePanel.getGraphics();
  54. // show game instructions on the screen
  55. g.setFont(new Font("Arial", Font.BOLD, 20));
  56. g.drawString("Neko the cat is looking for it's red ball!" ,100,100);
  57. g.drawString("Click the mouse to place Neko's ball." ,100,120);
  58. g.drawString("Can you move the ball to keep Neko from getting it?",100,140);
  59. g.drawString("(Pull window larger to make the game easier)" ,100,160);
  60.  
  61. gamePanel.addMouseListener(this); //call me!
  62.  
  63. soundFile.loop();
  64. }
  65.  
  66. //main
  67. public static void main(String args[]) {
  68. NekoTheCat Nyan = new NekoTheCat();
  69. Nyan.run();
  70. }
  71.  
  72. @Override
  73. public void mouseClicked(MouseEvent me) {
  74. g.setColor(Color.white); // set to background color
  75. g.fillRect(ballxPosition, ballyPosition, ballSize, ballSize); //x,y,width,height
  76.  
  77. ballxPosition = me.getX();
  78. ballyPosition = me.getY();
  79. g.drawImage(redBall,ballxPosition,ballyPosition,gamePanel); //draw ball at new click point
  80.  
  81. // System.out.println("Mouse clicked at x=" + ballxPosition + ", y=" + ballyPosition);
  82.  
  83. ballHasBeenPlaced = true;
  84.  
  85. }
  86.  
  87. @Override
  88. public void mousePressed(MouseEvent e) {
  89.  
  90. }
  91.  
  92. @Override
  93. public void mouseReleased(MouseEvent e) {
  94.  
  95. }
  96.  
  97. @Override
  98. public void mouseEntered(MouseEvent e) {
  99.  
  100. }
  101.  
  102. @Override
  103. public void mouseExited(MouseEvent e) {
  104.  
  105. }
  106.  
  107. @Override
  108. public void run() {
  109. System.out.println("thread has entered run");
  110. // g.drawImage(catRight1,0,0,gamePanel);//imageName,x coordinate,y coordinate,where to draw
  111. // g.drawImage(catRight2,1*catWidth,0,gamePanel);
  112. // g.drawImage(catLeft1, 2*catWidth,0,gamePanel);
  113. // g.drawImage(catLeft2, 3*catWidth,0,gamePanel);
  114. // g.drawImage(redBall, 4*catWidth,0,gamePanel);
  115. while (true) {
  116. while ((catxPosition > 0) && (catxPosition < gamePanel.getSize().width))
  117. {
  118. // run Neko!
  119. // 1. Blank out the last image
  120. g.setColor(Color.white); // set to background color
  121. g.fillRect(catxPosition, catyPosition, ballSize, ballSize); //x,y,width,height
  122.  
  123. // 2. Bump the location for the new image
  124. catxPosition += xBump;
  125.  
  126. // 3. Select the next image.
  127. if (currentImage == cat1) currentImage = cat2;
  128. else currentImage = cat1;
  129.  
  130. // 4. Draw the next cat image
  131. g.drawImage(currentImage,catxPosition,catyPosition,gamePanel); //draw ball at new click point
  132.  
  133. // 5. Pause briefly to let human eye see the new image!
  134. try {Thread.sleep(sleepTime);}
  135. catch(InterruptedException ie){}
  136.  
  137. // 6. If necessary, redirect the cat toward the ball.
  138. if (ballHasBeenPlaced) // first ensure that the ball is "in play"
  139. {
  140. // If cat is BELOW the ball
  141. // then move cat up 1 line.
  142. if (catyPosition > ballyPosition) catyPosition -= yBump;
  143.  
  144. // If cat is ABOVE the ball
  145. // then move cat down one line.
  146. if (catyPosition < ballyPosition) catyPosition += yBump;
  147.  
  148. // If the cat is running to the left
  149. // and the ball is to the right of the cat
  150. if (catIsRunningToTheLeft && (ballxPosition > catxPosition))
  151. reverseDirectionFromLeftToRight();
  152.  
  153. // If the cat is running to the right
  154. // and the ball is to the left of the cat
  155. if (catIsRunningToTheRight && (ballxPosition < catxPosition))
  156. reverseDirectionFromRightToLeft();
  157. }
  158.  
  159. // 7. Proximity test to see if Neko is "at" the ball.
  160. if ((Math.abs(catyPosition - ballyPosition) < 10) // y within 10
  161. && (Math.abs(catxPosition - ballxPosition) < 10)) // x within 10 pixels
  162. {
  163. // Take Neko-got-the-ball action!
  164. gamePanel.removeMouseListener(this);
  165. g.setColor(Color.red);
  166. g.setFont(new Font("Arial", Font.BOLD, 50));
  167. g.drawString("At last, I have my ball!",50,50);
  168. soundFile.stop();
  169. return;
  170. }
  171. }
  172. // turn Neko around.
  173. if (catxPosition > gamePanel.getSize().width)
  174. {
  175. reverseDirectionFromRightToLeft();
  176. catxPosition = gamePanel.getSize().width -1;
  177. }
  178.  
  179. if (catxPosition < 0)
  180. {
  181. reverseDirectionFromLeftToRight();
  182. catxPosition = 1;
  183. }
  184. } //bottom of outer while(true) loop
  185. } //end of run() method
  186.  
  187. private void reverseDirectionFromRightToLeft()
  188. {
  189. xBump = -xBump; // reverse increment
  190. cat1 = catLeft1;
  191. cat2 = catLeft2;
  192. catIsRunningToTheLeft = true;
  193. catIsRunningToTheRight = false;
  194. }
  195.  
  196. private void reverseDirectionFromLeftToRight()
  197. {
  198. xBump = -xBump;
  199. cat1 = catRight1;
  200. cat2 = catRight2;
  201. catIsRunningToTheRight = true;
  202. catIsRunningToTheLeft = false;
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement