我正在使用Ovals
和Graphics
对象用java编写一个简单的游戏。它被称为病毒,其作用是这样的:中间有一个椭圆形,外面有六个椭圆形。这些外部椭圆的大小应该会增加,直到点击为止,当它们消失时,玩家会得到10分。如果一个椭圆接触到了中心椭圆,那么中心椭圆的健康状况就会下降。当它达到零时,游戏结束。我遇到的问题是外部椭圆的大小不会增加。为什么会发生这种情况?
这是我的代码:
package virus;
import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class VirusGamePanel extends JPanel implements MouseListener{
private static final long serialVersionUID = 1L;//serialVersionUID field
Random colour = new Random();//the outside ovals will always be a random colour
private int sizeX = 1;//the x size of the outside ovals
private int sizeY = 1;//the y size of the outside ovals
int score = 0;
static String scorestring = "Score: ";
Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));//generate the random colour
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.magenta);
g.drawString(scorestring+score,275,250);
g.setColor(Color.orange);
g.drawOval(200,150,200,200);
g.setColor(rand);
g.drawOval(270,50,50,50);
g.drawOval(100,100,50,50);
g.drawOval(450,100,50,50);
g.drawOval(100,400,50,50);
g.drawOval(450,400,50,50);
g.drawOval(275,450,50,50);
g.fillOval(270,50,sizeX,sizeY);//these six ovals are supposed to increase in size
g.fillOval(100,100,sizeX,sizeY);
g.fillOval(450,100,sizeX,sizeY);
g.fillOval(100,400,sizeX,sizeY);
g.fillOval(450,400,sizeX,sizeY);
g.fillOval(275,450,sizeX,sizeY);
inc();
}
public static void main(String[] args) {
JPanel panel = new VirusGamePanel();
JFrame frame = new JFrame("Virus");
frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
private void inc()//increase the size of the ovals
{
for(int i = 0; i<25000; i++)
{
sizeX++;
sizeY++;
repaint();
}
}
带线程的新代码:
package virus;
import java.awt.*;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class VirusGamePanel extends JPanel implements MouseListener,Runnable{
private static final long serialVersionUID = 1L;
Random colour = new Random();
private int sizeX = 1;
private int sizeY = 1;
int score = 0;
Thread thr = new Thread();
static String scorestring = "Score: ";
Color rand = new Color(colour.nextInt(255),colour.nextInt(255),colour.nextInt(255));
public void paint(Graphics g)
{
super.paint(g);
thr.start();
g.setColor(Color.magenta);
g.drawString(scorestring+score,275,250);
g.setColor(Color.orange);
g.drawOval(200,150,200,200);
g.setColor(rand);
g.fillOval(270,50,sizeX,sizeY);
g.fillOval(100,100,sizeX,sizeY);
g.fillOval(450,100,sizeX,sizeY);
g.fillOval(100,400,sizeX,sizeY);
g.fillOval(450,400,sizeX,sizeY);
g.fillOval(275,450,sizeX,sizeY);
inc();
}
public static void main(String[] args) {}
private void inc()
{
thr.run();
}
public void run(){
for(int i = 0; i<25000; i++)
{
sizeX++;
sizeY++;
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
这对我有效:
public class VirusGamePanel extends JPanel{
private static final long serialVersionUID = 1L;//serialVersionUID field
Random colour = new Random();//the outside ovals will always be a random colour
private int sizeX = 0;//the x size of the outside ovals
private int sizeY = 0;//the y size of the outside ovals
int score = 0;
static String scorestring = "Score: ";
Color rand = new Color(colour.nextInt(255), colour.nextInt(255), colour.nextInt(255)); //generate the random colour
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.magenta);
g.drawString(scorestring+score,275,250);
g.setColor(Color.orange);
g.drawOval(200, 150, 200, 200);
g.setColor(rand);
g.fillOval(270 - sizeX / 2, 50 - sizeY / 2, sizeX, sizeY);//these six ovals are supposed to increase in size
g.fillOval(100 - sizeX / 2,100 - sizeY / 2, sizeX, sizeY);
g.fillOval(450 - sizeX / 2,100 - sizeY / 2, sizeX, sizeY);
g.fillOval(100 - sizeX / 2,400 - sizeY / 2, sizeX, sizeY);
g.fillOval(450 - sizeX / 2,400 - sizeY / 2, sizeX, sizeY);
g.fillOval(275 - sizeX / 2,450 - sizeY / 2, sizeX, sizeY);
inc();
}
public static void main(String[] args) {
JPanel panel = new VirusGamePanel();
JFrame frame = new JFrame("Virus");
frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
private void inc()//increase the size of the ovals
{
sizeX++;
sizeY++;
repaint();
}
}
只是固定了椭圆的中心点,并在inc
方法中删除了环。如果要绘制椭圆边界,只需添加参数与fillOval
命令相同的drawOval
命令即可。
编辑:
如果您想减缓增长过程,只需在paint
方法的inc()
调用之前添加以下内容:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
您的代码是递归的。在inc()中,您调用了重新绘制,它调用了VirusGamePanel.paint(),它调用inc(。。。
每次对paint()的调用都会重新调用paint(()方法。因此,不要在inc()中循环,而是执行以下操作:
private void inc()// increase the size of the ovals
{
sizeX++;
sizeY++;
repaint();
}
for(int i = 0; i<25000; i++)
{
sizeX++;
sizeY++;
repaint();
}
您的计算机无法像cpu增加sizeX和sizeY那样快速地重新绘制。添加一些以毫秒为单位的等待时间(尤其是如果你画了一个像800x600这样的大区域)
for(int i = 0; i<25000; i++)
{
sizeX++;
sizeY++;
repaint();
try{ sleep(100);} catch(InterruptedException e){}
}
但是,从另一个线程执行此操作!所以,你的应用程序。不会冻结。
这是10 FPS。但你尝试了2G FPS。你好。
下面是一个线程的示例。
class TEN_FPS extends Thread
{
public void run()
{
while(working)
{
//calculations here
repaint();
try{sleep(100);}catch(InterruptedException e){}
}
}
}
然后在主要方法:
working=true;
TEN_FPS.start();
当你完成这个程序:
working=false;
将释放您的守护进程线程。