我用p5js做了一個(gè)道奇球賽:)玩家可以使用箭頭鍵將一個(gè)黃色圓圈從畫布的左上角移動(dòng)到右下角,同時(shí)避開六個(gè)線性移動(dòng)的紅點(diǎn),其中三個(gè)是水平移動(dòng)的,另一個(gè)是垂直移動(dòng)的。
我只想->如果黃點(diǎn)碰到其中一個(gè)紅點(diǎn),游戲停止,背景變黑。
let c;
let cspeed = 4
let x= 25;
let y = 25;
let level=0;
let width = 800
let height = 800
function setup() {
createCanvas(800, 800);
c=0
let d = dist(c,height/4,x,y); //becuz of 'dist is not defined' issue, i moved it to setup() position
let e = dist(c,height/2,x,y);
let f = dist(c,height/9,x,y);
let g = dist(height/2,c,x,y);
let h = dist(height/4,c, x,y);
let j = dist(height/9,c, x,y);
print(d);
print(e);
print(f);
print(g);
print(h);
print(j);
if (d<20||e<20||f<20||g<20||j<20);{
loseScreen();
}
}
function draw() {
keyPressed()
background(220);
fill(50);
textSize(30)
text(level, 770, 30);
fill('green')
square(0, 0, 50);
fill('purple');
square(750, 750, 50);
fill('yellow')
ellipse(x, y, 40);
c+=cspeed;
fill('red');
noStroke();
ellipse(c, height / 4, 50, 50);
ellipse(c, height / 2, 50, 50);
ellipse(c, height / 9, 50, 50);
ellipse(height / 4, c, 50, 50);
ellipse(height / 2, c, 50, 50);
ellipse(height / 9, c, 50, 50);
if (c > width || c < 0) {
cspeed *= -1;
}
}
if (c > width || c < 0) {
cspeed *= -1;
}
if (x > 770 && y >770){
x = 25
y= 25;
cspeed *= 1.5
level++;
}
function loseScreen(){ // it's not working,,
noStroke();
fill('black');
square(0,0,800);
}
function keyPressed() {
//print(keyCode, key);
if (keyCode == LEFT_ARROW && keyIsPressed) {
x = x - 5;
if (x<20){
x=20
}
} else if (keyCode == RIGHT_ARROW&& keyIsPressed) {
x = x + 5;
if (x>780){
x=780
}
} else if(keyCode == DOWN_ARROW&& keyIsPressed){
y = y + 5;
if (y>780){
y=780
}
}else if(keyCode == UP_ARROW&& keyIsPressed){
y = y - 5;
if (y<20){
y=20
}
}
}
這是我的p5js代碼https://editor.p5js.org/kiskl/sketches/XzwAM5pDA
我將感謝您提供的任何幫助!
應(yīng)你的要求,我進(jìn)一步闡述了你第一個(gè)問題的答案。我之前的解決方案的訣竅是,可以使用for循環(huán)中的幾行來檢查每個(gè)點(diǎn)的沖突,這也是為什么在這樣的情況下使用數(shù)組通常更好。
現(xiàn)在,在hard-coded點(diǎn)上使用數(shù)組肯定有好處。