![]() |
O_o Guy Java Source Code and Game
As promised here’s the source code to O_o Guy (formerly known as Noodles). The code is a mess and there’s a lot of bugs, but it’s still playable.
Commands: Up/Down/Left/Right move our hero. R restarts the level.
And how can you not love a game with art like this?
If you just want to play: Click Here
Otherwise hit read more for the source.
Everything needed to build: Click Here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | package O_oGuy; import java.applet.*; import java.awt.*; import java.util.*; import java.io.*; import java.awt.event.*; public class Main extends Applet implements ActionListener, KeyListener { int[][] playingField; private enum move { left, right, up, down, none, } public enum tile { clear(0), wall(1), box(2), boulder(3), hole(4), player(5), exit(6), lasergun(7), laserbeam(8), buttonswitch(9), butterswitchoff(10), vanushingblock(11), fluff(12); private int value; tile(int set) { value = set; } public int tile() { return value; } } ArrayList<Point> myLasers = new ArrayList<Point>(); private boolean boulderMovingState = false; //Converse, player movement must be allowed int playerX = 0; int playerY = 0; int boulderX = -1; int boulderY = -1; move boulderDirection = move.none; int currentLevel = 0; private Image img; private javax.swing.Timer t = new javax.swing.Timer(125, this); private Image backBuffer; private Graphics backG; private void restartLevel() { currentLevel--; nextLevel(); } private void nextLevel() { //File myFile = new File(""); try { //if (myFile.exists()) { if (true) { //Read the map! currentLevel++; //BufferedReader myReader = new BufferedReader(new FileReader(myFile)); InputStream is = getClass().getResourceAsStream(currentLevel + ".map"); InputStreamReader isr = new InputStreamReader(is); BufferedReader myReader = new BufferedReader(isr); int sizex = Integer.parseInt(myReader.readLine()); int sizey = Integer.parseInt(myReader.readLine()); playingField = new int[sizey][sizex]; myLasers.clear(); //char[] sep = { ',' }; for (int y = 0; y < sizey; y++) { String[] myLine = myReader.readLine().split(","); for (int x = 0; x < sizex; x++) { int value = Integer.parseInt(myLine[x].toString().trim()); if (value == tile.player.value) { playerX = x; playerY = y; System.out.println("PLAYER SET"); } if (value == tile.lasergun.value) { myLasers.add(new Point(x, y)); } if (value == -1) { value = 0; } playingField[y][x] = value; } } myReader.close(); //this.Window.Title = "Noodles map # " + currentLevel; this.showStatus("O_o Guy map # " + currentLevel); } } catch (IOException e) { //IOException. This should NOT happen currentLevel--; System.err.println(e); } } private void movePlayer(int x, int y) { playingField[playerY + y][playerX + x] = 5; playingField[playerY][playerX] = 0; playerX = playerX + x; playerY = playerY + y; } private void movePlayer(move myMove) { int x = 0; int y = 0; //We're moving in some direction! if (myMove == move.left) { x = -1; } else if (myMove == move.right) { x = 1; } else if (myMove == move.up) { y = -1; } else if (myMove == move.down) { y = 1; } try { if (playingField[playerY + y][playerX + x] == tile.clear.value) { //Nothing there movePlayer(x, y); } else if (playingField[playerY + y][playerX + x] == tile.box.value) { //Box is there if ((playingField[playerY + 2 * y][playerX + 2 * x] == tile.clear.value) || //Nothing (playingField[playerY + 2 * y][playerX + 2 * x] == tile.laserbeam.value) //Laser Beam ) { //Space behind it is clear playingField[playerY + 2 * y][playerX + 2 * x] = tile.box.value; movePlayer(x, y); } } else if (playingField[playerY + y][playerX + x] == tile.boulder.value) { // It's a boulder! if ((playingField[playerY + 2 * y][playerX + 2 * x] == tile.clear.value) || //Nothing (playingField[playerY + 2 * y][playerX + 2 * x] == tile.hole.value) || //Hole (playingField[playerY + 2 * y][playerX + 2 * x] == tile.laserbeam.value) //Laser Beam ) { //That we need to move! boulderMovingState = true; boulderX = playerX + x; boulderY = playerY + y; boulderDirection = myMove; //The boulder moves one square. Then it keeps moving until it hits something. moveBoulder(); //This needs to be done AFTER we move the boulder movePlayer(x, y); } } else if (playingField[playerY + y][playerX + x] == tile.exit.value) { //NEXT LEVEL nextLevel(); } else if (playingField[playerY + y][playerX + x] == tile.laserbeam.value) { //It's a laser! YOU DIE restartLevel(); } else if (playingField[playerY + y][playerX + x] == tile.buttonswitch.value) { //It's the switch! clearVanishing(); } else if (playingField[playerY + y][playerX + x] == tile.fluff.value) { //It's a piece of fluff movePlayer(x, y); } } catch (Exception e) { //DO NOTHING //boulderMovingState = false; } } private void moveBoulder() { int x = 0; int y = 0; //We're moving in some direction! if (boulderDirection == move.left) { x = -1; } if (boulderDirection == move.right) { x = 1; } if (boulderDirection == move.up) { y = -1; } if (boulderDirection == move.down) { y = 1; } try { if ((playingField[boulderY + y][boulderX + x] == tile.clear.value) || //Nothing (playingField[boulderY + y][boulderX + x] == tile.laserbeam.value) //Laser Beam ) { playingField[boulderY + y][boulderX + x] = tile.boulder.value; playingField[boulderY][boulderX] = tile.clear.value; boulderX = boulderX + x; boulderY = boulderY + y; } else { boulderMovingState = false; } if (playingField[boulderY + y][boulderX + x] == tile.hole.value) //Hole { playingField[boulderY + y][boulderX + x] = tile.clear.value; playingField[boulderY][boulderX] = tile.clear.value; boulderMovingState = false; } } catch (Exception e) { //DO NOTHING boulderMovingState = false; } } protected void clearVanishing() { //Clear all boxes for (int x = 0; x < playingField.length; x++) { for (int y = 0; y < playingField[0].length; y++) { if (playingField[x][y] == tile.vanushingblock.value) { playingField[x][y] = tile.clear.value; } else if (playingField[x][y] == tile.buttonswitch.value) { playingField[x][y] = tile.butterswitchoff.value; } } } } protected void RenderLasers() { //Clear all the laser beams for (int x = 0; x < playingField.length; x++) { for (int y = 0; y < playingField[0].length; y++) { if (playingField[x][y] == tile.laserbeam.value) { playingField[x][y] = tile.clear.value; } } } for (int i = 0; i < myLasers.size(); i++) { Point myLaserPoint = myLasers.get(i); drawOneLaser(myLaserPoint.x, myLaserPoint.y); } } protected void drawOneLaser(int x, int y) { int myLaserPointX = x; int myLaserPointY = y; //While next square is empty for (int i = x + 1; i < playingField[0].length; i++) { //The player is what is blocking the beam! if (playingField[myLaserPointY][myLaserPointX + i] == tile.player.value) { restartLevel(); } if (playingField[myLaserPointY][myLaserPointX + i] == tile.clear.value) { playingField[myLaserPointY][myLaserPointX + i] = tile.laserbeam.value; } else { break; } } } @Override public void init() { addKeyListener(this); try { //img = getImage(new URL("noodles.png")); //img = getImage(new URL("http://sarah.plt1.com/upload/noodles.png")); //System.out.println(this.getCodeBase().getPath()); img = getImage(this.getClass().getResource("art.png")); } catch (Exception e) { System.err.println(e); } nextLevel(); backBuffer = createImage(800, 600); backG = backBuffer.getGraphics(); backG.setColor(Color.BLACK); t.start(); //To make stuff work. } @Override public void update(Graphics g) { g.drawImage(backBuffer, 0, 0, this); } @Override public void paint(Graphics g) { update(g); } public void oldPaint(Graphics g) { if (playingField != null) { g.drawImage(img, 0, 0, playingField[0].length * 48, playingField.length * 48, 0, 0, 48, 48, null); RenderLasers(); for (int x = 0; x < playingField.length; x++) { for (int y = 0; y < playingField[0].length; y++) { //48 pixels!!!! int step = playingField[x][y]; g.drawImage(img, y * 48, x * 48, y * 48 + 48, x * 48 + 48, step * 48, 0, step * 48 + 48, 48, null); } } } } public void actionPerformed(ActionEvent e) { if (boulderMovingState == true) { moveBoulder(); } oldPaint(backG); repaint(); } @SuppressWarnings("static-access") public void keyPressed(KeyEvent e) { if (!boulderMovingState) { if (e.getKeyCode() == e.VK_LEFT) { movePlayer(move.left); } if (e.getKeyCode() == e.VK_RIGHT) { movePlayer(move.right); } if (e.getKeyCode() == e.VK_UP) { movePlayer(move.up); } if (e.getKeyCode() == e.VK_DOWN) { movePlayer(move.down); } if (e.getKeyCode() == e.VK_R) { restartLevel(); } if (e.getKeyCode() == e.VK_SCROLL_LOCK) { nextLevel(); } } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } } |



Yes I know vanishing isn’t spelt correctly
you realise it should be o_O guy (or rather, o_O upside down)
good game
where was the original concept from?
what we need is at the end a message “well done. you have finished the game. you really have no life”
Zorab: I think life should have a message like that, right at the end