Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This is a game that uses switch cases.
- *
- * @author (Rochita Chatterjee)
- * @version (Version1 1/16/17)
- */
- import javax.swing.*; // imports useful classes
- import java.awt.*;
- import java.util.*;
- import java.text.*;
- import java.lang.*;
- import java.applet.*;
- public class Game extends JFrame // JFrame
- {
- public void paint (Graphics g)
- {
- super.paint(g);
- int x = 150, y = 200; // this is the center/starting point
- int pointSize = 10; // this is the size of the pixel
- Random random = new Random (); // generate random numbers
- for( int count = 1; count <=300; count ++) //allows game to continue
- {
- int move = random.nextInt(4)+1;// random numb
- switch(move)
- {
- case 1:
- x = x + pointSize;
- g.drawRect(x, y, pointSize, pointSize); // shifts rectangle to the right based on rand. number
- break;
- case 2:
- y = y + pointSize; // shifts rectangle up based on rand. number
- g.drawRect(x, y, pointSize, pointSize);
- break;
- case 3:
- x = x - pointSize; // shifts rectangle to the left based on rand. number
- g.drawRect(x, y, pointSize, pointSize);
- break;
- case 4:
- y = y - pointSize; // // shifts rectangle down based on rand. number
- g.drawRect(x, y, pointSize, pointSize);
- break;
- }
- }
- }
- public static void main (String [] args)
- {
- Game SIZE = new Game (); // setting the frame for the pictures
- SIZE.setSize(300,400);
- SIZE.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement