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

What is a proxy, A proxy is an object that is formed after applying advice ...

A proxy is an object that is formed after applying advice to a target object. When you think of client objects the target object and the proxy object are the similar.

What do you understand by java virtual machine, Java Programming 1. Wha...

Java Programming 1. What do you understand by Java Virtual Machine? 2. Write a simple Java program to display a string message and explain the steps of compilation and execu

Talent agency system, Talent agencies like ICM, CAA and Paradigm represent ...

Talent agencies like ICM, CAA and Paradigm represent writers, directors and actors (among other talent).  These agencies take in hundreds of millions of dollars a year collecting 1

Coding, code for error correction and detection using even odd parity

code for error correction and detection using even odd parity

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

Sum of six digits, How do I write a java program that enters a six digit nu...

How do I write a java program that enters a six digit number and takes the sum of the six digits?

What is constraints explain with example, What is Constraints explain with ...

What is Constraints explain with example? One of the purposes to use constructors and setter techniques rather than directly accessing fields is to enforce constraints. For exa

How we can changing the implementation, How we can changing the Implementat...

How we can changing the Implementation ? Suppose the Car class requires to be used in a simulation of New York City traffic in that each actual car on the street is represente

RatinalAssignment, 1. Make a class Rational to provide at least following m...

1. Make a class Rational to provide at least following methos and constructors: Rational r1= new Rational (3,5); Rational r2= new Rational (4,7); Rational r3=r1.add(r2);

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