Java 创建一个对象的多个实例。(游戏)
我被要求为我的CS(高中)班制作一个游戏,作为我的期末作业。我们没有正确地学习制作游戏所需的所有编码,所以我在这方面的知识非常贫乏。不管怎么说,我正在尝试制作的游戏类似于“Flappy Fall”(一款苹果应用商店游戏),其中对象从屏幕顶部掉落,然后下降到屏幕底部。目标是在这些物体到达底部之前捕捉它们。我能够让一个物体坠落,并且还创建了“捕手”,但我不知道如何创建多个坠落物体,也不知道一旦物体被捕手抓住后如何移除。到目前为止,我有“JavaGame”、“Catcher”和“Ball”等课程。任何帮助都将不胜感激Java 创建一个对象的多个实例。(游戏),java,Java,我被要求为我的CS(高中)班制作一个游戏,作为我的期末作业。我们没有正确地学习制作游戏所需的所有编码,所以我在这方面的知识非常贫乏。不管怎么说,我正在尝试制作的游戏类似于“Flappy Fall”(一款苹果应用商店游戏),其中对象从屏幕顶部掉落,然后下降到屏幕底部。目标是在这些物体到达底部之前捕捉它们。我能够让一个物体坠落,并且还创建了“捕手”,但我不知道如何创建多个坠落物体,也不知道一旦物体被捕手抓住后如何移除。到目前为止,我有“JavaGame”、“Catcher”和“Ball”等课程。任何
int x, y;
int t = 1;
private Image dbImage;
private Graphics dbGraphics;
Image player;
Image bkg;
static Catcher p = new Catcher(150, 450);
public JavaGame() {
//Game Images
ImageIcon b = new ImageIcon("bkg.png");
bkg = b.getImage();
//Game properties
setTitle("Game");
setSize(350, 600);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addKeyListener(new Keys());
addMouseMotionListener(new Mouse());
}
public void paint(Graphics g) {
//
dbImage = createImage(getWidth(), getHeight());
dbGraphics = dbImage.getGraphics();
draw(dbGraphics);
g.drawImage(dbImage, 0, 0, this);
}
public void draw(Graphics g) {
g.drawImage(bkg, 0, 0, this); //Creates background
p.draw(g);
//while (t < 100) {
p.b.draw(g);
//t++;
//}
g.setColor(Color.WHITE);
g.drawString(""+p.score, 175, 50);
repaint();
}
public static void main(String[] args) {
JavaGame jg = new JavaGame();
//Threads
Thread p1 = new Thread(p);
p1.start();
Thread ball = new Thread(p.b);
ball.start();
}
public class Keys extends KeyAdapter {
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
}
public class Mouse implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
p.mouseDragged(e);
}
public void mouseMoved(MouseEvent e) {
p.mouseMoved(e);
}
}
}由于我不想做你的作业,我只给出一个简短的回答: 通过多次调用
newball()
。
当然,您可能希望将它们添加到集合中,如
列表=新的ArrayList();
列表。添加(球)
完成后将它们从集合中删除。我会稍微重新组织代码。在主游戏中,您可以拥有一组“球”类型。我会让你选择收款。但你会想把“新”的球添加到集合中,然后在它们被抓到后将其移除
int x, y, ranX, xDirection;
int score;
Rectangle catch1;
Ball b = new Ball(170, 1);
public Catcher (int x, int y) {
score = 0;
this.x = x;
this.y = y;
catch1 = new Rectangle(this.x, this.y, 50, 15);
}
public void run() {
try {
while(true) {
move();
Thread.sleep(5);
}
}
catch(Exception e) {
System.out.println("Error");
}
}
public void collision() {
if (b.ball.intersects(catch1)) {
b.ball(Color.blue);
score++;
System.out.println(score);
}
}
public void move() {
collision();
catch1.x += xDirection;
if (catch1.x <= 0)
catch1.x = 0;
if (catch1.x >= 300)
catch1.x = 300;
}
public void setXDirection(int xDir) {
xDirection = xDir;
}
public void keyPressed(KeyEvent m) {
int keyCode = m.getKeyCode();
if (keyCode == m.VK_LEFT) {
setXDirection(-1);
}
if (keyCode == m.VK_RIGHT) {
setXDirection(+1);
}
m.consume();
}
public void keyReleased(KeyEvent m) {
int keyCode = m.getKeyCode();
if (keyCode == m.VK_LEFT) {
setXDirection(0);
}
if (keyCode == m.VK_RIGHT) {
setXDirection(0);
}
m.consume();
}
public void mouseDragged(MouseEvent e) {
catch1.x = e.getX()-25;
e.consume();
}
public void mouseMoved(MouseEvent e) {
catch1.x = e.getX()-25;
e.consume();
}
public void draw(Graphics g) {
g.fillRect(catch1.x, catch1.y, catch1.width, catch1.height);
}
int x, y, yDirection;
Rectangle ball;
public Ball (int x, int y) {
this.x = x;
this.y = y;
ball = new Rectangle(this.x, this.y, 10, 10);
}
public void run() {
try{
while(true) {
move();
Thread.sleep(5);
}
}
catch(Exception e) {
System.out.println("Error");
}
}
public void move() {
if (ball.y >= 600) {
ball.y = 600;
}
if (ball.y > 0) {
ball.y++;
}
}
public void setYDirection(int yDir) {
yDirection = yDir;
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(ball.x, ball.y, ball.width, ball.height);
System.out.println(ball.x+ " "+ ball.y+ " " + ball.width + " " + ball.height);
}