gpt4 book ai didi

processing - 处理中的突围(游戏)

转载 作者:行者123 更新时间:2023-12-04 06:01:46 29 4
gpt4 key购买 nike

因此,我正在处理(编程语言)中创建游戏突破,但无法弄清楚检查与 bat 碰撞的功能。

到目前为止,我写的关于与球棒碰撞的部分只是将球与底座碰撞并以相反的方向返回。目前,游戏是一种永无止境的现象,球只是与墙壁碰撞。我想要做的是,将球与球棒相撞。

哦,这是我的作业,所以请指出正确的方向,而不是为我做。

这是代码:

// Basic Breakout game
// Code from Matthre Yee-King

// brick position
float brickX;
float brickY;

// brick width and height
float brickH;
float brickW;

// ball position
float ballX;
float ballY;

// ball diameter
float ballD;

// ball direction
float ballDx;
float ballDy;

// bat position
float batX;

//bat width
float batW;
float batH;

//bat colour
float batB;

void setup() {
size (500, 500, P2D);

// set sizes of game items
brickW = 100;
brickH = 50;
batW = 100;
batH = 25;
ballD = 25;
batB = 255;

// random brick position
brickX = random(0, width - brickW);
brickY = random (0, height / 2);

// bat in the centre
batX = (width/2) - (batW/2);

// ball atop bat
ballX = batX + (batW/2);
ballY = height - batH - (ballD/2);

// ball movement
ballDx = random(-5, 5);
ballDy = -5;
rectMode(CORNER);
ellipseMode(CENTER);
}

void draw() {
// check for ball collision
// with top or sides of bat
checkBallAgainstBat();

// check for ball collision with
// left right and top walls
// and bounce
checkBallAgainstWalls();

// check ball against brick
checkBallAgainstBrick();

// move the ball
ballX += ballDx;
ballY += ballDy;
background(0);

// draw the bat
fill(0, 255, 0);
rect(batX, height - batH, batW, batH);

// draw the brick
fill(0, 0, batB);
batB = (batB + 10) % 255;
rect(brickX, brickY, brickW, brickH);

// draw the ball
fill(255, 0, 0);
ellipse(ballX, ballY, ballD, ballD);

if (keyCode == 37) { // left cursor key
batX -= 10;

// keep it on the screen
if (batX < 0) {
batX = 0;
}
}

if (keyCode == 39) {
batX += 10;
if (batX > (width - batW)) {
batX = width - batW;
}
}
}

// when they let go of the key, reset the keyCode
void keyReleased() {
keyCode = -1;
}

// this function checks if the ball has hit the top or sides of the bat and
// updates its direction as appropriate so the ball bouncs off the bat
void checkBallAgainstBat() {
if (ballY + ballD > height - batH) {
ballDy *= -1;
}
}

// this function checks if the ball has hit the brick. It should bounce off
// the brick and return true if so
boolean checkBallAgainstBrick() {
return false;
}

// this function checks if the ball has hit the top, left or right
// walls and update its
// direction as appropriate so the ball bounces off the walls
void checkBallAgainstWalls() {
if (ballX + ballD > width) {
ballDx *= -1;
}
if (ballX - ballD < 0) {
ballDx *= -1;
}
if (ballY - ballD < 0) {
ballDy *= -1;
}
}

最佳答案

由于突破中的 bat 是固定宽度,因此您的碰撞检测可以非常简单(在伪代码中):

if (lower_edge(ball) > top_edge(bat)) { 
// the ball has entered territory where it might have collided
if ((left_edge(ball) <= right_edge(bat)) && (right_edge(ball) >= left_edge(bat))) {
// the ball's within the horizontal bounds of the bat, so it's a "hit"
... calculate deflection ...
} else {
// oops, ball's gone past the bat and wasn't hit
strike_out();
} else {
// ball's still above the bat somewhere. do nothing
}

用英语:如果球的下边缘已经越过了球棒的上边缘,我们就有可能发生碰撞。这只是检查比赛 field 的垂直轴。然后检查球的左边缘或右边缘是否落在球棒的水平位置内。如果球的任何一侧都没有与球棒重叠,那么你就输了。否则你已经发生了碰撞并且你进行了碰撞检测。

关于processing - 处理中的突围(游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821105/

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