S天狼I S天狼I
关注数: 44 粉丝数: 17 发帖数: 2,875 关注贴吧数: 16
磁力。【马里奥游戏基本框架】src_5 File Enemy.java package mario; import java.awt.image.BufferedImage; public class Enemy implements Runnable { // 坐标private int x;private int y; // 初始坐标private int startX;private int startY; // 类型private int type; // 显示的图片private BufferedImage showImage = null; // 移动方向private boolean isLeftOrUp = false; // 场景对象private BackGround bg = null; // 移动的极限范围private int upMax = 0;private int downMax = 0; // 移动图片状态private int movingImage = 0; // 定义移动线程private Thread t = new Thread(this); // 创建蘑菇public Enemy(int x, int y, boolean isLeft, int type, BackGround bg) {this.x = x;this.startX = x;this.y = y;this.startY = y;this.isLeftOrUp = isLeft;this.type = type;this.bg = bg; // 取得蘑菇图片 if (this.type == 1) {this.showImage = StaticImage.TriangleImage.get(0);} t.start(); // 线程不会立即启动t.suspend();} // 创建食人花 public Enemy(int x, int y, int upMax, int downMax, boolean isUp, int type,BackGround bg) {this.x = x;this.startX = x;this.y = y;this.startY = y;this.upMax = upMax;this.downMax = downMax;this.isLeftOrUp = isUp;this.type = type;this.bg = bg; if (2 == this.type) {this.showImage = StaticImage.flowerImage.get(0);} t.start(); // 线程不会立即启动t.suspend();} public void reset() { // 将坐标初始回去this.x = this.startX;this.y = this.startY; // 如果是蘑菇怪 if(1 == this.type) {this.showImage = StaticImage.TriangleImage.get(0);}else {this.showImage = StaticImage.flowerImage.get(0);}} public void dead() { // 移除敌人this.bg.getAllEnemy().remove(this); // 将敌人添加到被移除表中this.bg.getRemovedEnemy().add(this);} public BufferedImage getShowImage() {return showImage;} public int getType() {return type;} public void setShowImage(BufferedImage showImage) {this.showImage = showImage;} public Thread getT() {return t;} public int getX() {return x;} public int getY() {return y;} public void run() {while (true) { // 蘑菇if (1 == this.type) { boolean canRight = true;boolean canLeft = true; // 判断当前是否可以移动,是否与障碍物接触 for (int i = 0; i < this.bg.getAllObstruction().size(); i++) {Obstruction ob = this.bg.getAllObstruction().get(i); // 当前处在障碍物高度范围之内 if (ob.getX() == this.x + 60&& (this.y < ob.getY() + 60 && this.y > ob.getY() - 60)) {canRight = false;} else if (ob.getX() == this.x - 60&& (this.y < ob.getY() + 60 && this.y > ob.getY() - 60)) {canLeft = false;} // 判断是否超出了边界if (this.x < 0) {canLeft = false;} else if (this.x > 540) {canRight = false;}} // 如果正在向左,却不能向左了if (!canLeft && this.isLeftOrUp) {this.isLeftOrUp = false;} // 如果正在向右,却不能向右了if (!canRight && !this.isLeftOrUp) {this.isLeftOrUp = true;} // 如果往左 if (this.isLeftOrUp) {this.x -= 2;} else {this.x += 2;} // 交替更换图片 if (this.movingImage == 0) {this.movingImage = 1; this.showImage = StaticImage.TriangleImage.get(1); } else {this.showImage = StaticImage.TriangleImage.get(0);this.movingImage = 0;}}// 食人花else if (2 == this.type) { // 先判断是否移动到最大值 if (this.isLeftOrUp && this.y == this.upMax) {this.isLeftOrUp = false;} else if (this.isLeftOrUp == false && this.y == this.downMax) {this.isLeftOrUp = true;} // 如果往上 if (this.isLeftOrUp) {this.y -= 2;} else {this.y += 2;} if (this.movingImage == 1) {this.movingImage = 0;this.showImage = StaticImage.flowerImage.get(1);} else {this.showImage = StaticImage.flowerImage.get(0);this.movingImage = 1;}} try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}} }}
磁力。【马里奥游戏基本框架】src_4 File Obstruction.java package mario; import java.awt.image.BufferedImage; public class Obstruction implements Runnable{ // 保存的坐标private int x; private int y; // 保存的类型private int type; // 初始的类型private int startType; // 欲显示图片private BufferedImage showImage = null; // 降旗的线程private Thread t = new Thread(this); // 当前场景private BackGround bg = null; public Obstruction(int x, int y, int type) { this.x = x; this.y = y; this.type = type; this.startType = type;this.setImage(); // 设置图片} public Obstruction(int x, int y, int type, BackGround bg) { this.x = x; this.y = y; this.type = type; this.startType = type; this.setImage(); // 设置图片 if(11 == type) { this.bg = bg;this.t.start();}} // 重置所有的图片public void reset() { // 先修改类型为原始的类型this.type = startType; // 再重置图片 this.setImage();} // 根据当前图片状态改变显示图片 public void setImage() { // 每个障碍物type都不一样,type所对应的是ArrayList中第n个障碍物,ArrayList下标是从0开始 showImage = StaticImage.ObstructionImage.get(type);} public int getX() {return x;} public int getY() {return y;} public void setType(int type) {this.type = type;} public int getType() {return type;} public BufferedImage getShowImage() {return showImage;} // 完成游戏结束时,降旗的操作 public void run() { while(true) { if(this.bg.isOver()) { if(this.y < 420) {this.y += 5;} else { // 降旗结束this.bg.setDownOver(true);}} try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}}}
磁力。【马里奥游戏基本框架】src_3 package mario; import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.List; public class BackGround {// 当前场景的图片private BufferedImage bgImage = null; // 当前场景顺序private int sort; // 当前是否为最后一个场景private boolean flag; // 所有敌人private List<Enemy> allEnemy = new ArrayList<Enemy>(); // 所有的障碍物private List<Obstruction> allObstruction = new ArrayList<Obstruction>(); // 被消灭的敌人private List<Enemy> removedEnemy = new ArrayList<Enemy>(); // 被消灭的敌人private List<Obstruction> removedObstruction = new ArrayList<Obstruction>();// 游戏结束的标记private boolean isOver = false; // 降旗是否结束private boolean isDownOver = false;public BackGround(int sort, boolean flag) {this.sort = sort;this.flag = flag; // 是最后一个场景if (flag) {// 读取末尾场景bgImage = StaticImage.lastImage;}// 不是最后一个else {// 读取非末尾场景bgImage = StaticImage.firstImage;} if (1 == sort) {// 建立整个地面for (int i = 0; i < 15; i++) {// 窗体宽900,图片宽60,所以需要15块// 用的是第11个图片,但因为ArrayList下标是从0开始,所以type就是10this.allObstruction.add(new Obstruction(i * 60, 540, 10));}// 绘制问号方块this.allObstruction.add(new Obstruction(120, 360, 4));this.allObstruction.add(new Obstruction(360, 360, 4));this.allObstruction.add(new Obstruction(480, 360, 4));this.allObstruction.add(new Obstruction(420, 180, 4)); // 绘制墙壁this.allObstruction.add(new Obstruction(300, 360, 5));this.allObstruction.add(new Obstruction(420, 360, 5));this.allObstruction.add(new Obstruction(540, 360, 5)); // 绘制水管this.allObstruction.add(new Obstruction(660, 480, 0));this.allObstruction.add(new Obstruction(720, 480, 1));this.allObstruction.add(new Obstruction(660, 540, 2));this.allObstruction.add(new Obstruction(720, 540, 3));// 绘制陷阱(隐形的砖块)this.allObstruction.add(new Obstruction(480, 180, 8)); // 定义蘑菇敌人this.allEnemy.add(new Enemy(600, 480, true, 1, this));// 定义食人花this.allEnemy.add(new Enemy(690, 540, 420, 540, true, 2, this));}// 绘制第二个场景else if (2 == sort) {// 建立整个地面for (int i = 0; i < 15; i++) {// 如果是10,就不绘制,作为陷阱使用if (i != 10) {// 窗体宽900,图片宽60,所以需要15块// 用的是第11个图片,但因为ArrayList下标是从0开始,所以type就是10this.allObstruction.add(new Obstruction(i * 60, 540, 10)); }}// 绘制水管 this.allObstruction.add(new Obstruction(120, 420, 0)); this.allObstruction.add(new Obstruction(180, 420, 1)); this.allObstruction.add(new Obstruction(120, 480, 2)); this.allObstruction.add(new Obstruction(180, 480, 3)); this.allObstruction.add(new Obstruction(120, 540, 2)); this.allObstruction.add(new Obstruction(180, 540, 3));// 绘制水管 this.allObstruction.add(new Obstruction(300, 360, 0)); this.allObstruction.add(new Obstruction(360, 360, 1)); this.allObstruction.add(new Obstruction(300, 420, 2)); this.allObstruction.add(new Obstruction(360, 420, 3)); this.allObstruction.add(new Obstruction(300, 480, 2)); this.allObstruction.add(new Obstruction(360, 480, 3)); this.allObstruction.add(new Obstruction(300, 540, 2)); this.allObstruction.add(new Obstruction(360, 540, 3));// 绘制水管 this.allObstruction.add(new Obstruction(660, 360, 0)); this.allObstruction.add(new Obstruction(720, 360, 1)); this.allObstruction.add(new Obstruction(660, 420, 2)); this.allObstruction.add(new Obstruction(720, 420, 3)); this.allObstruction.add(new Obstruction(660, 480, 2)); this.allObstruction.add(new Obstruction(720, 480, 3)); this.allObstruction.add(new Obstruction(660, 540, 2)); this.allObstruction.add(new Obstruction(720, 540, 3)); // 绘制问号this.allObstruction.add(new Obstruction(540, 300, 4)); this.allObstruction.add(new Obstruction(420, 180, 4)); // 绘制敌人this.allEnemy.add(new Enemy(660, 480, true, 1, this)); this.allEnemy.add(new Enemy(330, 540, 300, 540, true, 2, this)); this.allEnemy.add(new Enemy(690, 300, 300, 540, false, 2, this)); this.allEnemy.add(new Enemy(150, 420, 360, 540, false, 2, this)); } else if (3 == sort) { for (int i = 0; i < 15; i++) { // 窗体宽900,图片宽60,所以需要15块// 用的是第11个图片,但因为ArrayList下标是从0开始,所以type就是10 this.allObstruction.add(new Obstruction(i * 60, 540, 10));} this.allObstruction.add(new Obstruction(550, 200, 11, this)); this.allObstruction.add(new Obstruction(520, 480, 7));}} public List<Obstruction> getAllObstruction() {return allObstruction;} public BufferedImage getBgImage() {return bgImage;} public List<Enemy> getAllEnemy() {return allEnemy;} public boolean isFlag() {return flag;} public List<Enemy> getRemovedEnemy() {return removedEnemy;} public List<Obstruction> getRemovedObstruction() {return removedObstruction;} public int getSort() {return sort;} public boolean isOver() {return isOver;} public void setOver(boolean isOver) {this.isOver = isOver;} public boolean isDownOver() {return isDownOver;} public void setDownOver(boolean isDownOver) {this.isDownOver = isDownOver;} public void setBgImage(BufferedImage bgImage) {this.bgImage = bgImage;} // 重置所有的障碍物与敌人,返回到原有坐标,并恢复状态public void reset() { this.allEnemy.addAll(this.removedEnemy); this.allObstruction.addAll(this.removedObstruction); for (int i = 0; i < this.allEnemy.size(); i++) {this.allEnemy.get(i).reset();}for (int i = 0; i < this.allObstruction.size(); i++) {this.allObstruction.get(i).reset();}} // 敌人开始移动 public void enemyStartMove() {for (int i = 0; i < this.allEnemy.size(); i++) { // 使线程启动this.allEnemy.get(i).getT().resume();}} }
磁力。【马里奥游戏基本框架】src_2 File StaticImage.java package mario; import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List; import javax.imageio.ImageIO; public class StaticImage { // 马里奥角色图片public static List<BufferedImage> allMarioImage = new ArrayList<BufferedImage>(); // 第一张场景游戏图片public static BufferedImage firstImage = null; // 最后一张场景游戏图片public static BufferedImage lastImage = null; // 开始游戏图片public static BufferedImage beginImage = null; // 食人花的图片public static List<BufferedImage> flowerImage = new ArrayList<BufferedImage>(); // 蘑菇头图片public static List<BufferedImage> TriangleImage = new ArrayList<BufferedImage>(); // 乌龟图片public static List<BufferedImage> TurtleImage = new ArrayList<BufferedImage>(); // 障碍物图片public static List<BufferedImage> ObstructionImage = new ArrayList<BufferedImage>(); // 马里奥死亡图public static BufferedImage marioDeadImage = null; // 路径private static String imagePath = System.getProperty("user.dir")+ "\\bin\\"; // 初始化所有的图片 public static void init() throws IOException { // 读取马里奥的图片for (int i = 1; i <= 10; i++) {allMarioImage.add(ImageIO.read(new File(imagePath + i + ".jpg")));} // 获取场景图片beginImage = ImageIO.read(new File(imagePath + "start.jpg")); lastImage = ImageIO.read(new File(imagePath + "firststageend.jpg"));firstImage = ImageIO.read(new File(imagePath + "firststage.jpg")); // 获取怪物图片 for (int i = 1; i <= 5; i++) { if (i <= 2) { flowerImage.add(ImageIO.read(new File(imagePath + "flower" + i+ ".jpg")));} if (i <= 3) {TriangleImage.add(ImageIO.read(new File(imagePath + "triangle"+ i + ".jpg")));} TurtleImage.add(ImageIO.read(new File(imagePath + "Turtle" + i+ ".jpg")));} // 获取障碍物图片 for (int i = 1; i <= 12; i++) {ObstructionImage.add(ImageIO.read(new File(imagePath + "ob" + i+ ".jpg")));} // 马里奥死亡的图片 marioDeadImage = ImageIO.read(new File(imagePath + "over.jpg")); } }
磁力。【马里奥游戏基本框架】src_1 File Ui.java package mario; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.swing.JFrame;import javax.swing.JOptionPane; public class Ui extends JFrame implements KeyListener, Runnable { // 全部的场景private List<BackGround> allBG = new ArrayList<BackGround>();// 马里奥对象private Mario mario = null;// 当前要显示的场景private BackGround nowBG = null;// 刷新显示内容private Thread th = new Thread(this);// 是否已经开始游戏private boolean isBegin = false; public static void main(String[] args) {new Ui();} public Ui() {setTitle("马里奥游戏");setSize(900, 600); int x = Toolkit.getDefaultToolkit().getScreenSize().width;int y = Toolkit.getDefaultToolkit().getScreenSize().height;setLocation(x / 4, y / 4); // 设置窗口位置setResizable(false); // 窗体不能被最大化 // 初始化图片try {// 将所有图片读入StaticImage保存StaticImage.init();} catch (IOException e) {e.printStackTrace();} // 创建全部场景,一共三个场景for (int i = 1; i <= 3; i++) { // 如果3 == i, 那么就是最后一个场景if (3 == i) {this.allBG.add(new BackGround(i, true));} else {// 否则不是this.allBG.add(new BackGround(i, false));} } // 将第一个场景设置为当前场景this.nowBG = allBG.get(0);// 初始化马里奥对象this.mario = new Mario(0, 480);// 将当前场景放在马里奥场景成员中this.mario.setBg(this.nowBG); th.start(); // 重绘窗体,会调用paint方法this.repaint();this.addKeyListener(this); // 添加按键消息 this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);} // 为了防止屏幕闪烁,加入双缓冲技术@Overridepublic void paint(Graphics g) {// 建立临时缓冲图片BufferedImage image = new BufferedImage(900, 600,BufferedImage.TYPE_3BYTE_BGR);// 取得画笔对象Graphics g2 = image.getGraphics();// 将图片绘制到缓冲图片image中g2.drawImage(this.nowBG.getBgImage(), 0, 0, this); // 游戏开始if (this.isBegin) { // 先绘制敌人,再绘制障碍物,是因为为了把食人花覆盖掉// 绘制敌人图片Iterator<Enemy> it2 = this.nowBG.getAllEnemy().iterator();while (it2.hasNext()) { Enemy e = it2.next(); g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this);} // 绘制障碍物Iterator<Obstruction> it = this.nowBG.getAllObstruction().iterator(); // 也可以不使用迭代,而是用for循环while (it.hasNext()) { // 取得障碍物对象Obstruction ob = it.next(); g2.drawImage(ob.getShowImage(), ob.getX(), ob.getY(), this);} // 绘制马里奥图片g2.drawImage(this.mario.getShowImage(), this.mario.getX(),this.mario.getY(), this); g2.drawString("生命:" + this.mario.getLives(), 750, 50); } else { g2.drawImage(StaticImage.beginImage, 0, 0, this);}// 把image缓冲图片绘制到当前窗体中 g.drawImage(image, 0, 0, this);} @Override// 键被按下 public void keyPressed(KeyEvent ke) {// L: 37 U:38 R:39 D:40 // 防止在游戏开始前按键,使人物出现后自动变化位置 if (isBegin) { // 向右移动if (ke.getKeyCode() == 39) { this.mario.rightMove();}// 向左移动 else if (37 == ke.getKeyCode()) {this.mario.leftMove();}// 跳跃 else if (38 == ke.getKeyCode()) { this.mario.jump();}}else {// 按空格if (32 == ke.getKeyCode()) { this.isBegin = true;// 开始之后才能使敌人移动this.nowBG.enemyStartMove();// 设置分数和生命this.mario.setLives(3);this.mario.setScore(0);}} } @Override// 键被抬起 public void keyReleased(KeyEvent ke) { // 向右移动停止if (ke.getKeyCode() == 39) {this.mario.rightStop();}// 向左移动停止else if (ke.getKeyCode() == 37) {this.mario.leftStop();}} @Overridepublic void keyTyped(KeyEvent e) { } public void run() {while (true) { // 马里奥死亡了if (this.mario.isDead()) { JOptionPane.showMessageDialog(this, "马里奥死亡");System.exit(-1);} this.repaint(); // 马里奥走到了最右端if (this.mario.getX() >= 840) { // 重置当前场景this.nowBG = this.allBG.get(this.nowBG.getSort()); // 将当前场景添加到马里奥里面去this.mario.setBg(this.nowBG); // 修改马里奥横轴坐标this.mario.setX(0);this.mario.setY(480); // 将场景中的敌人移动this.nowBG.enemyStartMove();} if(this.mario.isClear()) { // 游戏结束 JOptionPane.showMessageDialog(this, "游戏通关");} try {Thread.sleep(30);} catch (InterruptedException e) {e.printStackTrace();} }} }
磁力。【文件批量修改器】src_1 File Ui.java package org; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; public class Ui implements ActionListener { JTextField tfPath = new JTextField(40); // 文件路径文本框 JTextField tfType = new JTextField(40); // 文件类型文本框 JTextField tfReFix = new JTextField(40); // 文件重名为后缀文本框 private String path; private String[] postfix; private String refix; private int flag = 0; public void init() { JFrame jf = new JFrame("批量文件操作器"); jf.setLayout(new BorderLayout(10, 20)); jf.setSize(250, 270); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(-1);} }); // 文件路径文本框 JPanel jp1 = new JPanel(new GridLayout()); JLabel lb1 = new JLabel("文件绝对路径:"); jp1.add(lb1); jp1.add(tfPath); // 文件类型文本框 JPanel jp2 = new JPanel(new GridLayout()); JLabel lb2 = new JLabel("文件类型:"); jp2.add(lb2);jp2.add(tfType); // 两个单选框JPanel jp4 = new JPanel(new FlowLayout()); JRadioButton jrb1 = new JRadioButton("删副本(多)"); JRadioButton jrb2 = new JRadioButton("重名为(单)"); jrb1.addActionListener(this); jrb2.addActionListener(this); jp4.add(jrb1); jp4.add(jrb2); ButtonGroup bg = new ButtonGroup(); bg.add(jrb1); bg.add(jrb2); // 重名为后缀文本框 JPanel jp5 = new JPanel(new GridLayout()); JLabel lb3 = new JLabel("重名为后缀"); jp5.add(lb3); jp5.add(tfReFix); // 确认按钮JButton bn = new JButton("开始");bn.addActionListener(new ActionListener() { @Overridepublic void actionPerformed(ActionEvent e) { if (1 == flag) { path = Parse.addBackslash(tfPath.getText()); // 获得路径postfix = Parse.analyzePostfix(tfType.getText()); // 获得后缀名 if(path != null && postfix!= null) { DeleteThread ft = new DeleteThread(); ft.setPath(path); ft.setPostfix(postfix); Thread t1 = new Thread(ft); t1.start(); } }else if(2 == flag) { path = Parse.addBackslash(tfPath.getText()); // 获得路径postfix = Parse.analyzePostfix(tfType.getText()); // 获得后缀名refix = tfReFix.getText(); //获得重命名后缀if(path != null && postfix != null && refix != null) {ReNameThread rnt = new ReNameThread(path, postfix[0], refix); Thread t2 = new Thread(rnt); t2.start();}} else {} } } ); JPanel jp3 = new JPanel();jp3.add(bn); JPanel jp = new JPanel(new GridLayout(5, 1, 0, 10)); jp.add(jp1); jp.add(jp2); jp.add(jp4); jp.add(jp5); jp.add(jp3); jf.add(jp, BorderLayout.NORTH); int x = Toolkit.getDefaultToolkit().getScreenSize().width; int y = Toolkit.getDefaultToolkit().getScreenSize().height;jf.setLocation(x - 600, y - 600); jf.setVisible(true); } public void actionPerformed(ActionEvent e) {if ("删副本(多)" == e.getActionCommand()) {flag = 1;} else if ("重名为(单)" == e.getActionCommand()) {flag = 2;}} }
1 下一页