// 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); } }
private void initGame() { mario = new Mario(32, 240); // start x, ground y coins = new ArrayList<>(); goombas = new ArrayList<>(); super mario bros java game 240x320
// goombas Iterator<Goomba> goombaIt = goombas.iterator(); while (goombaIt.hasNext()) { Goomba g = goombaIt.next(); g.update(); if (mario.getBounds().intersects(g.getBounds())) { if (mario.vy > 0 && mario.y + mario.height - g.y < 16) { // stomp goombaIt.remove(); score += 20; mario.vy = -8; // small bounce } else { gameRunning = false; // game over } } } // UI g2
Rectangle getBounds() { return new Rectangle(x, y, TILE_SIZE, TILE_SIZE); } // UI g2.setColor(Color.BLACK)
// --- Goomba --- class Goomba { int x, y; int vx = -1; int width = 16, height = 16; Goomba(int x, int y) { this.x = x; this.y = y; } void update() { x += vx; if (x % 32 == 0) vx = -vx; } Rectangle getBounds() { return new Rectangle(x, y, width, height); } void draw(Graphics2D g, int screenX, int screenY) { g.setColor(new Color(101, 67, 33)); g.fillRect(screenX, screenY, width, height); g.setColor(Color.BLACK); g.fillOval(screenX + 3, screenY + 4, 3, 3); g.fillOval(screenX + 10, screenY + 4, 3, 3); } }
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;
private Timer timer;
