java画羊_Java游戏-牧羊犬与绵羊

2023-12-02 18:20

本文主要是介绍java画羊_Java游戏-牧羊犬与绵羊,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是一款非常简单的Java游戏,以修正到半截的LGame-Simple-0.2.5开发(也就是LGame-Simple-0.2.5-test版)。

该游戏目前内置有关卡十一关(可配置),以绘制的牧羊犬与绵羊为主要角色,其中牧羊犬将根据玩家鼠标所在位置移动,而绵羊则始终远离牧羊犬,如何利用此一特性,驱赶指定数量绵羊到达指定羊圈中呢?这个,也就是本游戏的游戏目标了。

游戏源码如下:

package org.loon.test;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Transparency;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.awt.image.BufferedImage;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.StreamTokenizer;

import org.loon.framework.game.simple.GameScene;

import org.loon.framework.game.simple.action.IAction;

import org.loon.framework.game.simple.core.Deploy;

import org.loon.framework.game.simple.core.LSystem;

import org.loon.framework.game.simple.core.LTimer;

import org.loon.framework.game.simple.core.LTimerContext;

import org.loon.framework.game.simple.core.ResourceLoader;

import org.loon.framework.game.simple.core.Screen;

import org.loon.framework.game.simple.utils.GraphicsUtils;

/**

* Copyright 2008 - 2009

*

* 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.

*

* @project loonframework

* @author chenpeng

* @email:ceponline@yahoo.com.cn

* @version 0.1

*/

