代码演示
package com.arvin;
import java.awt.image.BufferedImage;
public class Mario implements Runnable {
private int x, y;
private String status;
private BufferedImage show = null;
private BackGround backGround = new BackGround();
private Thread thread = null;
private int xSpeed;
private int ySpeed;
private int indexOf;
private int upTime = 0;
private boolean isOK;
private boolean isDeath = false;
private int score = 0;
public Mario(){
}
public Mario(int x, int y){ this.x = x; this.y = y; show = StaticValue.Stand_Right; this.status = "stand--Right"; thread = new Thread(this); thread.start();
}
public void death() { isDeath = true; }
public void leftMove () {
xSpeed = -5;
if (backGround.isReach()) { xSpeed = 0; }
if (status.indexOf("jump") != -1){ status = "jump--left"; }else{ status = "move--left"; } }
public void rightMove () { xSpeed = 5;
if (backGround.isReach()) { xSpeed = 0; }
if (status.indexOf("jump") != -1){ status = "jump--right"; }else{ status = "move--right"; } }
public void leftStop () { xSpeed = 0; if (status.indexOf("jump") != -1){ status = "jump--left"; }else{ status = "stop--left"; } }
public void rightStop () { xSpeed = 0; if (status.indexOf("jump") != -1){ status = "jump--right"; }else{ status = "stop--right"; } }
public void jump () { if (status.indexOf("jump") == -1){ if (status.indexOf("left") != -1) { status = "jump--left"; }else{ status = "jump--right"; } ySpeed = -10; upTime = 7; }
if (backGround.isReach()) { ySpeed = 0; } }
public void fall () { if (status.indexOf("left") != -1){ status = "jump--left"; }else{ status = "jump--right"; } ySpeed = 10; }
@Override public void run() {
while (true) {
boolean onObstacle = false;
boolean canRight = true;
boolean canLeft = true;
if (backGround.isFlag() && this.x >= 500){ this.backGround.setReach(true); if(this.backGround.isBase()) { status = "move--right"; if (x < 690) { x += 5; }else{ isOK = true; } }else{ if (y < 395) { xSpeed = 0; this.y += 5; status = "jump--right"; }
if (y > 395) { this.y = 395; status = "stop--right"; } }
}else{
for (int i = 0; i <backGround.getObstacleList().size(); i++) { Obstacle obstacle = backGround.getObstacleList().get(i); if (obstacle.getY() == this.y + 25 && (obstacle.getX() > this.x -30 && obstacle.getX() < this.x +25)){ onObstacle = true; }
if ((obstacle.getY() >= this.y -30 && obstacle.getY() <= this.y -20) && (obstacle.getX() > this.x -30 && obstacle.getX() < this.x + 25)){ if (obstacle.getType() == 0) { backGround.getObstacleList().remove(obstacle); score += 1; } upTime = 0; }
if (obstacle.getX() == this.x + 25 && (obstacle.getY() > this.y -30 && obstacle.getY() < this.y +25)){ canRight = false; }
if (obstacle.getX() == this.x - 30 && (obstacle.getY() > this.y - 30 && obstacle.getY() < this.y + 25)) { canLeft = false; } }
for (int i = 0; i < backGround.getEnemyList().size(); i++) { Enemy enemy = backGround.getEnemyList().get(i);
if (enemy.getY() == this.y + 20 && (enemy.getX() -25 <= this.x && enemy.getX() +35 >= this.x)) { if (enemy.getType() == 1) { enemy.death(); score += 2;
upTime = 3; ySpeed = -10; }else if (enemy.getType()== 2){ death(); } } if ((enemy.getX() + 35 > this.x && enemy.getX() - 25 < this.x) && (enemy.getY() + 35 >this.y && enemy.getY() -20 < this.y)) { death(); } }
if (onObstacle && upTime == 0){ if (status.indexOf("left") != -1){ if (xSpeed != 0){ status = "move--left"; }else{ status = "stop--left"; } }else{ if (xSpeed != 0){ status = "move--right"; }else{ status = "stop--right"; } } }else{ if (upTime != 0){ upTime--; }else{ fall(); } y += ySpeed; }
if (canLeft && xSpeed < 0 || canRight && xSpeed > 0) { x += xSpeed;
if (x < 0){ x = 0; } }
if (status.contains("move")) { indexOf = indexOf == 0 ? 1 : 0; }
if ("move--left".equals(status)) { show = StaticValue.Run_Left.get(indexOf); }
if ("move--right".equals(status)) { show = StaticValue.Run_Right.get(indexOf); }
if ("stop--left".equals(status)) { show = StaticValue.Stand_Left; }
if ("stop-right".equals(status)) { show = StaticValue.Stand_Right; }
if ("jump--left".equals(status)){ show = StaticValue.Jump_Left; }
if ("jump--right".equals(status)){ show = StaticValue.Jump_Right; }
try { Thread.sleep(50); } catch (InterruptedException exception) { exception.printStackTrace(); }
} } }
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
public BufferedImage getShow() { return show; }
public void setShow(BufferedImage show) { this.show = show; }
public void setBackGround(BackGround backGround) { this.backGround = backGround; }
public boolean isOK() { return isOK; }
public boolean isDeath() { return isDeath; }
public int getScore() { return score; } }
|
版權聲明: 此文章版權歸Arvin所有,如有轉載,請註明來自原作者