我试图通过制作一个简单的小行星游戏来学习更多关于Java中的向量和旋转的知识。我一直在尝试使用Vector2d类,但我觉得我只能使用Points和double来完成它。我对Vector2d的使用是多余的吗?你会如何改变我的计划?
来源:
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.vecmath.Vector2d;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Asteroids extends JPanel implements Runnable{
BufferedImage img;
boolean up, left, right;
double angle = 0;
int imgw = 0;
int imgh = 0;
int magnitude = 0;
JFrame f;
ArrayList<Projectile> bullets;
Vector2d rocket = new Vector2d();
Vector2d Magnitude = new Vector2d();
AffineTransform at = new AffineTransform();
public Asteroids(JFrame f){
this.f = f;
setSize(900, 800);
try {
img = ImageIO.read(new File("res/rocket.png"));
} catch (IOException e) {}
imgw = img.getWidth();
imgh = img.getHeight();
bullets = new ArrayList<Projectile>();
f.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_UP:
up= true;
break;
case KeyEvent.VK_LEFT:
left = true;
right = false;
break;
case KeyEvent.VK_RIGHT:
right = true;
left = false;
break;
}
}
public void keyReleased(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_UP:
up= false;
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_SPACE:
Vector2d dir = new Vector2d(Math.cos(angle-Math.toRadians(90)),Math.sin(angle-Math.toRadians(90)));
bullets.add(new Projectile(dir,rocket));
break;
}
}
});
Thread t = new Thread(this);
t.start();
}
public static void main(String[] Args){
JFrame frame = new JFrame();
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
frame.getContentPane().setCursor(blankCursor);
frame.add(new Asteroids(frame));
frame.setVisible(true);
frame.setSize(900,500);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.fillRect(0,0,this.getWidth(),this.getHeight());
g2.drawImage(img,at,this);
g2.setColor(Color.red);
for(Projectile p: bullets){
p.draw(g2);
}
}
public void run(){
rocket = new Vector2d(100, 400);
Magnitude = new Vector2d(Magnitude = new Vector2d(magnitude*Math.cos(angle-Math.toRadians(90)),magnitude*Math.sin(angle-Math.toRadians(90))));
while(true){
if(left){
angle-=Math.toRadians(5);
}
if(right){
angle+=Math.toRadians(5);
}
if(up){
Vector2d m2 = new Vector2d(Math.cos(angle-Math.toRadians(90)),Math.sin(angle-Math.toRadians(90)));
m2.normalize();
m2.x/=5;
m2.y/=5;
Magnitude.add(m2);
}
if(angle>=Math.toRadians(360)){
angle-=Math.toRadians(360);
}else if(angle<=Math.toRadians(-360)){
angle+=Math.toRadians(360);
}
Vector2d rocketN = new Vector2d(rocket);
rocketN.normalize();
rocket.add(Magnitude);
System.out.println(Math.toDegrees(angle));
at.setToTranslation(rocket.x-img.getWidth()/2, rocket.y-img.getHeight()/2);
at.rotate(angle, imgw/2, imgh/2);
Magnitude.x*=0.99;
Magnitude.y*=0.99;
for(Projectile p: bullets){
p.update();
}
repaint();
try {Thread.sleep(25);
} catch (InterruptedException e) {}
}
}
public class Projectile{
int magnitude;
private Vector2d Magnitude, position;
public Projectile(Vector2d dir, Vector2d Position){
this.position = new Vector2d(Position);
magnitude = 10;
Magnitude = new Vector2d(dir);
position.x+=(5*Magnitude.x);
position.y+=(5*Magnitude.y);
Magnitude.x*=magnitude;
Magnitude.y*=magnitude;
}
public void update(){
position.add(Magnitude);
}
public void draw(Graphics2D g2){
g2.drawOval((int)position.x, (int)position.y,4,4);
}
}
}
目前没有小行星射击,你可以随身携带并射击。这是我使用的图像:http://findicons.com/files/icons/1495/space/32/rocket_ship.png
答案 0 :(得分:3)
我认为不是(回答你的问题)
但最重要的是KeyListener的使用错误,改为使用KeyBindings,示例为密钥UP, DOWN, LEFT, RIGHT
(我在Graphics2D上表现不佳)但是方法可能导致另一个问题
public void draw(Graphics2D g2){
检查这个thread如何使用Graphics2D移动,特别是@trashgod回答
答案 1 :(得分:0)
我从来没有听说过KeyBindings,总是使用VK_LETTER。在此之前,我使用了getKeyChar。我认为他们都工作正常。