我正在开发一款马里奥游戏。到目前为止,我已经为背景,运动(包括重力)制作了代码,并在关卡的开头插入了一个蟾蜍。框架在一个永无止境的循环中重复,不幸的是,当你运行程序时,当马里奥移动时,蟾蜍图像与马里奥角色保持一致(当马里奥向右移动时,蟾蜍尾随他)。我希望当马里奥离开屏幕时,蟾蜍会被遗忘;就像在真正的马里奥游戏中一样,如果你选择不杀死Goomba,Goomba在你移动到足够的时候就不会出现在屏幕上。我怎么能这样做?
以下是我的一些代码
import java.awt.Image;
import javax.swing.ImageIcon;
public class Toads {
int x, y, nx, nx2, distanceTraveled;
Image i;
ImageIcon redToad = new ImageIcon("images/toad2.png");
public Toads() {
i = redToad.getImage(); //Give the player the image
x = 190; //The original x position of the player
y = 273; //The original y position of the player
nx = -0; //Repeating background 1
nx2 = -575; //Repeating background 2
distanceTraveled = 24;
}
public Image getImage() {return i;}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class board extends JPanel implements ActionListener {
player p;
Image background, menuBg;
Timer time;
private menu Menu;
private frame Frame;
private Toads toad;
public static enum STATE {MENU,HELP,GAME};
public static STATE State = STATE.MENU;
public board() {
this.addMouseListener(new mouseInput());
p = new player();
Menu = new menu();
toad = new Toads();
addKeyListener(new AL()); //Listen for keys
setFocusable(true); //Allows movement
ImageIcon i = new ImageIcon("images/MarioMenu.jpg"); //Image for menu
menuBg = i.getImage();
i = new ImageIcon("images/Mario_Background.png"); //Image for background
background = i.getImage(); //Give the background the image
time = new Timer(20,this); //Timer set to update "this" class every 20 milliseconds(Approximately 50fps)
time.start(); //Actually start the timer
}
public void actionPerformed(ActionEvent e) {
p.move(); //Call the move method from the player class
repaint(); //Repaint
}
public void paintComponent(Graphics g) { //Graphics method
if(State==STATE.GAME) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; //casts 2d graphics(or however you would explain it)
g2d.drawImage(background, -p.nx, 0, null); //Draw the background image
g2d.drawImage(background, -p.nx2, 0, null); //Draw the background image
if(-p.nx<-575) //If going forwards
p.nx=-575; //Start placing forwards every 575px in front on the last one
else if(-p.nx>575) //If going backwards
p.nx=575; //Start placing backwards every 575px behind the last one
if(-p.nx2<-575) //If going forwards
p.nx2=-575; //Start placing forwards every 575px in front on the last one
else if(-p.nx2>575) //If going backwards
p.nx2=575; //Start placing backgrounds every 575px behind the last one
g2d.drawImage(p.getImage(), p.getX(), p.getY(), null); //Draw the player at the position he is currently(Coordinate values taken from player class)
g2d.drawImage(toad.getImage(), toad.x, toad.y, null);
}
else if (State == STATE.HELP){
}
else {
g.drawImage(menuBg, 0, 0, null);
Menu.render(g);
}
}
private class AL extends KeyAdapter { //Action Listener extends key adapter
public void keyPressed(KeyEvent e) { //On key press
p.keyPressed(e); //Send whatever key was pressed TO the keyPressed method in the player class
}
public void keyReleased(KeyEvent e) { //On key release
p.keyReleased(e); //Send whatever key was released TO the keyReleased method in the player class
}
}
}
基本上,我一直在寻找一种让框架像马里奥游戏一样工作的方法 - 例如,如果整个级别是3000像素乘3000像素,你会如何制作它以使屏幕(例如)500像素乘500像素并与马里奥一起移动(和其他物体一起移动#34;屏幕&#34;不要随着马里奥移动)? 提前谢谢你的帮助。
答案 0 :(得分:0)
创建一个名为screenX和screenY的变量,它是屏幕左上角的坐标,相对于整个世界的左上角呈现给玩家。渲染时,首先通过执行简单的矩形碰撞检查来检查对象是否在屏幕上。如果它在屏幕上,则在(actualX - screenX, actualY - screenY)
处呈现它。这应该完全符合你的需要。