我是编程新手,我在如何控制动画速度方面遇到了一些问题。我希望你能给我一些关于这段代码的提示。
public class Maze extends Canvas {
Map map;
Mario mario;
private Timer time;
private BufferStrategy strategy;
private int move;
boolean gameRunning = true;
public Maze(){
map = new Map();
mario = new Mario();
JFrame frame = new JFrame();
frame.setTitle("SuperMario2D");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(438, 438));
panel.setLayout(null);
setBounds(0, 0, 454, 476);
panel.add(this);
setIgnoreRepaint(true);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
addKeyListener(new KeyHandler());
requestFocus();
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public void gameLoop() {
long lastLoopTime = System.currentTimeMillis();
while (gameRunning) {
// work out how long its been since the last update, this
// will be used to calculate how far the entities should
// move this loop
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0,800,600);
map.draw(g);
mario.draw(g, move);
g.dispose();
strategy.show();
}
try {
Thread.sleep(10);
} catch (Exception e) {}
}
public class KeyHandler extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP){
mario.move(0, -1); //if it's not equal this will not execute because Y-1 will equal to w
move = 1;
System.out.println("You press up");
}
if(keycode == KeyEvent.VK_DOWN){
mario.move(0, 1);
move = 2;
System.out.println("You press down");
}
if(keycode == KeyEvent.VK_LEFT){
mario.move(-1, 0);
move = 3;
System.out.println("You press left");
}
if(keycode == KeyEvent.VK_RIGHT){
mario.move(1, 0);
move = 4;
System.out.println("You press right");
}
}
public void keyReleased(KeyEvent e){
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP){
move = 5;
System.out.println("You released up");
}
if(keycode == KeyEvent.VK_DOWN){
move = 6;
System.out.println("You released down");
}
if(keycode == KeyEvent.VK_LEFT){
move = 7;
System.out.println("You released down");
}
if(keycode == KeyEvent.VK_RIGHT){
move = 8;
System.out.println("You released down");
}
}
public void keyTyped(KeyEvent e){
}
}
public static void main(String[] args){
Maze maze = new Maze();
maze.gameLoop();
}
}
这是加载我的动画图片的代码
public class Mario {
private int x, y;
private int tileX, tileY; //for collision
//private Image player;
private Image[] playerDown = new Image[3];
private Image[] playerUp = new Image[3];
private Image[] playerLeft = new Image[3];
private Image[] playerRight = new Image[3];
private Image[] playerJumpD = new Image[3];
private Image[] playerJumpU = new Image[3];
private Image[] playerJumpL = new Image[3];
private Image[] playerJumpR = new Image[3];
private int cycle = 0;
private long scene1 = 0;
private long scene2 = 500;
public Mario(){
moveDown();
moveUp();
moveLeft();
moveRight();
/*
//jump
jumpDown();
jumpUp();
jumpLeft();
jumpRight();
*/
tileX = 1;
tileY = 1;
}
public void moveDown(){
playerDown[0] = new ImageIcon("images/marioFW1.png").getImage();
playerDown[1] = new ImageIcon("images/marioFW2.png").getImage();
playerDown[2] = new ImageIcon("images/marioFW3.png").getImage();
}
public void moveUp(){
playerUp[0] = new ImageIcon("images/marioB1.png").getImage();
playerUp[1] = new ImageIcon("images/marioB2.png").getImage();
playerUp[2] = new ImageIcon("images/marioB3.png").getImage();
}
public void moveLeft(){
playerLeft[0] = new ImageIcon("images/marioJW1.png").getImage();
playerLeft[1] = new ImageIcon("images/marioJW2.png").getImage();
playerLeft[2] = new ImageIcon("images/marioJW3.png").getImage();
}
public void moveRight(){
playerRight[0] = new ImageIcon("images/marioR1.png").getImage();
playerRight[1] = new ImageIcon("images/marioR2.png").getImage();
playerRight[2] = new ImageIcon("images/marioR3.png").getImage();
}
/*
//jump
public void jumpDown(){
playerJumpD[0] = new ImageIcon("images/marioFJ1.png").getImage();
playerJumpD[1] = new ImageIcon("images/marioFJ2.png").getImage();
playerJumpD[2] = new ImageIcon("images/marioFJ3.png").getImage();
}
public void jumpUp(){
playerJumpU[0] = new ImageIcon("images/marioBJ1.png").getImage();
playerJumpU[1] = new ImageIcon("images/marioBJ2.png").getImage();
playerJumpU[2] = new ImageIcon("images/marioBJ3.png").getImage();
}
public void jumpLeft(){
playerJumpL[0] = new ImageIcon("images/marioJL1.png").getImage();
playerJumpL[1] = new ImageIcon("images/marioJL2.png").getImage();
playerJumpL[2] = new ImageIcon("images/marioJL3.png").getImage();
}
public void jumpRight(){
playerJumpR[0] = new ImageIcon("images/marioRJ1.png").getImage();
playerJumpR[1] = new ImageIcon("images/marioRJ2.png").getImage();
playerJumpR[2] = new ImageIcon("images/marioRJ3.png").getImage();
}
*/
public void move(int dx, int dy){
// x += dx;
// y += dy;
tileX += dx;
tileY += dy;
}
public Image getMoveDown(){
if(cycle == playerDown.length){
cycle = 0;
}
return playerDown[cycle++];
}
public Image getMoveUp(){
if(cycle == playerUp.length){
cycle = 0;
}
return playerUp[cycle++];
}
public Image getMoveLeft(){
if(cycle == playerLeft.length){
cycle = 0;
}
return playerLeft[cycle++];
}
public Image getMoveRight(){
if(cycle == playerRight.length){
cycle = 0;
}
return playerRight[cycle++];
}
/*
//jump
public Image getJumpD(){
if(cycle == playerJumpR.length){
cycle = 0;
}
return playerJumpR[cycle++];
}
public Image getJumpU(){
if(cycle == playerJumpU.length){
cycle = 0;
}
return playerJumpU[cycle++];
}
public Image getJumpL(){
if(cycle == playerJumpL.length){
cycle = 0;
}
return playerJumpL[cycle++];
}
public Image getJumpR(){
if(cycle == playerJumpR.length){
cycle = 0;
}
return playerJumpR[cycle++];
}
*/
/*
public void checkMove(){
if (System.currentTimeMillis() - scene1 < scene2) {
return;
}
scene1 = System.currentTimeMillis();
}
*/
public int getTileX(){
return tileX;
}
public int getTileY(){
return tileY;
}
public void draw(Graphics g, int move) {
if(move == 0)
g.drawImage(playerDown[1], getTileX(), getTileY(), null );
if(move == 1)
g.drawImage(getMoveUp(), getTileX(), getTileY(), null );
if(move == 2)
g.drawImage(getMoveDown(), getTileX(), getTileY(), null );
if(move == 3)
g.drawImage(getMoveLeft(), getTileX(), getTileY(), null );
if(move == 4)
g.drawImage(getMoveRight(), getTileX(), getTileY(), null );
if(move == 5)
g.drawImage(playerUp[1], getTileX(), getTileY(), null );
if(move == 6)
g.drawImage(playerDown[1], getTileX(), getTileY(), null );
if(move == 7)
g.drawImage(playerLeft[1], getTileX(), getTileY(), null );
if(move == 8)
g.drawImage(playerRight[1], getTileX(), getTileY(), null );
}
}
这是成功加载地图的地方。我不确定我是如何让我在加速图形模式下运行的。
public class Map {
private Scanner m;
private String[] Map = new String[14]; //how many tiles in the map
private Image tiles, grass2, grass3, grass3b;
private Image wall, sand1;
private Image finish;
public Map(){
tiles = new ImageIcon("images/grass2.png").getImage();
wall = new ImageIcon("images/grass1.png").getImage();
grass2 = new ImageIcon("images/grass2.png").getImage();
grass3 = new ImageIcon("images/grass3.png").getImage();
grass3b = new ImageIcon("images/grass3b.png").getImage();
sand1 = new ImageIcon("images/sand1.png").getImage();
finish = new ImageIcon("images/tile.png").getImage();
openFile();
readFile();
closeFile();
}
public void openFile(){
try{
m = new Scanner(new File("images/Map.txt"));
}catch(Exception e){
System.out.println("Can't open the file: "+e);
}
}
public void readFile(){
while(m.hasNext()){ // read through the file
for(int i = 0; i < 14; i++){
Map[i] = m.next();
}
}
}
public void closeFile(){
m.close();
}
public String getMap(int x, int y){
String index = Map[y].substring(x, x + 1);
return index;
}
public Image getTiles(){
return tiles;
}
public Image getWall(){
return wall;
}
public Image getFinish(){
return finish;
}
public Image getImageGrass2(){
return grass2;
}
public Image getImageGrass3(){
return grass3;
}
public Image getImageGrass3b(){
return grass3b;
}
public Image getImageSand1(){
return sand1;
}
//added
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for(int y = 0; y < 14; y++){
for(int x = 0; x < 14; x++){
/**draw the finish line**/
if(getMap(x, y).equals("f")){
g2.drawImage(getFinish(), x*32,y*32, null);
}
/**-----------------------)*/
if(getMap(x, y).equals("g")){
//the size of the image is X 20 by Y20
g2.drawImage(getTiles(), x*32, y*32, null);
}
if(getMap(x, y).equals("w")){
//the size of the image is X 20 by Y20
g2.drawImage(getWall(), x*32, y*32, null);
}
if(getMap(x, y).equals("d")){
//the size of the image is X 20 by Y20
g2.drawImage(getImageGrass3(), x*32, y*32, null);
}
if(getMap(x, y).equals("b")){
//the size of the image is X 20 by Y20
g2.drawImage(getImageGrass3b(), x*32, y*32, null);
}
if(getMap(x, y).equals("s")){
//the size of the image is X 20 by Y20
g2.drawImage(getImageSand1(), x*32, y*32, null);
}
}
}
}
}