我试图在视频的帮助下制作一个brickBreaker游戏,但是当我编译时,我的公共空洞中出现了一个错误,其中涉及到代码底部的KeyEvent。
我得到如下错误:
找不到符号类KeyEvent。
package brickBreaker;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent.*;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.JPanel;
public class Gameplay extends JPanel implements KeyListener, ActionListener{
private boolean play = false;
private int score = 0;
private int totalBricks = 21;
private Timer time;
private int delay = 8;
private int playerX = 310;
private int ballposX = 120;
private int ballposY = 350;
private int balldirX = -1;
private int balldirY = -2;
private MapGenerator map;
public Gameplay(){
map = new MapGenerator(3, 7);
addKeyListener(this);
addActionListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
// timer = new Timer(delay, this);
// timer.start();
}
public void paint(Graphics g){
//background
g.setColor(Color.black);
g.fillRect(1,1, 692, 592);
//Drawing map
map.draw((Graphics2D)g);
//borders
g.setColor(Color.yellow);
g.fillRect(0,0,3,592);
g.fillRect(0,0,692,3);
g.fillRect(691, 0, 3, 592);
//the paddle
g.setColor(Color.green);
g.fillRect(playerX, 550, 100, 8);
//the ball
g.setColor(Color.yellow);
g.fillOval(ballposX, ballposY, 20, 20);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e){
timer.start();
if(play){
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))){
balldirY = -balldirY;
}
A: for(int i = 0; i < map.map.length; i++){
for(int j = 0; j<map.map[0].length; j++){
if(map.map[i][j] > 0){
int brickX = j* map.brickwidth + 80;
int brickY = i * map.brickheight +50;
int brickwidth = map.brickwidth;
int brickheight = map.brickheight;
Rectangle rect = new Rectangle(brickX, brickY, brickwidth, brickheight);
Rectangle ballRect= new Rectangle(ballposX, ballposY, 20, 20);
Rectangle brickRect = rect;
if(ballRect.intersects(brickRect)){
map.setBrickValue(0, i, j);
totalBricks--;
score += 5;
if(ballposX + 1 <= brickRect.x || ballposX+1 >= brickRect.x+brickRect.width){
balldirX = -balldirX;
} else{
balldirY = -balldirY;
}
break A;
}
}
}
}
ballposX += balldirX;
ballposY+= balldirY;
if(ballposX < 0){
balldirX = -balldirX;
}
if(ballposY < 0){
balldirY = -balldirY;
}
if(ballposX > 670){
balldirX = -balldirX;
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
if(playerX >= 600){
playerX = 600;
} else{
moveRight();
}
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
if(playerX >= 10){
playerX = 10;
} else{
moveLeft();
}
}
}
public void moveRight(){
play = true;
playerX+=20;
}
public void moveLeft(){
play = true;
playerX-=20;
}
}
答案 0 :(得分:0)
将此行import java.awt.event.KeyEvent.*;
更改为此import java.awt.event.KeyEvent;
。
修改强>
您还需要实现这两个方法,因为它们是KeyListener
接口的一部分。
@Override
public void keyReleased(KeyEvent arg0) {
// TODO
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO
}