package com.sxt;
import java.awt.image.BufferedImage;
public class Mario implements Runnable{
// 用于表示横纵坐标
private int x;
private int y;
// 用于表示当前的状态
private String status;
// 用于显示当前状态对应的图像
private BufferedImage show=null;
// 定义一个BackGround对象,用来获取障碍物的信息
private BackGround backGround=new BackGround();
// 用来实现马里奥的动作
private Thread thread=null;
// 马里奥的移动速度
private int xSpeed;
//马里奥的跳跃速度
private int ySpeed;
// 定义一个索引
private int index;
// 表示马里奥的上升时间
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_R;
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;
}
// 判断马里奥是否在空中,indexOf()==-1意思是没有匹配到jump元素
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 ob = backGround.getObstacleList().get(i);
//判断马里奥是否位于障碍物上
if (ob.getY() == this.y + 25 && (ob.getX() > this.x - 30 && ob.getX() < this.x + 25)) {
onObstacle = true;
}
// 判断马里奥是否跳起来顶到砖块
if (ob.getY() >= this.y - 30 && ob.getY() <= this.y - 20 && (ob.getX() > this.x - 30 && ob.getX() < this.x + 25)) {
if (ob.getType() == 0) {
backGround.getObstacleList().remove(ob);
score+=1;
}
upTime = 0;
}
// 判断是否可以往右走
if (ob.getX() == this.x + 25 && (ob.getY() > this.y - 30 && ob.getY() < this.y + 25)) {
canRight = false;
}
// 判断是否可以往左走
if (ob.getX() == this.x - 30 && (ob.getY() > this.y - 30 && ob.getY() < this.y + 25)) {
canLeft = false;
}
}
// 判断马里奥是否碰到敌人死亡或者踩死蘑菇敌人
for (int i=0;i<backGround.getEnemyList().size();i++){
Enemy e=backGround.getEnemyList().get(i);
if (e.getY()==this.y+20&&(e.getX()-25<=this.x&&e.getX()+35>=this.x)){
if (e.getType()==1){
e.death();
score+=2;
upTime=3;
ySpeed=-10;
}
else if (e.getType()==2){
//马里奥死亡
death();
}
}
if ((e.getX()+35>this.x&&e.getX()-25<this.x)&&(e.getY()+35>this.y&&e.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")){
index=index==0?1:0;
}
// 判断马里奥是否是向左移动
if ("move--left".equals(status)){
show=StaticValue.run_L.get(index);
}
// 判断马里奥是否是向右移动
if ("move--right".equals(status)){
show=StaticValue.run_R.get(index);
}
// 判断马里奥是否是向左停止
if ("stop--left".equals(status)){
show=StaticValue.stand_L;
}
// 判断马里奥是否是向右停止
if ("stop--right".equals(status)){
show=StaticValue.stand_R;
}
// 判断马里奥是否是向左跳跃的
if("jump--left".equals(status)){
show=StaticValue.jump_L;
}
// 判断马里奥是否是向右跳跃的
if ("jump--right".equals(status)){
show=StaticValue.jump_R;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.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 Bu
没有合适的资源?快使用搜索试试~ 我知道了~
超级玛丽简单入门小游戏
共48个文件
png:26个
class:7个
java:7个
需积分: 0 0 下载量 184 浏览量
2023-03-26
10:04:31
上传
评论
收藏 2.24MB ZIP 举报
温馨提示
超级玛丽简单入门小游戏
资源推荐
资源详情
资源评论
收起资源包目录
超级玛丽游戏.zip (48个子文件)
超级玛丽游戏
pom.xml 568B
src
test
java
main
resources
java
image
soil_up.png 3KB
flag.png 2KB
fungus2.png 4KB
bg2.png 171KB
brick.png 3KB
tower.png 29KB
s_mario_run2_L.png 2KB
s_mario_stand_R.png 2KB
s_mario_run2_R.png 2KB
soil_base.png 3KB
pipe4.png 970B
fungus3.png 2KB
pipe2.png 3KB
s_mario_stand_L.png 2KB
bg.png 84KB
gan.png 4KB
pipe3.png 1KB
s_mario_run1_L.png 2KB
fungus1.png 4KB
flower1.1.png 3KB
s_mario_jump1_L.png 2KB
brick2.png 2KB
flower1.2.png 2KB
pipe1.png 3KB
s_mario_run1_R.png 2KB
s_mario_jump1_R.png 2KB
Music
music.wav 2.41MB
com
sxt
Mario.java 8KB
Obstacle.java 1KB
MyFrame.java 5KB
BackGround.java 8KB
StaticValue.java 4KB
Music.java 605B
Enemy.java 4KB
.idea
jarRepositories.xml 880B
runConfigurations.xml 346B
workspace.xml 5KB
misc.xml 545B
compiler.xml 551B
.gitignore 184B
target
classes
com
sxt
Obstacle.class 2KB
MyFrame.class 5KB
Enemy.class 3KB
Music.class 1KB
Mario.class 5KB
StaticValue.class 3KB
BackGround.class 5KB
generated-sources
annotations
共 48 条
- 1
资源评论
qq_57377447
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功