|
楼主 |
发表于 2017-9-15 13:09:59
|
显示全部楼层
1.Background.javapackage org.liky.mario;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class BackGround {
//当前场景的显示图片
private BufferedImage bgImage = null;
public BufferedImage getBgImage() {
return bgImage;
}
//场景的顺序
private int sort;
public int getSort() {
return sort;
}
//当前是否为最后一个场景
private boolean flag;
//通过集合来保存
//全部的敌人
private List<Enemy> allEnemy = new ArrayList<Enemy>();
//全部的障碍物
private List<Obstruction> allObstruction = new ArrayList<Obstruction>();
public List<Obstruction> getAllObstruction() {
return allObstruction;
}
//被消灭的敌人
private List<Enemy> removedEnemy = new ArrayList<Enemy>();
//被消灭的障碍物
private List<Obstruction> removedObstruction = new ArrayList<Obstruction>();
public List<Obstruction> getRemovedObstruction() {
return removedObstruction;
}
//构造方法
public BackGround(int sort,boolean flag) {
this.sort = sort;
this.flag = flag;
if (flag) {
bgImage = StaticValue.endImage;
}else {
bgImage = StaticValue.bgImage;
}
if (sort == 1) {
//绘制地面
for(int i = 0; i < 15;i++) {
this.allObstruction.add(new Obstruction(i*60, 540, 9));
}
//绘制砖块和问号
this.allObstruction.add(new Obstruction(120, 360, 4));
this.allObstruction.add(new Obstruction(300, 360, 0));
this.allObstruction.add(new Obstruction(360, 360, 4));
this.allObstruction.add(new Obstruction(420, 360, 0));
this.allObstruction.add(new Obstruction(480, 360, 4));
this.allObstruction.add(new Obstruction(540, 360, 0));
this.allObstruction.add(new Obstruction(420, 180, 4));
//绘制水管
this.allObstruction.add(new Obstruction(660, 540, 6));
this.allObstruction.add(new Obstruction(720, 540, 5));
this.allObstruction.add(new Obstruction(660, 480, 8));
this.allObstruction.add(new Obstruction(720, 480, 7));
//绘制敌人
this.allEnemy.add(new Enemy(600,480,true,1));
this.allEnemy.add(new Enemy(690,540,true,2,420,540));
}
//绘制第二个场景
if (sort == 2) {
for(int i = 0; i < 15;i++) {
if(i != 10) {
this.allObstruction.add(new Obstruction(i*60, 540, 9));
}
}
//第一根水管
this.allObstruction.add(new Obstruction(60, 540, 6));
this.allObstruction.add(new Obstruction(120, 540, 5));
this.allObstruction.add(new Obstruction(60, 480, 6));
this.allObstruction.add(new Obstruction(120, 480, 5));
this.allObstruction.add(new Obstruction(60, 420, 8));
this.allObstruction.add(new Obstruction(120, 420, 7));
//第二根水管
this.allObstruction.add(new Obstruction(240, 540, 6));
this.allObstruction.add(new Obstruction(300, 540, 5));
this.allObstruction.add(new Obstruction(240, 480, 6));
this.allObstruction.add(new Obstruction(300, 480, 5));
this.allObstruction.add(new Obstruction(240, 420, 6));
this.allObstruction.add(new Obstruction(300, 420, 5));
this.allObstruction.add(new Obstruction(240, 360, 8));
this.allObstruction.add(new Obstruction(300, 360, 7));
}
}
public List<Enemy> getAllEnemy() {
return allEnemy;
}
public List<Enemy> getRemovedEnemy() {
return removedEnemy;
}
//重置方法,将所有的障碍物和敌人返回原有坐标,并将其状态也修改
public void reset() {
}
}
2.Enemy.javapackage org.liky.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;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BufferedImage getShowImage() {
return showImage;
}
//显示图片
private BufferedImage showImage;
//移动方向
private boolean isLeftOrUp = true;
//移动的范围
private int upMax = 0;
private int downMax = 0;
//定义线程
private Thread t = new Thread(this);
//定义图片状态
private int imageType = 0;
//创建普通敌人
public Enemy(int x,int y,boolean isLeft,int type) {
this.x = x;
this.y = y;
this.isLeftOrUp = isLeft;
this.type = type;
if (type == 1) {
this.showImage = StaticValue.allTriangleImage.get(0);
}
t.start();
}
//创建食人花
public Enemy(int x,int y,boolean isUp,int type,int upMax,int downMax) {
this.x = x;
this.y = y;
this.isLeftOrUp = isUp;
this.type = type;
this.upMax = upMax;
this.downMax = downMax;
if (type == 2) {
this.showImage = StaticValue.allFlowerImage.get(0);
}
t.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
//对于不同的类型要进行不同的处理
if(type == 1) {
if (this.isLeftOrUp) {
this.x -= 2;
}else {
this.x += 2;
}
if (imageType == 0) {
imageType = 1;
}else {
imageType = 0;
}
this.showImage = StaticValue.allTriangleImage.get(imageType);
}
if(type == 2) {
if (this.isLeftOrUp) {
this.y -= 2;
}else {
this.y += 2;
}
if (imageType == 0) {
imageType = 1;
}else {
imageType = 0;
}
if(this.isLeftOrUp && this.x == this.upMax) {
this.isLeftOrUp = false;
}
if(!this.isLeftOrUp && this.x == this.downMax) {
this.isLeftOrUp = true;
}
this.showImage = StaticValue.allFlowerImage.get(imageType);
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void reset() {
}
public void dead() {
}
}
3.Mario.javapackage org.liky.mario;
import java.awt.image.BufferedImage;
import org.omg.PortableServer.THREAD_POLICY_ID;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
public class Mario implements Runnable{
//坐标
private int x;
public void setY(int y) {
this.y = y;
}
public void setX(int x) {
this.x = x;
}
private int y;
//定义一个场景对象,来保存Mario所在的场景
private BackGround bg ;
public void setBg(BackGround bg) {
this.bg = bg;
}
//加入线程
private Thread t = null;
//定义速度属性
private int xmove = 0;
//定义一个垂直方向的速度
private int ymove = 0;
//状态
private String status;
//显示的图片
private BufferedImage showImage;
//生命数和分数
private int score;
private int life;
//当前移动中显示的图片索引
private int moving = 0;
//上升的时间
private int upTime = 0;
//构造方法
public Mario(int x,int y) {
this.x = x;
this.y = y;
//初始化Mario图片
this.showImage = StaticValue.allMarioImage.get(0);
this.score = 0;
this.life = 3;
t = new Thread(this);
t.start();
this.status = "right--standing";
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BufferedImage getShowImage() {
return showImage;
}
public void leftMove() {
//改变速度
xmove = -5;
//改变状态
//如果当前已经是跳跃的状态,应该保留原有状态,而不能修改为移动状态
if(this.status.indexOf("jumping") != -1) {
this.status = "left--jumping";
}else {
this.status = "left--moving";
}
}
public void rightMove() {
xmove = 5;
if(this.status.indexOf("jumping") != -1) {
this.status = "right--jumping";
}else {
this.status = "right--moving";
}
}
public void leftStop() {
this.xmove = 0;
if(this.status.indexOf("jumping") != -1) {
this.status = "left--jumping";
}else {
this.status = "left--standing";
}
}
public void rightStop() {
this.xmove = 0;
if(this.status.indexOf("jumping") != -1) {
this.status = "right--jumping";
}else {
this.status = "right--standing";
}
}
public void jump() {
if (this.status.indexOf("jumping") == -1) {
if (this.status.indexOf("left") != -1) {
this.status = "left--jumping";
}else {
this.status = "right--jumping";
}
ymove = -5;
upTime = 36;
}
}
//加入下落的方法
public void down() {
if (this.status.indexOf("left") != -1) {
this.status = "left--jumping";
}else {
this.status = "right--jumping";
}
ymove = 5;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
//判断当前mario是否与障碍物碰撞
//定义标记
boolean canLeft = true;
boolean canRight = true;
//定义一个当前Mario是否处在障碍物上的标记
boolean onLand = false;
for (int i = 0; i < this.bg.getAllObstruction().size();i++) {
Obstruction ob = this.bg.getAllObstruction().get(i);
//不允许继续向右移动
if (ob.getX() == this.x + 60 && (ob.getY() + 50 > this.y && ob.getY() - 50 < this.y)) {
canRight = false;
}
//不允许继续向左移动
if (ob.getX() == this.x - 60 && (ob.getY() + 50 > this.y && ob.getY() - 50 < this.y)) {
canLeft = false;
}
//如果符合条件,则表示Mario当前处在一个障碍物上
if (ob.getY() == this.y + 60 && (ob.getX() + 60 > this.x && ob.getX() - 60 < this.x)) {
onLand = true;
}
//判断当前mario是否在跳跃中顶到了一个障碍物
if (ob.getY() == this.y - 60 && (ob.getX() + 50 > this.x && ob.getX() - 50 < this.x)) {
//对于砖块的处理
if (ob.getType()==0) {
//将该砖块从场景中移除
this.bg.getAllObstruction().remove(ob);
//将被移除的砖块保存到一个集合中
this.bg.getRemovedObstruction().add(ob);
}
//对于?的处理
if (ob.getType()==4) {
ob.setType(2);
ob.setImage();
}
upTime = 0;
}
}
if(onLand && upTime == 0) {
if (this.status.indexOf("left") != -1) {
if(xmove != 0) {
this.status = "left--moving";
}else {
this.status = "left--standing";
}
}else {
if(xmove != 0) {
this.status = "right--moving";
}else {
this.status = "right--standing";
}
}
}else {
//表示当前为上升的状态
if (upTime != 0) {
upTime --;
}else {
this.down();
}
y += ymove;
}
if (canLeft && xmove < 0 || (canRight && xmove > 0)) {
//改变坐标
x += xmove;
if (x < 0) {
x = 0;
}
}
//定义一个图片取得的初始索引数
int temp = 0;
//当前为面向左
if(this.status.indexOf("left") != -1) {
temp += 5;
}
//判断当前是否为移动
if(this.status.indexOf("moving") != -1) {
temp += this.moving;
moving++;
if (moving == 4) {
moving = 0;
}
}
if (this.status.indexOf("jumping") != -1) {
temp += 4;
}
//改变显示图片
this.showImage = StaticValue.allMarioImage.get(temp);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.MyFrame.javapackage org.liky.mario;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class MyFrame extends JFrame implements KeyListener,Runnable {
private List<BackGround> allBG = new ArrayList<BackGround>();
private Mario mario = null;
private BackGround nowBG = null;
private Thread t = new Thread(this);
public static void main(String[] args) {
new MyFrame();
}
public MyFrame() {
this.setTitle("马里奥游戏程序");
this.setSize(900, 600);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((width - 900)/2, (height - 600)/2);
this.setResizable(false);
//初始化全部的图片
StaticValue.init();
//创建全部的场景
for(int i = 0;i <= 3;i++) {
this.allBG.add(new BackGround(i, i==3?true:false));
}
//将第一个场景设置为当前场景
this.nowBG = this.allBG.get(1);
//初始化Mario对象
this.mario = new Mario(0, 480);
//将场景放入mario对象场景的属性中
this.mario.setBg(nowBG);
this.repaint();
this.addKeyListener(this);
t.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
//建立临时的缓冲图片
BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = image.getGraphics();
//绘制背景
g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);
//绘制敌人
Iterator<Enemy> iterEnemy = this.nowBG.getAllEnemy().iterator();
while(iterEnemy.hasNext()) {
Enemy e = iterEnemy.next();
g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this);
}
//绘制障碍物
Iterator<Obstruction> iter = this.nowBG.getAllObstruction().iterator();
while(iter.hasNext()) {
Obstruction ob = iter.next();
g2.drawImage(ob.getShowimage(), ob.getX(), ob.getY(), this);
}
g2.drawImage(this.mario.getShowImage(), this.mario.getX(), this.mario.getY(), this);
//把缓冲图片绘制到窗体中
g2.drawImage(image, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent ke) {
// TODO Auto-generated method stub
//当按下39时(-->),mario向右移动
if(ke.getKeyCode() == 39) {
this.mario.rightMove();
}
//当按下37时(<--),mario向左移动
if(ke.getKeyCode() == 37) {
this.mario.leftMove();
}
//当按下32(空格键)时,mario开始跳跃
if(ke.getKeyCode() == 32) {
this.mario.jump();
}
System.out.println(ke.getKeyCode());
}
@Override
public void keyReleased(KeyEvent ke) {
// TODO Auto-generated method stub
//当抬起39时(-->),mario停止向右移动
if(ke.getKeyCode() == 39) {
this.mario.rightStop();
}
//当抬起37时(<--),mario停止向左移动
if(ke.getKeyCode() == 37) {
this.mario.leftStop();
}
System.out.println(ke.getKeyCode());
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
this.repaint();
try {
Thread.sleep(50);
if(this.mario.getX() >= 840) {
//切换场景
this.nowBG = this.allBG.get(this.nowBG.getSort()+1);
//将当前场景设置mario中
this.mario.setBg(this.nowBG);
//修改mario的坐标
this.mario.setX(0);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5.Obstruction.javapackage org.liky.mario;
import java.awt.image.BufferedImage;
public class Obstruction {
// 保存的坐标
private int x;
private int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
//类型
private int type;
public void setType(int type) {
this.type = type;
}
public int getType() {
return type;
}
//初始的类型
private int startType;
//显示图片
private BufferedImage showimage = null;
//构造方法
public BufferedImage getShowimage() {
return showimage;
}
public Obstruction(int x,int y,int type) {
this.x = x;
this.y = y;
this.type = type;
setImage();
}
//重置方法
public void reset() {
//修改类型为初始类型
this.type = startType;
//改变显示图片
this.setImage();
}
//根据类型改变显示图片
public void setImage() {
showimage = StaticValue.allObstructionImage.get(type);
}
}
6.StaticValue.javapackage org.liky.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;
import com.sun.org.apache.bcel.internal.generic.IfInstruction;
public class StaticValue {
public static List<BufferedImage> allMarioImage = new ArrayList<BufferedImage>();
public static BufferedImage startImage = null;
public static BufferedImage endImage = null;
public static BufferedImage bgImage = null;
public static List<BufferedImage> allFlowerImage = new ArrayList<BufferedImage>();
public static List<BufferedImage> allTriangleImage = new ArrayList<BufferedImage>();
public static List<BufferedImage> allTurtleImage = new ArrayList<BufferedImage>();
public static List<BufferedImage> allObstructionImage = new ArrayList<BufferedImage>();
public static BufferedImage marioDeadImage = null;
public static String imagePath = System.getProperty("user.dir")+"/bin/";
//将全部图片初始化
public static void init() {
//将所有mario的图片保存到静态属性中
for(int i = 1;i <= 10; i++) {
try {
allMarioImage.add(ImageIO.read(new File(imagePath + i + ".gif")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导入全部背景图片
try {
startImage = ImageIO.read(new File(imagePath + "start.gif"));
bgImage = ImageIO.read(new File(imagePath + "firststage.gif"));
endImage = ImageIO.read(new File(imagePath + "firststageend.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//导入所有敌人的图片
for (int i = 1;i <= 5; i++) {
try {
if (i <= 2) {
allFlowerImage.add(ImageIO.read(new File(imagePath + "flower"+i+".gif")));
}
if (i <=3) {
allTriangleImage.add(ImageIO.read(new File(imagePath + "triangle"+i+".gif")));
}
allTurtleImage.add(ImageIO.read(new File(imagePath + "Turtle"+i+".gif")));
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//导入障碍物的图片
for(int i = 1;i <= 12;i++) {
try {
allObstructionImage.add(ImageIO.read(new File(imagePath + "ob"+i+".gif")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导入Mario死亡的图片
try {
marioDeadImage = ImageIO.read(new File(imagePath + "over.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
|