A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 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 装备物品类
  1. package project2;
  2. /*
  3. * item's ab class.
  4. * author: weiqian wang<wangw>
  5. * */
  6. import java.util.ArrayList;


  7. import org.newdawn.slick.Graphics;
  8. import org.newdawn.slick.Image;


  9. public abstract class Item
  10. {
  11.         /** Every different type of item */
  12.         public static enum Type
  13.         {
  14.                 WEAPON, ACCESSORY, CONSUMABLE, QUEST
  15.         }

  16.         /** The item's name */
  17.         protected String name;
  18.         /** The type of the item */
  19.         protected Type type;
  20.         /** The Image object representing the item in the world */
  21.         protected Image avatar;
  22.         /** A list of the bonuses applied to the item */
  23.         protected ArrayList<Bonus> bonuses;

  24.         /**
  25.          * Create an item object with optional bonuses
  26.          *
  27.          * @param name
  28.          *            Name of the item
  29.          * @param type
  30.          *            Item.Type of the item
  31.          * @param avatar
  32.          *            Image representing the item
  33.          * @param bonuses
  34.          *            Bonuses to be applied to the item (null for none)
  35.          */
  36.         public Item(String name, Type type, Image avatar, ArrayList<Bonus> bonuses)
  37.         {
  38.                 this.name = name;
  39.                 this.type = type;
  40.                 this.avatar = avatar;

  41.                 // Add the bonuses
  42.                 this.bonuses = bonuses;
  43.         }

  44.         /**
  45.          * Draw the item at the given coordinates.
  46.          *
  47.          * @param g
  48.          *            The graphics device to draw with
  49.          * @param x
  50.          *            The x coordinates to draw at
  51.          * @param y
  52.          *            The y coordinates to draw at
  53.          */
  54.         public void render(Graphics g, float x, float y)
  55.         {
  56.                 this.avatar.draw(x, y);
  57.         }

  58.         /**
  59.          * @return the name
  60.          */
  61.         public String getName()
  62.         {
  63.                 return name;
  64.         }

  65.         /**
  66.          * @return the avatar
  67.          */
  68.         public Image getAvatar()
  69.         {
  70.                 return avatar;
  71.         }

  72.         /**
  73.          * @return the type
  74.          */
  75.         public Type getType()
  76.         {
  77.                 return type;
  78.         }

  79.         /**
  80.          * @return the bonuses
  81.          */
  82.         public ArrayList<Bonus> getBonuses()
  83.         {
  84.                 return bonuses;
  85.         }
  86. }
复制代码
Bouns.java 装备属性加成类
  1. package project2;
  2. /*
  3. * Use for the bonuses provide from items to player.
  4. * author: weiqian wang<wangw>
  5. * */

  6. import java.util.ArrayList;

  7. public class Bonus
  8. {
  9.         /** All the possible bonus properties */
  10.         public static enum Type
  11.         {
  12.                 DAMAGE, HP, MAXHP, MP, MAXMP, SPEED, COOLDOWN, STRENGTH,
  13.         };

  14.         /** An empty bonus array for non-bonus item construction */
  15.         public static final ArrayList<Bonus> EMPTY_BONUS = new ArrayList<Bonus>(0);

  16.         /** The type of the bonus instance */
  17.         private Bonus.Type type;
  18.         /** The value of the bonus instance */
  19.         private double value;

  20.         /**
  21.          * Create a bonus of the given type and value
  22.          *
  23.          * @param type
  24.          *            The type of bonus
  25.          * @param value
  26.          *            The value of the bonus
  27.          */
  28.         public Bonus(Bonus.Type type, double value)
  29.         {
  30.                 this.type = type;
  31.                 this.value = value;
  32.         }

  33.         /** Serialises the bonus for printing */
  34.         @Override
  35.         public String toString()
  36.         {
  37.                 String str = this.type + ": ";
  38.                 switch (this.type)
  39.                 {
  40.                 case SPEED:
  41.                         str += "+" + (int) (this.value * 100) + "%";
  42.                         break;
  43.                 case COOLDOWN:
  44.                         str += "-" + (int) this.value;
  45.                         break;
  46.                 default:
  47.                         str += "+" + (int) this.value;
  48.                         break;
  49.                 }

  50.                 return str;
  51.         }

  52.         /**
  53.          * @return the type
  54.          */
  55.         public Bonus.Type getType()
  56.         {
  57.                 return type;
  58.         }

  59.         /**
  60.          * @return the value
  61.          */
  62.         public double getValue()
  63.         {
  64.                 return value;
  65.         }
  66. }
复制代码

