gpt4 book ai didi

JAVA super 马里奥让马里奥掉进缺口

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:43 25 4
gpt4 key购买 nike

我的问题很简单:我很难找到一个解决方案,让马里奥在间隙(空旷的空间,没有地面)上行走时摔倒。如果您知道执行此操作的有效方法,请告诉我。另外,如果您不明白,请告诉我是否需要我对代码的解释,谢谢!

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&&centerY!=555&&right){
centerY+=15;
jump=false;
step=0;
}
if(jumpmax==300&&centerY!=555&&left){
centerY+=15;
jump=false;
step=0;
}
if(jumpmax==300&&centerY!=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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com