//declaringglobalvaribales
float dia1=50;
//diameteroftheface
float x=400;
float y=400;
float speedX=4;
float speedY=0;
//setup
void setup() {
size(810, 810);
} //draw
void draw() {
background(225);
fill(225, 0, 0);
stroke(0);
ellipse(x, y, dia1, dia1);
fill(0, 225, 0);
//a nose
triangle(x, y, x+10, y+10, x-10, y+10);
//movingtheballXway
x=x+speedX;
//movingtheballYway
y=y+speedY;
//if it hits the left or right corners it will turn around
if (x>width-dia1/2 || x<2+dia1/2) {
speedX=speedX*-1;
}
// it its hits the top or bottom it will turn around
if (y>height-dia1/2 || y<2+dia1/2) {
speedY=speedY*-1;
}
// this code to move it according to the keys W S D A
void keyPressed() {
if (keyCode=='W') {
speedX=0;
speedY=-4;
}
if (keyCode=='S') {
speedX=0;
speedY=4;
}
if (keyCode=='A') {
speedX=-4;
speedY=0;
}
if (keyCode=='D') {
speedX=4;
speedY=0;
}
}
我用鼻子做了這個球,用鑰匙W S D A在屏幕上移動。如果碰到邊緣,它會彈回來。如何將球向其移動的方向旋轉?我嘗試使用旋轉功能,但會拋出所有東西?
我試圖改變球面臨使其面對,因爲它的移動以同樣的方式的方向。我想爲此使用旋轉,但是一旦我使用旋轉,它會拋出所有的座標。旋轉它也沒有幫助。
我註釋掉我試圖做的東西。例如,我試圖將它翻譯成250,250
,然後再旋轉它,但是然後切換X和Y座標。此外,球不會一直走到角落,它會移出(因爲它被翻譯下來)。
我需要什麼樣的旋轉/轉換邏輯的改變?