【006期】游戏的开始和结束界面,得分和生命值及其游戏逻辑。

核心代码

java 复制代码
int score;
int lives;

boolean gameOver;
void draw() {
  background(255);
  if (!gameOver) {

     /*游戏中的其他所有逻辑写在此处*/

     
    displayScoreAndLives(); // 显示得分和生命值
  } else {
    displayGameOverScreen(); // 显示游戏结束画面
  }
}
void displayScoreAndLives() {
  fill(0);
  textAlign(LEFT);
  textSize(12);
  text("Score: " + score, 10, 20);
  text("Lives: " + lives, 10, 35);
}

void displayGameOverScreen() {
  textAlign(CENTER);
  textSize(32);
  text("Game Over", width/2, height/2);
  textSize(16);
  text("Final Score: " + score, width/2, height/2 + 25);
}

void restartGame() {
  score = 0;
  lives = 3;
  gameOver = false;
  //各项属性值.clear();
}

这个判断距离的函数非常好,值得收藏

java 复制代码
  boolean isUnderMouse(float mx, float my) {
    return mx >= x && mx <= x + tileWidth && my >= y && my <= y + tileHeight;
  }

下面是一个简单的黑白格游戏的Processing代码示例。游戏中会有黑色的格子从屏幕顶端落下,玩家需要点击它们。如果错过了黑色格子,玩家的生命值会减少。当生命值为零时,游戏结束,并显示最终得分。

java 复制代码
ArrayList<Tile> tiles;
int score;
int lives;
final int tileWidth = 50;
final int tileHeight = 20;
boolean gameOver;

void setup() {
  size(200, 400);
  tiles = new ArrayList<Tile>();
  score = 0;
  lives = 3;
  gameOver = false;
  // 每秒生成一个黑色格子
  frameRate(60);
}

void draw() {
  background(255);
  if (!gameOver) {
    handleTiles(); // 处理格子的移动和生命值
    displayScoreAndLives(); // 显示得分和生命值
  } else {
    displayGameOverScreen(); // 显示游戏结束画面
  }
}

void mouseClicked() {
  if (!gameOver) {
    for (Tile tile : tiles) {
      if (tile.isUnderMouse(mouseX, mouseY)) {
        score++;
        tile.clicked = true;
        break;
      }
    }
  } else {
    // 点击屏幕重新开始游戏
    restartGame();
  }
}

void handleTiles() {
  // 每60帧添加一个新的黑色格子
  if (frameCount % 60 == 0) {
    tiles.add(new Tile(int(random(width/tileWidth)) * tileWidth, -tileHeight));
  }
  
  // 更新和渲染每个格子
  for (int i = tiles.size()-1; i >= 0; i--) {
    Tile tile = tiles.get(i);
    tile.update();
    if (!tile.clicked && tile.y > height) {
      lives--;
      tiles.remove(i);
    } else if (tile.clicked || tile.y > height) {
      tiles.remove(i);
    }
    if (lives <= 0) {
      gameOver = true;
    }
  }
}

void displayScoreAndLives() {
  fill(0);
  textAlign(LEFT);
  textSize(12);
  text("Score: " + score, 10, 20);
  text("Lives: " + lives, 10, 35);
}

void displayGameOverScreen() {
  textAlign(CENTER);
  textSize(32);
  text("Game Over", width/2, height/2);
  textSize(16);
  text("Final Score: " + score, width/2, height/2 + 25);
}

void restartGame() {
  score = 0;
  lives = 3;
  gameOver = false;
  tiles.clear();
}

class Tile {
  float x, y;
  boolean clicked = false;

  Tile(float x, float y) {
    this.x = x;
    this.y = y;
  }

  void update() {
    y += 2; // 格子下落速度
    if (!clicked) {
      fill(0); // 未被点击为黑色
    } else {
      fill(100); // 被点击后变为灰色
    }
    rect(x, y, tileWidth, tileHeight);
  }
  
  boolean isUnderMouse(float mx, float my) {
    return mx >= x && mx <= x + tileWidth && my >= y && my <= y + tileHeight;
  }
}

在这个代码中,我们创建了一个Tile类来表示游戏中的黑色格子。handleTiles函数负责添加新格子,更新它们的位置,并在格子划出屏幕底端时减少生命值。mouseClicked函数会检查玩家是否点击了某个格子,并在游戏结束时允许重新开始。

displayScoreAndLives函数显示当前得分和剩余生命值,而displayGameOverScreen函数显示游戏结束画面和最终得分。

这个游戏示例非常基础,您可以根据自己的需求进一步扩展和美化界面。

相关推荐
everyStudy37 分钟前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
luthane38 分钟前
python 实现average mean平均数算法
开发语言·python·算法
dangoxiba1 小时前
【Unity学习心得】如何使用Unity制作“饥荒”风格的俯视角2.5D游戏
游戏·unity·c#·游戏引擎
凡人的AI工具箱1 小时前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python
sleP4o1 小时前
Python操作MySQL
开发语言·python·mysql
是店小二呀1 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
洛寒瑜1 小时前
【读书笔记-《30天自制操作系统》-23】Day24
开发语言·汇编·笔记·操作系统·应用程序
ephemerals__1 小时前
【c++】动态内存管理
开发语言·c++
咩咩觉主1 小时前
en造数据结构与算法C# 群组行为优化 和 头鸟控制
开发语言·c#
CVer儿1 小时前
条件编译代码记录
开发语言·c++