Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Using RMI, program a PONG game that enables two users to play against one another.

Pong class:

package graphics;/*
* Copyright (c) 2006, Your Corporation. All Rights Reserved.
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Pong extends JApplet
        implements Runnable, MouseMotionListener, MouseListener {

    Thread runner;
    Image offscreeni;
    Graphics offscreeng;
    Rectangle plane;
    Point ballPoint, racketPoint, enemyPoint, ballSpeed;
    boolean start, death = false;
    int playerScore, enemyScore = 0;
    int tuffHit = 8;

    public void init() {
        addMouseListener(this);
        addMouseMotionListener(this);
        final int width = this.getSize().width;
        final int height = this.getSize().height;
        offscreeni = createImage(width, height);
        offscreeng = offscreeni.getGraphics();
        setBackground(Color.black);
        ballPoint = new Point((width / 2), (height / 2));
        racketPoint = new Point((width - 35), ((height / 2) - 25));
        enemyPoint = new Point(35, ((height / 2) - 25));
        plane = new Rectangle(15, 15, width, (height - 30));
        ballSpeed = new Point(0, 0);
        repaint();
    }

    public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void run() {
        while (true) {
            checkRacket();
            checkEnemy();
            checkWalls();
            moveBall();
           moveEnemy(enemyPoint.y + 25);
            repaint();
            sleep();
        }

    }

    private void sleep() {
        try {
            Thread.sleep(35);
            //lower this number to improve speed.
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    void moveBall() {
        ballPoint.x = (ballPoint.x + ballSpeed.x);
        ballPoint.y = (ballPoint.y + ballSpeed.y);
    }

    void moveEnemy(int enemyPos) {
        int dist = Math.abs(ballPoint.y - enemyPos);
        final int y = enemyPoint.y;
        if (ballSpeed.x < 0) {
            if (enemyPos < (ballPoint.y - 3)) enemyPoint.y = (y + dist / tuffHit);
            else if (enemyPos > (ballPoint.y + 3)) enemyPoint.y = (y - dist / tuffHit);
        } else {
            if (enemyPos < (this.getSize().height / 2 - 3))
                enemyPoint.y = (y + 2);
            else if (enemyPos > (this.getSize().height / 2 + 3))
                enemyPoint.y = (y - 2);
        }
    }

    void checkRacket() {
        if (ballSpeed.x < 0) return;

        if ((ballPoint.x + ballSpeed.x) >= racketPoint.x - 6 &
                (ballPoint.x < racketPoint.x))
            if ((ballPoint.y + 8) > racketPoint.y & ballPoint.y < (racketPoint.y + 50)) {
                int racketHit = (ballPoint.y - (racketPoint.y + 25));
                ballSpeed.y = (ballSpeed.y + (racketHit / 7));
                ballSpeed.x = (ballSpeed.x * -1);
            }
    }

    void checkEnemy() {
        if (ballSpeed.x > 0) return;
        if ((ballPoint.x + ballSpeed.x) <= enemyPoint.x + 4 & (ballPoint.x > enemyPoint.x))
            if ((ballPoint.y + 8) > enemyPoint.y & ballPoint.y < (enemyPoint.y + 50)) {
                int racketHit = (ballPoint.y - (enemyPoint.y + 25));
                ballSpeed.y = (ballSpeed.y + (racketHit / 7));
                ballSpeed.x = (ballSpeed.x * -1);
            }
    }

    void checkWalls() {
        final int x = ballSpeed.x;
        if ((ballPoint.x + x) <= plane.x) miss();

        if ((ballPoint.x + x) >= (plane.width - 20)) miss();
        final int y = ballSpeed.y;
        if ((ballPoint.y + y) <= plane.y) ballSpeed.y = (y * -1);
        if ((ballPoint.y + y) >= (plane.height + 8)) ballSpeed.y = (y * -1);

    }

    void miss() {
        if (ballSpeed.x < 0) {
            playerScore = (playerScore + 1);
            if (tuffHit > 2) tuffHit = (tuffHit - 1);
        } else enemyScore = (enemyScore + 1);
        ballSpeed.x = (ballSpeed.x * -1);
        ballPoint.x = (ballPoint.x + ballSpeed.x);

        for (int i = 3; i > 0; i = (i - 1)) {
            death = true;
            repaint();
            try {
                Thread.sleep(300);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            death = false;
            repaint();
            try {
                Thread.sleep(300);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        ballPoint = new Point((this.getSize().width / 2),
                (this.getSize().height / 2));
        ballSpeed.x = 0;
        ballSpeed.y = 0;
        start = false;

    }

    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        offscreeng.setColor(Color.black);
        offscreeng.fillRect(0, 0, this.getSize().width, this.getSize().height);

        if (!death) offscreeng.setColor(Color.white);
        else offscreeng.setColor(Color.lightGray);
        offscreeng.drawString(Integer.toString(enemyScore), 100, 35);
        offscreeng.drawString(Integer.toString(playerScore), 215, 35);
        offscreeng.clipRect(plane.x, plane.y, plane.width - 28, plane.height + 1);
        offscreeng.drawRect(plane.x, plane.y, plane.width - 30, plane.height);
        offscreeng.fillRect(racketPoint.x, racketPoint.y, 6, 50);
        offscreeng.fillRect(enemyPoint.x, enemyPoint.y, 6, 50);
        offscreeng.fillOval(ballPoint.x, ballPoint.y, 8, 8);
        g.drawImage(offscreeni, 0, 0, this);

    }

    public void mouseDragged(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseMoved(MouseEvent e) {
            racketPoint.y = (e.getY() - 25);
            repaint();
    }

    public void mouseClicked(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mousePressed(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseReleased(MouseEvent e) {
         if (!start) {
            ballSpeed.x = 4;
            ballSpeed.y = 2;
            start = true;
        }
    }

    public void mouseEntered(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void mouseExited(MouseEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

Java, Programming

  • Category:- Java
  • Reference No.:- M9523124

Have any Question?


Related Questions in Java

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As