Program a pong game using rmi, JAVA Programming

Assignment Help:

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.
    }
}


Related Discussions:- Program a pong game using rmi

#, Consider the following code? What input is needed for x in order for the...

Consider the following code? What input is needed for x in order for the sum variable that is output at the end of the code to be 12 ? (In other words: what do I need to make X be

How can you define a readable program, How can you define a readable progra...

How can you define a readable program? A program that is easy to read & understand also easy to maintain and enhance. Readability is the ease in that text can be read and under

Need to develop web server, Project Description: I wish to prepare a Web...

Project Description: I wish to prepare a Web server which provides subsequent Web Services. 1. User login 2. User input data store to server 3. User fetches results fro

Midlet for Game, How to create a program using sprite sheet?

How to create a program using sprite sheet?

Describe in brief about the polymorphism, Describe Polymorphism? Polymo...

Describe Polymorphism? Polymorphism can be referred as one name many forms. It's the ability of methods to behave differently, depending upon object who is calling it. Key feat

What is a local member and a class variable, What is a local, member and a ...

What is a local, member and a class variable? Variables declared within a method are "local" variables. Variables declared within the class i.e not within any methods are "m

What is arithmetic promotion assignment and casting, What is Arithmetic pro...

What is Arithmetic promotion assignment and casting? In an assignment statement, i.e. if there's an equals sign, Java compares the type of the left hand side to the at last typ

Calculates whether a plane successfully lands or not, Write a program calle...

Write a program called LandThePlane that calculates whether a plane successfully lands or not.   The program begins by prompting (asking) the user for the following details, in th

Dialog window in Oracle by Java stored procedure, Is it possible to create ...

Is it possible to create any dialog window in Oracle in Java stored procedure?

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd