Super Mario Bros Java Game 240x320 Apr 2026

// coins for (Coin c : coins) { c.draw(g2, c.x - cameraX, c.y); }

buildLevel(); }

// coins coins.add(new Coin(15 * TILE_SIZE, 14 * TILE_SIZE)); coins.add(new Coin(42 * TILE_SIZE, 12 * TILE_SIZE)); coins.add(new Coin(43 * TILE_SIZE, 12 * TILE_SIZE)); coins.add(new Coin(60 * TILE_SIZE, 17 * TILE_SIZE));

// Level data (simple) private Tile[][] tiles; private int levelWidth = 80; // tiles super mario bros java game 240x320

// camera follows mario cameraX = mario.x - SCREEN_WIDTH / 3; if (cameraX < 0) cameraX = 0; if (cameraX > levelWidth * TILE_SIZE - SCREEN_WIDTH) cameraX = levelWidth * TILE_SIZE - SCREEN_WIDTH;

// ground and platforms for (int x = 0; x < levelWidth; x++) { // ground at y = 18 tiles (288px) tiles[x][18] = new Tile(x * TILE_SIZE, 18 * TILE_SIZE, Tile.Type.GROUND); }

// draw tiles for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < tiles[0].length; y++) { if (tiles[x][y] != null) { int screenX = x * TILE_SIZE - cameraX; int screenY = y * TILE_SIZE; if (screenX + TILE_SIZE > 0 && screenX < SCREEN_WIDTH) { tiles[x][y].draw(g2, screenX, screenY); } } } } // coins for (Coin c : coins) { c

// flag flag.draw(g2, flag.x - cameraX, flag.y);

// --- 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); } }

// 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 } } } c.x - cameraX

onGround = false; if (y + height > SCREEN_HEIGHT) { y = SCREEN_HEIGHT - height; vy = 0; onGround = true; } }

// --- 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); } }

// --- Mario class --- class Mario { int x, y; int width = 16, height = 16; int vx = 0, vy = 0; boolean left = false, right = false; boolean onGround = false;

// --- Tile --- class Tile { enum Type { GROUND } Type type; int x, y;