Dobry den,
mam problem z koliziou Pacmana a steny a to taky ze po naraze do steny sa sice zastavy ale o kusok sa posunie, a tym padom ked drzim stlacenu klavesu moze ist za stenu kolko chce... Uz dva tyzdne sa to snazim fixnut a nedari sa, zacinam byt zufaly :)
import javax.swing.JFrame;
public class Game extends JFrame {
public Game() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(486, 506);
setLocationRelativeTo(null);
setTitle("Pacman");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Game();
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Pacman pacman;
private Wall wall;
private ArrayList walls;
private int[][] pos = {
{0, 0}, {30, 0}, {60, 0},
{90, 0}, {120, 0}, {150, 0},
{180, 0}, {210, 0}, {240, 0},
{270, 0}, {300, 0}, {330, 0},
{360, 0}, {390, 0}, {420, 0},
{450, 0}, {0, 30}, {0, 60},
{0, 90}, {0, 120}, {0, 150},
{0, 180}, {0, 210}, {0, 240},
{0, 270}, {0, 300}, {0, 330},
{0, 360}, {0, 390}, {0, 420},
{0, 450}, {30, 450}, {60, 450},
{90, 450}, {120, 450}, {150, 450},
{180, 450}, {210, 450}, {240, 450},
{270, 450}, {300, 450}, {330, 450},
{360, 450}, {390, 450}, {420, 450},
{450, 450}, {450, 420}, {450, 390},
{450, 360}, {450, 330}, {450, 300},
{450, 270}, {450, 240}, {450, 210},
{450, 180}, {450, 150}, {450, 120},
{450, 90}, {450, 60}, {450, 30},
{210, 60}, {210, 90}, {210, 120},
{210, 150}, {210, 180}, {210, 210},
{210, 240}, {240, 60}, {240, 90},
{240, 120}, {240, 150}, {240, 180},
{240, 210}, {240, 240}, {240, 270},
{210, 270}, {180, 270}, {150, 270},
{120, 270}, {180, 240}, {150, 240},
{120, 240},
{120, 120}, {120, 90},
{120, 60}, {150, 120}, {150, 90},
{150, 60}
};
public Board() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
pacman = new Pacman();
initWalls();
timer = new Timer(5, this);
timer.start();
}
public void initWalls() {
walls = new ArrayList();
for (int i=0; i<pos.length; i++ ) {
walls.add(new Wall(pos[i][0], pos[i][1]));
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(pacman.getImage(), pacman.getX(), pacman.getY(), this);
for (int i = 0; i < walls.size(); i++) {
Wall a = (Wall)walls.get(i);
if (a.isVisible())
g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
pacman.move();
checkCollisions();
repaint();
}
public void checkCollisions() {
Rectangle r3 = pacman.getBounds();
for (int j = 0; j<walls.size(); j++) {
Wall a = (Wall) walls.get(j);
Rectangle r2 = a.getBounds();
if (r3.intersects(r2)) {
pacman.stop();
}
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
pacman.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
pacman.keyPressed(e);
}
}
}
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Pacman {
private String pacman = "pacman.png";
private int dx;
private int dy;
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image image;
final int blocksize = 30;
final int blocks = 16;
short[] screendata;
public Pacman() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(pacman));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
visible = true;
x = 31;
y = 31;
}
public void move() {
x += dx;
y += dy;
}
public void stop() {
dx = 0;
dy = 0;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -2;
dy = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 2;
dy = 0;
}
if (key == KeyEvent.VK_UP) {
dy = -2;
dx = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 2;
dx = 0;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_DOWN) {
}
}
}
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Wall {
private String wall = "wall.png";
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image image;
public Wall(int x, int y) {
ImageIcon ii = new ImageIcon(this.getClass().getResource(wall));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
visible = true;
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
public Image getImage() {
return image;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}