在英雄方向上改变图像,简单2D游戏

时间:2016-03-19 13:57:32

标签: java

大家好我每次在java游戏中遇到一些麻烦,因为我是java的新手,所以非常简单。而且这个游戏工作得很好,就像我能做到的那样好。但我仍然坚持如何实时更改图像。我想弄清楚如何让我的怪物面对我“英雄frylark”当他们追我时。我在我的怪物类中制作了两个简单的方法。 我如何应用这些方法来改变图像来自 image = getImage(“/ Game / images / police -right.png“); image = getImage(”/ Game / images / police-left.png“); 。 哦,在我的项目库中是golden_0_2_3.jar,其中包含一些游戏引擎的东西。

请。 非常感谢edwin。

import com.golden.gamedev.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.awt.event.KeyEvent;

public class MyGame extends Game {
// set the values
public Random random;
private Hero frylark;
private Monster[] monsters = new Monster[3];
private Token coin;
private GameBackGround grass;
private BufferedImage image;

public void initResources() {
    // set a new random number 
    random = new Random();
    // get the background image.
    image = getImage("/Game/images/background.png");
    // set the name "grass" to background and given the image from the image set above.
    grass = new GameBackGround("grass", image);
    // get the monsters image.
    image = getImage("/Game/images/police.png");
    // give the monsters their names "" and set them their image from the image set above.
    monsters[0] = new Monster("Monster", image);
    monsters[1] = new Monster("Monster2", image);
    monsters[2] = new Monster("Monster3", image);
    // get the tokens image.
    image = getImage("/Game/images/donut.png");
    // set the name "coin" for the token, then its x and y position, and set the image from the image set above.
    coin = new Token("coin", 400, 300, image);
    // get the heros image.
    image = getImage("/Game/images/snake.png");
    // set the name "frylark" for the hero, then his score "0" and lives "5". 
    frylark = new Hero("Frylark", 0, 5);

    //set the monsters random x and y positions.
    monsters[0].setX(random.nextInt(750));
    monsters[0].setY(random.nextInt(550));
    monsters[1].setX(random.nextInt(750));
    monsters[1].setY(random.nextInt(550));
    monsters[2].setX(random.nextInt(750));
    monsters[2].setY(random.nextInt(550));
}
// update method
public void update(long elapsedTime) {
    // Pause the hero "frylark" on hold of the space bar.
    if (!keyDown(KeyEvent.VK_SPACE)){
    // if dead stop frylark moving on the 5 second game over sequence, being displays details and playing the game over sound. 
    if (Hero.dead(frylark)){
    if(keyDown(KeyEvent.VK_LEFT))
    {
         // Move left
         frylark.moveLeft();
    }
    if (keyDown(KeyEvent.VK_RIGHT))
    {
         // Move right
         frylark.moveRight();
    }
    if (keyDown(KeyEvent.VK_UP))
    {
         // Move up on press of up key
         frylark.moveUp();
    }
    if (keyDown(KeyEvent.VK_DOWN))
    {
         // Move down on press of down key
         frylark.moveDown();
    }
    }
    if (keyDown(KeyEvent.VK_ESCAPE))
    {
         // Exit game  on press of esc key.
        System.exit(0);
    }
    }
    if (!keyDown(KeyEvent.VK_SPACE))
    {
        // Pause the monsters on hold of the space bar
        monsters[0].chase(frylark);
        monsters[1].chase(frylark);
        monsters[2].chase(frylark);
    }
    // if monster 0 has eaten frylark move to a random position and lose a life, plus play the lose life sound.
    if (monsters[0].eaten(frylark)) {
        monsters[0].setX(random.nextInt(750));
        monsters[0].setY(random.nextInt(550));
        frylark.loseLife();
        playSound("/Game/sounds/lost_a_life.wav");
    }
    // if monster 1 has eaten frylark move to a random position and lose a life, plus play the lose life sound.
    if (monsters[1].eaten(frylark)) {
        monsters[1].setX(random.nextInt(750));
        monsters[1].setY(random.nextInt(550));
        frylark.loseLife();
        playSound("/Game/sounds/lost_a_life.wav");
    }
    // if monster 2 has eaten frylark move to a random position and lose a life, plus play the lose life sound.
    if (monsters[2].eaten(frylark)) {
        monsters[2].setX(random.nextInt(750));
        monsters[2].setY(random.nextInt(550));
        frylark.loseLife();
        playSound("/Game/sounds/lost_a_life.wav");
    }
    // if coin is collected increase score and move to a random position, and play the coin collect sound.
    if (coin.collected(frylark)) {
        coin.setX (random.nextInt(750));
        coin.setY (random.nextInt(550));
        frylark.increaseScore();
        playSound("/Game/sounds/coin.wav");
    }


}

public void render(Graphics2D g) {
     // draw all the monsters, hero, and coin and background.
     g.drawImage(grass.getImage(),grass.getX(),grass.getY(),null);
     g.drawImage(monsters[0].getImage(), monsters[0].GetX(), monsters[0].GetY(), null);
     g.drawImage(monsters[1].getImage(), monsters[1].GetX(), monsters[1].GetY(), null);
     g.drawImage(monsters[2].getImage(), monsters[2].GetX(), monsters[2].GetY(), null);
     g.drawImage(image,frylark.getX(),frylark.getY(),null); 
     g.drawImage(coin.getImage(),coin.getX(),coin.getY(),null); 
     // if monster 0 overlaps another monster mover him back
     if (monsters[0].overlap(monsters)){
         monsters[0].x -=20;
         monsters[0].y -=70;
     }
    // if monster 1 overlaps another monster mover him back
     if (monsters[1].overlap(monsters)){
         monsters[1].x -=21;
         monsters[1].y -=70;
     }
    // if monster 2 overlaps another monster mover him back
     if (monsters[2].overlap(monsters)){
         monsters[2].x -=22;
         monsters[2].y -=70;
     }
     // draw the lives bar, and set the font colour and size
     g.setColor(Color.RED);
     g.setFont(new Font("default", Font.BOLD, 18));
     for (int i = 0; i < frylark.getLives(); i++) {
         g.fillRect( (i + 1) * 15, 10, 10, 10);
     }
     // draw the score
     g.setColor(Color.GREEN);
     g.drawString("Score: " + frylark.getScore(), 10, 50);
     // draw the level
     g.setColor(Color.YELLOW);
     g.drawString("level: " + frylark.getScoreNum(), 10, 80);
     // game over sequence, changes the font to size 40 and displays game over, as well as the payers score and level reached plus the game over sound.
     if (frylark.getLives() ==0){
         g.setColor(Color.RED);
         g.setFont(new Font("override", Font.BOLD, 40));
         g.drawString("Game over !", 280, 290);
         playSound("/Game/sounds/game_over.wav");
         g.drawString("You reached Level " + frylark.getScoreNum() + "  Your Score: " + frylark.getScore(), 60, 330);
     }

}
// main method which after all classes have been read and checked, "Game development environment OK! " will be printed to the console.
// then a new game is created and given dimensions and launched.
public static void main(String args[]) {
System.out.println("Game development environment OK! ");
GameLoader gameLoader = new GameLoader();
MyGame myGame = new MyGame();
gameLoader.setup(myGame,new Dimension(800,600),false);
gameLoader.start();
}


}

