简体   繁体   中英

I am trying to develop a game using java applet, the motion of a ball halts whole of my game?

This is just the begining of the game, where there are two squares, one can be controlled by arrow keys and other by mouse, they can fire balls on each other and simultaneously can be saved, the one getting maximum hits will win... In this code when I fire from the second square there is a long line which goes towards the second player and whole of the game has to halt..

package raship;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.io.IOException;

public class raship extends Applet implements KeyListener, MouseMotionListener, MouseListener, Runnable
{

    int width,flag1=0,flag2=0,height,x1,y1,x2,y2,calc1,calc2x,calc2y;

    Thread t=null;

    public void init()

    {
        //Toolkit toolkit=Toolkit.getDefaultToolkit();
        t=new Thread();
        width=getSize().width;
        height=getSize().height;
        x1=0;y1=height/2;
        x2=width-10;y2=height/2;
        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        setBackground(Color.gray);
        repaint();
    }
    public void keyPressed(KeyEvent e) 
    {
        int c=e.getKeyCode();
        System.out.println(c);
        if(c==KeyEvent.VK_LEFT)
        {
            System.out.println("yeah it's on");
            x1-=10;
        }
        else if(c==KeyEvent.VK_UP)
            y1-=10;
        else if(c==KeyEvent.VK_RIGHT)
            x1+=10;
        else if(c==KeyEvent.VK_DOWN)
            y1+=10;
        if(x1>=0 && y1>=0 && y1<=height-20 && x1<=3*width/4)
            repaint();
    }
    public void keyReleased(KeyEvent arg0) {

    }
    public void keyTyped(KeyEvent arg0) {

    }
    public void mouseDragged(MouseEvent e) {

    }
    public void mouseMoved(MouseEvent e) 
    {
        x2=e.getX();
        y2=e.getY();
        if(x2>=5*width/8 && x2<=width-20)
            repaint();
    }
    public void mouseClicked(MouseEvent e) 
    {
        flag2=1;
        calc2x=x2;
        calc2y=y2;
        System.out.println(calc2x);
    }
    public void mouseEntered(MouseEvent arg0) {

    }
    public void mouseExited(MouseEvent arg0) {

    }
    public void mousePressed(MouseEvent arg0) {

    }
    public void mouseReleased(MouseEvent arg0) {

    }
    public void paint(Graphics g)
    {
        width=getSize().width;
        height=getSize().height;
        g.setColor(Color.green);
        g.fillRect(x1, y1, 20, 20);
        g.setColor(Color.red);
        g.fillRect(x2, y2, 20, 20);
        if(flag2==1)
        {
            g.setColor(Color.yellow);
            while(true)
            {
                calc2x-=1;
                System.out.println(calc2x); 
                g.fillOval(calc2x,calc2y,10,10);
                try {
                    Thread.sleep(4);
                } catch (InterruptedException e) {e.printStackTrace();}
                if(calc2x<10)
                {
                    flag2=0;
                    break;
                }
            }
        }
    }
    @SuppressWarnings("static-access")
    public void run()
    {
        if(flag2==1)
        while(true)
        {
            {
                repaint();
                System.out.println("calc2x="+calc2x);
                if(calc2x<10)
                {
                    flag2=0;
                }
                try 
                {
                    t.sleep(4);
                } catch (InterruptedException e) {e.printStackTrace();}
                calc2x-=1;
            }
        }
    }
}

NEVER have Thread.sleep(...) in a paint method. EVER. This puts all your drawing to sleep. In fact simply calling Thread.sleep(...) in your GUI thread will be enough to put the GUI to sleep, but its worse still in a paint method, since that method must be called over and over, and needs to be blazing fast and over in the blink of an eye or less.

Instead:

  • Create a Swing JApplet, not an AWT Applet
  • Override the paintComponent method of a JPanel to do your drawing
  • Use a Swing Timer to do your game loop.

Edit
You state in comment:

@HovercraftFullOfEels if you can write the syntax of swing timer and swing applet it would be of great help....

You appear to be wanting me to write tutorials for you. I wish I had all the time to do that, but alas, I don't, and I feel that it would be much more efficient for both you and me for you to check out the decent tutorials with sample code that are already in existence just waiting for you to learn from. For example, please check out the following links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM