gpt4 book ai didi

java - 玩家对象向一个方向移动但转向不同的方向

转载 作者:行者123 更新时间:2023-12-01 17:07:54 25 4
gpt4 key购买 nike

当游戏第一次开始时,玩家会生成,朝方向 0(右)看。按右键(vk_right)将玩家 Sprite 向左旋转,但方向被设置为将玩家发送到(看起来)正确的方向。左右键改变角色的方向变量,而上/下键则加速/减速角色。

我不太擅长三角学,所以我可能到处都有一些错误(这就是我发布此内容的原因)。我似乎无法理解如何让角色朝他“看”的方向移动(方向变量)。

这是 Player.java 类:

package rpg2d;

import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;

public class Player implements KeyListener{

private double x,y,direction;
private double speed;
private final int max_speed;
private int hp,lives;
private Image img;
//other variables

public Player(int x, int y, int hp, int lives, String imgpath) {
this.x = x;
this.y = y;
this.hp = hp;
this.lives = lives;
img = new ImageIcon(this.getClass().getResource(imgpath)).getImage();
//loads the player image from the string path
max_speed = 6;
speed = 0;
direction = 0;
}

//returns the direction the player is 'facing' as an int
public int getDirection() {
return (int)direction;
}

//updates the player's location
public void move() {
x += speed * Math.cos(-1*direction);
y += speed * Math.sin(-1*direction);
}

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
turn(.6);
//this is meant to turn the player int a clockwise direction by 0.6
} else if (key == KeyEvent.VK_RIGHT){
turn(-.6);
//counterclockwise by 0.6
}
if (key == KeyEvent.VK_DOWN){
speed -= 0.3;
if(speed < 0) speed = 0;
//decelerate until stopped
} else if (key == KeyEvent.VK_UP){
speed += 0.3;
if(speed > max_speed) speed = max_speed;
//accelerates until it hits maximum speed
}
}

private void turn(double degrees) {
direction += degrees;
if(direction > 180) direction -= 180;
else if(direction < -180) direction += 180;
/* I honestly don't know whether 180 and -180 are the max and min
* for Polar Coordinates, so this could be a problem.
*/
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent arg0) {
}

public int getX() {
return (int)x;
}

public int getY() {
return (int)y;
}

public double getWidth() {
return img.getWidth(null);
}

public double getHeight() {
return img.getHeight(null);
}

public Image getImg() {
return img;
}

}

这是主要的游戏类:

package rpg2d;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

boolean gameRunning = true;
Graphics2D g;
JFrame frame;
JPanel screen;
Player player;

public static void main(String[] args) {
new Main();
}

@SuppressWarnings("serial")
public Main() {
frame = new JFrame("2D RPG Test");
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

player = new Player(10,10,100,3,"/img/player.png");
screen = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
AffineTransform trans = new AffineTransform();
AffineTransform old = new AffineTransform();
trans.setToIdentity();
trans.rotate(Math.toRadians(player.getDirection()),player.getWidth()/2,player.getHeight()/2);
g2d.setTransform(trans);
g2d.drawImage(player.getImg(), player.getX(), player.getY(), (int)player.getWidth(), (int)player.getHeight(), null);
g2d.setTransform(old);
g2d.drawString("X: " + player.getX() + " Y: " + player.getY(), 5,10);
trans.setToIdentity();
}
};

frame.add(screen);
frame.setVisible(true);
screen.addKeyListener(player);
screen.requestFocus();
Thread t = new Thread(new Runnable() {
public void run() {
gameLoop();
}
});
t.setDaemon(false);
t.start();
}

public void update() {
player.move();
}

public void gameLoop() {
final int FPS = 60;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
update();
screen.repaint();
}
}, 0, FPS);
}
}

最佳答案

我做了一些改进:

  1. 在计算 Cos 和 Sin 时,必须将度数转换为弧度,因为这些函数采用弧度(弧度是度数的不同单位)。
  2. 我更改了左右键的 - 和 +。按照惯例,逆时针方向增加,而不是相反;)
  3. 我修改了turn方法,因为它有一些轻微的错误(-180在三角函数中是0)

如果有更多错误请评论,我会再看一下:)祝你好运!

public void move() {
x += speed * Math.cos(Math.toRadians(direction));
y += speed * Math.sin(Math.toRadians(direction));
}

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
turn(-.6);
//this is meant to turn the player int a clockwise direction by 0.6
} else if (key == KeyEvent.VK_RIGHT){
turn(.6);
//counterclockwise by 0.6
}
.....//rest of code}

private void turn(double degrees) {
direction += degrees;
if(direction > 180) direction = 180;
else if(direction < 180) direction = 0;
}

关于java - 玩家对象向一个方向移动但转向不同的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24565475/

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