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

Posting on a wall, Viewing a User's Wall Where your project allows you t...

Viewing a User's Wall Where your project allows you to view a user's profile, your ZHTML must provide the following capabilities: 1. Provide a Wall for that user profile, dis

Develop a desktop chatbot application, Develop a desktop Chatbot applicatio...

Develop a desktop Chatbot application Project Description: Looking for an in depth and smart working chatbot Seeking someone with skills working with AI and chatbots and t

Javacard, I want to know if you can help me with a javacard programming pro...

I want to know if you can help me with a javacard programming project

What is an abstract class in java, Question: (a) Java does not support ...

Question: (a) Java does not support multiple inheritance but does provide the concept of ‘interface'. Explain how interfaces can help a programmer to implement multiple inheri

Use the constructor, Can we use the constructor, instead of init(), to init...

Can we use the constructor, instead of init(), to initialize servlet? Ans) Yes. Of course you can use the constructor instead of init(). There's nothing to stop you. But you sho

Write a cgi program, Write a CGI program in R, accessible through the class...

Write a CGI program in R, accessible through the class web server, that performs an analysis or creates a graph. You can provide a separate HTML page that displays the initial form

What are the different parts of autoproxying, a)  BeanNameAutoProxyCreator ...

a)  BeanNameAutoProxyCreator b) DefaultAdvisorAutoProxyCreator c)  Metadata autoproxying

Exercise\servletexercise, Ask question # Write a servlet to display a table...

Ask question # Write a servlet to display a table that contains factorials for the numbers from 0 to 10,

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