这是一个样本日志。最后一行是问题所在。我按了“D”就放了。注意,“D”显示为释放,而不是按下。对我来说没有意义...
按下的键:d
释放的密钥:d
// 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;
}
}
我使用您的代码并运行它以找出问题所在,您的问题并不一定在keyrelease()
函数中。当你按下一个键然后释放它时,它应该只说一次,当我按下a和s按钮时,它是这样的:
按下的键:a
按下的键:s
按下键时的示例输出不起作用:
我试图用Java创建一个简单的乒乓球游戏进行处理。我还没有完成,一切都很顺利,只是我不能让球从乒乓桨上反弹。我已经成功地做到了,如果球低于桨板,它会反弹回来,但出于某种原因,如果球高于桨板,它会穿过。 paddleFunctions选项卡:
当用户按下左右键时,我希望我的矩形停止。我研究了多个密钥处理,但无法找到任何东西。 谢谢你的帮助
我只能收到 如何处理
这个虫子严重毁了我的一周。我试图创建一个交互式排行榜,其中有三个数组:1是图像,2是我以字符串形式编写的整数。我正在尝试创建一个keyPressed事件,它将使数字随着代表团队的图像而改变,因为他们在梯子上或下梯子,我有一个mousePressed事件来执行一个循环,将窗口恢复到它的原始状态。 我的问题是,当我尝试运行代码时,keyPressed事件不会执行,只有在我单击鼠标后才执行。然后图像会移
这是我的代码中不起作用的一部分我是在使用python处理3的过程中这样做的