gpt4 book ai didi

java - 我在 Asteroids 游戏中使用 Vector2d 类是多余的吗?

转载 作者:行者123 更新时间:2023-12-04 05:54:57 26 4
gpt4 key购买 nike

我正在尝试通过制作一个简单的小行星游戏来更多地了解 Java 中的 vector 和旋转。我一直在尝试 Vector2d 类,但我觉得我可以只使用 Points 和 doubles 来完成它。我对 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

最佳答案

  • 我认为不是(回答您的问题)
  • 但最重要的是 KeyListener 的用法有误, 使用 KeyBindings相反, key 示例 UP, DOWN, LEFT, RIGHT
  • (我不擅长 Graphics2D)但另一个问题可能是由方法引起的

    public void draw(Graphics2D g2){

  • 检查这个 thread如何使用 Graphics2D 移动,尤其是@trashgod 的回答

    关于java - 我在 Asteroids 游戏中使用 Vector2d 类是多余的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9619509/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com