- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的问题很简单:我很难找到一个解决方案,让马里奥在间隙(空旷的空间,没有地面)上行走时摔倒。如果您知道执行此操作的有效方法,请告诉我。另外,如果您不明白,请告诉我是否需要我对代码的解释,谢谢!
public class SuperMario extends JPanel {
static BufferedImage brick;
static BufferedImage groundb;
static BufferedImage one; static BufferedImage onef;
static BufferedImage two; static BufferedImage twof;
static BufferedImage three; static BufferedImage threef;
static BufferedImage four; static BufferedImage fourf;
static BufferedImage five; static BufferedImage fivef;
static BufferedImage six; static BufferedImage sixf;
static BufferedImage seven; static BufferedImage sevenf;
static BufferedImage eight; static BufferedImage eightf;
boolean gameover=false;
boolean movenow=false;
boolean left=false;
boolean right=true;
boolean jump=false;
int jumpmax=0;
int step=0;
int frame=0;
boolean framebegin=false;
boolean grounded=true;
int x1=1;
public final int map[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int size=map.length;
boolean movemap=false;
public static void main(String[] args) {
JFrame window = new JFrame("Super Mario");
SuperMario content = new SuperMario();
window.setContentPane(content);
window.setSize(1200, 800);
window.setLocation(100,0);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setResizable(false); // User can't change the window's size.
window.setVisible(true);
try {
brick = ImageIO.read(new File("SuperMario/brick.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
groundb = ImageIO.read(new File("SuperMario/ground.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
one = ImageIO.read(new File("SuperMario/1.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
two = ImageIO.read(new File("SuperMario/2.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
three = ImageIO.read(new File("SuperMario/3.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
four = ImageIO.read(new File("SuperMario/4.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
five = ImageIO.read(new File("SuperMario/5.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
six = ImageIO.read(new File("SuperMario/6.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
seven = ImageIO.read(new File("SuperMario/7.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
eight = ImageIO.read(new File("SuperMario/8.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
onef = ImageIO.read(new File("SuperMario/1f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
twof = ImageIO.read(new File("SuperMario/2f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
threef = ImageIO.read(new File("SuperMario/3f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
fourf = ImageIO.read(new File("SuperMario/4f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
fivef = ImageIO.read(new File("SuperMario/5f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
sixf = ImageIO.read(new File("SuperMario/6f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
sevenf = ImageIO.read(new File("SuperMario/7f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
eightf = ImageIO.read(new File("SuperMario/8f.jpg"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
SuperMario(){
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(framebegin){
frame++;
if(frame>=100&&jump){
}
else if(frame>=100&&!jump){
frame=0;
framebegin=false;
step=0;
}
}
if(mario.centerX<0)
mario.centerX=0;
repaint();
}
};
ActionListener actionanimation = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(mario.animation==8){
mario.animation=1; movenow=false;}
if(mario.animation<=8&&movenow){
mario.animation++;
}
repaint();
}
};
timer = new Timer( 20, action );
marioanimation = new Timer (100, actionanimation);
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
requestFocus();
}
} );
addFocusListener( new FocusListener() {
public void focusGained(FocusEvent evt) {
timer.start();
marioanimation.start();
repaint();
}
public void focusLost(FocusEvent evt) {
timer.stop();
marioanimation.stop();
repaint();
}
} );
addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
int code = evt.getKeyCode();
if (code == KeyEvent.VK_LEFT&&mario.centerY==555&&!movemap) {
left=true; right=false; movenow=true;
mario.centerX-=30;
step++; framebegin=true;
}
if (code == KeyEvent.VK_RIGHT&&mario.centerY==555&&!movemap) {
right=true; left=false; movenow=true;
if(mario.centerX>=600){
movemap=true;
}
if(!movemap)
mario.centerX+=30;
else if(movemap&&mario.centerX!=600)
mario.centerX-=20;
step++; framebegin=true;;
}
if (code == KeyEvent.VK_UP&&mario.centerY==555&&!jump) {
jump=true;}
}
} );
}
public Ground ground;
public Mario mario;
public Timer timer;
public Timer marioanimation;
public void paintComponent(Graphics g){
setBackground(Color.WHITE);
if(ground==null){
ground = new Ground();
mario = new Mario();
}
ground.draw(g);
mario.draw(g);
}
public class Ground implements ImageObserver {
int centerX=0, centerY=0;
int newx=0;
int nbr=1;
void draw(Graphics g){
for(int y = 1; y<4; y++){
centerY=y*50+580;
for(int x=1; x<size; x++){
centerX=(x-1)*50+newx;
if(movemap){
newx-=20;
movemap=false;
}
if(map[x]==1){
g.drawImage(groundb, centerX, centerY, 50, 50, this);
}
}
}
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
// TODO Auto-generated method stub
return false;
}
}
public class Mario implements ImageObserver {
int centerX=0;
int centerY=555;
int animation=1;
void draw(Graphics g){
if(jump&&jumpmax!=300&&step<3){
centerY-=15;
jumpmax+=15;
}
if(centerY==555){
jumpmax=0;
}
if(jump&&jumpmax!=300&&right&&step>=3){
centerY-=15;
jumpmax+=15;
centerX+=15;
}
else if(jump&&jumpmax!=300&&left&&step>=3){
centerY-=15;
jumpmax+=15;
centerX-=15;
}
if(jumpmax==300&¢erY!=555&&right){
centerY+=15;
jump=false;
step=0;
}
if(jumpmax==300&¢erY!=555&&left){
centerY+=15;
jump=false;
step=0;
}
if(jumpmax==300&¢erY!=555&&step<3){
centerY+=15;
jump=false;
step=0;
}
if(right){
if(animation==1){
g.drawImage(one, centerX, centerY, 75, 75, this);}
if(animation==2){
g.drawImage(two, centerX, centerY, 75, 75, this);}
if(animation==3){
g.drawImage(three, centerX, centerY, 75, 75, this);}
if(animation==4){
g.drawImage(four, centerX, centerY, 75, 75, this);}
if(animation==5){
g.drawImage(five, centerX, centerY, 75, 75, this);}
if(animation==6){
g.drawImage(six, centerX, centerY, 75, 75, this);}
if(animation==7){
g.drawImage(seven, centerX, centerY, 75, 75, this);}
if(animation==8){
g.drawImage(eight, centerX, centerY, 75, 75, this);}
}
else if(left){
if(animation==1){
g.drawImage(onef, centerX, centerY, 75, 75, this);}
if(animation==2){
g.drawImage(twof, centerX, centerY, 75, 75, this);}
if(animation==3){
g.drawImage(threef, centerX, centerY, 75, 75, this);}
if(animation==4){
g.drawImage(fourf, centerX, centerY, 75, 75, this);}
if(animation==5){
g.drawImage(fivef, centerX, centerY, 75, 75, this);}
if(animation==6){
g.drawImage(sixf, centerX, centerY, 75, 75, this);}
if(animation==7){
g.drawImage(sevenf, centerX, centerY, 75, 75, this);}
if(animation==8){
g.drawImage(eightf, centerX, centerY, 75, 75, this);}
}
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
// TODO Auto-generated method stub
return false;
}
}
}
最佳答案
首先,请阅读this 。我们希望帮助您,但您至少不应该让我们为难。
无论如何,我们现在来回答你的问题。
好吧,从我从你的代码中读到的内容来看,我可以看到map[]是地面。我假设 1 是一 block 地面,0 是一个间隙。如果是这样,我将尝试继续我的解释。
我要做的是将屏幕划分为与map[]数组中的任何元素一样多的列。例如,如果 map 数组中有 20 个元素,则程序应将屏幕分为 20 列。数组中的每个元素对应于屏幕上的一列。
然后,应该检查 Mario 的 x 值,程序应该检查他所在的部分。如果他的部分在 map 数组中为 0,则他应该跌倒。如果 map 数组的值为 1,则他在地面上。
关于JAVA super 马里奥让马里奥掉进缺口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450848/
问题比较Java,但是我想在Android中实现: 假设有 3 个或更多类相互扩展: class A { ... int color; ... } class B extend
我知道标题听起来有点奇怪,但这正是我愿意做的。简单解释:A类是B类的子类,B类也是C类的子类>. 现在,所有这些类都包含方法m()。在我的 A 类中,这是我唯一可以访问的类,因为其他类仅在运行时可用,
我有一个 UIViewController 类 A 和 B。 A 使用以下方式加载 B:[A.view addSubView B.view]。 B 有一个带有“后退”按钮的导航栏。我想在单击时返回到
我有以下(第三方)类结构。我们将调用第三方项目 ProjectSeriously,并注意我使用 System.out.println 代替其他复杂的功能(100 行代码) . class A {
在下面的代码中,我从 Game 扩展了 MyGame。我有两个问题: 我们是否需要为所有render()、dispose()、pause()调用super方法 和 resize(w,h)?很多人都没有
例如,假设我想在调用 super.viewDidLoad() 时跳过一级。所以我希望能够做这样的事情: override func viewDidLoad() { super.super.vi
public class Faculty extends Employee { public static void main(String[] args) { new Fac
假设我有: class Superclass { //fields... methodA() {...} methodB() {...} ... } class Subclass exte
这个问题在这里已经有了答案: Why is super.super.method(); not allowed in Java? (22 个答案) 关闭 9 年前。 我怀疑我想做的事情是否可行。我有
我有一个实现 Initializable 的类。 public abstract class ExampleClass implements Initializable { public vo
我想知道,我有这个大数组,是否可以只在内存中使用一次而不是每个线程一次?以 stackoverflow 上的标签为例。他们几乎从不改变,为什么不为他们留下一个内存点呢?甚至可能将该数组永久保存在内存中
假设这三个类具有这个简单的层次结构: class A { func foo() { print("A") } } class B: A { override fu
有没有办法在 TypeScript 中调用 super.super.methodName。我想避免调用super.methodName,但我想调用二祖的methodName方法。 谢谢。 最佳答案 T
这个问题已经有答案了: When do I use super()? (11 个回答) 已关闭 7 年前。 package Geometry; public abstract class Geomet
我必须执行and()在我的实现 Predicate 的业务对象上. 出现问题的代码是 and() 行调用: Predicate predicate = new M
我有一个实现接口(interface)的抽象父类(super class): public abstract class FooMatrix implements Matrix { publi
我有四个 UIView:viewA 是 Root View ,它有 viewB 作为它的 subview 。 viewB 将 viewC 作为其 subview ,而 viewC 将 viewD 作为
有什么区别: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() 和:
我有一个通用接口(interface) interface ListList extends List> .由于某些原因,我无法转换 ListList至 List> .有什么方法可以做到吗?为什么它不
我想调用带有两个参数的父类(super class)的构造函数,所以我调用了 super(arguments),但是编译器说: “类 Person 中的构造函数 Person 不能应用于给定类型; 要
我是一名优秀的程序员,十分优秀!