Fontloader 字体类 这个不是我写的
  1. package project2;
  2. /* 433-294 Object Oriented Software Development
  3. * Slick font loader.
  4. * Makes it very easy to load font files into Slick Font objects.
  5. * Author: Matt Giuca <mgiuca>
  6. */

  7. import java.io.File;

  8. import org.newdawn.slick.Font;
  9. import org.newdawn.slick.TrueTypeFont;
  10. import org.newdawn.slick.SlickException;

  11. /** This class is provided to reduce the complexity of loading fonts.
  12. */
  13. public class FontLoader
  14. {
  15.     /** Loads a font from a TTF file.
  16.      * @param ttf_filename Path to a TTF font file to use to draw text.
  17.      * @param textsize Font size, in points.
  18.      * @return A font object. May be used with Graphics.setFont to set the
  19.      *   default rendering font for the game.
  20.      */
  21.         public static Font loadFont(String ttf_filename, float textsize)
  22.         throws SlickException
  23.     {
  24.         // First, load a java.awt.Font of the given TTF file
  25.         java.awt.Font awtfont;
  26.         try
  27.         {
  28.             awtfont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT,
  29.                 new File(ttf_filename));
  30.         }
  31.         catch (java.io.IOException e)
  32.         {
  33.             throw new SlickException("Could not open font file "
  34.                 + ttf_filename);
  35.         }
  36.         catch (java.awt.FontFormatException e)
  37.         {
  38.             throw new SlickException("Could not load font " + ttf_filename);
  39.         }
  40.         // Create a "derived font" with the correct font size
  41.         awtfont = awtfont.deriveFont(textsize);
  42.         // Now wrap it in a Slick TrueTypeFont object (with anti-aliasing)
  43.         return new TrueTypeFont(awtfont, true);
  44.     }
  45. }
复制代码

GameObject.java 游戏通用变量
  1. package project2;
  2. /*
  3. * Use for the common variable between each class.
  4. * author: weiqian wang<wangw>
  5. * */

  6. import org.newdawn.slick.Graphics;
  7. import org.newdawn.slick.Image;

  8. public interface GameObject
  9. {
  10.         /**
  11.          * Render the object in the game world.
  12.          *
  13.          * @param g
  14.          *            The Slick graphics object, used for drawing.
  15.          */
  16.         public void render(Graphics g);

  17.         /** Serialises the unit */
  18.         @Override
  19.         public String toString();

  20.         /**
  21.          * @return the x
  22.          */
  23.         public double getX();

  24.         /**
  25.          * @return the y
  26.          */
  27.         public double getY();


  28.         /**
  29.          * @return the name
  30.          */
  31.         public String getName();

  32.         /**
  33.          * @return the avatar
  34.          */
  35.         public Image getAvatar();

  36.         /**
  37.          * @return the physWidth
  38.          */
  39.         public int getPhysWidth();

  40.         /**
  41.          * @return the physHeight
  42.          */
  43.         public int getPhysHeight();
  44. }
复制代码

__world.java World世界实例
  1. package project2;
  2. /*
  3. * single instance of World.
  4. * author: weiqian wang<wangw>
  5. * */

  6. import java.util.ArrayList;

  7. import org.newdawn.slick.GameContainer;
  8. import org.newdawn.slick.Graphics;
  9. import org.newdawn.slick.SlickException;
  10. import org.newdawn.slick.state.StateBasedGame;

  11. import Villagers.Villager;



  12. public class __world extends World{

  13.         public __world() throws SlickException {
  14.                 super();
  15.                 // TODO Auto-generated constructor stub
  16.         }
  17.         

  18.         @Override
  19.         public void init(GameContainer arg0, StateBasedGame arg1)
  20.                         throws SlickException {
  21.                 new ArrayList<Villager>();
  22.         }

  23.         @Override
  24.         public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
  25.                         throws SlickException {
  26.                 super.render(arg2);
  27.                
  28.         }

  29.         @Override
  30.         public void update(double dir_x, double dir_y, int delta)
  31.                         throws SlickException {
  32.                 super.update(dir_x, dir_y, delta);
  33.                
  34.         }

  35.         @Override
  36.         public int getID() {
  37.                 // TODO Auto-generated method stub
  38.                 return 0;
  39.         }

  40.         @Override
  41.         public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
  42.                         throws SlickException {
  43.                 // TODO Auto-generated method stub
  44.                
  45.         }


  46.         

  47.         
  48. }
复制代码

  1. [hide] project2base.zip (14.83 KB, 下载次数: 131) [/hide]
复制代码







0 个回复

您需要登录后才可以回帖 登录 | 加入黑马