Java 接球游戏
Posted
技术标签:
【中文标题】Java 接球游戏【英文标题】:Java catch the ball Game 【发布时间】:2015-08-23 11:13:46 【问题描述】:我的学校 Java 项目遇到问题。 计划是做一个简单的游戏,你需要接球,如果你接球,你就会得到分数。 目前我有两个问题:
我不知道如何让球以随机宽度出现并使其保持在该宽度(因为随机值不断变化)。 我如何做一个声明来检查接球手是否接住了球?这是我当前的代码:
import instruct.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.Timer;
public class opdracht extends WindowPanel implements MouseMotionListener
List<comet> comets;
Image afb1;
Image afb2;
Image comet;
int xmuis;
int score;
int random;
int h;
int plaats;
static int randomNum;
private static final int D_W = 700;
private static final int X_INC = 10;
public opdracht() throws IOException
score = 0;
h = -100;
afb1 = ImageIO.read(new File("afb/space.jpg"));
afb2 = ImageIO.read(new File("afb/pipe.png"));
BufferedImage cometbuf = ImageIO.read(new File("afb/comet.png"));
File output = new File("comet.png");
ImageIO.write(cometbuf, "png", output);
comet = ImageIO.read(new File("comet.png"));
addMouseMotionListener(this);
try
drawcomet();
catch (InterruptedException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
plaats = randomNum;
comets = new LinkedList<>();
Timer timer = new Timer(40, new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
Iterator<comet> it = comets.iterator();
while (it.hasNext())
comet ball = it.next();
if (ball.h > D_W)
it.remove();
System.out.println(comets.size());
else
ball.h += X_INC;
repaint();
);
timer.start();
public void paintComponent(Graphics g)
g.drawImage(afb1, 0, 0, 1200, 800, this);
g.setColor(Color.yellow);
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
g.drawString("score = " + score, 1020, 30);
for (comet ball : comets)
ball.drawcomet(g);
g.drawImage(afb2, xmuis, 730, 70, 75, this);
public static void randInt(int min, int max)
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
randomNum = rand.nextInt((max - min) + 1) + min;
System.out.print(randomNum);
public void drawcomet() throws InterruptedException
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable()
@Override
public void run()
comets.add(new comet(comet));
, 0, 2, TimeUnit.SECONDS);
public class comet
protected int h;
Image comet;
public comet(Image image)
comet = image;
public void drawcomet(Graphics g)
g.drawImage(comet, plaats, h, 75, 50, null);
public void mouseMoved(MouseEvent e)
xmuis = e.getX();
repaint();
public void mouseDragged(MouseEvent e)
// do something
public static void main(String[] a) throws IOException
new opdracht().createGUI();
package instruct;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WindowPanel extends JPanel
JFrame frame;
public WindowPanel()
this.setPreferredSize(new Dimension(1200, 800));
this.setFocusable(true);
this.requestFocusInWindow();
public void paintComponent(Graphics g)
super.paintComponent(g);
// System.out.println( "class: "+ getClass().getName() );
frame.setTitle("Space Game");
protected void createAndShowGUI()
frame = new JFrame();
frame.setSize(1200, 800);
frame.setLocation(300, 100);
frame.setResizable(false);
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
// Create a new blank cursor.
Cursor blankCursor =
Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0),
"blank cursor");
// Set the blank cursor to the JFrame.
frame.getContentPane().setCursor(blankCursor);
public void createGUI()
javax.swing.SwingUtilities.invokeLater(new Runnable()
public void run()
createAndShowGUI();
);
public JFrame getFrame()
return frame;
【问题讨论】:
哪个jar对应instruct
包(在第一个import语句中)? WindowPanel
是来自那个 jar 还是另一个第三方 jar 的类? createGUI
方法在哪里实现(在WindowPanel
类中)?
【参考方案1】:
第一个问题:“我不知道如何让球以随机宽度出现。”
我假设您想给球(=comet
类的实例)一个随机的 x 位置(=plaats
字段)?您可以进行以下更改(使 randomNum
字段不再需要,这现在可以是局部变量):
//plaats = randomNum;
plaats = randInt(0, 1200);
// more code...
//public static void randInt(int min, int max)
public static int randInt(int min, int max)
// more code...
return randomNum;
第二个问题:“如何做出检查接球手是否缓存球的语句。”
要确定球是否被接住,您可以在球的 y 坐标(h
字段?)等于管道顶部(大约 730)时比较 xmuis
和 plaats
。
【讨论】:
首先我要感谢你的帮助,但我认为我在问第一个问题时犯了一个小错误,因为我已经知道如何让它出现在随机位置但是我遇到的问题是plaats
的值在不断变化。女巫结果彗星一直在到处闪烁。我希望彗星的宽度是随机的,并且它在坠落时应该保持在这个宽度上。
您可以将plaats
设为comet
类的字段。这样,每颗彗星都有一个随机的 x 坐标,该坐标保持相同的值(对于特定的彗星)。
感谢您的帮助。我通过将 plaats
更改为 final int final int plaats = randInt(0,1200);
解决了这个问题,现在它就像魅力一样以上是关于Java 接球游戏的主要内容,如果未能解决你的问题,请参考以下文章
scratch接球游戏 电子学会图形化编程scratch等级考试三级真题和答案解析2022年3月