├── bin ├── App.class ├── wall.png ├── PacMan.class ├── cherry.png ├── cherry2.png ├── pacmanUp.png ├── redGhost.png ├── blueGhost.png ├── pacmanDown.png ├── pacmanLeft.png ├── pinkGhost.png ├── powerFood.png ├── orangeGhost.png ├── pacmanRight.png ├── scaredGhost.png └── PacMan$Block.class ├── src ├── wall.png ├── cherry.png ├── cherry2.png ├── pacmanUp.png ├── redGhost.png ├── blueGhost.png ├── pacmanDown.png ├── pacmanLeft.png ├── pinkGhost.png ├── powerFood.png ├── orangeGhost.png ├── pacmanRight.png ├── scaredGhost.png ├── App.java └── PacMan.java ├── .vscode └── settings.json └── README.md /bin/App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/App.class -------------------------------------------------------------------------------- /bin/wall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/wall.png -------------------------------------------------------------------------------- /src/wall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/wall.png -------------------------------------------------------------------------------- /bin/PacMan.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/PacMan.class -------------------------------------------------------------------------------- /bin/cherry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/cherry.png -------------------------------------------------------------------------------- /bin/cherry2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/cherry2.png -------------------------------------------------------------------------------- /bin/pacmanUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/pacmanUp.png -------------------------------------------------------------------------------- /bin/redGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/redGhost.png -------------------------------------------------------------------------------- /src/cherry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/cherry.png -------------------------------------------------------------------------------- /src/cherry2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/cherry2.png -------------------------------------------------------------------------------- /src/pacmanUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/pacmanUp.png -------------------------------------------------------------------------------- /src/redGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/redGhost.png -------------------------------------------------------------------------------- /bin/blueGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/blueGhost.png -------------------------------------------------------------------------------- /bin/pacmanDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/pacmanDown.png -------------------------------------------------------------------------------- /bin/pacmanLeft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/pacmanLeft.png -------------------------------------------------------------------------------- /bin/pinkGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/pinkGhost.png -------------------------------------------------------------------------------- /bin/powerFood.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/powerFood.png -------------------------------------------------------------------------------- /src/blueGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/blueGhost.png -------------------------------------------------------------------------------- /src/pacmanDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/pacmanDown.png -------------------------------------------------------------------------------- /src/pacmanLeft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/pacmanLeft.png -------------------------------------------------------------------------------- /src/pinkGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/pinkGhost.png -------------------------------------------------------------------------------- /src/powerFood.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/powerFood.png -------------------------------------------------------------------------------- /bin/orangeGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/orangeGhost.png -------------------------------------------------------------------------------- /bin/pacmanRight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/pacmanRight.png -------------------------------------------------------------------------------- /bin/scaredGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/scaredGhost.png -------------------------------------------------------------------------------- /src/orangeGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/orangeGhost.png -------------------------------------------------------------------------------- /src/pacmanRight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/pacmanRight.png -------------------------------------------------------------------------------- /src/scaredGhost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/src/scaredGhost.png -------------------------------------------------------------------------------- /bin/PacMan$Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hirun-HM/PACMAN_GAME/HEAD/bin/PacMan$Block.class -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /src/App.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | 3 | 4 | public class App { 5 | public static void main(String[] args) throws Exception { 6 | int rowCount = 21; 7 | int columnCount = 19; 8 | int tileSize = 32; 9 | int boardWidth = columnCount * tileSize; 10 | int boardHeight = rowCount * tileSize; 11 | 12 | JFrame frame = new JFrame("Pac Man"); 13 | // frame.setVisible(true); 14 | frame.setSize(boardWidth,boardHeight); 15 | frame.setLocationRelativeTo(null); 16 | frame.setResizable(false); 17 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 | 19 | PacMan pacmanGame = new PacMan(); 20 | frame.add(pacmanGame); 21 | frame.pack(); 22 | pacmanGame.requestFocus(); 23 | frame.setVisible(true); 24 | 25 | 26 | } 27 | } 28 | 29 | //app running 30 | -------------------------------------------------------------------------------- /src/PacMan.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.awt.event.*; 3 | import java.util.HashSet; 4 | import java.util.Random; 5 | import javax.swing.*; 6 | 7 | public class PacMan extends JPanel implements ActionListener,KeyListener { 8 | 9 | class Block { 10 | int x; 11 | int y; 12 | int width; 13 | int height; 14 | Image image; 15 | 16 | int startX; 17 | int startY; 18 | char direction = 'U'; // U D L R 19 | int velocityX = 0; 20 | int velocityY = 0; 21 | 22 | Block(Image image, int x, int y, int width, int height) { 23 | this.image = image; 24 | this.x = x; 25 | this.y = y; 26 | this.width = width; 27 | this.height = height; 28 | this.startX = x; 29 | this.startY = y; 30 | } 31 | 32 | void updateDirection(char direction){ 33 | char prevDirection = this.direction; 34 | this.direction = direction; 35 | updateVelocity(); 36 | this.x += this.velocityX; 37 | this.y += this.velocityY; 38 | for(Block wall : walls){ 39 | if(collision(this, wall)) { 40 | this.x -= this.velocityX; 41 | this.y -= this.velocityY; 42 | this.direction = prevDirection; 43 | updateVelocity(); 44 | } 45 | } 46 | } 47 | 48 | void updateVelocity() { 49 | if(this.direction == 'U'){ 50 | this.velocityX = 0; 51 | this.velocityY = -tileSize/4; 52 | } 53 | else if (this.direction == 'D') { 54 | this.velocityX = 0; 55 | this.velocityY = tileSize/4; 56 | } 57 | else if (this.direction == 'L') { 58 | this.velocityX = -tileSize/4; 59 | this.velocityY = 0; 60 | 61 | } 62 | else if (this.direction == 'R') { 63 | this.velocityX = tileSize/4; 64 | this.velocityY = 0; 65 | 66 | } 67 | } 68 | 69 | void reset(){ 70 | this.x = this.startX; 71 | this.y = this.startY; 72 | } 73 | } 74 | 75 | private int rowCount = 21; 76 | private int columnCount = 19; 77 | private int tileSize = 32; 78 | private int boardWidth = columnCount * tileSize; 79 | private int boardHeight = rowCount * tileSize; 80 | 81 | private Image wallImage; 82 | private Image blueGhostImage; 83 | private Image orangeGhostImage; 84 | private Image pinkGhostImage; 85 | private Image redGhostImage; 86 | 87 | private Image pacmanUpImage; 88 | private Image pacmanDownImage; 89 | private Image pacmanLeftImage; 90 | private Image pacmanRightImage; 91 | 92 | // X = wall, O = skip, P = pac man, ' ' = food 93 | // Ghosts: b = blue, o = orange, p = pink, r = red 94 | private String[] tileMap = { 95 | "XXXXXXXXXXXXXXXXXXX", 96 | "X X X", 97 | "X XX XXX X XXX XX X", 98 | "X X", 99 | "X XX X XXXXX X XX X", 100 | "X X X X", 101 | "XXXX XXXX XXXX XXXX", 102 | "OOOX X X XOOO", 103 | "XXXX X XXrXX X XXXX", 104 | "O bpo O", 105 | "XXXX X XXXXX X XXXX", 106 | "OOOX X X XOOO", 107 | "XXXX X XXXXX X XXXX", 108 | "X X X", 109 | "X XX XXX X XXX XX X", 110 | "X X P X X", 111 | "XX X X XXXXX X X XX", 112 | "X X X X X", 113 | "X XXXXXX X XXXXXX X", 114 | "X X", 115 | "XXXXXXXXXXXXXXXXXXX" 116 | }; 117 | 118 | HashSet walls; 119 | HashSet foods; 120 | HashSet ghosts; 121 | Block pacman; 122 | 123 | Timer gameLoop; 124 | char[] direction = {'U', 'D', 'L','R'};//up down left right 125 | Random random = new Random(); 126 | int score = 0; 127 | int lives = 3; 128 | boolean gameOver = false; 129 | 130 | 131 | 132 | PacMan() { 133 | setPreferredSize(new Dimension(boardWidth, boardHeight)); 134 | setBackground(Color.BLACK); 135 | addKeyListener(this); 136 | setFocusable(true); 137 | 138 | // load images 139 | wallImage = new ImageIcon(getClass().getResource("./wall.png")).getImage(); 140 | blueGhostImage = new ImageIcon(getClass().getResource("./blueGhost.png")).getImage(); 141 | orangeGhostImage = new ImageIcon(getClass().getResource("./orangeGhost.png")).getImage(); 142 | pinkGhostImage = new ImageIcon(getClass().getResource("./pinkGhost.png")).getImage(); 143 | redGhostImage = new ImageIcon(getClass().getResource("./redGhost.png")).getImage(); 144 | 145 | pacmanUpImage = new ImageIcon(getClass().getResource("./pacmanUp.png")).getImage(); 146 | pacmanDownImage = new ImageIcon(getClass().getResource("./pacmanDown.png")).getImage(); 147 | pacmanLeftImage = new ImageIcon(getClass().getResource("./pacmanLeft.png")).getImage(); 148 | pacmanRightImage = new ImageIcon(getClass().getResource("./pacmanRight.png")).getImage(); 149 | 150 | loadMap(); 151 | for(Block ghost : ghosts){ 152 | char newDirection = direction[random.nextInt(4)]; 153 | ghost.updateDirection(newDirection); 154 | } 155 | gameLoop = new Timer(50, this); 156 | gameLoop.start(); 157 | 158 | } 159 | 160 | public void loadMap() { 161 | walls = new HashSet(); 162 | foods = new HashSet(); 163 | ghosts = new HashSet(); 164 | 165 | for (int r = 0; r < rowCount; r++) { 166 | for (int c = 0; c < columnCount; c++) { 167 | String row = tileMap[r]; 168 | char tileMapChar = row.charAt((c)); 169 | 170 | int x = c * tileSize; 171 | int y = r * tileSize; 172 | 173 | if (tileMapChar == 'X') {// block wall 174 | Block wall = new Block(wallImage, x, y, tileSize, tileSize); 175 | walls.add(wall); 176 | } else if (tileMapChar == 'b') {// blue ghost 177 | Block ghost = new Block(blueGhostImage, x, y, tileSize, tileSize); 178 | ghosts.add(ghost); 179 | } else if (tileMapChar == 'o') {// orange ghost 180 | Block ghost = new Block(orangeGhostImage, x, y, tileSize, tileSize); 181 | ghosts.add(ghost); 182 | } else if (tileMapChar == 'p') {// pink ghost 183 | Block ghost = new Block(pinkGhostImage, x, y, tileSize, tileSize); 184 | ghosts.add(ghost); 185 | } else if (tileMapChar == 'r') {// red ghost 186 | Block ghost = new Block(redGhostImage, x, y, tileSize, tileSize); 187 | ghosts.add(ghost); 188 | }else if (tileMapChar == 'P') {// pacman 189 | pacman = new Block(pacmanRightImage,x,y,tileSize,tileSize); 190 | }else if (tileMapChar == ' ') {//food 191 | Block food = new Block( null,x +14,y +14, 4, 4); 192 | foods.add(food); 193 | } 194 | } 195 | 196 | } 197 | } 198 | 199 | public void paintComponent(Graphics g){ 200 | super.paintComponent(g); 201 | draw(g); 202 | } 203 | 204 | public void draw(Graphics g){ 205 | g.drawImage(pacman.image, pacman.x, pacman.y, pacman.width, pacman.height, null); 206 | 207 | for(Block ghost : ghosts){ 208 | g.drawImage(ghost.image, ghost.x, ghost.y, ghost.width, ghost.height, null); 209 | } 210 | 211 | for(Block wall : walls){ 212 | g.drawImage(wall.image, wall.x, wall.y, wall.width, wall.height, null); 213 | } 214 | 215 | g.setColor(Color.WHITE); 216 | for(Block food : foods){ 217 | g.fillRect(food.x, food.y, food.width, food.height); 218 | } 219 | //score 220 | g.setFont(new Font("Arial", Font.PLAIN, 18)); 221 | if(gameOver){ 222 | g.drawString("Game Over: " + String.valueOf(score), tileSize/2, tileSize/2); 223 | } 224 | else{ 225 | g.drawString("x" + String.valueOf(lives) + " Score: " + String.valueOf(score), tileSize/2, tileSize/2); 226 | } 227 | } 228 | 229 | public void move(){ 230 | pacman.x += pacman.velocityX; 231 | pacman.y += pacman.velocityY; 232 | 233 | //check wall collisions 234 | for(Block wall : walls){ 235 | if(collision(pacman, wall)){ 236 | pacman.x -= pacman.velocityX; 237 | pacman.y -= pacman.velocityY; 238 | break; 239 | } 240 | } 241 | 242 | //check ghost collisions 243 | for (Block ghost : ghosts){ 244 | if(collision(ghost, pacman)){ 245 | lives -= 1; 246 | if (lives == 0) { 247 | gameOver= true; 248 | return; 249 | } 250 | resetPositions(); 251 | 252 | } 253 | if(ghost.y == tileSize*9 && ghost.direction != 'U' && ghost.direction !='D'){ 254 | ghost.updateDirection('U'); 255 | } 256 | ghost.x += ghost.velocityX; 257 | ghost.y += ghost.velocityY; 258 | for(Block wall : walls){ 259 | if(collision(ghost, wall) || ghost.x <=0 || ghost.x + ghost.width >= boardWidth){ 260 | ghost.x -= ghost.velocityX; 261 | ghost.y -= ghost.velocityY; 262 | char newDirection = direction[random.nextInt(4)]; 263 | ghost.updateDirection(newDirection); 264 | } 265 | } 266 | } 267 | 268 | //check food collision 269 | Block foodEaten = null; 270 | for (Block food : foods){ 271 | if(collision(pacman, food)){ 272 | foodEaten = food; 273 | score += 10; 274 | } 275 | } 276 | foods.remove(foodEaten); 277 | 278 | if (foods.isEmpty()) { 279 | loadMap(); 280 | resetPositions(); 281 | } 282 | } 283 | 284 | public boolean collision(Block a, Block b){ 285 | return a.x < b.x + b.width && 286 | a.x + a.width > b.x && 287 | a.y < b.y + b.height && 288 | a.y + a.height > b.y; 289 | } 290 | 291 | public void resetPositions(){ 292 | pacman.reset(); 293 | pacman.velocityX = 0; 294 | pacman.velocityY = 0; 295 | for(Block ghost : ghosts){ 296 | ghost.reset(); 297 | char newDirection = direction[random.nextInt(4)]; 298 | ghost.updateDirection(newDirection); 299 | } 300 | } 301 | 302 | @Override 303 | public void actionPerformed(ActionEvent e){ 304 | move(); 305 | repaint(); 306 | if (gameOver) { 307 | gameLoop.stop(); 308 | } 309 | } 310 | 311 | @Override 312 | public void keyTyped(KeyEvent e){} 313 | 314 | @Override 315 | public void keyPressed(KeyEvent e){} 316 | 317 | @Override 318 | public void keyReleased(KeyEvent e){ 319 | 320 | if (gameOver) { 321 | loadMap(); 322 | resetPositions(); 323 | lives=3; 324 | score=0; 325 | gameOver=false; 326 | gameLoop.start(); 327 | } 328 | //System.out.println("KeyEvent: " + e.getKeyCode()); 329 | if(e.getKeyCode() == KeyEvent.VK_UP) { 330 | pacman.updateDirection('U'); 331 | 332 | } 333 | else if(e.getKeyCode() == KeyEvent.VK_DOWN) { 334 | pacman.updateDirection('D'); 335 | 336 | } 337 | else if(e.getKeyCode() == KeyEvent.VK_LEFT) { 338 | pacman.updateDirection('L'); 339 | 340 | } 341 | else if(e.getKeyCode() == KeyEvent.VK_RIGHT) { 342 | pacman.updateDirection('R'); 343 | 344 | } 345 | 346 | if(pacman.direction == 'U'){ 347 | pacman.image = pacmanUpImage; 348 | } 349 | else if(pacman.direction == 'D'){ 350 | pacman.image = pacmanDownImage; 351 | } 352 | if(pacman.direction == 'L'){ 353 | pacman.image = pacmanLeftImage; 354 | } 355 | if(pacman.direction == 'R'){ 356 | pacman.image = pacmanRightImage; 357 | } 358 | } 359 | } 360 | --------------------------------------------------------------------------------