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

Develop a shopping carts application , To develop a shopping carts applicat...

To develop a shopping carts application for an online store of your choice Outcomes: 1. Apply the GUI components of Java and other tools to create user-friendly interfaces.

Want a script developer, I want a Script developer who will make script for...

I want a Script developer who will make script for me and if you can show good works there are bonuses too. Skills required are Facebook Marketing, Java, Javascript, PHP, and Sc

What is a portal?, A portal is a service or a Web site that gives broad ran...

A portal is a service or a Web site that gives broad range of services and resources like e-mail, forums, search engines, weather information, news, on-line shopping, stock quotes.

Design a game in java, Ten pigs were all in a pen, enjoying their morning s...

Ten pigs were all in a pen, enjoying their morning slop when a bird flew down from a nearby tree and began picking all the best bits out from the trough. This greatly perturbed the

Websphere - weblogic, WebSphere - Weblogic and Apache: Building/Ins...

WebSphere - Weblogic and Apache: Building/Installation  and maintenance of Solaris and Linux machines. Exposure to Sun Hardware : Sunfire v880,v890.V440,X4600

PadovanString, write a program that counts the number of occurrences of the...

write a program that counts the number of occurrences of the string in the n-th Padovan string P(n).   program in java // aakash , suraj , prem sasi kumar kamaraj college

Array, public class Tester { static void test(int[] a) { int[] b = new in...

public class Tester { static void test(int[] a) { int[] b = new int[2]; a = b; System.out.print(b.length); System.out.print(a.length); } public static void main(String[] arg

Error, UnsupportedClassVersionError

UnsupportedClassVersionError

Java web programming, Write a JSP to display a table that contains factoria...

Write a JSP to display a table that contains factorials for the numbers from 0 to 10,

Java program to do the following operations:, Read information of n student...

Read information of n students. The class name should be kkhsou with the member variable name, rollno, center_code and percentage in last examestion.Display the students data whose

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