Advertisement
rochitac

Point Translate Game

Jan 18th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. /**
  2. * This is a game that uses switch cases.
  3. *
  4. * @author (Rochita Chatterjee)
  5. * @version (Version1 1/16/17)
  6. */
  7. import javax.swing.*; // imports useful classes
  8. import java.awt.*;
  9. import java.util.*;
  10. import java.text.*;
  11. import java.lang.*;
  12. import java.applet.*;
  13. public class Game extends JFrame // JFrame
  14. {
  15. public void paint (Graphics g)
  16. {
  17. super.paint(g);
  18.  
  19. int x = 150, y = 200; // this is the center/starting point
  20. int pointSize = 10; // this is the size of the pixel
  21.  
  22.  
  23. Random random = new Random (); // generate random numbers
  24. for( int count = 1; count <=300; count ++) //allows game to continue
  25. {
  26. int move = random.nextInt(4)+1;// random numb
  27.  
  28. switch(move)
  29. {
  30. case 1:
  31. x = x + pointSize;
  32. g.drawRect(x, y, pointSize, pointSize); // shifts rectangle to the right based on rand. number
  33.  
  34. break;
  35.  
  36. case 2:
  37. y = y + pointSize; // shifts rectangle up based on rand. number
  38. g.drawRect(x, y, pointSize, pointSize);
  39.  
  40. break;
  41.  
  42. case 3:
  43. x = x - pointSize; // shifts rectangle to the left based on rand. number
  44. g.drawRect(x, y, pointSize, pointSize);
  45.  
  46. break;
  47.  
  48. case 4:
  49. y = y - pointSize; // // shifts rectangle down based on rand. number
  50. g.drawRect(x, y, pointSize, pointSize);
  51.  
  52. break;
  53. }
  54. }
  55. }
  56. public static void main (String [] args)
  57. {
  58. Game SIZE = new Game (); // setting the frame for the pictures
  59. SIZE.setSize(300,400);
  60. SIZE.setVisible(true);
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement