如何使图像在碰撞时消失,然后重新出现在屏幕上的随机位置

How to make it so an image disappears upon collision and then re-appears in a random spot on the screen

我正在创建一个类似于 Snake 的游戏,其中我的蝙蝠侠角色图像与小丑角色图像发生碰撞并 "catches" 它。一旦第一个消失,另一个小丑就会出现在屏幕上的随机位置,供蝙蝠侠追逐,然后依此类推。

我已经做到了,当我的蝙蝠侠角色与其发生碰撞时,第一个小丑消失了,但我正在努力让第二个小丑出现在屏幕上。我不确定 for 循环是否是最好的 - 但我如何才能做到只有一个小丑出现直到蝙蝠侠 "catches" 它然后下一个可以出现 - 或者可能是一个 if 语句。任何帮助将不胜感激,因为我现在感觉真的很困难。

//loads music for background

import processing.sound.*;

SoundFile file;

String audioName = "dark_knight_rises.wav";

String path;

//loads gif library for background

import gifAnimation.*;

Gif myAnimation;

PImage batman;       //pixel images

PImage joker;        //pixel images

int batmanX = 100;        //batman X position on screen

int batmanY = 100;        //batman Y position on screen

int jokerX = 500;         //joker X position on screen

int jokerY = 500;         //joker Y position on screen

int batman1Size = 50;     //for batman distance

int joker2Size = 50;      //for joker distance

int width = 100;          //width for characters

int height = 100;         // height for characters

boolean showImage = true; // boolean to help joker disappear and reappear

int score = 0;            // declaration for high score




void setup() {

  size(1067, 800);

  //plays background music

  path = sketchPath(audioName);

  file = new SoundFile(this, path);

  file.play();

  //loads background and pixel-style batman/joker

  myAnimation = new Gif(this, "background.gif");

  myAnimation.play();

  batman = loadImage("pixelbatman.png");

  joker = loadImage("pixeljoker.png");
}

void draw() {

  image(myAnimation, 0, 0);   //lays down gif background 

  //display the score

  textSize(20);

  text("Jokers caught:", 900, 40);

  text(score, 1045, 40);


  image(batman, batmanX, batmanY, width, height);   //places Batman character on screen

  //pixel batman movement

  if (keyPressed) {

    if (key == CODED) {

      if (keyCode == UP) batmanY--;

      if (keyCode == RIGHT) batmanX++;

      if (keyCode == DOWN) batmanY++;

      if (keyCode == LEFT) batmanX--;

    }
  }    //end of pixel batman movement


  if (showImage) image(joker, jokerX, jokerY, width, height);   //if Boolean is true, place Joker character on screen

  if (dist(batmanX, batmanY, jokerX, jokerY) < (batman1Size + joker2Size)/10 ) { //if batman and joker collide - joker disappears

    showImage = false;

    score += 1; //increase the score by 1 when batman eats a joker


    // idea is that the first joker disappears and another pops up in a random position

    if (showImage == false) {

      image(joker, random(jokerX, 0), random(0, jokerY), width, height);

    }

  } // end of what happens if batman and joker collide

} // end of void draw

答案比您想象的要简单。事实上,你已经完成了大部分工作,你只需要一只橡皮鸭。

我会接受那份工作。

draw()函数中,你做了很多事情。当你有空的时候,我建议你把这些分解一下。 draw() 是处理中的主循环,因此它总是会被代码堵塞。当你这样做时,目标是有一个易于理解的循环,像这样:

void draw() {
  drawBackground();
  manageInput();
  collisions();
  drawCharacters();
}

现在,谈谈您感兴趣的问题:

你做的好事:

  • 你使用清晰的命名法(你的命名约定使事情易于阅读,初学者犯的一个错误是缩短变量名,因为他们知道他们在说什么,然后它会混淆其他人,包括他们自己)
  • 总体来说非常棒,能够添加 gif 和声音等内容!

我们要做什么来实现小丑的事情:

  1. 删除 showImage 变量。我明白你在这里做什么,这是个好主意,但我们不需要它。
  2. 在您检查碰撞的 If 中,就在您写 showImage = false 的地方,我们将更新小丑的坐标:

像这样(我把它随机化了,但现在你明白了你可以自定义它):

jokerX = (int)random(width);
jokerY = (int)random(height);
  1. 删除if (showImage == false).

Aaand...给你!如果你读得好,你会注意到你的错误是,在 if (showImage == false) 部分,你 "teleported" 小丑并没有真正改变他的协调。

如果您有任何问题,我会留下来。玩得开心!