Java壁球/网球游戏开发求助:难度调整与球拍拖拽实现
解决你的Java壁球游戏两个问题:动态调整球拍尺寸 + 鼠标拖拽移动
我来帮你搞定这两个需求,先从调整球拍尺寸说起,再处理鼠标拖拽的功能。
一、根据难度动态调整球拍尺寸
你现在的Racquet类里,球拍的宽高是static final常量,这就导致无法动态修改。我们需要把它们改成实例变量,并提供修改的方法,然后在难度切换时更新这些值。
步骤1:修改Racquet类,让尺寸可动态调整
把原来的静态常量改成实例变量,添加getter/setter方法,同时更新所有引用这些常量的地方:
import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.awt.event.MouseWheelEvent; public class Racquet { private static final int Y = 330; // 把static final改成实例变量,默认值保持原来的60和10 private int width = 60; private int height = 10; int x = 0; int xa = 0; private Game game; public Racquet(Game game) { this.game = game; } // 添加setter方法用于修改尺寸,同时做边界检查防止球拍出界 public void setWidth(int width) { this.width = width; if (x + width > game.getWidth()) { x = game.getWidth() - width; } } public void setHeight(int height) { this.height = height; } public int getWidth() { return width; } public int getHeight() { return height; } public void move() { // 把原来的WIDTH换成width if (x + xa > 0 && x + xa < game.getWidth() - width) x = x + xa; } public void paint(Graphics2D g) { // 替换WIDTH和HEIGHT为实例变量 g.fillRect(x, Y, width, height); } public Rectangle getBounds() { // 替换WIDTH和HEIGHT return new Rectangle(x, Y, width, height); } // 其余原有方法保持不变... }
步骤2:在Game类中添加Racquet的getter方法
假设你的Game类里已经实例化了Racquet,需要添加一个getter让DifficultyLevel能访问到它:
public class Game { // 你的其他变量... private Racquet racquet; public Game() { racquet = new Racquet(this); // 其他初始化逻辑... } public Racquet getRacquet() { return racquet; } // 其余方法... }
步骤3:在DifficultyLevel中更新球拍尺寸
修改changeDiff方法,对应不同难度设置球拍尺寸(比如中等和困难难度宽度减半):
private void changeDiff(){ if (selection == 0){ sg.speed=1; sg.Score=0; sg.setBackground(Color.cyan); // 恢复默认尺寸 sg.getRacquet().setWidth(60); sg.getRacquet().setHeight(10); }else if (selection == 1){ sg.speed=2; sg.Score=0; sg.setBackground(Color.pink); // 宽度减半,高度如果需要也可以改成5 sg.getRacquet().setWidth(30); sg.getRacquet().setHeight(10); }if (selection == 2){ sg.speed=3; sg.Score=0; sg.setBackground(Color.yellow); // 宽度减半 sg.getRacquet().setWidth(30); sg.getRacquet().setHeight(10); } }
二、实现鼠标拖拽移动球拍
我们需要在Game类中添加鼠标拖拽监听器,实时更新球拍的位置。
步骤1:让Game类实现MouseMotionListener
修改Game类,注册鼠标监听器并实现拖拽逻辑:
import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; // 假设Game继承自JPanel或其他容器类 public class Game extends JPanel implements MouseMotionListener { private Racquet racquet; public Game() { racquet = new Racquet(this); // 注册鼠标移动监听器 addMouseMotionListener(this); // 其他初始化... } @Override public void mouseDragged(MouseEvent e) { // 计算球拍的新位置:鼠标x坐标减去球拍宽度的一半,让球拍中心对齐鼠标 int newX = e.getX() - racquet.getWidth() / 2; // 边界检查,防止球拍超出屏幕 if (newX < 0) { newX = 0; } else if (newX + racquet.getWidth() > getWidth()) { newX = getWidth() - racquet.getWidth(); } // 更新球拍位置(如果之前给Racquet加了setX方法,用setX更规范) racquet.x = newX; // 重绘界面,让球拍位置立即更新 repaint(); } @Override public void mouseMoved(MouseEvent e) { // 鼠标移动但未拖拽时,可以留空,或者也可以让球拍跟着鼠标移动,看你的需求 } // 其余原有方法... }
(可选)优化封装:给Racquet添加setX方法
为了不直接访问Racquet的x变量(不符合封装原则),可以在Racquet里加一个方法:
public void setX(int x) { this.x = x; // 同样做边界检查 if (this.x < 0) { this.x = 0; } else if (this.x + width > game.getWidth()) { this.x = game.getWidth() - width; } }
然后在Game的mouseDragged里改成:
racquet.setX(e.getX() - racquet.getWidth() / 2);
这样就完成了鼠标拖拽移动球拍的功能,拖拽的时候球拍会跟着鼠标走,还不会超出屏幕边界。
内容的提问来源于stack exchange,提问作者Haiqa Pervez
相关产品推荐
相关产品推荐

