标签:java graphics swing 2d-games
我正在使用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();
}
标签:java,graphics,swing,2d-games
来源: https://codeday.me/bug/20190826/1727138.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。