如何确保玩家不会穿墙 [英] How to make sure the player does not walk through the wall

查看:23
本文介绍了如何确保玩家不会穿墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个真正的 2D 游戏(吃豆人).我认为游戏看起来不错,但我有一个大问题 - 碰撞.对象仍在穿墙.我被它困住了,所以我决定寻求经验丰富的真正程序员的帮助.(当然我做了一些研究,但我不想做COPY和Paste之类的事情,因为我不明白).正如我所说,游戏快完成了,我需要做的就是阻止吃豆子通过.例如,我将大的白色矩形绘制为平台.我希望有人能够帮助我.在这个项目中,我学到了很多东西,碰撞是我理解的东西,但不知道如何正确编程.我想我已经快要弄清楚了,但缺少一些东西.

I am programming my first actually 2D game(Pac-Man). I think game looks good,but I have one big problem - Collision. Object still going through the wall.I'm stuck with it so I decided to ask for help real programmers with great experience. (Of course I did some research, but I don't want to do things like COPY and Paste, because I didn't understand). As I said, game is almost done, just all I need to do is keep pacman from going through. Like example I draw large white rectangle as platform. I hope somebody is able to help me. Throughout this project I've learned much and collision is something which I understand, but don't know how correctly program it. I think I am close to figure it out, but something is missing.

PS:我在 WindowBuilder 中创建了窗口,所以编译可能是一个问题:(

PS: I've created window in WindowBuilder, so compiling might be a problem :(

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class Main extends JFrame implements ActionListener{
    JPanel contentPane;
    Rectangle packman ;
    Rectangle platform;
    Rectangle secondPlat;

    private int count = 0;
    private int x = 170, y = 50;
    private int xVel = 1, yVel = 1;
    Timer timer;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Main frame = new Main();
        frame.setVisible(true);
    }

    public Main() {
        // TODO Auto-generated constructor stub
        this.setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        getContentPane().setBackground(Color.gray);
        packman = new Rectangle(x,y,50,50);
        platform = new Rectangle(100,70,50,100);
        secondPlat = new Rectangle(220,50,50,100);
        timer = new Timer(0,this);
        timer.start();
        this.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                switch(e.getKeyCode()) {
                    case 37: //doleva
                    count = 1;  
                    repaint();
                    break;
                case 38: //nahorů       
                    count = 2;  
                    repaint();
                    break;
                case 39://doprava           
                    count = 3;          
                    repaint();
                    break;
                case 40://dolů      
                    count =4;   
                    repaint();
                    break;
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                System.out.println("Char" + e.getKeyCode());
                System.out.println("Hod" + e.getKeyCode());
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        g.drawRect(x,y,packman.width,packman.height);
        g.setColor(Color.blue);
        g.fillRect(x,y,packman.width,packman.height);

        g.drawRect(platform.x,platform.y,platform.width,platform.height);
        g.setColor(Color.blue);
            
        g.drawRect(secondPlat.x,secondPlat.y,secondPlat.width,secondPlat.height);
        g.setColor(Color.blue);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (count == 1) {
            x = x - xVel;
            repaint();
            zkontrolujKolizi();
        }
        
        if (count ==2) {
            y = y - yVel;
            repaint();
            zkontrolujKolizi();
        }
        if (count ==3) {
            x = x + xVel;
            repaint();
            zkontrolujKolizi();
        }
        if (count ==4) {
            y  = y+yVel; 
            repaint();
            zkontrolujKolizi();
        }
    }

    public void zkontrolujKolizi() {
        // TODO Auto-generated method stub
        if (packman.intersects(platform) || packman.intersects(secondPlat)) {
            System.out.println("Got ya!");
        }
    }
}

推荐答案

在你的代码中你更新了 x 和 y,但这不是在packman"中完成的.对象,始终位于其初始位置;所以当你检查与墙的交叉点时,packman 总是在 (170,50);我更改了动画方法以反映 packman 中的更改和绘制方法,以便您使用更新的 packman 坐标.

In your code you update x and y, but this is not done in the "packman" object, that always sits at its initial position; so when you check intersection with the wall the packman is always at (170,50); I changed both the animation method to reflect the changes in packman and the paint method, so that you use the updated packman coordinates.

动画:

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if (count == 1) {
        packman.x = packman.x - xVel;
        repaint();
        zkontrolujKolizi();
        
    
    }
    
    if (count ==2) {
        packman.y = packman.y - yVel;
        repaint();
        zkontrolujKolizi();
    }
    if (count ==3) {
        packman.x = packman.x + xVel;
        repaint();
        zkontrolujKolizi();
    }
    if (count ==4) {
        packman.y  = packman.y+yVel; 
        repaint();
        zkontrolujKolizi();
    }
    
    
}

油漆:

@Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        g.drawRect(packman.x,packman.y,packman.width,packman.height);
        g.setColor(Color.blue);
        g.fillRect(packman.x,packman.y,packman.width,packman.height);
        
        g.drawRect(platform.x,platform.y,platform.width,platform.height);
        g.setColor(Color.blue);
        
        g.drawRect(secondPlat.x,secondPlat.y,secondPlat.width,secondPlat.height);
        g.setColor(Color.blue);
        
    }

当然,代码中还有很多需要重构的地方,但这就是没有检测到冲突的原因.

Of course, there is much more to refactor in the code, but this is the reason why the collisions were not detected.

为了避免穿过墙壁,计算新位置,检查碰撞,如果为真,回滚位置变化:

To avoid moving through the walls, calculate new position, check collision, if true, rollback the position changes:

@Override
public void actionPerformed(ActionEvent arg0) {
    int rollbackX=packman.x;
    int rollbackY=packman.y;

    switch (count) {
    case 1:
        packman.x = packman.x - xVel;
        break;
    case 2:
        packman.y = packman.y - yVel;
        break;
    case 3:
        packman.x = packman.x + xVel;
        break;
    case 4:
        packman.y  = packman.y+yVel;
        break;
    }

    //Collision found, rollback
    if (zkontrolujKolizi()) {
        packman.x=rollbackX;
        packman.y=rollbackY;
    } else {        
        repaint();
    }
}

public boolean zkontrolujKolizi() {
    return packman.intersects(platform) || packman.intersects(secondPlat);
}

这篇关于如何确保玩家不会穿墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