和我的怪物课

import java.util.Random;
import java.awt.image.BufferedImage;

    public class Monster {
    private String name;
    int x;
    int y;
    private BufferedImage image;
    Random rand;

    public Monster (String nameIn, BufferedImage imageIn)
    {   
        name = nameIn;
        x = 0;
        y = 0;
        image = imageIn;

    }


    public void chase(Hero hero) {
        if (hero.getX() < x) { // if hero is to the left
            x--;
        }

        if (hero.getX() > x) { // if hero is to the right
            x++ ;
        }

        if (hero.getY() < y) { // if hero is to the above
            y--;
        }

        if (hero.getY() > y) { // if hero is to the below
            y++;
        }

    }

     public boolean overlap(Monster monsters[]){
         if (monsters[0].x == monsters[1].x && monsters[0].y == monsters[1].y || monsters[0].x == monsters[2].x && monsters[0].y == monsters[2].y ||
                 monsters[1].x == monsters[0].x && monsters[1].y == monsters[0].y || monsters[1].x == monsters[2].x && monsters[1].y == monsters[2].y ||
                    monsters[2].x == monsters[0].x && monsters[2].y == monsters[0].y || monsters[2].x == monsters[1].x && monsters[2].y == monsters[1].y) {
             return true;
         }
         else{
        return false;
         }
     }


    public boolean eaten(Hero hero) {
        if (hero.getX() == x && hero.getY() == y) {
            return true;
        }
        else {
            return false;
        }
    }

    public BufferedImage getImage() {
        return image;
    }


    public int GetX(){
        return x;
    }

    public int GetY(){
        return y;
    }

    public String getName()
    {
        return this.name;
    }

    public void setX(int xIn) {
        x = xIn;
    }

    public void setY(int yIn) {
        y = yIn;
    }

    public boolean left(Hero hero) {
        if (hero.getX() < x) {
            return true;
        }
        else {
            return false;
        }
    }
    public boolean right(Hero hero) {
        if (hero.getX() > x) {
            return true;
        }
        else {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我会修改你的Monster构造函数以接受这两个图像。然后修改Monster.getImage()以调用left()并根据结果返回正确的一个。你可能也不需要调用right(),因为如果怪物没有朝左,那么你知道它需要面对正确。除非你想要更复杂,并且还要添加面向前方或后方的视图。