在处理中分配状态各种值 [英] Assign states various values in Processing

查看:51
本文介绍了在处理中分配状态各种值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于图块的游戏,其中各种图块具有不同的状态.我现在想添加一个玩家对象,这个对象会不断检查瓷砖的状态以便移动.

I am working on a tile based game, where various tiles have different states. I am now looking to add a player object, this object will continually check the states of the tiles in order to move.

我的瓷砖类:

class Tile {

  int x, y;
  boolean empty = true;
  boolean baseCell = true; 
  boolean active = false; 

  public static final int EMPTY = 0;
  public static final int BASE = 1;
  public static final int ACTIVE = 2;

  Tile(int x_, int y_, boolean baseCell_) {
    x=x_;
    y=y_;
    baseCell=baseCell_;
  }

  void display() {
    if (baseCell) {
      fill(0, 0, 255);
    } else {
      if (empty) {
        fill (255);
      } else if (active) {
        fill(255, 100, 50);
      }
    }
    stroke(0);
    rect(x, y, 50, 50);
  }
}

游戏开始时,绘制了4个baseCell,无法更改.用户可以单击其他单元格将它们的值从空更改为活动或将活动更改为空.

When the game starts, 4 baseCells are drawn and cannot be changed. The user is able to click on other cells to change their values from empty to active or active to empty.

目前我不确定如何为布尔值分配我设置的静态值 - 或者这可能不是最好的方法.

Currently I am unsure how to assign booleans the static values I have set - or perhaps this is not the best approach.

任何帮助或指导将不胜感激.

Any help or guidance would be much appreciated.

推荐答案

正如我在推荐中提到的,不是对每个状态使用布尔值,而是可以使用单个整数来跟踪单元格状态.这也将允许您使用 switch 语句.if/else 没有任何问题,您可以实现相同的目的,但它可能有助于整理.

As I was mentioning in the commend, rather than using a boolean for each state, you can use a single integer to keep track of the cell state. This will also allow you to use a switch statement. There's nothing wrong with if/else, you can achieve the same thing, but it may help things tidy.

以下是上述概念的基本证明,主要基于您现有的代码:

Here's a basic proof of the above concept based mostly on your existing code:

//a few test tiles
int numTiles = 3;
//prepare an array to store each tile
Tile[] tiles = new Tile[numTiles];

void setup(){
  size(150,150);
  //initialize each tile
  for(int i = 0 ; i  < numTiles; i++) 
    tiles[i] = new Tile(50*i,50,random(1) > .5);//50% pseudo-random change of getting a BASE cell
}
void draw(){
  background(255);
  //render tiles (they'll handle their own states internally)
  for(int i = 0 ; i  < numTiles; i++){ 
    tiles[i].display();
  }
}
void mouseReleased(){
  //update each tile on click
  for(int i = 0 ; i  < numTiles; i++)
    tiles[i].click(mouseX,mouseY);
}

class Tile {

  int x, y;
  int size = 50;
  int state = 0; 

  public static final int EMPTY = 0;
  public static final int BASE = 1;
  public static final int ACTIVE = 2;

  Tile(int x_, int y_, boolean baseCell_) {
    x=x_;
    y=y_;
    if(baseCell_) state = BASE;
    else          state = EMPTY;
  }

  void click(int mx,int my){
    //check if the mouse coordinates are within the tile's rectangle
    boolean isOver = (mx >= x && mx <= x + size) && (my >= y && my <= y + size);
    if(isOver){//if so, update states
      switch(state){
        case EMPTY:
          state = ACTIVE;
        break;
        case ACTIVE:
          state = EMPTY;
        break;
        case BASE:
          println("BASE cell clicked, change what happens here");
        break;
      }
    }
  }

  void display() {
    switch(state){
        case EMPTY:
          fill(255);
        break;
        case ACTIVE:
          fill(255, 100, 50);
        break;
        case BASE:
          fill(0,0,255);
        break;
      }

    stroke(0);
    rect(x, y, size, size);
  }
}

稍后您可以选择从 switch 语句中调用函数以使代码更易于管理(尤其是在您可能使用多个状态时)

Later on you may choose to call functions from within the switch statements to keep the code easier to manage (especially if you might use many states)

这篇关于在处理中分配状态各种值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