⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamesprites.java

📁 一个基于JAVA的手机射击类游戏``有过关模式
💻 JAVA
字号:
package jsky;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;

public class GameSprites extends Sprite {

	private int xSpeed, ySpeed; //移动速度

	private int action; //边界动作

	private GameCanvas canvas;// 这里用到了关卡画布的父类

	public static final int HIDE = 1; //隐藏

	public static final int WRAP = 2; //折返

	public static final int BOUNCE = 3; //反弹

	public static final int STOP = 4; //停止

	private boolean moveMode; //移动模式是否随机

	private Random random; //随机数产生器

	/**
	 * 静态精灵构造函数参数说明
	 * 
	 * @param image
	 *            精灵的图片元素
	 * @param xMoveSpeed
	 *            精灵的X轴速度
	 * @param yMoveSpeed
	 *            精灵的Y轴速度
	 * @param boundsAction
	 *            精灵的边界动作
	 * @param parentCanvas
	 *            精灵依附的画布
	 */
	// ===================没有动画的精灵的constructor start==================
	public GameSprites(Image image, int xMoveSpeed, int yMoveSpeed,
			int boundsAction, GameCanvas parentCanvas) {
		super(image);// Sprite的构造函数 静态的

		// Set the XY speed
		xSpeed = xMoveSpeed;
		ySpeed = yMoveSpeed;

		// Set the bounds action
		action = boundsAction;

		// Set the parent canvas
		canvas = parentCanvas;
	}

	// ===================没有动画的精灵的constructor end====================

	/**
	 * 动态精灵构造函数参数说明
	 * 
	 * @param image
	 *            精灵的图片元素
	 * @param frameWidth
	 *            精灵帧动画的宽度
	 * @param frameHeight
	 *            精灵帧动画的高度
	 * @param boundsAction
	 *            精灵的边界动作
	 * @param parentCanvas
	 *            精灵依附的画布
	 * @param isRandomMove
	 *            精灵的移动方式是否随机
	 */
	// =========================有动画的精灵的constructor start==============
	public GameSprites(Image image, int frameWidth, int frameHeight,
			  int boundsAction,
			GameCanvas parentCanvas, boolean isRandomMove) {
		super(image, frameWidth, frameHeight);// Sprite的构造函数 动态的

		action = boundsAction;
		canvas = parentCanvas;
		moveMode = isRandomMove;
	}

	// =======================有动画的精灵的constructor end================

	public void update() {
		// Set the Random number generator;
		random = new Random();
		if (moveMode) {
			if (random.nextInt() % 20 == 0) {
				xSpeed = Math.min(Math.max(xSpeed + random.nextInt() % 2, -8),
						8);
				ySpeed = Math.min(Math.max(ySpeed + random.nextInt() % 2, -8),
						8);
			}
			// Move the sprite based on its speed
			move(xSpeed, ySpeed);
		} else {
			move(xSpeed, ySpeed);
		}
	}

	public void checkBounds() {
		
		if (action == HIDE) {
			if (getX() < 0 || getX() > (canvas.getWidth() - getWidth())
					|| getY() < 0
					|| getY() > (canvas.getHeight() - getHeight()))
				setVisible(false);
		}
		
		else if (action == WRAP) {
			
			if (getX() < -getWidth())
				setPosition(canvas.getWidth(), getY());
			else if (getX() > canvas.getWidth())
				setPosition(-getWidth(), getY());
			if (getY() < -getHeight())
				setPosition(getX(), canvas.getHeight());
			else if (getY() > canvas.getHeight())
				setPosition(getX(), -getHeight());
		}
	
		else if (action == BOUNCE) {
			if (getX() < 0 || getX() > (canvas.getWidth() - getWidth()))
				xSpeed = -xSpeed;
			if (getY() < 0 || getY() > (canvas.getHeight() - getHeight()))
				ySpeed = -ySpeed;
		}
		else {
			if (getX() < 0)
				setPosition(0, getY());
			else if (getX() > (canvas.getWidth() - getWidth()))
				setPosition(canvas.getWidth() - getWidth(), getY());
			if (getY() < 0)
				setPosition(getX(), 0);
			else if (getY() > (canvas.getHeight() - getHeight()))
				setPosition(getX(), canvas.getHeight() - getHeight());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -