gpt4 book ai didi

Java:Pacman 不会根据 keyTyped 移动

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:44 27 4
gpt4 key购买 nike

问题:我无法通过左右箭头键移动可怜的吃 bean 人。我基本上尝试使用“差异”作为 pacman 设计的 x 坐标;如果“差异”改变,吃 bean 人就会移动。如果有人能对这款精彩的吃 bean 游戏有所了解,那就太好了。我更喜欢一个简单的解决方案,而不是重新设计整个代码;我还很新。

主要:

    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);
}
}

吃 bean 人类:

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
{}
}

最佳答案

您似乎没有注册您的KeyListener。您必须在 tegnPacman() 方法中的 setBackground(Color.WHITE); 之后添加以下行:

setBackground(Color.WHITE); 
addKeyListener(this); // add this line.

这会将您的关键监听器添加到您的 JFrame 中,从而触发事件。

关于Java:Pacman 不会根据 keyTyped 移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218263/

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