-1

对于一个学校计算机项目,我决定用 Java 制作一个吃豆人风格的游戏(和另外两个人)。我们都没有太多经验,所以到目前为止,我们基本上一直在自学。我们已经取得了相当大的进步,但在这一点上我们似乎已经停滞不前。

当胖子碾过糖果时,我们怎样才能让糖果消失呢?我们已经尝试使用重绘,但我们似乎无法让事情正常工作。

(我还有其他需要帮助的事情,但它们的问题不同)

谢谢您的帮助!

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

迷宫类:

package Fatman;

import javax.swing.*;

public class Maze {
    public static void main(String[] args){
        new Maze();
    }

    public Maze(){
        JFrame f = new JFrame();
        f.setTitle("Fatman!");
        f.add(new Board());
        f.setSize(816, 838);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

地图类

    package Fatman;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class Map {

    private Scanner m;
    private String Map[] = new String[25]; 

    private Image spaceregcandy,
                  srcb,
                  safehouse,
                  spacebigcandy,
                  blackspace,
                  space,
                  portal1,
                  portal2,
                  wall;

    public Map(){

        ImageIcon img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\spaceregcandy.png");
        spaceregcandy = img.getImage();
        //image icon has already been initiated, so it doesn't have to be written again
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\spaceregcandyblue.png");
        srcb = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\safehouse.png");
        safehouse = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\wall232x.png");
        wall = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\spacebigcandy.png");
        spacebigcandy = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\blackspace.png");
        blackspace = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\space.png");
        space = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\portal1.png");
        portal1 = img.getImage();
        img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\portal2.png");
        portal2 = img.getImage();

        openFile();
        readFile();
        closeFile();
        }

    public Image getSpaceregcandy(){
        return spaceregcandy;
    }
    public Image getSrcb(){
        return srcb;
    }
    public Image getSafehouse(){
        return safehouse;
    }
    public Image getWall(){
        return wall;
    }
    public Image getSpacebigcandy(){
        return spacebigcandy;
    }
    public Image getBlackspace(){
        return blackspace;
    }
    public Image getSpace(){
        return space;
    }
    public Image getPortal1(){
        return portal1;
    }
    public Image getPortal2(){
        return portal2;
    }

    public String getMap(int x, int y){
        String index = Map[y].substring(x, x + 1);
        return index;
        //in y position, if y = 2, goes to second row (substring gets x position)
    }

    public void openFile(){

        try{
        m = new Scanner(new File("C:\\Users\\Martin\\Desktop\\Fatman Project\\map3.txt"));
        }catch(Exception e){
            System.out.println("error loading map");
        }
    }

    public void readFile(){
        while(m.hasNext()){
            for(int i = 0; i < 25; i++){
                Map[i] = m.next();
            }
        }

    }

    public void closeFile(){
        m.close();
    }
}

板级

package Fatman;

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;

public class Board extends JPanel implements ActionListener{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Timer timer;

    private Image player;

    private Map m;
    private Player p;

    public Board(){

        m = new Map();
        p = new Player();
        addKeyListener(new Al());
        setFocusable(true);
        timer = new Timer(1, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e){
        repaint();

    }

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

        for(int y = 0; y < 25; y++){
            for(int x = 0; x <25; x++){
                if(m.getMap(x, y).equals("o")){
                    g.drawImage(m.getSpaceregcandy(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("O")){
                    g.drawImage(m.getSrcb(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("x")){
                    g.drawImage(m.getWall(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("H")){
                    g.drawImage(m.getSafehouse(), x *32, y *32, null);
                }   
                if(m.getMap(x, y).equals("C")){
                    g.drawImage(m.getSpacebigcandy(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("b")){
                    g.drawImage(m.getBlackspace(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("s")){
                    g.drawImage(m.getSpace(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("p")){
                    g.drawImage(m.getPortal1(), x *32, y *32, null);
                }
                if(m.getMap(x, y).equals("P")){
                    g.drawImage(m.getPortal2(), x *32, y *32, null);
                }
        }


    }


        g.drawImage(p.getPlayer(), p.getTileX() * 32, p.getTileY() * 32, null);

}

    public class Al extends KeyAdapter{

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

            if(keycode == KeyEvent.VK_UP){
                if(!m.getMap(p.getTileX(), p.getTileY() -1).equals("x")){
                    if(!m.getMap(p.getTileX(), p.getTileY() -1).equals("b")){                       
                        }
                        p.move(0, -1);                  

                        System.out.println(m.getMap(p.getTileX(), p.getTileY()));

            }
                }

            if(keycode == KeyEvent.VK_DOWN){
                if(!m.getMap(p.getTileX(), p.getTileY() +1).equals("x")){
                    if(!m.getMap(p.getTileX(), p.getTileY() +1).equals("b")){
                        p.move(0, 1);
                        System.out.println(m.getMap(p.getTileX(), p.getTileY()));
            }
                }
            }
            if(keycode == KeyEvent.VK_LEFT){
                if(!m.getMap(p.getTileX() - 1, p.getTileY()).equals("x")){
                    if(!m.getMap(p.getTileX() - 1, p.getTileY()).equals("b")){
                        p.move(-1, 0);
                        System.out.println(m.getMap(p.getTileX(), p.getTileY()));
            }
                }
            }
            if(keycode == KeyEvent.VK_RIGHT){
                if(!m.getMap(p.getTileX() + 1, p.getTileY()).equals("x")){
                    if(!m.getMap(p.getTileX() + 1, p.getTileY()).equals("b")){
                        p.move(1, 0);
                        System.out.println(m.getMap(p.getTileX(), p.getTileY()));
            }
            }
            }
            }


        public void keyReleased(KeyEvent e){

        }

        public void keyTyped(KeyEvent e){

}
}
}

玩家等级

package Fatman;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Player {

    private int tileX, tileY;
    private int dx, dy;

    private Image player; 

    public Player(){

        ImageIcon img = new ImageIcon("C:\\Users\\Martin\\Desktop\\Fatman Project\\FATMANsimplified32xbrown.png");
        player = img.getImage();

        tileX = 12;
        tileY = 18;
    }

    public Image getPlayer(){
        return player;
    }
    public int getTileX(){
        return tileX;
    }
    public int getTileY(){
        return tileY;
    }

    public void move(int dx, int dy){

        tileX += dx;
        tileY += dy;
    }

}

有任何想法吗?

4

2 回答 2

0

没有什么Player.move()是吃点。您必须更新变量的值Map.Map[]以跟踪它应该表示的内容。否则,此代码只是空词/符号,它们一次只能执行程序员的一条指令,但不使用编程语言的力量让计算机根据其满足的条件进行工作。

于 2013-11-04T23:13:25.547 回答
0

好吧,我认为你不可能成为 3 人组的三分之二,除非你吃了你的一个成员,但是这么说......你的问题更适合https://gamedev.stackexchange.com/作为在我看来,这更像是一个游戏开发问题。

然而,你可以做的,是将糖果位置设置在屏幕外(例如:candy.x = -100;)

尽管如此,你的游戏循环在哪里?

公共无效开始游戏(){

} 公共无效停止游戏(){

} public void update(){ //强制图形和用户输入更新游戏状态 }

也考虑去 www.java-gaming.org 他们有很棒的教程可以帮助你。

于 2013-11-04T15:39:46.310 回答