簡體   English   中英

角色從屏幕邊緣掉落

[英]Character falls off the edge of the screen

因此,我正在制作一個游戲,用戶控制了一個矩形,該矩形沿着底部的框從左到右移動,但是它掉到了邊緣,而且我不知道如何阻止這種情況,我們將不勝感激。 我已經嘗試了很多方法來阻止這種情況,但是沒有任何效果,我真的需要一些幫助才能將矩形實際顯示在屏幕上。

public Rectangle character;
public Rectangle bottomBox;

public int charW = 100;
public int charH = 15;

public boolean right = false;
public boolean left = false;
public boolean up = false;
public boolean down = false;
public boolean mouseActive = false;

public Point mouse;

public Keying(Display f, Images i){
    character = new Rectangle(180, 180, charW, charH); 
    bottomBox = new Rectangle (0, 350, 9000, 30);

    f.addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_D){
                mouseActive = false;
                right = true;
                character.x += 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_A){
                mouseActive = false;
                left = true;
                character.x -= 1;
            }
            if(e.getKeyCode() == KeyEvent.VK_M){
                mouseActive = false;
            }
        }

        public void keyReleased(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_D){
                right = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_A){
                left = false;
            }
        }
    });

    f.addMouseMotionListener(new MouseMotionAdapter() {

        public void mouseMoved(MouseEvent e){
            int mouseX = e.getX();
            int mouseY = e.getY();
            mouse = new Point(mouseX, mouseY); 
            if(mouseActive && character.x != Main.w - charW){
                character.x = mouse.x;
                character.y = mouse.y;
            }
            repaint();
        }           
    });

    f.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
        mouse = new Point (e.getX(), e.getY());

        if(e.getButton() == MouseEvent.NOBUTTON){
            character.x = mouse.x;
            character.y = mouse.y;
        }
        }
    });
}    

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Point pt1 = new Point(character.x, character.y + character.height);
    if(!bottomBox.contains(pt1) && !mouseActive){
        character.y++;         
    }

    this.setBackground(Color.YELLOW);
    {g.setColor(Color.BLACK);
    g.fillRect(character.x, character.y, character.width, character.height);}
    {g.setColor(Color.darkGray);
    g.fillRect(bottomBox.x, bottomBox.y, bottomBox.width, bottomBox.height);

    if(right && character.x != Main.w - charW){
        character.x += 1;
    }
    if(left && character.x != 0){
        character.x -= 1;
    }
    repaint();   
}

}}

我確實需要一些幫助,以將矩形實際保留在屏幕上。

好了,您需要做一些基本的邏輯檢查,以查看矩形是否可以在面板的邊界內繪制。

現在,您要確定矩形的位置和寬度,因此只需檢查總和是否不大於面板的寬度即可。

例如,您可以檢查“使用鍵盤運動”中的邏輯。 所有代碼示例都包含move(...)方法,該方法進行邊界檢查。

另外,從示例中注意,您的繪畫代碼不應用於計算字符的位置。 繪制方法應僅基於角色的當前屬性來繪制角色。 只有用戶操作才能更改角色的屬性,因為您無法控制何時調用繪畫方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM