Super Mario Bros Java Game 240x320 【Firefox Newest】
// Game objects private ArrayList<Coin> coins; private ArrayList<Goomba> goombas; private Flag flag;
private void updateGame() { mario.update();
// 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); }
// 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)); super mario bros java game 240x320
Rectangle getBounds() { return new Rectangle(x, y, TILE_SIZE, TILE_SIZE); }
// 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); } }
void update() { if (left) vx = -3; else if (right) vx = 3; else vx = 0; // Game objects private ArrayList<
// flag collision if (mario.getBounds().intersects(flag.getBounds())) { gameWin = true; gameRunning = false; }
private void buildLevel() { tiles = new Tile[levelWidth][SCREEN_HEIGHT / TILE_SIZE + 2];
for (int x = leftTile - 1; x <= rightTile + 1; x++) { for (int y = topTile - 1; y <= bottomTile + 1; y++) { if (x >= 0 && x < levelWidth && y >= 0 && y < tiles[0].length && tiles[x][y] != null) { Rectangle tileRect = tiles[x][y].getBounds(); private Flag flag
// Game state private boolean gameRunning = true; private boolean gameWin = false; private int score = 0;
public MarioGame240x320() { setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT)); setBackground(Color.CYAN); setFocusable(true); addKeyListener(this);
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); } }
x += vx; vy += 0.5; y += vy;
// 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); } } } }