Player and Blocks collision (Terraria) : <p>I am creating a game like Terraria. I already have a player and I can place blocks and delete them by clicking on an existing block. But I can´t figure out how to make player and block collision. I want to create physics like in terraria. Can you help me somehow?
Here are my 3 classes (I have 4 but the 4. class is only JFrame class):</p>

<p>Here is my Main Class (gameloop): </p>

<pre><code>package Package;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Game extends JPanel implements ActionListener, MouseListener, KeyListener {

    Timer gameLoop = new Timer(5, this);
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 1500;
    public static final int HEIGHT = 900;
    public static final Dimension windowSize = new Dimension(WIDTH, HEIGHT);

    Player player = new Player();

    public static ArrayList&lt;Block&gt; blocks = new ArrayList&lt;Block&gt;();

    private int xDistance;
    private int yDistance;

    public Game() {
        setPreferredSize(windowSize);
        setFocusable(true);
        addMouseListener(this);
        addKeyListener(this);
        setBackground(Color.WHITE);
        for (int i = 0; i &lt; Game.WIDTH; i += 50) {
            for (int j = Game.HEIGHT - 150; j &lt; Game.HEIGHT; j += 50) {
                blocks.add(new Block(i, j));
            }
        }
        start();
    }

    public void start() {
        gameLoop.start();
    }

    public void stop() {
        gameLoop.stop();
    }

    public void paint(Graphics g) {

        for (Block b : blocks) {
            b.render(g);
        }

        player.render(g);
    }

    public void actionPerformed(ActionEvent e) {
        player.move();
        player.offScreen();

        repaint();
    }

    public void mouseClicked(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {
        int mouseX = e.getX();
        int mouseY = e.getY();
        boolean foundBlock = false;

        xDistance = mouseX % 50;
        yDistance = mouseY % 50;

        for (Block b : blocks) {
            if (b.x == mouseX - xDistance &amp;&amp; b.y == mouseY - yDistance) {
                if (mouseX &gt;= player.x - 150 &amp;&amp; mouseX &lt;= player.x + 200 &amp;&amp; mouseY &gt;= player.y - 150 &amp;&amp; mouseY &lt;= player.y + 250) {
                    blocks.remove(b);
                    foundBlock = true;
                    break;
                }
            }
        }

        if (foundBlock == false) {
            if (mouseX &gt;= player.x - 150 &amp;&amp; mouseX &lt;= player.x + 200 &amp;&amp; mouseY &gt;= player.y - 150 &amp;&amp; mouseY &lt;= player.y + 250) {
                blocks.add(new Block(mouseX - xDistance, mouseY - yDistance));
            }
        }
    }

    public void mouseReleased(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}



    public void keyTyped(KeyEvent e) {}

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

        if (arrows == KeyEvent.VK_D) {
            player.xSpeed = 2;
        }
        if (arrows == KeyEvent.VK_A) {
            player.xSpeed = -2;
        }
    }

    public void keyReleased(KeyEvent e) {
        int arrows = e.getKeyCode();

        if (arrows == KeyEvent.VK_D) {
            player.xSpeed = 0;
        }
        if (arrows == KeyEvent.VK_A) {
            player.xSpeed = 0;
        }
    }
}
</code></pre>

<p>Here is my Player class:</p>

<pre><code>package Package;

import java.awt.Color;
import java.awt.Graphics;

public class Player {

    public int x = 14 * 50;
    public int y = Game.HEIGHT - (5 * 50);
    public int xSpeed = 0;
    public int ySpeed = 0;
    public boolean jump = false;
    public int ticks = 6;

    public void render(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 50, 100);
    }

    public void move() {
        x += xSpeed;
        y += ySpeed;
    }

    public void offScreen() {
        if (x &lt;= 0) {
            xSpeed = 0;
    } else if (x + 50 &gt;= Game.WIDTH) {
            xSpeed = 0;
        }
    }
}
</code></pre>

<p>Here is my Block Class:</p>

<pre><code>package Package;

import java.awt.Color;
import java.awt.Graphics;

public class Block {

    public int x;
    public int y;

    public Block(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void render(Graphics g) {
        g.setColor(Color.GRAY);
        g.fillRect(x, y, 50, 50);
        g.setColor(Color.BLACK);
        g.drawRect(x, y, 50, 50);
    }
}
</code></pre>
