2010-05-31 107 views
0

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

} 

} 

我真的很感謝所有我能得到的幫助!

+0

添加鏈接;如果不正確請回復。 – trashgod 2010-05-31 18:56:24

+2

一些美容說明:軟件包名稱應該全部小寫,類名(和構造函數)應該以大寫字母開頭。嘗試與[Sun Java命名約定](http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367)保持一致,以避免在快速掃描您的代碼時在同伴Javaians中發生混淆和紅鯡魚。哦,也儘量不要將標籤與空格混合。它應該是4個空格。 – BalusC 2010-05-31 18:59:28

回答

3

This might help.這是一套教程,旨在幫助人們製作基於瓦片的遊戲。包括側面平臺遊戲。見http://www.tonypa.pri.ee/tbw/tut07.html。順便說一句,你在角色移動方法中做了相當密集的圖像加載。不要這樣做。首先緩存圖像。此外,你可以雙緩衝你的畫布,使其平滑。有關詳細信息,請參閱代碼here