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: 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!");
}
}
}