如何在场景之间切换'使用开关语句

发布于 2025-01-27 22:20:26 字数 1870 浏览 1 评论 0原文

因此,我正在创建一个交互式故事,我想通过单击按钮在场景之间切换(例如标准的旧口袋妖怪游戏,除了没有动作,只是单击)。

我可能应该使用开关语句吗?但是我不知道如何实施场景的更改并将场景加载到案例中。我也不知道我是否需要在情况下使用“ IF”语句。

这可能会令人困惑,因为这对我来说很困惑。感谢大家提供的任何帮助!我很拼命;)

到目前为止,代码如下:

//PImages
PImage startScreen;
PImage[] waves = new PImage[3];
PImage[] scenes = new PImage[1];

int switchVariable;

//Objects
Button play;


void setup() {
  size(750, 600);
  background(#A3E9EA);
  
  //Initialising Objects
  play = new Button(50, 480, 330);
  
  //loading wave images
  waves[0] = loadImage("wave1.png");
  waves[1] = loadImage("wave2.png");
  waves[2] = loadImage("wave3.png");
  
  //loading start image
  startScreen = loadImage("start-screen.png");
  
  //loading scenes
  scenes[0] = loadImage("scene-one.png");
  
  //setting frame rate
  frameRate(6);
}


void draw() {
background(#A3E9EA);
frameCount++;
println (frameCount);

//drawing wave animation
  if (frameCount < 5) {
    image(waves[0], 0, 0);
  } 
  else {
    image(waves[1], 0, 0);
  } 
  
  if (frameCount < 15 & frameCount > 10) {
    background(#A3E9EA);
    image(waves[2], 0, 0);
    frameCount = 0;
  }
  
  //drawing start screen
  image(startScreen, 0, 0);
  
  //displaying play button
  if (play.visible) play.buttonDisplay();
}
  
  
void mousePressed() {
  
  if (play.visible) {
    float d = dist(play.x+110, play.y+22, mouseX, mouseY);
    if (d <= play.radius){
      background(#A3E9EA);
      image(scenes[0], 0, 0);
    }
  }
}

按钮类:

class Button {
  float radius;
  float x;
  float y;
  PImage[] buttonImage = new PImage[2];
  boolean visible;
  
  Button(float _radius, float _x, float _y) {
    radius = _radius;
    visible = true;
    x = _x;
    y = _y;
    buttonImage[0] = loadImage("play-game-button.png");
  }
  
  void buttonDisplay() {
    image(buttonImage[0], x, y);
  }
}

so I'm creating an interactive story, and I want to switch between scenes by clicking buttons (like a standard old pokemon game except there isn't movement, just clicking).

I should probably use a switch statement right? But I don't know how to implement the changing of scenes and loading scenes into the cases. I also don't know if I would need to use 'if' statements within the cases.

This might be confusing, because it is very confusing to me. I'd appreciate any help you guys offer! I am desperate ;)

Code so far is below:

//PImages
PImage startScreen;
PImage[] waves = new PImage[3];
PImage[] scenes = new PImage[1];

int switchVariable;

//Objects
Button play;


void setup() {
  size(750, 600);
  background(#A3E9EA);
  
  //Initialising Objects
  play = new Button(50, 480, 330);
  
  //loading wave images
  waves[0] = loadImage("wave1.png");
  waves[1] = loadImage("wave2.png");
  waves[2] = loadImage("wave3.png");
  
  //loading start image
  startScreen = loadImage("start-screen.png");
  
  //loading scenes
  scenes[0] = loadImage("scene-one.png");
  
  //setting frame rate
  frameRate(6);
}


void draw() {
background(#A3E9EA);
frameCount++;
println (frameCount);

//drawing wave animation
  if (frameCount < 5) {
    image(waves[0], 0, 0);
  } 
  else {
    image(waves[1], 0, 0);
  } 
  
  if (frameCount < 15 & frameCount > 10) {
    background(#A3E9EA);
    image(waves[2], 0, 0);
    frameCount = 0;
  }
  
  //drawing start screen
  image(startScreen, 0, 0);
  
  //displaying play button
  if (play.visible) play.buttonDisplay();
}
  
  
void mousePressed() {
  
  if (play.visible) {
    float d = dist(play.x+110, play.y+22, mouseX, mouseY);
    if (d <= play.radius){
      background(#A3E9EA);
      image(scenes[0], 0, 0);
    }
  }
}

Button Class:

class Button {
  float radius;
  float x;
  float y;
  PImage[] buttonImage = new PImage[2];
  boolean visible;
  
  Button(float _radius, float _x, float _y) {
    radius = _radius;
    visible = true;
    x = _x;
    y = _y;
    buttonImage[0] = loadImage("play-game-button.png");
  }
  
  void buttonDisplay() {
    image(buttonImage[0], x, y);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2025-02-03 22:20:26

下面的源代码显示了您问题的一种可能的方法。它不依赖“开关”,而是使用具有类似于Java步进的自定义控件。步进控制值与场景数组中的图像相对应,而背景()则相应设置。需要单独的按钮类,并在此演示下为此进行编码。

color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
color GREEN = color(0, 255, 0);
color ORANGE = color(247, 168, 7);

PFont font;

PImage[] scenes = new PImage[6];

// **** Up/Down Buttons init, min, max values **** //
final int _initValue = 2;
final int _maxValue = 5;
final int _minValue = 0;

int stepperValue = 0;

Button _up;
Button _dwn;
Button _quit;

final int _displayX = 160;
final int _displayY = 30;
final int _displayW = 100;
final int _displayH = 24;

final int _txtSize = 18;

void stepperValueDisplay(int value) {
  fill(WHITE); // background color
  noStroke();
  rect(_displayX, _displayY, _displayW, _displayH, 0);
  fill(BLACK); // text color
  textSize(_txtSize);
  textAlign(CENTER);
  String str = String.format("Scene %d", value);
  text(str, _displayX, _displayY, _displayW, _displayH);
}

void scene_0(){
  background(RED);
  save("scene0.png");
}

void scene_1(){
  background(GREEN);
  save("scene1.png");
}

void scene_2(){
  background(BLACK);
  save("scene2.png");
}

void scene_3(){
  background(YELLOW);
  save("scene3.png");
}

void scene_4(){
  background(LTGRAY);
 save("scene4.png"); 
}

void scene_5(){
  background(ORANGE);
 save("scene5.png"); 
}


void setup() {
  size(600, 600);
  background(BLUE);
  font = createFont("Menlo-Bold", 20);
  _dwn = new Button( _displayX - 40, _displayY, 40, 24, "--", LTGRAY, BLACK);
  _up = new Button( _displayX + _displayW, _displayY, 40, 24, "++", LTGRAY, BLACK);
  stepperValue = _initValue;
  _quit = new Button(width - 60, 20, 30, 24, "Q", LTGRAY, BLACK);
  scene_0();
  scene_1();
  scene_2();
  scene_3();
  scene_4();
  scene_5();
  scenes[0] = loadImage("scene0.png");
  scenes[1] = loadImage("scene1.png");
  scenes[2] = loadImage("scene2.png");
  scenes[3] = loadImage("scene3.png");
  scenes[4] = loadImage("scene4.png");
  scenes[5] = loadImage("scene5.png");
}

void draw() {
  background(scenes[stepperValue]);
  _up.display();
  _dwn.display();
  _quit.display();
  stepperValueDisplay(stepperValue);
}

void mousePressed() {
  if (mouseX > _quit.x && mouseX < _quit.x + _quit.w && mouseY > _quit.y && mouseY < _quit.y + _quit.h) {
    exit();
  }
  if (mouseX > _up.x && mouseX < _up.x + _up.w && mouseY > _up.y && mouseY < _up.y + _up.h) {
    stepperValue++;
    if (stepperValue > _maxValue) {
      stepperValue = _maxValue;
    }
    stepperValueDisplay(stepperValue);
  }
  if (mouseX > _dwn.x && mouseX < _dwn.x + _dwn.w && mouseY > _dwn.y && mouseY < _dwn.y + _dwn.h) {
    stepperValue--;
    if (stepperValue < _minValue) {
      stepperValue = _minValue;
    }
    stepperValueDisplay(stepperValue);
  }
}

按钮类:

int _btnTxtSize = 18;

class Button {
 float x, y, w, h;
 String title;
 color bkgrndColor;
 color txtColor;

 // Constructor
 Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color textColor) {
   x = xpos;
   y = ypos;
   w = wt;
   h = ht;
   title = titleStr;
   bkgrndColor = background;
   txtColor = textColor;
 }
 
 void display(){
   fill(bkgrndColor);
   noStroke();
   rect( x, y, w, h, 0); 
   fill(txtColor);
   textSize(_btnTxtSize);
   textAlign(CENTER);
   text(title, x, y, w, h);
 }
 }
 

The source code below shows one possible approach to your question. It does not rely on ‘switch’ but instead uses a custom control with up and down buttons similar to a Java stepper. The stepper control value corresponds with images in the scenes array and the background() is set accordingly. A separate button class is required and code for this follows the demo.

color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
color GREEN = color(0, 255, 0);
color ORANGE = color(247, 168, 7);

PFont font;

PImage[] scenes = new PImage[6];

// **** Up/Down Buttons init, min, max values **** //
final int _initValue = 2;
final int _maxValue = 5;
final int _minValue = 0;

int stepperValue = 0;

Button _up;
Button _dwn;
Button _quit;

final int _displayX = 160;
final int _displayY = 30;
final int _displayW = 100;
final int _displayH = 24;

final int _txtSize = 18;

void stepperValueDisplay(int value) {
  fill(WHITE); // background color
  noStroke();
  rect(_displayX, _displayY, _displayW, _displayH, 0);
  fill(BLACK); // text color
  textSize(_txtSize);
  textAlign(CENTER);
  String str = String.format("Scene %d", value);
  text(str, _displayX, _displayY, _displayW, _displayH);
}

void scene_0(){
  background(RED);
  save("scene0.png");
}

void scene_1(){
  background(GREEN);
  save("scene1.png");
}

void scene_2(){
  background(BLACK);
  save("scene2.png");
}

void scene_3(){
  background(YELLOW);
  save("scene3.png");
}

void scene_4(){
  background(LTGRAY);
 save("scene4.png"); 
}

void scene_5(){
  background(ORANGE);
 save("scene5.png"); 
}


void setup() {
  size(600, 600);
  background(BLUE);
  font = createFont("Menlo-Bold", 20);
  _dwn = new Button( _displayX - 40, _displayY, 40, 24, "--", LTGRAY, BLACK);
  _up = new Button( _displayX + _displayW, _displayY, 40, 24, "++", LTGRAY, BLACK);
  stepperValue = _initValue;
  _quit = new Button(width - 60, 20, 30, 24, "Q", LTGRAY, BLACK);
  scene_0();
  scene_1();
  scene_2();
  scene_3();
  scene_4();
  scene_5();
  scenes[0] = loadImage("scene0.png");
  scenes[1] = loadImage("scene1.png");
  scenes[2] = loadImage("scene2.png");
  scenes[3] = loadImage("scene3.png");
  scenes[4] = loadImage("scene4.png");
  scenes[5] = loadImage("scene5.png");
}

void draw() {
  background(scenes[stepperValue]);
  _up.display();
  _dwn.display();
  _quit.display();
  stepperValueDisplay(stepperValue);
}

void mousePressed() {
  if (mouseX > _quit.x && mouseX < _quit.x + _quit.w && mouseY > _quit.y && mouseY < _quit.y + _quit.h) {
    exit();
  }
  if (mouseX > _up.x && mouseX < _up.x + _up.w && mouseY > _up.y && mouseY < _up.y + _up.h) {
    stepperValue++;
    if (stepperValue > _maxValue) {
      stepperValue = _maxValue;
    }
    stepperValueDisplay(stepperValue);
  }
  if (mouseX > _dwn.x && mouseX < _dwn.x + _dwn.w && mouseY > _dwn.y && mouseY < _dwn.y + _dwn.h) {
    stepperValue--;
    if (stepperValue < _minValue) {
      stepperValue = _minValue;
    }
    stepperValueDisplay(stepperValue);
  }
}

Button Class:

int _btnTxtSize = 18;

class Button {
 float x, y, w, h;
 String title;
 color bkgrndColor;
 color txtColor;

 // Constructor
 Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color textColor) {
   x = xpos;
   y = ypos;
   w = wt;
   h = ht;
   title = titleStr;
   bkgrndColor = background;
   txtColor = textColor;
 }
 
 void display(){
   fill(bkgrndColor);
   noStroke();
   rect( x, y, w, h, 0); 
   fill(txtColor);
   textSize(_btnTxtSize);
   textAlign(CENTER);
   text(title, x, y, w, h);
 }
 }
 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文