2
class Sprite {
float x;
float y;
float dx;
float dy;
}
Sprite player= new Sprite();
class Asteroids extends Sprite {
float speedmultiplier;
float speedupposition;
color asteroidcolor;
color hitcolor;
String state; // "entering", "moving", "speed up"
當我將randomRestart設置爲超過500(屏幕大小)的任何東西時,小行星完全停止動畫。在「重新啓動」狀態下,更新函數中還有一個x = random(550,650),同時還有一個roix.x = random(550,650)。這些會影響randomRestart嗎?randomRestart設置爲(550,650)時不會顯示小行星?
另一方面的問題是,我將如何能夠輸入命中檢測與小行星,使玩家失去生命?
在此先感謝您的幫助。
void randomRestart(){
x= random(550,650);
y= random(50,550);
speedmultiplier= random(1.10,1.5);
dx= random(1.5,2.5);
dy= 0;
speedupposition= random(300,450);
asteroidcolor= color(50,40,18);
hitcolor= color(256, 0,0);
state = "entering";
}
void render(){
pushMatrix();
if(state.equals("entering")){
//translate(x,y);
fill(asteroidcolor);
noStroke();
ellipse(x,y,50,50);
}
else if(state.equals("speed up")){
//translate(x,y);
fill(asteroidcolor);
noStroke();
ellipse(x,y,50,50);
}
else if(state.equals("hit")){
//translate(x,y);
fill(hitcolor);
noStroke();
ellipse(x,y,50,50);
}
else if (state.equals("restart")){
translate(x,y);
fill(asteroidcolor);
noStroke();
ellipse(x,y,50,50);
}
popMatrix();
}
void update(){
if (state.equals("entering")){
x += -dx;
if (state.equals("speed up")){
x += -dx * speedmultiplier;
}
}
if (x < 0 || x > width){
state ="restart";
randomRestart();
}
else if (state.equals("restart")){
x += dx;
if (x < -50 || x >width){
state = "entering";
x= random(550,650);
dx = -dx;
randomRestart();
}
}
}
Asteroids randomAsteroids(){
Asteroids roid = new Asteroids();
roid.x= random(550,650);
roid.y= random(50,550);
roid.speedmultiplier= random(1.10,2.0);
roid.dx= random(1.5,3.0);
roid.dy= 0;
roid.speedupposition= random(300,450);
roid.asteroidcolor= color(50,40,18);
roid.asteroidcolor= color(50,50,50);
roid.hitcolor= color(256, 0,0);
return roid;
}
int NUM_ROIDS= 10;
ArrayList<Asteroids> roidsList;
void setup(){
size(500,500);
player.x = 50;
player.y= 250;
player.dx=0;
player.dy=20.5;
roidsList = new ArrayList<Asteroids>();
int i = 0;
while (i<8){
Asteroids a = new Asteroids();
a.randomRestart();
roidsList.add(a);
++i;
}
}
void draw(){
background(255);
fill(0,0,0);
noStroke();
rect(player.x,player.y,10,30);
player.x += player.dx;
for (Asteroids b : roidsList){
b.render();
b.update();
}
}
void keyPressed(){
if (key =='w'){
player.y += -player.dy;
}
if (key =='s'){
player.y += player.dy;
}
}
你能發表完整的代碼嗎?目前運行代碼並不容易 –