开发者

How do you make a character jump, both on objects and just normal jump?

I'm kind of a beginner when it comes to java programming, and I have a project in school where I'm going to create a game much like Icy Tower. And my question is, how am I going to write to make the character stand on the ground and be able to jump up on objects?

Here's my code so far:

Part one

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;
 }
    }

}

Part two

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 {

 开发者_如何学Pythonpublic void keyReleased(KeyEvent e) {
     jumper.keyReleased(e);
    }
 public void keyPressed(KeyEvent e) {
     jumper.keyPressed(e);
 }
  }
}

Part three

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();   

}

}

I really appreciate all the help I can get!


This might help. It's a set of tutorials aimed at helping people make tile-based games. Including side-on platform games. See http://www.tonypa.pri.ee/tbw/tut07.html. By the way, you're doing quite intensive image-loading stuff in the character movement methods. Don't do that. Cache the images first. Also, you can double-buffer your Canvas to make it smooth. See the code here for details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