Interface Grafica Snake

2 respostas
R

Boa tarde, surgiu-me um problema numa ultima fase de implementação grafica do Snake.
Criei uma janela de menus com o botão Novo Jogo, o problema é que quando clico, cria a janela e o jogo corre normalmente, o problema é que não há imagem do tabuleiro de jogo, não consigo ver nenhum objecto que é criado, até a cobra chocar na parede e me surgir a indicação de game over.
alguem me pode ajudar com isto?

obrigado

2 Respostas

ViniGodoy

Como você está fazendo para desenhar o tabuleiro? É um jogo desktop ou J2ME?

Você seguiu as dicas do Ponto V?
http://www.pontov.com.br/site/index.php/java/48-java2d

R
Classe Menu
public class Jogo extends JFrame implements ActionListener
{
    String nomeJogador;
    int largura;
    int altura;
    Ranking ranking;
    JButton novoJogo;
    JButton abrirJogo;
    JButton configuracoes;
    
    public Jogo()
    {
        super("Jogo da Snake");
        setTitle("Snake em Java");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(200, 200);
        setSize(200, 200);
        setLayout(new FlowLayout());
        
        ranking = new Ranking("default", 0);
        
        novoJogo = new JButton("Novo Jogo");
        abrirJogo = new JButton("Abrir Jogo");
        configuracoes = new JButton("Configurações");
        novoJogo.addActionListener(this);
        abrirJogo.addActionListener(this);
        configuracoes.addActionListener(this);
        
        add("Center", novoJogo);
        add("Center", abrirJogo);
        add("Center", configuracoes);
        
        mostrar(false);
        
        //new Snake(10, 10, "Pedro", this);
        
    }
    
    
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == novoJogo)
        {
           //criaNovoJogo();
           mostrar(true);
        }
        if(e.getSource() == abrirJogo)
        {

        }
        if(e.getSource() == configuracoes)
        {
            janelaC();
        }
    }
    
    public void janelaC()
    {
        new JanelaConfiguracoes(this);
    }
    
    public void criaNovoJogo()
    {
        setVisible(false);
        new Snake(largura, altura, nomeJogador, this);
    }
    
    public void mostrar(boolean mostraTerreno)
    {
        if(mostraTerreno)
        {
            this.setVisible(false);
            new Snake(10, 10, "Pedro", this);
        }
        else
        {
            this.setVisible(true);
        }
    }
    
    public void setNomeJogador(String nome)
    {
        nomeJogador = nome;
    }
    public void setLargura(int larg)
    {
        largura = larg;
    }
    public void setAltura(int alt)
    {
        altura = alt;
    }
    
    public void adicionaPontuacao(String nomeJogador, int pontos)
    {
        ranking.inserirPontuacao(nomeJogador, pontos);
    }
    
}
Classe Interface
public class Interface extends JFrame implements KeyListener, java.io.Serializable
{
    private static final Color EMPTY_COLOR = Color.white;
    private static final Color COR_COBRA = Color.blue;
    private static final Color COR_MANGUSTO = Color.red;
    private static final Color COR_ESCARAVELHO = Color.gray;
    private static final Color COR_OVO = Color.green;
    private final int GRID_VIEW_SCALING_FACTOR = 20;
    private FieldView fieldView;
    private Snake associatedSnake;
    private int height;
    private int width;
    private boolean paused = false;
    /**
     * Create a view of the given width and height.
     */
    public Interface(Snake associatedGame)
    {
        setTitle("Snake em Java");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(100, 50);
        
        fieldView = new FieldView(associatedGame.getTerreno().getAltura(), associatedGame.getTerreno().getLargura());
        this.associatedSnake = associatedGame;
        this.height = associatedGame.getTerreno().getAltura();
        this.width = associatedGame.getTerreno().getLargura();
        
        addKeyListener(this);

        Container contents = getContentPane();
        contents.add(fieldView, BorderLayout.CENTER);
        pack();
    }
    
    public void desenhaTerreno()
    {
        Terreno terreno = associatedSnake.getTerreno();
        Posicao[][] posicoes = terreno.getPosicoes();
        
        limpa();
        fieldView.preparePaint();
        for(int row = 0; row < height; row++) {
            for(int col = 0; col < width; col++) {
                if(posicoes[row][col].getEnte() != null)
                {
                    if(posicoes[row][col].getEnte().getTipoEnte().equals("COBRA"))
                    {
                        fieldView.drawMark(col, row, COR_COBRA);
                    }
                    else
                    {
                        if(posicoes[row][col].getEnte().getTipoEnte().equals("MANGUSTO"))
                        {
                            fieldView.drawMark(col, row, COR_MANGUSTO);
                        }
                        else
                        {
                            if(posicoes[row][col].getEnte().getTipoEnte().equals("ESCARAVELHO"))
                            {
                                fieldView.drawMark(col, row, COR_ESCARAVELHO);
                            }
                            else
                            {
                                if(posicoes[row][col].getEnte().getTipoEnte().substring(0, 3).equals("OVO"))
                                {
                                    fieldView.drawMark(col, row, COR_OVO);
                                }
                                else
                                {
                                    
                                }
                            }
                        }
                    }
                }
            }
        }
        fieldView.drawString("Pontos: " + associatedSnake.getPontos(), GRID_VIEW_SCALING_FACTOR, GRID_VIEW_SCALING_FACTOR);
        if(paused)
        {
            fieldView.drawString("GAME PAUSED", GRID_VIEW_SCALING_FACTOR, GRID_VIEW_SCALING_FACTOR * 2);
        }
        fieldView.repaint();
    }
    
