简体   繁体   English

(Java 1.8, Eclipse) SNAKEGAME - 纹理问题

[英](Java 1.8, Eclipse) SNAKEGAME - Texture-Problems

i made a SnakeGame and its working in its basics, but i started to texture my snake with images.我制作了一个 SnakeGame 及其基础工作,但我开始用图像纹理我的蛇。 i wanted the textures to change the direction whenever the snake changes directions aswell.当蛇改变方向时,我希望纹理也改变方向。 and it is working, the problem i have is that every time the snake changes direction all of the snake body parts change direction at the same time which leads to texture bugs, how can i change the texture for the next bodyPart per one Timer tick?它正在工作,我遇到的问题是每次蛇改变方向时,所有蛇的身体部位都会同时改变方向,这会导致纹理错误,我如何每一个 Timer 滴答来改变下一个 bodyPart 的纹理? And also how can i edit the Texture for the last bodyPart only for a tail还有我怎么能只为尾巴编辑最后一个bodyPart的纹理

the code below is only the code from the important class "gamepanel" the texture code is in the method draw(Graphics g)下面的代码只是重要类“gamepanel”中的代码纹理代码在方法draw(Graphics g)中

thanks for the help i could not find any solutions to this.感谢您的帮助,我找不到任何解决方案。

    package game;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import java.util.Random;


