所以当按下某个键时,我试图通过递增它们来移动桨。我这样做是因为当我没有使用keyrelease时,你无法同时移动它们。
我现在遇到的问题是,如果我按下它将要去的方向(两者都可以同时进行,这是好的),但是一旦按下相反的键,它将停止,并且将无法再搬一次有什么提示吗?
以下是我正在谈论的所有内容
public void paddleMove(){
if(leftDown==true){
y-=10;
}
if(leftUp ==true){
y+=10;
}
if(rightDown ==true){
ytwo-=10;
}
if(rightUp ==true){
ytwo+=10;
}
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON){
if(e.getKeyCode() == KeyEvent.VK_A){
// y-=10;
leftDown = true;
}
if(e.getKeyCode() == KeyEvent.VK_S){
// y+=10;
leftUp = true;
}
if(e.getKeyCode() == KeyEvent.VK_QUOTE){
// ytwo-=10;
rightDown = true;
}
if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){
//ytwo+=10;
rightUp = true;
}
}
}
public void keyRelease(KeyEvent r){
if(r.getKeyCode() == KeyEvent.VK_A){
leftDown = false;
}
if(r.getKeyCode() == KeyEvent.VK_S){
leftUp = false;
}
if(r.getKeyCode() == KeyEvent.VK_QUOTE){
rightDown = false;
}
if(r.getKeyCode() == KeyEvent.VK_SEMICOLON){
rightUp = false;
}
}
这是完整的代码
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pong extends JFrame implements ActionListener{
//implement constants
PongPanel pongPanel = new PongPanel();
//JFrame pong x and y coordinates
static final int jfpX = 150;
static final int jfpY = 20;
// JFrame pong width and height
static final int jfpW = 800;
static final int jfpH = 600;
Thread thrd;
public static void main(String[] args) {
Pong jfp = new Pong();
jfp.setVisible(true);
}
public Pong(){
setBounds(jfpX,jfpY,jfpW,jfpH);
setTitle("Pong");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.black);
add(pongPanel);
addKeyListener(pongPanel);
thrd = new Thread (pongPanel);
thrd.start();
}
public void actionPerformed(ActionEvent e) {
}
}
class PongPanel extends JPanel implements Runnable, KeyListener{
Random random = new Random();
static final int jpW = 800;
static final int jpH = 600;
int paddleStart = (jpH/2)-35;
int paddleStarttwo = (jpH/2)-35;
int ballStartX = (jpW/2)-20;
int ballStartY = (jpH/2)-20;
int ytwo,x,y;
int ballD = 30;
int paddleW1 = 20;
int paddleH1 = 100;
int paddleW2 = 20;
int paddleH2 = 100;
int min = -2;
int max = 2;
int randomBallx, randomBally;
boolean leftUp = false;
boolean leftDown = false;
boolean rightUp = false;
boolean rightDown = false;
// int randomBallx = random.nextInt(max-min+1)+min;
// int randomBally = random.nextInt(max-min+1)+min;
int rand1 = random.nextInt(2-1 + 1)+1; // random for function to determine ballx and bally
int rand2 = random.nextInt(2-1+2)+1;
int dx = 4;
int dy = 4; //direction of y
public void ballNotZero(){// makes sure the ball doesnt go straight up and down
if (randomBallx ==0){
randomBallx = random.nextInt(max-min+1)+min;
}
if(randomBally == 0){
randomBally=random.nextInt(max-min+1)+min;
}
// if(rand1 ==1){
// randomBallx=-1;
// }
// if(rand1 ==2){
// randomBallx=1;
// }
// if(rand2 ==1){
// randomBally =-1;
// }
// if(rand2==2){
// randomBally = 1;
// }
}
public PongPanel(){
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Color ball;
Color paddleOne;
Color paddleTwo;
ball = new Color(255,0,255);
paddleOne = new Color(255,0,0);
paddleTwo = new Color(0,0,255);
g.setColor(ball);
g.fillOval(ballStartX+randomBallx,ballStartY+randomBally,ballD,ballD);
g.setColor(paddleOne);
g.fillRect(20,paddleStart+y,paddleW1,paddleH1);
g.setColor(paddleTwo);
g.fillRect(760,paddleStarttwo+ytwo,paddleW2,paddleH2);
}
public void run() {
while(true){
ballNotZero();
detectPaddle();
paddleMove();
randomBall();
ballMove();
repaint();
try {Thread.sleep(75); } catch(Exception e){
}
}
}
public static boolean intervallContains(int low, int high, int n) { //determines if something is in a certain range
return n >= low && n <= high;
}
public void detectPaddle(){ //determines if ball is close enough to paddle for detection
int withinY = (paddleStart+y) -(ballStartY+randomBally);
int withinY1 = (paddleStarttwo+ytwo)-(ballStartY+randomBally);
if (ballStartX+randomBallx <=20 && intervallContains(-50,50,withinY)){
dx = -dx;
}
if(ballStartX+randomBallx >=760 && intervallContains(-50,50,withinY1)){
dx = -dx;
}
}
public void randomBall(){
if(randomBallx >=0 ){
randomBallx+=dx;
}
if(randomBallx<0){
randomBallx-=dx;
}
if(randomBally>=0){
randomBally+=dy;
}
if(randomBally<0){
randomBally-=dy;
}
// randomBallx+=randomBallx;
// randomBally+=randomBally;
}
public void ballMove(){
if(ballStartY+randomBally > jpH-60){
dy= -dy;
}
if(ballStartY+randomBally <0){
dy = -dy;
}
}
public void paddleMove(){
if(leftDown==true){
y-=10;
}
if(leftUp ==true){
y+=10;
}
if(rightDown ==true){
ytwo-=10;
}
if(rightUp ==true){
ytwo+=10;
}
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON){
if(e.getKeyCode() == KeyEvent.VK_A){
// y-=10;
leftDown = true;
}
if(e.getKeyCode() == KeyEvent.VK_S){
// y+=10;
leftUp = true;
}
if(e.getKeyCode() == KeyEvent.VK_QUOTE){
// ytwo-=10;
rightDown = true;
}
if(e.getKeyCode() == KeyEvent.VK_SEMICOLON){
//ytwo+=10;
rightUp = true;
}
}
}
public void keyRelease(KeyEvent r){
if(r.getKeyCode() == KeyEvent.VK_A){
leftDown = false;
}
if(r.getKeyCode() == KeyEvent.VK_S){
leftUp = false;
}
if(r.getKeyCode() == KeyEvent.VK_QUOTE){
rightDown = false;
}
if(r.getKeyCode() == KeyEvent.VK_SEMICOLON){
rightUp = false;
}
}
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
看看你的逻辑......
if (e.getKeyCode() == KeyEvent.VK_A) {
// y-=10;
leftDown = true;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
// y+=10;
leftUp = true;
}
这基本上说如果我按 A AND S 那么leftDown
和leftUp
可以同时true
,显然不可能(为了你想做的事),他们需要互相取消...
它应该更像......
if (e.getKeyCode() == KeyEvent.VK_A) {
// y-=10;
leftDown = true;
leftUp = false;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
// y+=10;
leftUp = true;
leftDown = false
}
相反。这意味着,如果我按 A ,leftDown
为true
而leftUp
为false
,但如果我还按 S ,然后leftDown
为false
,leftUp
为true
。这意味着玩家只能向一个方向移动,无论他们按哪个键序列。
我在这儿的时候......
此if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_QUOTE || e.getKeyCode() == KeyEvent.VK_SEMICOLON) {
是多余的,并且不会为您的代码添加任何值,您将以任何方式检查这些状态......
而不是使用一堆断开的if
语句,如...
if (e.getKeyCode() == KeyEvent.VK_A) {
//...
}
if (e.getKeyCode() == KeyEvent.VK_S) {
//...
}
if (e.getKeyCode() == KeyEvent.VK_QUOTE) {
//...
}
if (e.getKeyCode() == KeyEvent.VK_SEMICOLON) {
//...
}
您应该使用if-else
语句....
if (e.getKeyCode() == KeyEvent.VK_A) {
//...
} else if (e.getKeyCode() == KeyEvent.VK_S) {
//...
} else if (e.getKeyCode() == KeyEvent.VK_QUOTE) {
//...
} else if (e.getKeyCode() == KeyEvent.VK_SEMICOLON) {
//...
}
这真的,非常小,但是更明显的是你打算检查单个状态,因为KeyEvent
只会出现在一个单独的键上......
哦,我还应该添加,使用Key Bindings代替KeyListener
s。 KeyListener
会遇到许多你不想要处理的焦点相关问题......