黑马程序员技术交流社区
标题:
java完整2D游戏连载--part4 (gameObject Fontloader 装备属性 装备 ...
[打印本页]
作者:
2666fff
时间:
2015-5-7 23:45
标题:
java完整2D游戏连载--part4 (gameObject Fontloader 装备属性 装备 ...
本帖最后由 2666fff 于 2015-5-7 23:49 编辑
首先声明:这是一个完整2D游戏,包括打怪,升级,跑地图,开宝箱,加血,捡装备等等基本游戏元素,
所有代码,都是我自己在学校的时候一行一行敲出来的,绝无雷同。
更多代码可以去看我的github,跟我论坛账号同一用户名。
接上篇 :
java完整2D游戏连载--part3 (地图 人物 物品栏 类)
http://bbs.itheima.com/thread-194627-1-1.html
到本部分为止,基本完成游戏基本package,附上project2package 代码,zip附加
具体文件结构请查看第一篇
java完整2D游戏连载--part1(所有代码均为自主完成)
http://bbs.itheima.com/thread-194611-1-1.html
item.java 装备物品类
package project2;
/*
* item's ab class.
* author: weiqian wang<wangw>
* */
import java.util.ArrayList;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
public abstract class Item
{
/** Every different type of item */
public static enum Type
{
WEAPON, ACCESSORY, CONSUMABLE, QUEST
}
/** The item's name */
protected String name;
/** The type of the item */
protected Type type;
/** The Image object representing the item in the world */
protected Image avatar;
/** A list of the bonuses applied to the item */
protected ArrayList<Bonus> bonuses;
/**
* Create an item object with optional bonuses
*
* @param name
* Name of the item
* @param type
* Item.Type of the item
* @param avatar
* Image representing the item
* @param bonuses
* Bonuses to be applied to the item (null for none)
*/
public Item(String name, Type type, Image avatar, ArrayList<Bonus> bonuses)
{
this.name = name;
this.type = type;
this.avatar = avatar;
// Add the bonuses
this.bonuses = bonuses;
}
/**
* Draw the item at the given coordinates.
*
* @param g
* The graphics device to draw with
* @param x
* The x coordinates to draw at
* @param y
* The y coordinates to draw at
*/
public void render(Graphics g, float x, float y)
{
this.avatar.draw(x, y);
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @return the avatar
*/
public Image getAvatar()
{
return avatar;
}
/**
* @return the type
*/
public Type getType()
{
return type;
}
/**
* @return the bonuses
*/
public ArrayList<Bonus> getBonuses()
{
return bonuses;
}
}
复制代码
Bouns.java 装备属性加成类
package project2;
/*
* Use for the bonuses provide from items to player.
* author: weiqian wang<wangw>
* */
import java.util.ArrayList;
public class Bonus
{
/** All the possible bonus properties */
public static enum Type
{
DAMAGE, HP, MAXHP, MP, MAXMP, SPEED, COOLDOWN, STRENGTH,
};
/** An empty bonus array for non-bonus item construction */
public static final ArrayList<Bonus> EMPTY_BONUS = new ArrayList<Bonus>(0);
/** The type of the bonus instance */
private Bonus.Type type;
/** The value of the bonus instance */
private double value;
/**
* Create a bonus of the given type and value
*
* @param type
* The type of bonus
* @param value
* The value of the bonus
*/
public Bonus(Bonus.Type type, double value)
{
this.type = type;
this.value = value;
}
/** Serialises the bonus for printing */
@Override
public String toString()
{
String str = this.type + ": ";
switch (this.type)
{
case SPEED:
str += "+" + (int) (this.value * 100) + "%";
break;
case COOLDOWN:
str += "-" + (int) this.value;
break;
default:
str += "+" + (int) this.value;
break;
}
return str;
}
/**
* @return the type
*/
public Bonus.Type getType()
{
return type;
}
/**
* @return the value
*/
public double getValue()
{
return value;
}
}
复制代码
Fontloader 字体类 这个不是我写的
package project2;
/* 433-294 Object Oriented Software Development
* Slick font loader.
* Makes it very easy to load font files into Slick Font objects.
* Author: Matt Giuca <mgiuca>
*/
import java.io.File;
import org.newdawn.slick.Font;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.SlickException;
/** This class is provided to reduce the complexity of loading fonts.
*/
public class FontLoader
{
/** Loads a font from a TTF file.
* @param ttf_filename Path to a TTF font file to use to draw text.
* @param textsize Font size, in points.
* @return A font object. May be used with Graphics.setFont to set the
* default rendering font for the game.
*/
public static Font loadFont(String ttf_filename, float textsize)
throws SlickException
{
// First, load a java.awt.Font of the given TTF file
java.awt.Font awtfont;
try
{
awtfont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT,
new File(ttf_filename));
}
catch (java.io.IOException e)
{
throw new SlickException("Could not open font file "
+ ttf_filename);
}
catch (java.awt.FontFormatException e)
{
throw new SlickException("Could not load font " + ttf_filename);
}
// Create a "derived font" with the correct font size
awtfont = awtfont.deriveFont(textsize);
// Now wrap it in a Slick TrueTypeFont object (with anti-aliasing)
return new TrueTypeFont(awtfont, true);
}
}
复制代码
GameObject.java 游戏通用变量
package project2;
/*
* Use for the common variable between each class.
* author: weiqian wang<wangw>
* */
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
public interface GameObject
{
/**
* Render the object in the game world.
*
* @param g
* The Slick graphics object, used for drawing.
*/
public void render(Graphics g);
/** Serialises the unit */
@Override
public String toString();
/**
* @return the x
*/
public double getX();
/**
* @return the y
*/
public double getY();
/**
* @return the name
*/
public String getName();
/**
* @return the avatar
*/
public Image getAvatar();
/**
* @return the physWidth
*/
public int getPhysWidth();
/**
* @return the physHeight
*/
public int getPhysHeight();
}
复制代码
__world.java World世界实例
package project2;
/*
* single instance of World.
* author: weiqian wang<wangw>
* */
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import Villagers.Villager;
public class __world extends World{
public __world() throws SlickException {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init(GameContainer arg0, StateBasedGame arg1)
throws SlickException {
new ArrayList<Villager>();
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
throws SlickException {
super.render(arg2);
}
@Override
public void update(double dir_x, double dir_y, int delta)
throws SlickException {
super.update(dir_x, dir_y, delta);
}
@Override
public int getID() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
throws SlickException {
// TODO Auto-generated method stub
}
}
复制代码
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2