public class gamepanel extends JPanel implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 42L;
    static final int SCREEN_WIDTH = 600;
    static final int SCREEN_HEIGHT = 600;
    static final int UNIT_SIZE = 25;
    static final int GAME_UNITS = (SCREEN_WIDTH*SCREEN_HEIGHT)/UNIT_SIZE;
    int DELAY = 100;
    final int x[] = new int[GAME_UNITS];
    final int y[] = new int[GAME_UNITS];
    int bodyParts = 6;
    String str = String.valueOf(bodyParts);
    int applesEaten;
    int appleX;
    int appleY;
    char direction = 'R';
    boolean running = false;
    Timer timer;
    Random random;
    
    Image apple = new ImageIcon(this.getClass().getResource("/images/redapple.png")).getImage();
    
    
    Image headup = new ImageIcon(this.getClass().getResource("/images/headup.png")).getImage();
    Image headr = new ImageIcon(this.getClass().getResource("/images/headr.png")).getImage();
    Image headl = new ImageIcon(this.getClass().getResource("/images/headl.png")).getImage();
    Image headd = new ImageIcon(this.getClass().getResource("/images/headd.png")).getImage();
    
    
    Image bodyup = new ImageIcon(this.getClass().getResource("/images/bodyup.png")).getImage();
    Image bodyd = new ImageIcon(this.getClass().getResource("/images/bodyd.png")).getImage();
    Image bodyr = new ImageIcon(this.getClass().getResource("/images/bodyr.png")).getImage();
    Image bodyl = new ImageIcon(this.getClass().getResource("/images/bodyl.png")).getImage();
    
    
    
    gamepanel(){
        random = new Random();
        this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
        this.setBackground(new Color(33, 33, 33));
        this.setFocusable(true);
        this.addKeyListener(new MyKeyAdapter());
        startGame();
        
    }
    
    public void startGame() {
        newApple();
        running = true;
        timer = new Timer(DELAY,this);
        timer.start();
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }
    public void draw(Graphics g) {
        if(running) {
                
            /* for(int i=0;i<SCREEN_HEIGHT/UNIT_SIZE;i++) {
                    g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT);
                    g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE);
                } */
                
            
                //g.setColor(new Color(73, 235, 116));
                //g.fillOval(appleX,appleY, UNIT_SIZE, UNIT_SIZE);
                g.drawImage(apple, appleX,appleY, UNIT_SIZE, UNIT_SIZE, null, null);
        
                for(int i = 0;i< bodyParts; i++) { 
                    //HEAD TEXTURE
                    if(i==0 && direction=='U') {
                        g.drawImage(headup,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                    } else if(i==0 && direction=='R') {
                        g.drawImage(headr,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                        
                    } else if(i==0 && direction=='L') {
                        g.drawImage(headl,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                        
                    } else if(i==0 && direction=='D') {
                        g.drawImage(headd,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                        
                    //BODY TEXTURE
                        
                    }  else if(i!=0 && direction=='U')  {
                            g.drawImage(bodyup,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                        
                            } else if(i!=0 && direction=='R')   {
                                g.drawImage(bodyr,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                                } else if(i!=0 && direction=='L')   {
                                    g.drawImage(bodyl,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                                }   else if(i!=0 && direction=='D') {
                                    g.drawImage(bodyd,x[i],y[i],UNIT_SIZE, UNIT_SIZE,null);
                                }
                }
                g.setColor(Color.red);
                g.setFont(new Font("Arial",Font.BOLD,40));
                FontMetrics metrics = getFontMetrics(g.getFont());
                g.drawString("Score: "+applesEaten, (SCREEN_WIDTH - metrics.stringWidth("Score: "+applesEaten))/2, g.getFont().getSize());
        
        }else {
            gameOver(g);
        } 
    }
    
    public void newApple() {
        appleX = random.nextInt((int)(SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
        appleY = random.nextInt((int)(SCREEN_HEIGHT/UNIT_SIZE))*UNIT_SIZE;
        
    }
    
    public void move() {
        for(int i = bodyParts;i>0;i--) {
            x[i] = x[i-1];
            y[i] = y[i-1];
        }
        switch(direction) {
        case 'U' :
            y[0] = y[0] - UNIT_SIZE;
            
            break;
        case 'D' :
            y[0] = y[0] + UNIT_SIZE;
            break;
        case 'L' :
            x[0] = x[0] - UNIT_SIZE;
            break;
        case 'R' :
            x[0] = x[0] + UNIT_SIZE;
            break;
        }
    }
    public void checkApple() {
        int i = bodyParts;
        if((x[i] == appleX) && (y[i] == appleY)) {
            newApple();
        } else if((x[0] == appleX) && (y[0] == appleY)) {
            bodyParts++;
            applesEaten++;
            DELAY--;
            newApple();
        }
        
    }
    public void checkCollisions() {
        //checks if head collides with body
        for(int i = bodyParts; i>0;i--) {
            if ((x[0]==x[i]) && (y[0]==y[i])) {
                running = false;
            }
        }
        // checks if head touches left border
        if(x[0] < 0) {
            running = false;
        }
        // check right border collide
        if(x[0] > SCREEN_WIDTH) {
            running = false;
        }
        // check top border
        if(y[0] < 0) {
            running = false;
        }
        //checkk bottom border
        if(y[0] > SCREEN_HEIGHT) {
            running = false;
        }
        if(!running) {
            timer.stop();
        }
    }
    public void gameOver(Graphics g) {
        //SCORE
        g.setColor(Color.red);
        g.setFont(new Font("Arial",Font.BOLD,40));
        FontMetrics metrics1 = getFontMetrics(g.getFont());
        g.drawString("Your Score: "+applesEaten, (SCREEN_WIDTH - metrics1.stringWidth("Your Score: "+applesEaten))/2, g.getFont().getSize());

        //GAme Over Text
        g.setColor(Color.red);
        g.setFont(new Font("Arial",Font.BOLD,75));
        FontMetrics metrics2 = getFontMetrics(g.getFont());
        g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over"))/2, SCREEN_HEIGHT/2);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(running) {
            move();
            checkApple();
            checkCollisions();
        }
        repaint();
        
    }
    
    public class MyKeyAdapter extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                if(direction != 'R') {
                    direction = 'L';
                }
                break;
            case KeyEvent.VK_RIGHT:
                if(direction != 'L') {
                    direction = 'R';
                }
                break;
            case KeyEvent.VK_UP:
                if(direction != 'D') {
                    direction = 'U';
                }
                break;
            case KeyEvent.VK_DOWN:
                if(direction != 'U') {
                    direction = 'D';
                }
                break;
            }
            
        }
    }

}

maybe try updating the textures for each body part starting from the head 1 per game frame at a time?也许尝试一次从每个游戏帧的头部 1 开始更新每个身体部位的纹理? like 0 = head piece and will be updated first, 1 is the next body part in line so will be updated in the next frame when the snake has moved 1 unit and therefore lining up the timings比如 0 = 头部,将首先更新,1 是下一个身体部位,因此当蛇移动 1 个单位并因此排列时间时,将在下一帧更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM