0
我有一個運動圖像作爲背景級碰撞。處理
PImage background;
int x=0; //global variable background location
boolean up;
boolean down;
Rocket myRocket;
Alien alien1,alien2;
void setup(){
size(800,400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
myRocket = new Rocket();
alien1 = new Alien(800,200,4,-3);
alien2 = new Alien(800,200,5,2);
}
void draw()
{
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -=4;
if(x == -background.width)
x=0; //wrap background
myRocket.run();
alien1.run();
alien2.run();
}
void keyPressed(){
if(keyCode == UP)
{
up = true;
}
if(keyCode == DOWN)
{
down = true;
}
}
void keyReleased(){
if(keyCode == UP)
{
up = false;
}
if(keyCode == DOWN)
{
down = false;
}
}
頭等艙。外星人上升和下降向火箭。
class Alien {
int x;
int y;
int speedX,speedY;
Alien(int x,int y,int dx,int dy){
this.x = x;
this.y = y;
this.speedX = dx;
this.speedY = dy;
}
void run(){
alien();
restrict();
}
void alien(){
fill(0,255,0);
ellipse(x,y,30,30);
fill(50,100,0);
ellipse(x,y,50,15);
x = x - speedX;
y = y + speedY;
}
void restrict(){
if (y < 15 || y > 380){
speedY = speedY * -1;
}
if (x == 0){
x = 800;
}
}
}
第二類。你控制火箭上下運動
class Rocket {
int x;
int y;
int speedy;
Rocket(){
x = 40;
y = 200;
speedy = 3;
}
void run(){
defender();
move();
restrict();
}
void defender(){
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,255);
rect(x,y-10,20,10);
}
void move() {
if(up)
{
y = y - speedy;
}
if(down)
{
y = y + speedy;
}
}
void restrict(){
if (y < 10) {
y = y + speedy;
}
if (y > 380) {
y = y - speedy;
}
}
boolean IsShot(Rocket myRocket){
if (alien1.x == 40)
{
if(alien1.y>=y && alien1.y<=(y+50))
{
return true;
}
return false;
}
}
}
當其中一個外星人撞上火箭時,我希望遊戲停止。在boolean IsShot(Rocket myRocket)
我不斷收到錯誤「該方法必須返回結果類型布爾值。」
如果'alien1.x!= 40'會發生什麼? –
我得到相同的錯誤 –
直到你的代碼運行,你需要首先編譯才能知道。我的意思是,如果允許編譯,你向我們展示的代碼將返回什麼樣的條件?自己追蹤。現在有哪些代碼可以處理'alien1.x!= 40'的情況?仔細查看你的代碼,並追蹤出方法的路徑。 –