【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函数显示游戏结束画面和最终得分。

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

相关推荐
智商低情商凑39 分钟前
Go学习之 - Goroutines和channels
开发语言·学习·golang
半桶水专家39 分钟前
Go 语言时间处理(time 包)详解
开发语言·后端·golang
编程点滴40 分钟前
Go 重试机制终极指南:基于 go-retry 打造可靠容错系统
开发语言·后端·golang
实心儿儿1 小时前
C++ —— 模板进阶
开发语言·c++
萧鼎1 小时前
Python PyTesseract OCR :从基础到项目实战
开发语言·python·ocr
二川bro2 小时前
第57节:Three.js企业级应用架构
开发语言·javascript·架构
sali-tec2 小时前
C# 基于halcon的视觉工作流-章62 点云采样
开发语言·图像处理·人工智能·算法·计算机视觉
这人很懒没留下什么3 小时前
SpringBoot2.7.4整合Oauth2
开发语言·lua
ZHOUZAIHUI3 小时前
WSL(Ubuntu24.04) 安装PostgreSQL
开发语言·后端·scala
欧阳x天4 小时前
C++入门(二)
开发语言·c++