我在处理过程中编写乒乓球游戏时遇到了问题,这让我发疯了。桨最初工作正常。但经过一段时间后,他们似乎无法回应并锁定,似乎是随意的。我输入了一些代码来查看错误,程序有时无法注册keyPressed(),但是当我放开密钥时会打印keyReleased()。我已经查看了其他主题,但对于发生了什么不知所措。 (对于家庭作业,但我们的教练鼓励在Stack Overflow上发帖。)
这是一个示例日志。最后一行是问题所在。我按下了#34; d"并释放它。请注意" d"显示为已释放,但未按下。这对我来说没有意义......
KEY PRESSED:d
重新发布:d
KEY PRESSED:a
重要发布:a
重新发布:d
修改的 我实际上只是去设置我的"重复重复"设置为"关闭,"它似乎解决了这个问题! (我在macOS Sierra上。)为什么会这样?有没有办法让我改变我的代码,以便这个bug不会影响其他人?
// Pong Game
// Starter Code
import java.util.*;
Ball ball;
Paddle paddle1;
Paddle paddle2;
HeadsUp hup;
boolean right;
boolean left;
boolean a;
boolean d;
void setup() {
frameRate(100);
size(300, 600);
hup = new HeadsUp();
paddle1 = new Paddle();
paddle2 = new Paddle();
paddle2.setY(height * .05);
ball = new Ball();
}
public void keyPressed() {
println("KEY PRESSED: " + key);
changeDirection(true);
}
public void keyReleased() {
println("KEY RELEASED: " + key);
changeDirection(false);
}
public void changeDirection(boolean val) {
if (keyCode == LEFT) {
left = val;
}
if (keyCode == RIGHT) {
right = val;
}
if (key == 'a' || key == 'A') {
a = val;
}
if (key == 'd' || key == 'D') {
d = val;
}
}
private void updatePaddles(){
if (left) {
paddle1.moveLeft();
}
if (right) {
paddle1.moveRight();
}
if (a) {
paddle2.moveLeft();
}
if (d) {
paddle2.moveRight();
}
}
void draw() {
if (!hup.isGameOver()) {
background(0);
updatePaddles();
paddle1.draw();
paddle2.draw();
hup.draw();
ball.draw();
if (ball.update(hup, paddle1, paddle2)) {
ball = new Ball();
}
}
else {
stop();
}
}
void stop(){
background(0);
noLoop();
PFont f = createFont("Menlo", 16, true);
textFont(f);
fill(255);
text("GAME OVER!", 100, 300);
if (hup.p1BallsLeft != 0) {
text("Player One Wins!", 80, 350);
} else {
text("Player Two Wins!", 80, 350);
}
}
class Paddle {
float x;
float y;
float w;
float h;
Paddle() {
w = 75;
h = 15;
x = width / 4;
y = height * .9;
}
public void setY(float y) {
this.y = y;
}
public void moveLeft() {
if (x >= 0) x -= 2;
}
public void moveRight() {
if (x < 225) x += 2;
}
private void draw() {
fill(255);
rect(x, y, w, h, 0.1, 0.1, 0.1, 0.1);
}
}
class Ball {
PVector position;
PVector velocity;
static final int BALL_WIDTH = 16;
static final int BALL_HEIGHT = 16;
static final int BALL_RADIUS = BALL_WIDTH / 2;
Ball() {
Random rand = new Random();
boolean direction = rand.nextBoolean();
float vel = 2;
if (direction) vel *= -1;
position = new PVector(rand.nextInt(300), 300);
velocity = new PVector(2, vel);
}
boolean update(HeadsUp hup, Paddle paddle1, Paddle paddle2) {
position.add(velocity);
if (position.y > height) {
hup.update(1);
return true;
}
if (position.y < 0) {
hup.update(2);
return true;
}
checkIfHitWall();
if (velocity.y > 0) checkIfHitPaddle(paddle1);
else checkIfHitPaddle(paddle2);
return false;
}
void draw() {
fill(#ffff00);
ellipse(position.x, position.y, BALL_WIDTH, BALL_HEIGHT);
}
void checkIfHitWall() {
if ((position.x > width) || (position.x < 0)) {
velocity.x *= -1;
}
if (position.y < 0) {
velocity.y *= -1;
}
}
void checkIfHitPaddle(Paddle paddle) {
float distX = Math.abs(position.x - paddle.x - paddle.w / 2);
float distY = Math.abs(position.y - paddle.y - paddle.h /2);
boolean check = false;
if (distX > (paddle.w / 2 + BALL_RADIUS)) { return; }
if (distY > (paddle.h / 2 + BALL_RADIUS)) { return; }
if (distX <= (paddle.w/2)) { check = true; }
if (distY <= (paddle.h/2)) { check = true; }
float dx = distX - paddle.w / 2;
float dy = distY - paddle.h / 2;
if (dx * dx + dy * dy <= (BALL_RADIUS * BALL_RADIUS)) check = true;
if (check) {
velocity.y *= -1;
}
}
}
class HeadsUp {
int p1BallsLeft;
int p2BallsLeft;
boolean gameOver;
HeadsUp() {
p1BallsLeft = 3;
p2BallsLeft = 3;
gameOver = false;
}
void draw() {
text("Player 1 Balls Left: " + p1BallsLeft + "\nPlayer 2 Balls Left: " + p2BallsLeft, 125, 300 );
}
void update(int player) {
if (player == 1) {
p1BallsLeft--;
}
if (player == 2) {
p2BallsLeft--;
}
if (p1BallsLeft == 0 || p2BallsLeft == 0) gameOver = true;
if (!hup.isGameOver()) {
}
}
boolean isGameOver() {
return gameOver;
}
}
&#13;
答案 0 :(得分:0)
我拿了你的代码并运行它来找出问题,你的问题肯定不在upload extension
函数中。当你按一个键然后松开它时,它应该只说一次,当我按下a和s按钮时,它就像这样:
KEY PRESSED:a
KEY PRESSED:s
KEY PRESSED:a
KEY PRESSED:s
现在我认为你的程序正在考虑的是,由于a和s键被按下然后同时释放,它假设我点击了a和s键,这使得它相信它们被按下然后被释放在同一时间。
我能给你的唯一建议就是做一个调试过程,看看你的错误在哪里。