Super Mario Bros Java Game 240x320 【2024-2026】
// Game objects private ArrayList<Coin> coins; private ArrayList<Goomba> goombas; private Flag flag;
// 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 } } }
if (mario.getBounds().intersects(tileRect)) { // from top if (mario.vy >= 0 && mario.y + mario.height - tileRect.y <= 8) { mario.y = tileRect.y - mario.height; mario.vy = 0; mario.onGround = true; } // from bottom else if (mario.vy < 0 && tileRect.y + TILE_SIZE - mario.y <= 8) { mario.y = tileRect.y + TILE_SIZE; mario.vy = 0; } // from left/right else { if (mario.x + mario.width > tileRect.x && mario.x < tileRect.x) { mario.x = tileRect.x - mario.width; } else if (mario.x < tileRect.x + TILE_SIZE && mario.x + mario.width > tileRect.x + TILE_SIZE) { mario.x = tileRect.x + TILE_SIZE; } } } } } } } super mario bros java game 240x320
void draw(Graphics2D g, int screenX, int screenY) { g.setColor(Color.RED); g.fillRect(screenX, screenY, width, height); g.setColor(Color.BLACK); g.fillRect(screenX + 4, screenY + 4, 2, 2); g.fillRect(screenX + 10, screenY + 4, 2, 2); } }
@Override public void keyPressed(KeyEvent e) { if (!gameRunning) return; int k = e.getKeyCode(); if (k == KeyEvent.VK_LEFT) mario.left = true; if (k == KeyEvent.VK_RIGHT) mario.right = true; if (k == KeyEvent.VK_SPACE && mario.onGround) { mario.jump(); } } // Game objects private ArrayList<
x += vx; vy += 0.5; y += vy;
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 Flag flag
// goombas goombas.add(new Goomba(20 * TILE_SIZE, 18 * TILE_SIZE - 16)); goombas.add(new Goomba(44 * TILE_SIZE, 13 * TILE_SIZE - 16)); goombas.add(new Goomba(65 * TILE_SIZE, 18 * TILE_SIZE - 16));