创建窗口
package org.wn.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.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
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 = null;
//是否已经开始游戏
private boolean isStart = false;
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=1;i<=7;i++){
this.allBG.add(new BackGround(i, i==7?true:false));
}
//将第一个场景设置为当前场景
this.nowBG = this.allBG.get(0);
//初始化玛丽奥
this.mario = new Mario(0, 480);
//将玛丽奥放入场景中
this.mario.setBg(nowBG);
this.repaint();
this.addKeyListener(this);
this.t = new Thread(this);
t.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g) {
//双缓存
BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = image.getGraphics();
if(this.isStart){
//绘制背景
g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);
//绘制生命
g2.drawString("生命: "+this.mario.getLife(), 720, 50);
//绘制分数
g2.drawString("分数: "+this.mario.getScore(), 800, 50);
//绘制怪物敌人
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);
}else{
g2.drawImage(StaticValue.startImage, 0, 0, this);
}
//把缓存图片绘制进去
g.drawImage(image, 0, 0, this);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(this.isStart){
//玛丽奥的移动控制
if(e.getKeyCode()==39){
this.mario.rightMove();
}
if(e.getKeyCode()==37){
this.mario.leftMove();
}
//跳跃控制
if(e.getKeyCode()==38){
this.mario.jump();
}
}else if(e.getKeyCode()==32){
this.isStart = true;
this.nowBG.enemyStartMove();
this.mario.setScore(0);
this.mario.setLife(3);
}
}
public void keyReleased(KeyEvent e) {
if(this.isStart){
//控制玛丽奥的停止
if(e.getKeyCode()==39){
this.mario.rightStop();;
}
if(e.getKeyCode()==37){
this.mario.leftStop();;
}
}
}
public void run() {
while(true){
this.repaint();
try {
Thread.sleep(50);
if(this.mario.getX() >= 840){
//切换场景
this.nowBG = this.allBG.get(this.nowBG.getSort());
//将场景放入玛丽奥中
this.mario.setBg(nowBG);
this.nowBG.enemyStartMove();
//修改马里奥坐标
this.mario.setX(0);
}
if(this.mario.isDead()){
JOptionPane.showMessageDialog(this, "游戏结束");
System.exit(0);
}
if(this.mario.isClear()){
JOptionPane.showMessageDialog(this, "恭喜游戏通关!");
System.exit(0);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
静态状态类
package org.wn.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 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 mariDeadImage = null;
public static String ImagePath = System.getProperty("user.dir")+"/bin/";
//将图片初始化
public static void init(){
//玛丽奥图片初始化
for(int i=1;i<=10;i++){
try {
allMarioImage.add(ImageIO.read(new File(ImagePath+i+".png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导入背景图片
try {
startImage = ImageIO.read(new File(ImagePath+"start.jpg"));
bgImage = ImageIO.read(new File(ImagePath+"firststage.jpg"));
endImage = ImageIO.read(new File(ImagePath+"firststageend.jpg"));
} 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+".png")));
}
if(i<=3){
allTriangleImage.add(ImageIO.read(new File(ImagePath+"triangle"+i+".png")));
}
allTurtleImage.add(ImageIO.read(new File(ImagePath+"Turtle"+i+".png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导入障碍物图片
for(int i=1;i<=12;i++){
try {
allObstructionImage.add(ImageIO.read(new File(ImagePath+"ob"+i+".png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//导入玛丽奥死亡图片
try {
mariDeadImage = ImageIO.read(new File(ImagePath+"over.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
背景图片类
package org.wn.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 boolean isOver = false;
//定义降旗结束
private boolean isDown = false;
//用集合保存敌人
private List<Enemy> allEnemy = new ArrayList<Enemy>();
//用集合保存障碍物
private List<Obstruction> allObstruction = new ArrayList<Obstruction>();
//被消灭的敌人
private List<Enemy> removeEnemy = new ArrayList<Enemy>();
//被消灭的障碍物
private List<Obstruction> removeObstruction = new ArrayList<Obstruction>();
//敌人开始移动
public void enemyStartMove(){
for(int i=0;i<this.allEnemy.size();i++){
this.allEnemy.get(i).startMove();
}
}
//构造方法
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));
}
//绘制砖块和问号
this.allObstruction.add(new Obstruction(120, 360, 4,this));
this.allObstruction.add(new Obstruction(300, 360, 0,this));
this.allObstruction.add(new Obstruction(360, 360, 4,this));
this.allObstruction.add(new Obstruction(420, 360, 0,this));
this.allObstruction.add(new Obstruction(480, 360, 4,this));
this.allObstruction.add(new Obstruction(540, 360, 0,this));
this.allObstruction.add(new Obstruction(420, 180, 4,this));
//绘制水管
this.allObstruction.add(new Obstruction(660, 540, 6,this));
this.allObstruction.add(new Obstruction(720, 540, 5,this));
this.allObstruction.add(new Obstruction(660, 480, 8,this));
this.allObstruction.add(new Obstruction(720, 480, 7,this));
//隐藏砖块
this.allObstruction.add(new Obstruction(660, 300, 3,this));
//绘制怪物
this.allEnemy.add(new Enemy(600, 480, true, 1,this));
this.allEnemy.add(new Enemy(690, 540, true, 2, 420, 540,this));
}
//第二个场景
if(sort==2){
for(int i=0;i<15;i++){
if(i != 9 && i != 10 && i != 11 ){
this.allObstruction.add(new Obstruction(i*60, 540, 9,this));
}
}
//绘制水管
this.allObstruction.add(new Obstruction(60, 540, 6,this));
this.allObstruction.add(new Obstruction(120, 540, 5,this));
this.allObstruction.add(new Obstruction(60, 480, 6,this));
this.allObstruction.add(new Obstruction(120, 480, 5,this));
this.allObstruction.add(new Obstruction(60, 420, 8,this));
this.allObstruction.add(new Obstruction(120, 420, 7,this));
this.allObstruction.add(new Obstruction(300, 540, 6,this));
this.allObstruction.add(new Obstruction(360, 540, 5,this));
this.allObstruction.add(new Obstruction(300, 480, 6,this));
this.allObstruction.add(new Obstruction(360, 480, 5,this));
this.allObstruction.add(new Obstruction(300, 420, 6,this));
this.allObstruction.add(new Obstruction(360, 420, 5,this));
this.allObstruction.add(new Obstruction(300, 360, 8,this));
this.allObstruction.add(new Obstruction(360, 360, 7,this));
//绘制怪物
this.allEnemy.add(new Enemy(330, 360, true, 2, 300, 420,this));
}
//第三个场景
if(sort==3){
for(int i=0;i<15;i++){
this.allObstruction.add(new Obstruction(i*60, 540, 9,this));
}
//绘制砖块和问号
this.allObstruction.add(new Obstruction(180, 360, 4,this));
this.allObstruction.add(new Obstruction(420, 360, 4,this));
this.allObstruction.add(new Obstruction(660, 360, 4,this));
this.allObstruction.add(new Obstruction(420, 180, 4,this));
}
//第四个场景
if(sort==4){
for(int i=0;i<15;i++){
if(i<2||i>12){
this.allObstruction.add(new Obstruction(i*60, 540, 9,this));
}
}
this.allObstruction.add(new Obstruction(120, 360, 0,this));
this.allObstruction.add(new Obstruction(180, 360, 0,this));
this.allObstruction.add(new Obstruction(300, 180, 0,this));
this.allObstruction.add(new Obstruction(360, 180, 0,this));
this.allObstruction.add(new Obstruction(420, 180, 0,this));
this.allObstruction.add(new Obstruction(480, 180, 0,this));
this.allObstruction.add(new Obstruction(540, 180, 0,this));
this.allObstruction.add(new Obstruction(660, 360, 0,this));
this.allObstruction.add(new Obstruction(720, 360, 0,this));
}
//第五个场景
if(sort==5){
int z = 2;
for(int i=0;i<15;i++){
if(i%2==0 && i<7){
this.allObstruction.add(new Obstruction(i*60, 540-(i*60), 9,this));
for(int x=i;x>0;x--){
this.allObstruction.add(new Obstruction(i*60, 540-(x*60)+60, 10,this));
}
}
if(i%2==0 && i>7){
this.allObstruction.add(new Obstruction(i*60, 540-((i-z)*60), 9,this));
for(int x=i-z;x>0;x--){
this.allObstruction.add(new Obstruction(i*60, 540-(x*60)+60, 10,this));
}
z+=4;
}
}
}
//第六个场景
if(sort==6){
for(int i=0;i<15;i++){
this.allObstruction.add(new Obstruction(i*60, 540, 9,this));
}
this.allObstruction.add(new Obstruction(480, 480, 1,this));
this.allObstruction.add(new Obstruction(480, 420, 1,this));
this.allObstruction.add(new Obstruction(480, 360, 1,this));
this.allObstruction.add(new Obstruction(480, 300, 1,this));
this.allObstruction.add(new Obstruction(480, 240, 1,this));
this.allObstruction.add(new Obstruction(480, 180, 1,this));
this.allObstruction.add(new Obstruction(540, 240, 1,this));
this.allObstruction.add(new Obstruction(540, 300, 1,this));
this.allObstruction.add(new Obstruction(540, 360, 1,this));
this.allObstruction.add(new Obstruction(540, 420, 1,this));
this.allObstruction.add(new Obstruction(540, 480, 1,this));
this.allObstruction.add(new Obstruction(600, 300, 1,this));
this.allObstruction.add(new Obstruction(600, 360, 1,this));
this.allObstruction.add(new Obstruction(600, 420, 1,this));
this.allObstruction.add(new Obstruction(600, 480, 1,this));
this.allObstruction.add(new Obstruction(660, 360, 1,this));
this.allObstruction.add(new Obstruction(660, 420, 1,this));
this.allObstruction.add(new Obstruction(660, 480, 1,this));
this.allObstruction.add(new Obstruction(720, 420, 1,this));
this.allObstruction.add(new Obstruction(720, 480, 1,this));
this.allObstruction.add(new Obstruction(780, 480, 1,this));
//通关要点,隐形砖块
this.allObstruction.add(new Obstruction(300, 360, 3,this));
}
//第七个场景
if(sort==7){
for(int i=0;i<15;i++){
this.allObstruction.add(new Obstruction(i*60, 540, 9,this));
}
this.allObstruction.add(new Obstruction(490, 180, 11,this));
this.allObstruction.add(new Obstruction(520, 480, 2,this));
//地上障碍物
this.allObstruction.add(new Obstruction(240, 360, 1,this));
this.allObstruction.add(new Obstruction(240, 420, 1,this));
this.allObstruction.add(new Obstruction(240, 480, 1,this));
this.allObstruction.add(new Obstruction(180, 420, 1,this));
this.allObstruction.add(new Obstruction(180, 480, 1,this));
this.allObstruction.add(new Obstruction(120, 480, 1,this));
}
}
//重置方法,重置障碍物和敌人
public void reset(){
//将移除的障碍物和敌人还原
this.allEnemy.addAll(this.removeEnemy);
this.allObstruction.addAll(this.removeObstruction);
//调用障碍物和敌人的重置方法
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 BufferedImage getBgImage() {
return bgImage;
}
public List<Obstruction> getAllObstruction() {
return allObstruction;
}
public List<Obstruction> getRemoveObstruction() {
return removeObstruction;
}
public int getSort() {
return sort;
}
public List<Enemy> getAllEnemy() {
return allEnemy;
}
public List<Enemy> getRemoveEnemy() {
return removeEnemy;
}
public boolean isFlag() {
return flag;
}
public boolean isOver() {
return isOver;
}
public void setOver(boolean isOver) {
this.isOver = isOver;
}
public boolean isDown() {
return isDown;
}
public void setDown(boolean isDown) {
this.isDown = isDown;
}
}
角色大类
马里奥
package org.wn.mario;
import java.awt.image.BufferedImage;
import java.io.Externalizable;
import javax.swing.JOptionPane;
public class Mario implements Runnable{
//坐标
private int x;
private int y;
//定义玛丽奥所在场景
private BackGround bg;
//加入线程
private Thread t = null;
//移动速度e
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;
//标记玛丽奥是否死亡
private boolean isDead = false;
//完成游戏,游戏结束
private boolean isClear = false;
//构造方法
public Mario(int x,int y){
this.x = x;
this.y = y;
//初始化玛丽奥图片
this.showImage = StaticValue.allMarioImage.get(0);
this.score = 0;
this.life = 3;
this.t = new Thread(this);
t.start();
this.status = "right-standing";
}
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 = -10;
upTime = 18;
}
}
//下落方法
public void down(){
if(this.status.indexOf("left") != -1){
this.status = "left-jumping";
}else{
this.status = "right-jumping";
}
ymove = 10;
}
//死亡方法
public void dead(){
this.life--;
if(this.life == 0){
this.isDead = true;
}else{
this.bg.reset();
this.x = 0;
this.y = 480;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BufferedImage getShowImage() {
return showImage;
}
public void run() {
while(true){
//判断是否与障碍物碰撞
//定义标记
if(this.bg.isFlag() && this.x >= 520){
this.bg.setOver(true);
if(this.bg.isDown()){
//降旗后玛丽奥开始移
this.status = "right-moving";
if(this.x < 580){
//向右
this.x += 5;
}else{
if(this.y < 480){
//向下
this.y += 5;
}
this.x += 5;
if(this.x >= 780){
//游戏结束
this.setClear(true);
}
}
}else{
//如果为最后一个场景,同事Mario的x坐标到了550,游戏结束
//自动控制玛丽奥
if(this.y < 420){
this.y += 5;
}
if(this.y >= 420){
this.y = 420;
this.status = "right-standing";
}
}
}else{
boolean canLeft = true;
boolean canRight = true;
//能否跳跃标记
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)){
if(ob.getType() != 3){
canRight = false;
}
}
//不能向左移动
if(ob.getX()==this.x-60 && (ob.getY()+50>this.y && ob.getY()-50<this.y)){
if(ob.getType() != 3){
canLeft = false;
}
}
//判断能否跳跃
if(ob.getY()==this.y+60 && (ob.getX()+60>this.x && ob.getX()-60<this.x)){
if(ob.getType() != 3){
onLand = true;
}
}
//判断玛丽奥跳跃时是否撞到障碍物
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.getRemoveObstruction().add(ob);
}
//如果是问号||隐藏的砖块
if((ob.getType()==4 || ob.getType()==3) && upTime > 0){
//增加分数
score += 10;
ob.setType(2);
ob.setImage();
}
upTime = 0;
}
}
//对敌人的判断
for(int i=0;i<this.bg.getAllEnemy().size();i++){
Enemy e = this.bg.getAllEnemy().get(i);
if((e.getX()+50>this.x && e.getX()-50<this.x) && (e.getY()+60>this.y && e.getY()-60<this.y)){
//玛丽奥死亡
this.dead();
}
if(e.getY()==this.y+60 && (e.getX()+60>this.x && e.getX()-60<this.x)){
if(e.getType() == 1){
e.dead();
this.upTime = 5;
this.ymove = -10;
//敌人死亡,增加分数
score += 10;
}else if(e.getType() == 2){
this.dead();
}
}
}
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(this.y>600){
this.dead();
}
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){
this.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();
}
}
}
public void setBg(BackGround bg) {
this.bg = bg;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public boolean isDead() {
return isDead;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public boolean isClear() {
return isClear;
}
public void setClear(boolean isClear) {
this.isClear = isClear;
}
}
敌人
package org.wn.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;
//移动方向
private boolean isLeftOrUp = true;
//移动范围
private int upMax = 0;
private int downMax = 0;
//加入线程
private Thread t = null;
//定义图片变化
private int imageType = 0;
//定义所在场景
private BackGround bg ;
//蘑菇怪
public Enemy(int x,int y,boolean isLeft,int type,BackGround bg){
this.x = x;
this.y = y;
this.startx = x;
this.starty = y;
this.isLeftOrUp = isLeft;
this.type = type;
this.bg = bg;
if(type==1){
this.showImage = StaticValue.allTriangleImage.get(0);
}
this.t = new Thread(this);
t.start();
t.suspend();
}
//食人花
public Enemy(int x,int y,boolean isUp,int type,int upMax,int downMax,BackGround bg){
this.x = x;
this.y = y;
this.startx = x;
this.starty = y;
this.isLeftOrUp = isUp;
this.type = type;
this.upMax = upMax;
this.downMax = downMax;
this.bg = bg;
if(type==2){
this.showImage = StaticValue.allFlowerImage.get(0);
}
this.t = new Thread(this);
t.start();
t.suspend();
}
public void run() {
while(true){
//判断怪物类型
if(type==1){
if(this.isLeftOrUp){
this.x -= 5;
}else{
this.x += 5;
}
if(imageType==0){
imageType = 1;
}else{
imageType = 0;
}
//定义标记
boolean canLeft = true;
boolean canRight = true;
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;
}
}
if(!canLeft && this.isLeftOrUp || this.x == 0){
this.isLeftOrUp = false;
}else if(!canRight && !this.isLeftOrUp || this.x == 840){
this.isLeftOrUp = true;
}
this.showImage = StaticValue.allTriangleImage.get(imageType);
}
if(type==2){
if(this.isLeftOrUp){
this.y -=5;
}else{
this.y +=5;
}
if(imageType==0){
imageType = 1;
}else{
imageType = 0;
}
if(this.isLeftOrUp && this.y == this.upMax){
this.isLeftOrUp = false;
}
if(!this.isLeftOrUp && this.y == this.downMax){
this.isLeftOrUp = true;
}
this.showImage = StaticValue.allFlowerImage.get(imageType);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void reset(){
//还原坐标
this.x = this.startx;
this.y = this.starty;
//还原图片
if(this.type == 1){
this.showImage = StaticValue.allTriangleImage.get(0);
}else if(this.type == 2){
this.showImage = StaticValue.allFlowerImage.get(0);
}
}
public void dead(){
//死亡图片
this.showImage = StaticValue.allTriangleImage.get(2);
this.bg.getAllEnemy().remove(this);
this.bg.getRemoveEnemy().add(this);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BufferedImage getShowImage() {
return showImage;
}
public void setBg(BackGround bg) {
this.bg = bg;
}
public int getType() {
return type;
}
public void startMove(){
t.resume();
}
}
旗帜
package org.wn.mario;
import java.awt.image.BufferedImage;
public class Obstruction implements Runnable{
//坐标
private int x;
private int y;
//控制旗子
private Thread t = new Thread(this);
//类型
private int type;
//初始类型
private int starttype;
//显示图片
private BufferedImage showImage = null;
//取得场景
private BackGround bg;
//构造方法
public Obstruction(int x,int y,int type,BackGround bg){
this.x = x;
this.y = y;
this.type = type;
this.starttype = type;
this.bg = bg;
setImage();
if(this.type == 11){
t.start();
}
}
//重置方法
public void reset(){
this.type = starttype;
this.setImage();
}
//根据状态改变显示图片
public void setImage(){
showImage = StaticValue.allObstructionImage.get(type);
}
public BufferedImage getShowImage() {
return showImage;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public void run() {
// TODO Auto-generated method stub
while(true){
if(this.bg.isOver()){
if(this.y < 420){
this.y += 5;
}else{
this.bg.setDown(true);
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}