Super Mario Bros Java Game 240x320 (DIRECT BLUEPRINT)
private void initGame() { mario = new Mario(32, 240); // start x, ground y coins = new ArrayList<>(); goombas = new ArrayList<>();
public MarioGame240x320() { setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT)); setBackground(Color.CYAN); setFocusable(true); addKeyListener(this);
void update() { if (left) vx = -3; else if (right) vx = 3; else vx = 0;
@Override public void keyReleased(KeyEvent e) { int k = e.getKeyCode(); if (k == KeyEvent.VK_LEFT) mario.left = false; if (k == KeyEvent.VK_RIGHT) mario.right = false; } super mario bros java game 240x320
@Override public void keyTyped(KeyEvent e) {}
// fall off world if (mario.y > SCREEN_HEIGHT + 64) { gameRunning = false; } }
// 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); } private void initGame() { mario = new Mario(32,
// 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;
// collision with tiles handleTileCollisions();
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(); // start x
// mario mario.draw(g2, mario.x - cameraX, mario.y);
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); } }
// platform at x 40-45, y=13 for (int x = 40; x <= 45; x++) { tiles[x][13] = new Tile(x * TILE_SIZE, 13 * TILE_SIZE, Tile.Type.GROUND); }
buildLevel(); }