だから私はJavaで非常にシンプルなゲームを作ろうとしています。プログラム全体を1つのクラスで作成することで成功を収めてきましたが、オブジェクト指向の原則を追加しようとすると、すべてが崩壊します。私が欲しいのは、多かれ少なかれ砲塔である私の善良な人から弾丸を発射することです。このようなものは簡単に設定できますが、クラスを使用して善良な人を表現しようとすると、すべてがバラバラになります。これが私がこれまでに持っているものです。メインプログラムフレームを設定します。
import javax.swing.JFrame;
public class Shooter
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Car");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ShooterPanel panel = new ShooterPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
ShooterPanelクラス:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ShooterPanel extends JPanel
{
final int WIDTH = 200, HEIGHT = 100;
GoodGuy hero;
public ShooterPanel()
{
hero = new GoodGuy(0, HEIGHT-20, 20);
addKeyListener(new MoveListener());
setPreferredSize(new Dimension (WIDTH, HEIGHT));
setFocusable(true);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
hero.draw(page);
}
private class MoveListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_SPACE:
hero.shoot();
break;
}
}
//--------------------------------------------------------------
//Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
私たちの主人公:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GoodGuy
{
private int x, y, size;
private Bullet bullet;
public GoodGuy(int x, int y, int height)
{
size = height;
this.x = x;
this.y = y;
}
public int getSize()
{
return size;
}
public void draw(Graphics page)
{
page.drawRect(x, y, size, size);
}
public void shoot()
{
bullet = new Bullet(x+size, y);
}
}
そして彼の弾丸:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class Bullet
{
private int x, y;
final int LENGTH = 5, DELAY =200;
private Timer timer;
Graphics page1;
public Bullet(int x, int y)
{
this.x = x;
this.y = y;
timer = new Timer(DELAY, new BulletListener());
timer.start();
}
public void draw(Graphics page)
{
page.drawLine (x, y, x+LENGTH, y);
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
private class BulletListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x +=5;
draw(page1);
}
}
}
私がしたいのは、スペースバーを押して、善良な人が弾丸を表示し、画面を右に移動することです。弾丸をまったく描くことができません。私はいくつかのことを試みましたが、すべて無駄になりました。これはとても簡単だと思います。私が言ったように、オブジェクトを作成するのではなく、変数を使用して弾丸を描画することで、ShooterPanelクラスでこれをすべて行うことができますが、オブジェクト指向にしたいと思います。誰かが私のための解決策を持っていますか?助けていただければ幸いですが、解決策を探しています。漠然とした提案をするつもりなら、私は返答を望まないでしょう。編集:これが私のプログラムをどのように機能させたいかです:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class ShooterPanel extends JPanel
{
final int WIDTH = 200, HEIGHT = 100;
GoodGuy hero;
ArrayList<BadGuy> enemies = new ArrayList<BadGuy>();
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
private Timer timer, bulletTimer;
final int DELAY = 200;
int count=0;
private boolean bulletOut = false;
public ShooterPanel()
{
hero = new GoodGuy(0, HEIGHT-20, 20);
addKeyListener(new MoveListener());
timer = new Timer(DELAY, new EnemyListener());
bulletTimer = new Timer(100, new BulletListener());
setPreferredSize(new Dimension (WIDTH, HEIGHT));
//enemies.add(new BadGuy(WIDTH-10, HEIGHT-10));
setFocusable(true );
timer.start();
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
hero.draw(page);
for (int i = 0; i < enemies.size(); i++)
{
enemies.get(i).draw(page);
}
if (bulletOut)
for(Bullet bullet: bullets)
bullet.draw(page);
}
private class MoveListener implements KeyListener
{
//--------------------------------------------------------------
//Responds to the user pressing arrow keys by adjusting the
//image and image location accordingly.
//--------------------------------------------------------------
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_SPACE:
bullets.add(new Bullet(20,95));
bulletOut = true;
bulletTimer.start();
break;
}
}
//--------------------------------------------------------------
//Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
private class EnemyListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
if (count%20==0)
enemies.add(new BadGuy(WIDTH-10, HEIGHT-10));
for (BadGuy enemy: enemies)
{
enemy.move();
}
count++;
repaint();
}
}
private class BulletListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
for(Bullet bullet: bullets)
bullet.move();
for(int i = 0; i < bullets.size(); i++)
{
if (enemies.size() != 0)
{
if (bullets.get(i).getX() > enemies.get(0).getX())
{
bullets.remove(i);
enemies.remove(0);
}
}
if (bullets.size() != 0)
{
if (bullets.get(i).getX()>WIDTH)
bullets.remove(i);
}
}
repaint();
}
}
}
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GoodGuy
{
private int x, y, size;
private Bullet bullet;
public GoodGuy(int x, int y, int height)
{
size = height;
this.x = x;
this.y = y;
}
public int getSize()
{
return size;
}
public void draw(Graphics page)
{
page.drawRect(x, y, size, size);
}
public void shoot()
{
bullet = new Bullet(x+size, y);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BadGuy
{
private int x, y;
final int RADIUS = 5;
public BadGuy(int x, int y)
{
this.x = x;
this.y = y;
}
public void move()
{
x -= 5;
}
public void draw(Graphics page)
{
page.drawOval(x, y, RADIUS*2, RADIUS*2);
}
public int getX()
{
return x;
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class Bullet
{
private int x, y;
final int LENGTH = 5, DELAY =200;
public Bullet(int x, int y)
{
this.x = x;
this.y = y;
}
public void draw(Graphics page)
{
page.drawLine (x, y, x+LENGTH, y);
}
public void move()
{
x += 5;
}
public int getX()
{
return x;
}
}
シュータークラスは同じままです。これは私がプログラムに実行させたいことですが、それは私がプログラムに実行させたい方法ではありません。もっとオブジェクト指向で、各クラスが自分でやってくれたらいいのにと思います。ShooterPanelはすでに私の好みのために少し複雑になり始めており、プログラムに追加されるにつれて悪化するだけです。私が変更したいことの1つは、BulletクラスでActionListenerが定義された、各Bulletに独自のタイマーを設定することです。ただし、タイマーがイベントを発生させるたびに弾丸が再描画されるように、これを行う方法を理解することはできません。誰かがそうする方法を知っているなら、私に知らせてください。