gpt4 book ai didi

java - 需要关于突破游戏球速的帮助

转载 作者:行者123 更新时间:2023-11-30 04:29:59 25 4
gpt4 key购买 nike

这是一个 12 年级的面向对象编程项目。

我有一个名为 Ball 的类来构造我的球对象。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Ball{
private double xPos;
private double yPos;
private int direction;
public Ball(int ixPos, int iyPos, int idirection){
xPos = ixPos;
yPos = iyPos;
direction = idirection;
}
public int returnX(){
return (int)xPos;
}
public int returnY(){
return (int)yPos;
}
public int returnDirection(){
return direction;
}
public void move(){
xPos += 1*Math.cos(Math.toRadians(direction));
yPos -= 1*Math.sin(Math.toRadians(direction));
}
public void collide(int collideWith){
if(collideWith==1){//collide with left wall
if(90<direction && direction<180){
direction = 180-direction;
}
if(180<direction && direction<270){
direction = 540-direction;
}
}
if(collideWith==2){//collide with right wall
if(0<direction && direction<90){
direction = 180-direction;
}
if(270<direction && direction<360){
direction = 540-direction;
}
}
if(collideWith==3){//collide with up wall
if(0<direction && direction<90){
direction = 360-direction;
}
if(90<direction && direction<180){
direction = 360-direction;
}
}
if(collideWith==4){//collide with down wall
direction = 360-direction;
}
}
public void collidePaddle(int collidePos){
if(collidePos!=50 && collidePos!=0){
direction = (50-collidePos)*180/50;
}
}
}

正如您在“移动”功能中看到的,现在球的移动速度非常低。但我需要球跑得更快。如果我把1改成5之类的,就会有问题。在我的主类中,它检查球是否撞到墙壁或方 block 以改变方向,如果球每次可以移动的像素量大于 1,球就会进入墙壁或方 block 。

对我来说,似乎没有办法解决这个问题,不知道从哪里开始思考,是否有更好的方法来检查碰撞或其他什么?

谢谢。

最佳答案

您应该允许范围,而不是在 collidePaddle 检查中使用绝对检查。

例如...

public void collidePaddle(int collidePos){
if (collidePos >= 50) {
direction = (50-collidePos)*180/50;
// Correct the position of the ball to meet the minimum requirements
// of the collision...
} else if (collidePos <= 0) {
direction = (50-collidePos)*180/50;
// Correct the position of the ball to meet the minimum requirements
// of the collision...
}
}

(抱歉,我很高兴编写您的代码;))

这将允许球在虚拟环境中“传球”超出这些点,但是如果您纠正位置以进行补偿,那么在渲染时应该没有什么区别...

已更新

这是我正在谈论的一个非常简单的例子......

public class SimpleBouncyBall {

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

public SimpleBouncyBall() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CourtPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class CourtPane extends JPanel {

private Ball ball;
private int speed = 5;

public CourtPane() {
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle bounds = new Rectangle(new Point(0, 0), getSize());
if (ball == null) {
ball = new Ball(bounds);
}
speed = ball.move(speed, bounds);
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (ball != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Point p = ball.getPoint();
g2d.translate(p.x, p.y);
ball.paint(g2d);
g2d.dispose();
}
}

}

public class Ball {

private Point p;
private int radius = 12;

public Ball(Rectangle bounds) {
p = new Point();
p.x = 0;
p.y = bounds.y + (bounds.height - radius) / 2;
}

public Point getPoint() {
return p;
}

public int move(int speed, Rectangle bounds) {
p.x += speed;
if (p.x + radius >= (bounds.x + bounds.width)) {
speed *= -1;
p.x = ((bounds.x + bounds.width) - radius) + speed;
} else if (p.x <= bounds.x) {
speed *= -1;
p.x = bounds.x + speed;
}
p.y = bounds.y + (bounds.height - radius) / 2;
return speed;
}

public void paint(Graphics2D g) {
g.setColor(Color.RED);
g.fillOval(0, 0, radius, radius);
}
}
}

我的移动方法并不关心您是否越过了边界,因为它会将球重新定位到这些边界内

public int move(int speed, Rectangle bounds) {                
// Apply the delta
p.x += speed;
// Have we passed beyond the right side??
if (p.x + radius >= (bounds.x + bounds.width)) {
speed *= -1;
p.x = ((bounds.x + bounds.width) - radius) + speed;
// Have we past beyond the left side??
} else if (p.x <= bounds.x) {
speed *= -1;
p.x = bounds.x + speed;
}
p.y = bounds.y + (bounds.height - radius) / 2;
return speed;
}

尝试一下速度,看看你会得到什么;)

关于java - 需要关于突破游戏球速的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866580/

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