【发布时间】:2016-02-15 02:35:16
【问题描述】:
我有这个吃豆人游戏。它已经在工作了,但是我对他的其他形状特别是吃豆人的眼睛有问题。问题是当我按向左和向上箭头键时,眼睛的位置会出现错误的位置。见附图
有人可以帮我解决这个问题吗?这是我的代码。
public void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
//pacman
graphics.setColor(Color.yellow);
graphics.fillArc(xLocation, yLocation, 100, 100, angle, mouth);
//eyes
graphics.setColor(Color.BLACK);
graphics.fillOval(xLocationEyes, yLocationEyes, 20, 20);
food(graphics);
}
@Override
public void keyPressed(KeyEvent keyboard) {
int keyboardPress = keyboard.getKeyCode();
if(keyboardPress == KeyEvent.VK_RIGHT){
if(xLocation + 130 >= getWidth()){
xLocationEyes = getWidth()-130;
xLocation = 600-160;
}
xLocation += 30;
xLocationEyes += 30;
angle = 45;
repaint();
}
if(keyboardPress == KeyEvent.VK_LEFT){
if(xLocation <= 0){
xLocationEyes = 45;
xLocation = 30;
}
angle = -145;
xLocation -= 30;
xLocationEyes -= 30;
repaint();
}
}
【问题讨论】:
-
只根据方向改变眼睛的位置
-
@MadProgrammer 我试过了,但是两个形状(pacman 和他的眼睛)没有同步。
-
@MadProgrammer 你能在这里提供一些样本吗?欣赏它。
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
-
@AndrewThompson 你对此有什么建议吗?
标签: java android swing netbeans awt