如何在物体和正常跳跃上进行角色跳跃?

时间:2010-05-31 11:53:31

标签: java project projects

在Java编程方面,我是一个初学者,我在学校有一个项目,我将创建一个类似于Icy Tower的游戏。而我的问题是,我该如何写作以使角色站在地面上并能够跳上物体?

到目前为止,这是我的代码:

第一部分

package Sprites;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class jumper {

private String jump = "oka.png";

private int dx;
private int dy;
private int x;
private int y;
private Image image;

public jumper() {
 ImageIcon ii = new ImageIcon(this.getClass().getResource(jump));
 image = ii.getImage();
 x = 50;
 y = 100;
    }

public void move() {
 x += dx;
 y += dy;
}

public int getX() {
 return x;
}

public int getY() {
 return y;
}

public Image getImage() {
 return image;
}



public void keyPressed(KeyEvent e) {
 int key = e.getKeyCode();



 if (key == KeyEvent.VK_LEFT) {
     dx = -5;

     ImageIcon ii = new ImageIcon(this.getClass().getResource("oki.png"));
     image = ii.getImage();
 }

 if (key == KeyEvent.VK_RIGHT){
     dx = 5;

     ImageIcon ii = new ImageIcon(this.getClass().getResource("oka.png"));
     image = ii.getImage();
 }

 if (key == KeyEvent.VK_SPACE) {
     dy = -5;
 }



 if (key == KeyEvent.VK_DOWN) {
     dy = 5;
 }


} 

public void keyReleased(KeyEvent e) {
 int key = e.getKeyCode();


 if (key == KeyEvent.VK_LEFT) {
     dx = 0;
 }

 if (key == KeyEvent.VK_RIGHT){
     dx = 0;
 }

 if (key == KeyEvent.VK_SPACE) {
     dy = 0;
 }



 if (key == KeyEvent.VK_DOWN) {
     dy = 0;
 }
    }

}

第二部分

package Sprites;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

public class board extends JPanel implements ActionListener {

    private Timer klocka;
    private jumper jumper;

    public board() {

 addKeyListener(new TAdapter());
 setFocusable(true);
 setBackground(Color.WHITE);
 setDoubleBuffered(true);

 jumper = new jumper();

 klocka = new Timer(5, this);
 klocka.start();
    }

    public void paint(Graphics g) {
 super.paint(g);

 Graphics2D g2d = (Graphics2D)g;
 g2d.drawImage(jumper.getImage(), jumper.getX(), jumper.getY(), this);

 Toolkit.getDefaultToolkit().sync();
 g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
 jumper.move();
 repaint();
    }

    private class TAdapter extends KeyAdapter {

 public void keyReleased(KeyEvent e) {
     jumper.keyReleased(e);
    }
 public void keyPressed(KeyEvent e) {
     jumper.keyPressed(e);
 }
  }
}

第三部分

package Sprites;

import javax.swing.JFrame;

public class RType extends JFrame {
    public RType() {

 add(new board());

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setSize(800, 600);
 setLocationRelativeTo(null);
 setTitle("R - type");
 setResizable(false);
 setVisible(true);
    }

public static void main(String[] args) {
    new RType();   

}

}

我非常感谢能得到的所有帮助!

1 个答案:

答案 0 :(得分:3)

This might help.这是一套旨在帮助人们制作基于平铺的游戏的教程。包括侧面平台游戏。见http://www.tonypa.pri.ee/tbw/tut07.html。顺便说一下,你在角色运动方法中做了相当密集的图像加载。不要那样做。首先缓存图像。此外,您可以双重缓冲您的画布以使其平滑。有关详细信息,请参阅代码here