Notes
![]() ![]() Notes - notes.io |
import java.awt.*;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import javax.swing.*;
public class PacMan extends JPanel{
class Block{
int x;
int y;
int width;
int height;
Image image;
int startX;
int startY;
Block(Image image, int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.image = image;
this.startX = x;
this.startY = y;
}
}
private int rowCount = 21;
private int columnCount = 19;
private int tileSize = 32;
private int boardWidth = columnCount * tileSize;
private int boardHeight = rowCount * tileSize;
private Image wallImage;
private Image blueGhostImage;
private Image orangeGhostImage;
private Image pinkGhostImage;
private Image redGhostImage;
private Image pacmanUp;
private Image pacmanDown;
private Image pacmanLeft;
private Image pacmanRight;
private String[] tileMap = {
"XXXXXXXXXXXXXXXXXXX",
"X X X",
"X XX XXX X XXX XX X",
"X X",
"X XX X XXXXX X XX X",
"X X X X",
"XXXX XXXX XXXX XXXX",
"OOOX X X XOOO",
"XXXX X XXrXX X XXXX",
"O bpo O",
"XXXX X XXXXX X XXXX",
"OOOX X X XOOO",
"XXXX X XXXXX X XXXX",
"X X X",
"X XX XXX X XXX XX X",
"X X P X X",
"XX X X XXXXX X X XX",
"X X X X X",
"X XXXXXX X XXXXXX X",
"X X",
"XXXXXXXXXXXXXXXXXXX"
};
HashSet<Block> walls;
HashSet<Block> foods;
HashSet<Block> ghosts;
Block pacman;
PacMan(){
setPreferredSize(new Dimension(boardWidth,boardHeight));
setBackground(Color.BLACK);
//zurag unshix
wallImage = new ImageIcon(getClass().getResource("./wall.png")).getImage();
blueGhostImage = new ImageIcon(getClass().getResource("./blueGhost.png")).getImage();
orangeGhostImage = new ImageIcon(getClass().getResource("./orangeGhost.png")).getImage();
pinkGhostImage = new ImageIcon(getClass().getResource("./pinkGhost.png")).getImage();
redGhostImage = new ImageIcon(getClass().getResource("./redGhost.png")).getImage();
pacmanUp = new ImageIcon(getClass().getResource("./pacmanUp.png")).getImage();
pacmanDown = new ImageIcon(getClass().getResource("./pacmanDown.png")).getImage();
pacmanLeft = new ImageIcon(getClass().getResource("./pacmanLeft.png")).getImage();
pacmanRight = new ImageIcon(getClass().getResource("./pacmanRight.png")).getImage();
loadMap();
}
public void loadMap() {
walls = new HashSet<Block>();
foods = new HashSet<Block>();
ghosts = new HashSet<Block>();
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < columnCount; c++) {
String row = tileMap[r];
char tileMapChar = row.charAt(c);
int x = c*tileSize;
int y = r*tileSize;
if(tileMapChar=='X') {
Block wall = new Block(wallImage, x, y, tileSize, tileSize);
walls.add(wall);
}else if(tileMapChar=='b') {
Block ghost = new Block(blueGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}else if(tileMapChar=='o') {
Block ghost = new Block(orangeGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}else if(tileMapChar=='p') {
Block ghost = new Block(pinkGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}else if(tileMapChar=='r') {
Block ghost = new Block(redGhostImage, x, y, tileSize, tileSize);
ghosts.add(ghost);
}else if(tileMapChar=='P') {
pacman = new Block(pacmanRight, x, y, tileSize, tileSize);
}else if(tileMapChar==' ') {
Block food = new Block(null, x+14, y+14, 4, 4);
foods.add(food);
}
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
g.drawImage(pacman.image, pacman.x,
pacman.y, pacman.width, pacman.height, null);
for (Block ghost : ghosts) {
g.drawImage(ghost.image, ghost.x,
ghost.y, ghost.width, ghost.height, null);
}
for (Block wall : walls) {
g.drawImage(wall.image, wall.x,
wall.y, wall.width, wall.height, null);
}
g.setColor(Color.WHITE);
for(Block food : foods) {
g.fillRect(food.x,
food.y, food.width, food.height);
}
//
g.setFont(new Font("Arial", Font.PLAIN, 18));
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team