    public void limpa()
    {
        fieldView.preparePaint();
        for(int row = 0; row < height; row++) {
            for(int col = 0; col < width; col++) {
                    fieldView.drawMark(col, row, EMPTY_COLOR);
            }
        }
        fieldView.repaint();
    }
    
    public void ver()
    {
        setVisible(true);
    }
    
    private class FieldView extends JPanel implements ImageObserver
    {
        //private final int GRID_VIEW_SCALING_FACTOR = 20;

        private int gridWidth, gridHeight;
        private int xScale, yScale;
        Dimension size;
        private Graphics g;
        private Image fieldImage;

        /**
         * Create a new FieldView component.
         */
        public FieldView(int height, int width)
        {
            gridHeight = height;
            gridWidth = width;
            size = new Dimension(0, 0);
        }

        /**
         * Tell the GUI manager how big we would like to be.
         */
        public Dimension getPreferredSize()
        {
            return new Dimension(gridWidth * GRID_VIEW_SCALING_FACTOR,
                                 gridHeight * GRID_VIEW_SCALING_FACTOR);
        }
        
        /**
         * Prepare for a new round of painting. Since the component
         * may be resized, compute the scaling factor again.
         */
        public void preparePaint()
        {
            if(! size.equals(getSize())) {  // if the size has changed...
                size = getSize();
                fieldImage = fieldView.createImage(size.width, size.height);
                g = fieldImage.getGraphics();

                xScale = size.width / gridWidth;
                if(xScale < 1) {
                    xScale = GRID_VIEW_SCALING_FACTOR;
                }
                yScale = size.height / gridHeight;
                if(yScale < 1) {
                    yScale = GRID_VIEW_SCALING_FACTOR;
                }
            }
        }
        
        /**
         * Paint on grid location on this field in a given color.
         */
        public void drawMark(int x, int y, Color color)
        {
            g.setColor(color);
            g.fillRect(x * xScale, y * yScale, xScale-1, yScale-1);
        }
        
        public void drawImage(Image image, int x, int y)
        {
            g.setColor(Color.black);
            g.drawImage(image, x * xScale, y * yScale, null);     
        }
        
        public void drawString(String string, int x, int y)
        {
            g.setColor(Color.black);
            g.drawString(string, x, y);
        }

        /**
         * The field view component needs to be redisplayed. Copy the
         * internal image to screen.
         */
        public void paintComponent(Graphics g)
        {
            if(fieldImage != null) {
                g.drawImage(fieldImage, 0, 0, null);
            }
        }
    }
    
    public void keyPressed(KeyEvent e)
    {  
        if(e.getKeyText(e.getKeyCode()).equals("Up"))
        {
            associatedSnake.defineDireccao("up");
        }
        if(e.getKeyText(e.getKeyCode()).equals("Down"))
        {
            associatedSnake.defineDireccao("down");
        }
        if(e.getKeyText(e.getKeyCode()).equals("Left"))
        {
            associatedSnake.defineDireccao("left");
        }
        if(e.getKeyText(e.getKeyCode()).equals("Right"))
        {
            associatedSnake.defineDireccao("right");
        }
        if(e.getKeyText(e.getKeyCode()).equals("P"))
        {
            if(associatedSnake.getOK())
            {
                associatedSnake.setNotOK();
                paused = true;
            }
            else
            {
                associatedSnake.setOK();
                paused = false;
            }
        }
        if(e.getKeyText(e.getKeyCode()).equals("S"))
        {
            if(associatedSnake.getOK())
            {
                associatedSnake.setNotOK();
                paused = true;
            }
            associatedSnake.guardaPontuacao(); 
            associatedSnake.saveTobinary();
        }
        if(e.getKeyText(e.getKeyCode()).equals("O"))
        {
            if(associatedSnake.getOK())
            {
                associatedSnake.setNotOK();
                paused = true;
            }
            associatedSnake.loadFromBinary();
        }
    }
    
    public void keyReleased(KeyEvent e) {}  
  
    public void keyTyped(KeyEvent e) {}
}
Criado 13 de junho de 2010
Ultima resposta 13 de jun. de 2010
Respostas 2
Participantes 2