【发布时间】:2020-03-03 14:28:49
【问题描述】:
我刚开始在 Processing 中学习编程,我需要帮助来修改程序。我需要帮助修改我的代码,以便每当鼠标按下目标时,目标会改变方向并垂直移动,然后当再次按下目标时,目标会再次变为水平移动。我希望这在整个游戏中不断发生。
这是目前为止的代码:
float asize = 40;
float y = height;
int start; //Position of the ball
int speed=1; //ball speed
int balldirect=1; //ball's direction
int score=0; //Your score
int lives=5;
boolean lostcond =false;
boolean wincond = false;
void setup()
{
size (400,400);
smooth();
start=width/2;
fill(255,0,0);
textSize(13);
}
void draw()
{
background (0);
ellipse(start, height/2,asize,asize);
start=start+(speed*balldirect); //update the ball's position
if (start > width-20 || start<20) //if ball hits side of screen
{
balldirect=-balldirect; //change directions
}
text("Your score = "+score,10,10);
text("lives left= "+lives,width-80,10);
if (lives<=0)
{
textSize(20);
text("You Lost. Click to Restart", 125,100);
noLoop();
lostcond=true;
textSize(13);
}
}
void mousePressed()
{
if (dist(mouseX, mouseY, start, 200)<=20) //if target hit
{
score=score+speed; //Increase the speed
speed=speed+1; //Increase the Score
asize=asize-2;
y++;
if (y > height) {
y = 0;
}
}
else //We missed
{
if (speed<1) //If speed is greater than 1 decrease the speed
{
speed=speed-1;
}
lives=lives-1; //Take away one life
}
if (lostcond==true) //If we lost the game, reset now and start over
{
speed=1; //Reset all variables
lives=5;
score=0;
start=width/2;
balldirect=1;
lost=false;
loop();
}
if(score == 100){
wincond = true;
textSize(20);
text("You won!!!", 125,100);
noLoop();
}
}
【问题讨论】:
-
您的问题/问题是什么?你有什么尝试自己解决这个问题?您可以分享任何错误吗?
-
你已经有了你要找的逻辑,但是应用在 X 轴上:
start=start+(speed*balldirect);阅读并尝试应用在 Y 轴上。 -
如果不代替您做,很难帮助您。尝试一些东西,如果失败了再回来,那么你会有一个我们可以回答的具体问题,这既可以帮助你提高技术,也可能对未来遇到类似问题的用户有用。
标签: java animation processing