本文主要是介绍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();
}
}
截图如下:
————————————————————
为LGame-Simple-0.2.5新增的几个小例子之一,在
LGame-Simple-0.2.5正式发布以前,先发点这些小东西凑文章数……
这篇关于java画羊_Java游戏-牧羊犬与绵羊的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!