private void handleTileCollisions() { int leftTile = (mario.x + cameraX) / TILE_SIZE; int rightTile = (mario.x + cameraX + mario.width) / TILE_SIZE; int topTile = mario.y / TILE_SIZE; int bottomTile = (mario.y + mario.height) / TILE_SIZE;
onGround = false; if (y + height > SCREEN_HEIGHT) { y = SCREEN_HEIGHT - height; vy = 0; onGround = true; } }
// UI g2.setColor(Color.BLACK); g2.setFont(new Font("Arial", Font.BOLD, 12)); g2.drawString("SCORE: " + score, 8, 20); if (!gameRunning && !gameWin) { g2.setFont(new Font("Arial", Font.BOLD, 20)); g2.drawString("GAME OVER", SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT / 2); } if (gameWin) { g2.setFont(new Font("Arial", Font.BOLD, 20)); g2.drawString("YOU WIN!", SCREEN_WIDTH / 2 - 40, SCREEN_HEIGHT / 2); } }
// fall off world if (mario.y > SCREEN_HEIGHT + 64) { gameRunning = false; } } super mario bros java game 240x320
buildLevel(); }
void update() { if (left) vx = -3; else if (right) vx = 3; else vx = 0;
// Mario private Mario mario; private int cameraX = 0; private void handleTileCollisions() { int leftTile = (mario
// coins Iterator<Coin> coinIt = coins.iterator(); while (coinIt.hasNext()) { Coin c = coinIt.next(); if (mario.getBounds().intersects(c.getBounds())) { coinIt.remove(); score += 10; } }
@Override public void keyTyped(KeyEvent e) {}
// Game objects private ArrayList<Coin> coins; private ArrayList<Goomba> goombas; private Flag flag; int topTile = mario.y / TILE_SIZE
// flag at end flag = new Flag((levelWidth - 3) * TILE_SIZE, 18 * TILE_SIZE - TILE_SIZE); }
x += vx; vy += 0.5; y += vy;
// --- Flag --- class Flag { int x, y; Flag(int x, int y) { this.x = x; this.y = y; } Rectangle getBounds() { return new Rectangle(x, y, 8, 16); } void draw(Graphics2D g, int screenX, int screenY) { g.setColor(Color.RED); g.fillRect(screenX, screenY, 4, 20); g.fillPolygon(new int[]{screenX + 4, screenX + 16, screenX + 4}, new int[]{screenY, screenY + 8, screenY + 16}, 3); } }
Rectangle getBounds() { return new Rectangle(x, y, width, height); }
private Timer timer;