From d9237d5c0193e7fe0d057f520aafe45b19232c3b Mon Sep 17 00:00:00 2001 From: 13766800364 <13766800364@qq.com> Date: Thu, 9 Oct 2025 16:11:35 +0800 Subject: [PATCH] Add File --- .../gitee/drinkjava2/frog/objects/Food.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 history/016c_OneInput/src/main/java/com/gitee/drinkjava2/frog/objects/Food.java diff --git a/history/016c_OneInput/src/main/java/com/gitee/drinkjava2/frog/objects/Food.java b/history/016c_OneInput/src/main/java/com/gitee/drinkjava2/frog/objects/Food.java new file mode 100644 index 0000000..c015858 --- /dev/null +++ b/history/016c_OneInput/src/main/java/com/gitee/drinkjava2/frog/objects/Food.java @@ -0,0 +1,64 @@ +/* Copyright 2018-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + * OF ANY KIND, either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ +package com.gitee.drinkjava2.frog.objects; + +import static com.gitee.drinkjava2.frog.Env.ENV_HEIGHT; +import static com.gitee.drinkjava2.frog.Env.ENV_WIDTH; +import static com.gitee.drinkjava2.frog.Env.FOOD_QTY; + +import java.awt.Graphics; + +import com.gitee.drinkjava2.frog.Env; +import com.gitee.drinkjava2.frog.objects.EnvObject.DefaultEnvObject; +import com.gitee.drinkjava2.frog.util.RandomUtils; + +/** + * Food randomly scatter on Env + * 食物 + * + * @author Yong Zhu + * @since 1.0 + */ +public class Food extends DefaultEnvObject {//食物除了被吃,它自己没有什么活动,所以没有重写active方法 + static Food FOOD = new Food(); //FOOD是一个单例,整个环境只允许有一个FOOD实例 + + public static int food_ated = 0; + + @Override + public void build(Graphics g) { + food_ated = 0; + for (int i = 0; i < FOOD_QTY; i++) { // 随机位置生成食物 + int x = RandomUtils.nextInt(ENV_WIDTH); + int y = RandomUtils.nextInt(ENV_HEIGHT); + if (!Env.hasMaterial(x, y, Material.FOOD)) { + Env.setMaterial(x, y, Material.FOOD); //在环境里标记上FOOD + } + } + } + + @Override + public void destory(Graphics g) { + food_ated = 0; + for (int x = 0; x < ENV_WIDTH; x++) // 清除食物 + for (int y = 0; y < ENV_HEIGHT; y++) { + Env.clearMaterial(x, y, Material.FOOD); + } + } + + public static boolean foundAndAteFood(int x, int y) {// 如果x,y有食物,将其清0,返回true + if (Env.hasMaterial(x, y, Material.FOOD)) { + food_ated++; + Env.clearMaterial(x, y, Material.FOOD);//在环境里清除FOOD + return true; + } + return false; + } +}