对象继续移动一次
Object to continue moving after one go
我想知道是否有人可以帮助我完成这个程序。
我需要让水平运动的球重新开始移动,然后再射另一个垂直运动的球。我怎样才能让这种情况一遍又一遍地发生,而不是一次就停止?
另外,如何让键盘上的任意键而不是触摸板来发射球?
这是我的程序:
int xPos =200;
int yPos =700;
int xDir=3; // SPEED
float ball2X;
float ball2Y;
float ball2Speed=5;
boolean ball2Exist = false;
void setup () {
size (1280, 960);
}
void draw() {
background(255);
fill (255, 200, 200);
rect (0, 550, width, height);
fill (225);
//Player(ball)
ellipse(xPos, yPos, 140, 140);
fill (255, 200, 200);
xPos=xPos+xDir;
if (xPos>width-20 || xPos<20)
{
xDir=-xDir;
}
if(ball2Exist)
{
fill (255, 0, 0);
ellipse(ball2X, ball2Y, 70, 70);
ball2Y -= ball2Speed;
}
}
void mouseClicked() {
if(xDir!=0) // first click
xDir = 0;
else // second click
{
ball2X = xPos;
ball2Y = yPos;
ball2Exist = true;
}
}
为了让小球一键发射,可以使用keyPressed()
function in the same way that you used the mouseClicked()
函数。为了使水平球在发射球后继续运动,可以将球的速度xDir
设置为原来的值。为确保它继续朝同一方向前进,您应该在将其设置为 0 之前使用一个变量来存储其当前值,然后您可以在发射球后将 xDir 设置为此值。为了有多个红球,你想存储一个球信息列表,以便你可以同时绘制多个球,为了有效地做到这一点,你可能想要创建一个 Ball
class .
在下面的程序中,我做了一个球class来封装所有的球绘制,我用PVectors
来处理位置和速度的x和y坐标。
// Horizontal ball
Ball mainBall;
// List of red balls
ArrayList<Ball> balls;
void setup () {
size (1280, 960);
//Create main ball
mainBall = new Ball(200, 700, 3, 0, 140, color(255, 200, 200));
// Create list of balls
balls = new ArrayList<Ball>();
}
void draw() {
background(255);
fill (255, 200, 200);
rect (0, 550, width, height);
ellipseMode(CENTER);
//If the main ball collides with edge of window, reverse speed
mainBall.collisionUpdate();
//Draw the main ball
mainBall.drawBall();
//Create arraylist to store balls to get removed from the list
ArrayList<Ball> toRemove = new ArrayList<Ball>();
// For every ball
for(Ball ball : balls){
ball.drawBall();
//If the ball is off the screen need to remove it
if(ball.isNotVisible()){
toRemove.add(ball);
}
}
// Remove all the non-visible balls
balls.removeAll(toRemove);
}
void launch(){
// Add a new ball to the list
balls.add(new Ball(mainBall.position.x, mainBall.position.y, 0, -5, 70, color(255, 0, 0)));
}
void mouseClicked() {
launch();
}
void keyPressed(){
launch();
}
// Ball class
class Ball{
PVector position;
PVector speed;
float diameter;
color c;
Ball(float x, float y, float xSpeed, float ySpeed, float _diameter, color _c){
position = new PVector(x, y);
speed = new PVector(xSpeed, ySpeed);
diameter = _diameter;
c = _c;
}
void drawBall(){
fill(c);
circle(position.x, position.y, diameter);
// Update position of ball
position.add(speed);
}
void collisionUpdate(){
if(position.x < diameter/2 || position.x > width-diameter/2 || position.y < diameter/2 || position.y > height-diameter/2 ){
//Reverse speed
speed.mult(-1);
}
}
// Return true or false if the ball is off the screen
boolean isNotVisible(){
return position.x > width+diameter || position.x < -diameter || position.y > height+diameter || position.y < -diameter;
}
}
编辑:
使球减速并跳出 window 边界。我还使用随机函数来随机化球减速的速度和速率。
// Horizontal ball
Ball mainBall;
// List of red balls
ArrayList<Ball> balls;
PVector oldSpeed;
void setup () {
size (1280, 960);
//Create main ball
mainBall = new Ball(200, 700, 3, 0, 140, color(255, 200, 200));
// Create list of balls
balls = new ArrayList<Ball>();
}
void draw() {
background(255);
fill (255, 200, 200);
rect (0, 550, width, height);
ellipseMode(CENTER);
//If the main ball collides with edge of window, reverse speed
mainBall.collisionUpdate();
//Draw the main ball
mainBall.drawBall();
//Create arraylist to store balls to get removed from the list
ArrayList<Ball> toRemove = new ArrayList<Ball>();
// For every ball
for(Ball ball : balls){
// Balls bounce within the screen
ball.collisionUpdate();
// Slow the speed of the ball
ball.slowSpeed(random(0.98,0.999));
ball.drawBall();
//If the ball is off the screen need to remove it
if(ball.isNotVisible()){
toRemove.add(ball);
}
}
// Remove all the non-visible balls
balls.removeAll(toRemove);
}
void launch(){
if(!mainBall.isStationary()){
oldSpeed = mainBall.speed;
mainBall.setSpeed(0, 0);
}else{
// Add a new ball to the list
balls.add(new Ball(mainBall.position.x, mainBall.position.y, 0, random(-8, -5), 70, color(255, 0, 0)));
//Make the main ball move again
mainBall.setSpeed(oldSpeed);
}
}
void mouseClicked(){
launch();
}
void keyPressed(){
launch();
}
// Ball class
class Ball{
PVector position;
PVector speed;
float diameter;
color c;
Ball(float x, float y, float xSpeed, float ySpeed, float _diameter, color _c){
position = new PVector(x, y);
speed = new PVector(xSpeed, ySpeed);
diameter = _diameter;
c = _c;
}
void drawBall(){
fill(c);
circle(position.x, position.y, diameter);
// Update position of ball
position.add(speed);
}
void setSpeed(float xSpeed, float ySpeed){
speed = new PVector(xSpeed, ySpeed);
}
void setSpeed(PVector _speed){
speed = _speed;
}
boolean isStationary(){
return speed.x == 0 && speed.y == 0;
}
void collisionUpdate(){
if(position.x < diameter/2 || position.x > width-diameter/2 || position.y < diameter/2 || position.y > height-diameter/2 ){
//Reverse speed
speed.mult(-1);
}
}
void slowSpeed(float deceleration){
if(speed.mag() < 0.5){
speed = new PVector(0, 0);
}else{
speed.mult(deceleration);
}
}
// Return true or false if the ball is off the screen
boolean isNotVisible(){
return position.x > width+diameter || position.x < -diameter || position.y > height+diameter || position.y < -diameter;
}
}
我想知道是否有人可以帮助我完成这个程序。
我需要让水平运动的球重新开始移动,然后再射另一个垂直运动的球。我怎样才能让这种情况一遍又一遍地发生,而不是一次就停止?
另外,如何让键盘上的任意键而不是触摸板来发射球?
这是我的程序:
int xPos =200;
int yPos =700;
int xDir=3; // SPEED
float ball2X;
float ball2Y;
float ball2Speed=5;
boolean ball2Exist = false;
void setup () {
size (1280, 960);
}
void draw() {
background(255);
fill (255, 200, 200);
rect (0, 550, width, height);
fill (225);
//Player(ball)
ellipse(xPos, yPos, 140, 140);
fill (255, 200, 200);
xPos=xPos+xDir;
if (xPos>width-20 || xPos<20)
{
xDir=-xDir;
}
if(ball2Exist)
{
fill (255, 0, 0);
ellipse(ball2X, ball2Y, 70, 70);
ball2Y -= ball2Speed;
}
}
void mouseClicked() {
if(xDir!=0) // first click
xDir = 0;
else // second click
{
ball2X = xPos;
ball2Y = yPos;
ball2Exist = true;
}
}
为了让小球一键发射,可以使用keyPressed()
function in the same way that you used the mouseClicked()
函数。为了使水平球在发射球后继续运动,可以将球的速度xDir
设置为原来的值。为确保它继续朝同一方向前进,您应该在将其设置为 0 之前使用一个变量来存储其当前值,然后您可以在发射球后将 xDir 设置为此值。为了有多个红球,你想存储一个球信息列表,以便你可以同时绘制多个球,为了有效地做到这一点,你可能想要创建一个 Ball
class .
在下面的程序中,我做了一个球class来封装所有的球绘制,我用PVectors
来处理位置和速度的x和y坐标。
// Horizontal ball
Ball mainBall;
// List of red balls
ArrayList<Ball> balls;
void setup () {
size (1280, 960);
//Create main ball
mainBall = new Ball(200, 700, 3, 0, 140, color(255, 200, 200));
// Create list of balls
balls = new ArrayList<Ball>();
}
void draw() {
background(255);
fill (255, 200, 200);
rect (0, 550, width, height);
ellipseMode(CENTER);
//If the main ball collides with edge of window, reverse speed
mainBall.collisionUpdate();
//Draw the main ball
mainBall.drawBall();
//Create arraylist to store balls to get removed from the list
ArrayList<Ball> toRemove = new ArrayList<Ball>();
// For every ball
for(Ball ball : balls){
ball.drawBall();
//If the ball is off the screen need to remove it
if(ball.isNotVisible()){
toRemove.add(ball);
}
}
// Remove all the non-visible balls
balls.removeAll(toRemove);
}
void launch(){
// Add a new ball to the list
balls.add(new Ball(mainBall.position.x, mainBall.position.y, 0, -5, 70, color(255, 0, 0)));
}
void mouseClicked() {
launch();
}
void keyPressed(){
launch();
}
// Ball class
class Ball{
PVector position;
PVector speed;
float diameter;
color c;
Ball(float x, float y, float xSpeed, float ySpeed, float _diameter, color _c){
position = new PVector(x, y);
speed = new PVector(xSpeed, ySpeed);
diameter = _diameter;
c = _c;
}
void drawBall(){
fill(c);
circle(position.x, position.y, diameter);
// Update position of ball
position.add(speed);
}
void collisionUpdate(){
if(position.x < diameter/2 || position.x > width-diameter/2 || position.y < diameter/2 || position.y > height-diameter/2 ){
//Reverse speed
speed.mult(-1);
}
}
// Return true or false if the ball is off the screen
boolean isNotVisible(){
return position.x > width+diameter || position.x < -diameter || position.y > height+diameter || position.y < -diameter;
}
}
编辑:
使球减速并跳出 window 边界。我还使用随机函数来随机化球减速的速度和速率。
// Horizontal ball
Ball mainBall;
// List of red balls
ArrayList<Ball> balls;
PVector oldSpeed;
void setup () {
size (1280, 960);
//Create main ball
mainBall = new Ball(200, 700, 3, 0, 140, color(255, 200, 200));
// Create list of balls
balls = new ArrayList<Ball>();
}
void draw() {
background(255);
fill (255, 200, 200);
rect (0, 550, width, height);
ellipseMode(CENTER);
//If the main ball collides with edge of window, reverse speed
mainBall.collisionUpdate();
//Draw the main ball
mainBall.drawBall();
//Create arraylist to store balls to get removed from the list
ArrayList<Ball> toRemove = new ArrayList<Ball>();
// For every ball
for(Ball ball : balls){
// Balls bounce within the screen
ball.collisionUpdate();
// Slow the speed of the ball
ball.slowSpeed(random(0.98,0.999));
ball.drawBall();
//If the ball is off the screen need to remove it
if(ball.isNotVisible()){
toRemove.add(ball);
}
}
// Remove all the non-visible balls
balls.removeAll(toRemove);
}
void launch(){
if(!mainBall.isStationary()){
oldSpeed = mainBall.speed;
mainBall.setSpeed(0, 0);
}else{
// Add a new ball to the list
balls.add(new Ball(mainBall.position.x, mainBall.position.y, 0, random(-8, -5), 70, color(255, 0, 0)));
//Make the main ball move again
mainBall.setSpeed(oldSpeed);
}
}
void mouseClicked(){
launch();
}
void keyPressed(){
launch();
}
// Ball class
class Ball{
PVector position;
PVector speed;
float diameter;
color c;
Ball(float x, float y, float xSpeed, float ySpeed, float _diameter, color _c){
position = new PVector(x, y);
speed = new PVector(xSpeed, ySpeed);
diameter = _diameter;
c = _c;
}
void drawBall(){
fill(c);
circle(position.x, position.y, diameter);
// Update position of ball
position.add(speed);
}
void setSpeed(float xSpeed, float ySpeed){
speed = new PVector(xSpeed, ySpeed);
}
void setSpeed(PVector _speed){
speed = _speed;
}
boolean isStationary(){
return speed.x == 0 && speed.y == 0;
}
void collisionUpdate(){
if(position.x < diameter/2 || position.x > width-diameter/2 || position.y < diameter/2 || position.y > height-diameter/2 ){
//Reverse speed
speed.mult(-1);
}
}
void slowSpeed(float deceleration){
if(speed.mag() < 0.5){
speed = new PVector(0, 0);
}else{
speed.mult(deceleration);
}
}
// Return true or false if the ball is off the screen
boolean isNotVisible(){
return position.x > width+diameter || position.x < -diameter || position.y > height+diameter || position.y < -diameter;
}
}