public class Main extends Screen {

private boolean flag;

// 背景色、狗的颜色、羊的颜色

final static private Color bg_c1 = new Color(60, 115, 18),

bg_c2 = new Color(80, 135, 38), dog_c1 = new Color(162, 112, 6),

dog_c2 = new Color(184, 156, 4),

sheep_c1 = new Color(224, 222, 176), sheep_c2 = new Color(255, 255,

255);

private int sheeps, dogs;

private int[] sheep_f;

private double dog_rate, sheep_rate;

private double[] dog_px, dog_py, dog_vx, dog_vy;

private double[] sheep_px, sheep_py, sheep_vx, sheep_vy;

private int[] map_n, map_x, map_y, map_r, map_f;

// 游戏等级、游戏时间、地图大小、关卡索引、动作桢数

private int level, gtime, maps, stage, frames;

private int[] stage_dn, stage_dx, stage_dy, stage_sn, stage_sx, stage_sy,

stage_cl;

private int[][] stage_cn, stage_cx, stage_cy, stage_cr;

private LTimer timer = new LTimer(500);

// 制作一个监听类,用以判定当前关卡是否完成

private class ClickAction implements IAction {

public void doAction(long timer) {

if (flag) {

if (level == -1) {

resetLevel();

} else {

int index = 0;

for (int i = 0; i < maps; i++) {

if (map_f[i] == 1) {

index++;

}

}

if (index == maps) {

resetLevel();

}

}

}

}

}

public Main() {

this.level = -1;

this.stage = 100;

this.maps = 10;

this.dogs = 10;

this.sheeps = 100;

this.dog_rate = 1.0D;

this.sheep_rate = 1.0D;

this.flag = true;

this.sheep_f = new int[sheeps];

this.sheep_px = new double[sheeps];

this.sheep_py = new double[sheeps];

this.sheep_vx = new double[sheeps];

this.sheep_vy = new double[sheeps];

this.dog_px = new double[sheeps];

this.dog_py = new double[sheeps];

this.dog_vx = new double[sheeps];

this.dog_vy = new double[sheeps];

this.map_n = new int[maps];

this.map_x = new int[maps];

this.map_y = new int[maps];

this.map_r = new int[maps];

this.map_f = new int[maps];

this.stage_dn = new int[stage];

this.stage_dx = new int[stage];

this.stage_dy = new int[stage];

this.stage_sn = new int[stage];

this.stage_sx = new int[stage];

this.stage_sy = new int[stage];

this.stage_cl = new int[stage];

this.stage_cn = new int[stage][10];

this.stage_cx = new int[stage][10];

this.stage_cy = new int[stage][10];

this.stage_cr = new int[stage][10];

// 将过关判定匹配给鼠标右键点击事件

this.addMouseEvents(MouseEvent.BUTTON1, "MOUSE_CLICK",

new ClickAction());

// 创建游戏背景

this.createGraphics();

// 初始化关卡数据

this.initialize();

}

/**

* 创建游戏背景

*

*/

private void createGraphics() {

BufferedImage screen = GraphicsUtils.createImage(getWidth(),

getHeight(), Transparency.BITMASK);

Graphics2D g = screen.createGraphics();

g.setColor(bg_c1);

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(bg_c2);

g.fillRect(20, 20, getWidth() - 40, getHeight() - 40);

g.dispose();

setBackground(screen);

}

/**

* 加载初始数据

*

*/

private void initialize() {

InputStream in = ResourceLoader

.getResourceToInputStream("games/sheep.dat");

try {

StreamTokenizer str = new StreamTokenizer(new InputStreamReader(in));

int read;

while ((read = str.nextToken()) != -1) {

if (read == -3) {

if ("D".equals(str.sval)) {

str.nextToken();

stage = (int) str.nval;

str.nextToken();

stage_dn[stage] = (int) str.nval;

str.nextToken();

stage_dx[stage] = (int) str.nval;

str.nextToken();

stage_dy[stage] = (int) str.nval;

} else if ("S".equals(str.sval)) {

str.nextToken();

stage = (int) str.nval;

str.nextToken();

stage_sn[stage] = (int) str.nval;

str.nextToken();

stage_sx[stage] = (int) str.nval;

str.nextToken();

stage_sy[stage] = (int) str.nval;

} else if ("C".equals(str.sval)) {

str.nextToken();

stage = (int) str.nval;

str.nextToken();

int j = (int) str.nval;

str.nextToken();

stage_cn[stage][j] = (int) str.nval;

str.nextToken();

stage_cx[stage][j] = (int) str.nval;

str.nextToken();

stage_cy[stage][j] = (int) str.nval;

str.nextToken();

stage_cr[stage][j] = (int) str.nval;

if (stage_cl[stage] < j) {

stage_cl[stage] = j;

}

}

}

}

} catch (Exception ex) {

stage = 0;

flag = false;

} finally {

try {

in.close();

in = null;

} catch (Exception e) {

}

}

}

/**

* 绘制游戏画面

*/

public void draw(Graphics2D g) {

if (level == -1) {

drawStart(g);

return;

}

drawMap(g);

drawDog(g);

drawSheep(g);

drawTitle(g);

}

/**

* 更改游戏运行数据

*/

public void alter(LTimerContext time) {

frames++;

if (timer.action(time.getTimeSinceLastUpdate())) {

dog_rate = 0.2 * (double) frames;

sheep_rate = 0.2 * (double) frames;

frames = 0;

gtime++;

}

}

/**

* 刷新游戏等级

*

*/

private void resetLevel() {

level = level == -1 ? 0 : (level + 1) % (stage + 1);

flag = false;

gtime = 0;

dogs = stage_dn[level];

for (int i = 0; i < dogs; i++) {

dog_px[i] = stage_dx[level]

+ (int) (LSystem.random.nextDouble() * 30D);

dog_py[i] = stage_dy[level]

+ (int) (LSystem.random.nextDouble() * 30D);

dog_vx[i] = 0.0D;

dog_vy[i] = 0.0D;

}

sheeps = stage_sn[level];

for (int j = 0; j < sheeps; j++) {

sheep_px[j] = stage_sx[level]

+ (int) (LSystem.random.nextDouble() * 30D);

sheep_py[j] = stage_sy[level]

+ (int) (LSystem.random.nextDouble() * 30D);

sheep_vx[j] = 0.0D;

sheep_vy[j] = 0.0D;

sheep_f[j] = -1;

}

maps = stage_cl[level] + 1;

for (int j = 0; j < maps; j++) {

map_x[j] = stage_cx[level][j];

map_y[j] = stage_cy[level][j];

map_r[j] = stage_cr[level][j];

map_n[j] = stage_cn[level][j];

map_f[j] = 0;

}

}

/**

* 绘制游戏开始画面

*

* @param g

*/

private void drawStart(Graphics2D g) {

g.setColor(bg_c1);

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(bg_c2);

g.fillRect(20, 20, getWidth() - 40, getHeight() - 40);

g.setColor(Color.white);

g.drawString("点击屏幕开始游戏", 260, 200);

}

/**

* 绘制游戏状态提示

*

* @param g

*/

private void drawTitle(Graphics2D g) {

g.setColor(Color.white);

g.drawString("等级 : " + (level + 1), 25, getHeight() - 30);

g.drawString("绵羊 : " + sheeps, 100, getHeight() - 30);

g.drawString("猎犬 : " + dogs, 185, getHeight() - 30);

g.drawString("时间 : " + gtime, 255, getHeight() - 30);

}

/**

* 绘制游戏地图

*

* @param g

*/

private void drawMap(Graphics2D g) {

double mx, my, angle;

for (int i = 0; i < maps; i++) {

if (map_f[i] == 0) {

int j = 0;

for (int k = 0; k < sheeps; k++) {

mx = sheep_px[k] - (double) map_x[i];

my = sheep_py[k] - (double) map_y[i];

angle = Math.sqrt(mx * mx + my * my);

if (angle < (double) map_r[i]) {

j++;

}

}

if (j == map_n[i]) {

map_f[i] = 1;

for (int l = 0; l < sheeps; l++) {

mx = sheep_px[l] - (double) map_x[i];

my = sheep_py[l] - (double) map_y[i];

angle = Math.sqrt(mx * mx + my * my);

if (angle < (double) map_r[i]) {

sheep_f[l] = i;

}

}

}

g.setColor(Color.green);

g.drawOval(map_x[i] - map_r[i], map_y[i] - map_r[i],

map_r[i] * 2, map_r[i] * 2);

g.drawString("" + map_n[i], map_x[i] - 5, map_y[i] + 5);

} else if (map_f[i] == 1) {

flag = true;

g.setColor(Color.red);

g.drawOval(map_x[i] - map_r[i], map_y[i] - map_r[i],

map_r[i] * 2, map_r[i] * 2);

g.drawString("" + map_n[i], map_x[i] - 5, map_y[i] + 5);

}

}

}

/**

* 绘制猎犬

*

* @param g

*/

private void drawDog(Graphics2D g) {

for (int i = 0; i < dogs; i++) {

double dx = (double) getMouseX() - dog_px[i];

double dy = (double) getMouseY() - dog_py[i];

for (int j = 0; j < dogs; j++) {

double mx = dog_px[j] - dog_px[i];

double my = dog_py[j] - dog_py[i];

double angle = Math.sqrt(mx * mx + my * my) + 1.0D;

if (angle < 40D) {

dx += -mx / angle;

dy += -my / angle;

dog_vx[j] += mx / angle;

dog_vy[j] += my / angle;

} else {

dx += mx / angle;

dy += my / angle;

}

}

double offset = Math.sqrt(dx * dx + dy * dy) + 1.0D;

dog_vx[i] /= 1.01D;

dog_vy[i] /= 1.01D;

dog_vx[i] += dx / offset;

dog_vy[i] += dy / offset;

dog_px[i] += dog_vx[i] / dog_rate;

dog_py[i] += dog_vy[i] / dog_rate;

if (dog_px[i] < 0.0D) {

dog_vx[i] = 0.0D;

}

if (dog_py[i] < 0.0D) {

dog_vy[i] = 0.0D;

}

if (dog_px[i] > (double) (getWidth() - 5)) {

dog_vx[i] = 0.0D;

}

if (dog_py[i] > (double) (getHeight() - 5)) {

dog_vy[i] = 0.0D;

}

offset = Math.sqrt(dog_vx[i] * dog_vx[i] + dog_vy[i] * dog_vy[i]) + 1.0D;

double vx1 = dog_vx[i] / offset;

double vy1 = dog_vy[i] / offset;

int x = (int) dog_px[i] - 3;

int y = (int) dog_py[i] - 3;

int x1 = x + (int) (vx1 * 5D);

int y1 = y + (int) (vy1 * 5D);

g.setColor(Color.darkGray);

g.fillOval(x, y + 3, 8, 8);

g.setColor(dog_c2);

g.fillOval(x, y, 5, 5);

g.setColor(dog_c1);

g.fillOval(x1, y1, 5, 5);

}

}

/**

* 绘制绵羊

*

* @param g

*/

private void drawSheep(Graphics2D g) {

double nx, ny, angle;

for (int i = 0; i < sheeps; i++) {

double mx = 0.0D;

double my = 0.0D;

if (sheep_f[i] >= 0) {

mx = (double) map_x[sheep_f[i]] - sheep_px[i];

my = (double) map_y[sheep_f[i]] - sheep_py[i];

}

for (int j = 0; j < sheeps; j++) {

nx = sheep_px[j] - sheep_px[i];

ny = sheep_py[j] - sheep_py[i];

angle = Math.sqrt(nx * nx + ny * ny) + 1.0D;

if (angle < 15D) {

mx += -nx / angle;

my += -ny / angle;

sheep_vx[j] += nx / angle;

sheep_vy[j] += ny / angle;

} else {

mx += nx / angle;

my += ny / angle;

}

}

if (sheep_f[i] == -1) {

for (int k = 0; k < dogs; k++) {

nx = sheep_px[i] - dog_px[k];

ny = sheep_py[i] - dog_py[k];

angle = Math.sqrt(nx * nx + ny * ny) + 1.0D;

if (angle < 200D) {

mx += (nx / angle) * (double) sheeps;

my += (ny / angle) * (double) sheeps;

}

}

}

angle = Math.sqrt(mx * mx + my * my) + 1.0D;

sheep_vx[i] /= 1.1000000000000001D;

sheep_vy[i] /= 1.1000000000000001D;

nx = mx / angle;

ny = my / angle;

sheep_vx[i] += nx;

sheep_vy[i] += ny;

sheep_px[i] += sheep_vx[i] / sheep_rate;

sheep_py[i] += sheep_vy[i] / sheep_rate;

if (sheep_px[i] < 20D) {

sheep_px[i] = 20D;

sheep_vx[i] = 0.0D;

}

if (sheep_py[i] < 20D) {

sheep_py[i] = 20D;

sheep_vy[i] = 0.0D;

}

if (sheep_px[i] > (double) (getWidth() - 20)) {

sheep_px[i] = getWidth() - 20;

sheep_vx[i] = 0.0D;

}

if (sheep_py[i] > (double) (getHeight() - 20)) {

sheep_py[i] = getHeight() - 20;

sheep_vy[i] = 0.0D;

}

angle = Math.sqrt(sheep_vx[i] * sheep_vx[i] + sheep_vy[i]

* sheep_vy[i]) + 1.0D;

nx = sheep_vx[i] / angle;

ny = sheep_vy[i] / angle;

int x = (int) sheep_px[i] - 3;

int y = (int) sheep_py[i] - 3;

int x1 = x + (int) (nx * 5D);

int y1 = y + (int) (ny * 5D);

g.setColor(Color.darkGray);

g.fillOval(x, y + 3, 10, 10);

g.setColor(sheep_c2);

g.fillOval(x, y, 7, 7);

g.setColor(sheep_c1);

g.fillOval(x1, y1, 7, 7);

}

}

public void leftClick(MouseEvent e) {

}

public void middleClick(MouseEvent e) {

}

public void onKey(KeyEvent e) {

}

public void onKeyUp(KeyEvent e) {

}

public void rightClick(MouseEvent e) {

}

public static void main(String[] args) {

GameScene frame = new GameScene("牧羊犬与绵羊", 640, 400);

Deploy deploy = frame.getDeploy();

deploy.setScreen(new Main());

deploy.setLogo(false);

deploy.setShowFPS(true);

deploy.setFPS(100);

deploy.mainLoop();

frame.showFrame();

}

}

截图如下:

20091129_00633950977148750000.jpg

20091129_01633950977838437500.jpg

20091129_02633950977839218750.jpg

————————————————————

为LGame-Simple-0.2.5新增的几个小例子之一,在

LGame-Simple-0.2.5正式发布以前,先发点这些小东西凑文章数……

这篇关于java画羊_Java游戏-牧羊犬与绵羊的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/446331

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis