Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- /**
- * Created by Nivesh Varma on 07 Mar 17.
- */
- public class NekoTheCat implements MouseListener, Runnable{
- Image catRight1 = new ImageIcon(getClass().getResource("Neko1.gif")).getImage();
- Image catRight2 = new ImageIcon(getClass().getResource("Neko2.gif")).getImage();
- Image catLeft1 = new ImageIcon(getClass().getResource("Neko3.gif")).getImage();
- Image catLeft2 = new ImageIcon(getClass().getResource("Neko4.gif")).getImage();
- Image redBall = new ImageIcon(getClass().getResource("red-ball.gif")).getImage();
- Image cat1 = catRight1;
- Image cat2 = catRight2;
- Image currentImage = catRight1;
- JFrame gameWindow = new JFrame("Neko The Cat!");
- JPanel gamePanel = new JPanel();
- int catxPosition = 1;
- int catyPosition = 50;
- int catWidth = catRight1.getWidth(gamePanel);
- int catHeight = catRight1.getHeight(gamePanel);
- int ballxPosition = 0;
- int ballyPosition = 0;
- int ballSize = redBall.getWidth(gamePanel);
- int sleepTime = 100; // pause time between image repaints (in ms)
- int xBump = 10; // amount cat image is moved each repaint.
- int yBump = 10; // y shift change
- boolean catIsRunningToTheRight = true; // initially
- boolean catIsRunningToTheLeft = false;// initially
- boolean ballHasBeenPlaced = false;// initially
- Graphics g;
- AudioClip soundFile = Applet.newAudioClip(getClass().getResource("spacemusic.au"));
- //constructor
- public NekoTheCat() {
- //get ready
- gameWindow.getContentPane().add(gamePanel, "Center");
- gamePanel.setBackground(Color.white);
- gameWindow.setSize(960,540);
- gameWindow.setVisible(true);
- gameWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- g = gamePanel.getGraphics();
- // show game instructions on the screen
- g.setFont(new Font("Arial", Font.BOLD, 20));
- g.drawString("Neko the cat is looking for it's red ball!" ,100,100);
- g.drawString("Click the mouse to place Neko's ball." ,100,120);
- g.drawString("Can you move the ball to keep Neko from getting it?",100,140);
- g.drawString("(Pull window larger to make the game easier)" ,100,160);
- gamePanel.addMouseListener(this); //call me!
- soundFile.loop();
- }
- //main
- public static void main(String args[]) {
- NekoTheCat Nyan = new NekoTheCat();
- Nyan.run();
- }
- @Override
- public void mouseClicked(MouseEvent me) {
- g.setColor(Color.white); // set to background color
- g.fillRect(ballxPosition, ballyPosition, ballSize, ballSize); //x,y,width,height
- ballxPosition = me.getX();
- ballyPosition = me.getY();
- g.drawImage(redBall,ballxPosition,ballyPosition,gamePanel); //draw ball at new click point
- // System.out.println("Mouse clicked at x=" + ballxPosition + ", y=" + ballyPosition);
- ballHasBeenPlaced = true;
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void run() {
- System.out.println("thread has entered run");
- // g.drawImage(catRight1,0,0,gamePanel);//imageName,x coordinate,y coordinate,where to draw
- // g.drawImage(catRight2,1*catWidth,0,gamePanel);
- // g.drawImage(catLeft1, 2*catWidth,0,gamePanel);
- // g.drawImage(catLeft2, 3*catWidth,0,gamePanel);
- // g.drawImage(redBall, 4*catWidth,0,gamePanel);
- while (true) {
- while ((catxPosition > 0) && (catxPosition < gamePanel.getSize().width))
- {
- // run Neko!
- // 1. Blank out the last image
- g.setColor(Color.white); // set to background color
- g.fillRect(catxPosition, catyPosition, ballSize, ballSize); //x,y,width,height
- // 2. Bump the location for the new image
- catxPosition += xBump;
- // 3. Select the next image.
- if (currentImage == cat1) currentImage = cat2;
- else currentImage = cat1;
- // 4. Draw the next cat image
- g.drawImage(currentImage,catxPosition,catyPosition,gamePanel); //draw ball at new click point
- // 5. Pause briefly to let human eye see the new image!
- try {Thread.sleep(sleepTime);}
- catch(InterruptedException ie){}
- // 6. If necessary, redirect the cat toward the ball.
- if (ballHasBeenPlaced) // first ensure that the ball is "in play"
- {
- // If cat is BELOW the ball
- // then move cat up 1 line.
- if (catyPosition > ballyPosition) catyPosition -= yBump;
- // If cat is ABOVE the ball
- // then move cat down one line.
- if (catyPosition < ballyPosition) catyPosition += yBump;
- // If the cat is running to the left
- // and the ball is to the right of the cat
- if (catIsRunningToTheLeft && (ballxPosition > catxPosition))
- reverseDirectionFromLeftToRight();
- // If the cat is running to the right
- // and the ball is to the left of the cat
- if (catIsRunningToTheRight && (ballxPosition < catxPosition))
- reverseDirectionFromRightToLeft();
- }
- // 7. Proximity test to see if Neko is "at" the ball.
- if ((Math.abs(catyPosition - ballyPosition) < 10) // y within 10
- && (Math.abs(catxPosition - ballxPosition) < 10)) // x within 10 pixels
- {
- // Take Neko-got-the-ball action!
- gamePanel.removeMouseListener(this);
- g.setColor(Color.red);
- g.setFont(new Font("Arial", Font.BOLD, 50));
- g.drawString("At last, I have my ball!",50,50);
- soundFile.stop();
- return;
- }
- }
- // turn Neko around.
- if (catxPosition > gamePanel.getSize().width)
- {
- reverseDirectionFromRightToLeft();
- catxPosition = gamePanel.getSize().width -1;
- }
- if (catxPosition < 0)
- {
- reverseDirectionFromLeftToRight();
- catxPosition = 1;
- }
- } //bottom of outer while(true) loop
- } //end of run() method
- private void reverseDirectionFromRightToLeft()
- {
- xBump = -xBump; // reverse increment
- cat1 = catLeft1;
- cat2 = catLeft2;
- catIsRunningToTheLeft = true;
- catIsRunningToTheRight = false;
- }
- private void reverseDirectionFromLeftToRight()
- {
- xBump = -xBump;
- cat1 = catRight1;
- cat2 = catRight2;
- catIsRunningToTheRight = true;
- catIsRunningToTheLeft = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement