问题:我无法通过左右箭头键移动可怜的pacman。我基本上试图使用“差异”作为设计pacman的x坐标;如果改变“差异”,pacman将会移动。 如果有人能够对这场光荣的pacman游戏有所了解,那就太好了。我更喜欢一个简单的解决方案,而不是重新设计整个代码;我很新。
主:
public class Benyt {
public static void main(String[] args)
{
JFrame vindue = new JFrame ("Eksamensspil");
vindue.add(new Pacman());
vindue.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
vindue.setSize(317,540);
vindue.setVisible(true);
Graphics g = vindue.getGraphics();
Bold bold = new Bold(g, 125, 400);
}
}
pacman班:
package eksamensspil;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*; //
import java.awt.Graphics;
import java.awt.event.*;
public class Pacman extends JPanel implements KeyListener
{
GeneralPath pacman;
GeneralPath bane;
int difference = 0;
// constructor
public Pacman()
{
tegnPacman();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.draw(bane);
g2.drawString("Point: ", 230, 470);
g2.draw(pacman);
}
//keytyped
public void keyTyped(KeyEvent e)
{
tegnPacman();
if (e.getKeyCode() == KeyEvent.VK_LEFT)
difference = difference - 6;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
difference = difference + 6;
repaint();
}
private void tegnPacman()
{
setBackground(Color.WHITE);
// Koordinaterne defineres herunder
// Koordinater
int baneX = 300;
int baneY = 500;
// Pacman:
int pacmanCenterX = 150+difference;
int pacmanCenterY = 425;
// ======================PACMAN======================
pacman = new GeneralPath ();
//Der tegnes en hvid rektangel for at "overwrite" den gamle pacman
// BasicStroke stregtype = new BasicStroke(2, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND);
pacman.append(new Rectangle2D.Float(0, 400, 300, 50), false);
// pacman-figuren
pacman.append(new Arc2D.Float(pacmanCenterX-25, pacmanCenterY-25, 50, 50, 45 , -270 , Arc2D.PIE), false);
// Øjet tegnes
pacman.append(new Ellipse2D.Float(pacmanCenterX-15, pacmanCenterY-7, 4, 4), false);
// VENSTRE PIL
pacman.append(new Line2D.Float(pacmanCenterX-25, pacmanCenterY, pacmanCenterX-50, pacmanCenterY), false);
pacman.append(new Line2D.Float(pacmanCenterX-50, pacmanCenterY, pacmanCenterX-45, pacmanCenterY-5), false);
pacman.append(new Line2D.Float(pacmanCenterX-50, pacmanCenterY, pacmanCenterX-45, pacmanCenterY+5), false);
// HØJRE PIL
pacman.append(new Line2D.Float(pacmanCenterX+25, pacmanCenterY, pacmanCenterX+50, pacmanCenterY), false);
pacman.append(new Line2D.Float(pacmanCenterX+50, pacmanCenterY, pacmanCenterX+45, pacmanCenterY-5), false);
pacman.append(new Line2D.Float(pacmanCenterX+50, pacmanCenterY, pacmanCenterX+45, pacmanCenterY+5), false);
// =====================BANEN=========================
bane = new GeneralPath ();
//Banens rammer
bane.append(new Rectangle2D.Float(0,0,300,500), false);
//linje som afgrænser banen
bane.append(new Line2D.Float(0, baneY-50, baneX, baneY-50), false);
//Pointbox
bane.append(new Rectangle2D.Float(225, baneY-25, 75, 25), false);
}
public void keyPressed(KeyEvent e) // Krav for at KeyListener virker, ellers ubenyttet
{}
public void keyReleased(KeyEvent e) // Krav for at KeyListener virker, ellers ubenyttet
{}
}
答案 0 :(得分:0)
您似乎没有注册KeyListener
。您必须在tegnPacman()
之后的setBackground(Color.WHITE);
方法中添加以下行:
setBackground(Color.WHITE);
addKeyListener(this); // add this line.
这会将您的主要听众添加到JFrame
,从而触发事件。