【问题标题】:I am trying to develop a game using java applet, the motion of a ball halts whole of my game?我正在尝试使用 java applet 开发游戏,球的运动会停止我的整个游戏?
【发布时间】:2013-09-13 14:50:01
【问题描述】:

这只是游戏的开始,这里有两个方块,一个可以用方向键控制,一个可以用鼠标控制,它们可以互相射击,同时可以保存,获得最大命中的将获胜... 在这段代码中,当我从第二个方格开火时,有一条长线指向第二个玩家,整个游戏必须停止..

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;
            }
        }
    }
}

【问题讨论】:

    标签: java applet thread-safety awt


    【解决方案1】:

    从不在绘画方法中有Thread.sleep(...)。曾经。这会让你所有的绘画都进入睡眠状态。事实上,简单地在你的 GUI 线程中调用 Thread.sleep(...) 就足以让 GUI 进入睡眠状态,但在绘制方法中更糟糕的是,因为必须一遍又一遍地调用该方法,并且需要在眨眼或更少。

    改为:

    • 创建 Swing JApplet,而不是 AWT Applet
    • 重写 JPanel 的 paintComponent 方法来进行绘图
    • 使用 Swing Timer 进行游戏循环。

    编辑
    您在评论中声明:

    @HovercraftFullOfEels 如果你能写出摇摆定时器和摇摆小程序的语法,那会很有帮助....

    您似乎希望我为您编写教程。我希望我有所有的时间来做这件事,但是唉,我没有,而且我觉得你和我都可以更有效地查看包含已经存在的示例代码的体面教程只等你学习。例如,请查看以下链接:

    【讨论】:

    • +1 看起来很模糊,你甚至需要在绘画方法中使用Thread.sleep(...),因为它严格用于绘画。此外,while 循环似乎也有点奇怪。
    • 因为我是java小程序的新手,对此不太了解...实际上我想在此代码中生成多个移动球...这将通过单击鼠标来实现...。如果可能的话,请帮助...thanx提前。 !!
    • @aka_007:对于具体的答案,您需要提出具体的问题。
    • @HovercraftFullOfEels 如果你能写出摇摆定时器和摇摆小程序的语法,那会很有帮助....
    • @HovercraftFullOfEels okaethanx.. !!
    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多