Friday, 24 January 2014

Write a Program to make AX-Ball Game

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class game extends Applet implements MouseMotionListener, Runnable
{
    int x = 0;
    int boxx = 0;
    int boxy = 0;
    Thread t;
    public void init()
    {
        t = new Thread(this);
        t.start();
        this.addMouseMotionListener(this);
    }
    
    public void paint(Graphics g)
    {
        g.setColor(Color.red);
        g.fillRect(x, 380, 100, 20);  
        g.setColor(Color.BLACK);
        g.fillRect(boxx, boxy, 50, 50);

    }
    public void mouseMoved(MouseEvent e)
    {
        x = e.getX();
        repaint();
    }
    public void mouseDragged(MouseEvent e){}
    public void run()
    {
        try
        {
            while(true)
            {
                boxx = new Random().nextInt(350);                 
                for(int i=0;i<=330;i+=5)
                {
                    boxy = i; 
                    repaint();
                    t.sleep(10);
                }
                if((x<boxx && x+100 > boxx) || (boxx<x && boxx+50>x) || (x<boxx && x+100 >boxx))
                {
                    System.out.println("Yippiee");
                }
                                           
                else
                {
                    setBackground(Color.red);
                    break;
                }
            }
        }
        catch(Exception e){System.out.println(e);}
    }
}
 

/*<applet code="game" width="400" height="400"></applet>*/


OUTPUT



0 comments:

Post a Comment

Followers