// 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));
// 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 } } }
Rectangle getBounds() { return new Rectangle(x, y, TILE_SIZE, TILE_SIZE); }
// collision with tiles handleTileCollisions(); super mario bros java game 240x320
// mario mario.draw(g2, mario.x - cameraX, mario.y);
// Game objects private ArrayList<Coin> coins; private ArrayList<Goomba> goombas; private Flag flag;
// coins for (Coin c : coins) { c.draw(g2, c.x - cameraX, c.y); } // coins coins
@Override public void actionPerformed(ActionEvent e) { if (gameRunning && !gameWin) { updateGame(); } repaint(); }
private void initGame() { mario = new Mario(32, 240); // start x, ground y coins = new ArrayList<>(); goombas = new ArrayList<>();
initGame(); timer = new Timer(16, this); // ~60 FPS timer.start(); } // coins coins.add(new Coin(15 * TILE_SIZE
// --- Coin --- class Coin { int x, y; Coin(int x, int y) { this.x = x; this.y = y; } Rectangle getBounds() { return new Rectangle(x, y, TILE_SIZE, TILE_SIZE); } void draw(Graphics2D g, int screenX, int screenY) { g.setColor(Color.YELLOW); g.fillOval(screenX, screenY, TILE_SIZE, TILE_SIZE); } }
onGround = false; if (y + height > SCREEN_HEIGHT) { y = SCREEN_HEIGHT - height; vy = 0; onGround = true; } }
// 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 buildLevel() { tiles = new Tile[levelWidth][SCREEN_HEIGHT / TILE_SIZE + 2];
// 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));
// 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 } } }
Rectangle getBounds() { return new Rectangle(x, y, TILE_SIZE, TILE_SIZE); }
// collision with tiles handleTileCollisions();
// mario mario.draw(g2, mario.x - cameraX, mario.y);
// Game objects private ArrayList<Coin> coins; private ArrayList<Goomba> goombas; private Flag flag;
// coins for (Coin c : coins) { c.draw(g2, c.x - cameraX, c.y); }
@Override public void actionPerformed(ActionEvent e) { if (gameRunning && !gameWin) { updateGame(); } repaint(); }
private void initGame() { mario = new Mario(32, 240); // start x, ground y coins = new ArrayList<>(); goombas = new ArrayList<>();
initGame(); timer = new Timer(16, this); // ~60 FPS timer.start(); }
// --- Coin --- class Coin { int x, y; Coin(int x, int y) { this.x = x; this.y = y; } Rectangle getBounds() { return new Rectangle(x, y, TILE_SIZE, TILE_SIZE); } void draw(Graphics2D g, int screenX, int screenY) { g.setColor(Color.YELLOW); g.fillOval(screenX, screenY, TILE_SIZE, TILE_SIZE); } }
onGround = false; if (y + height > SCREEN_HEIGHT) { y = SCREEN_HEIGHT - height; vy = 0; onGround = true; } }
// 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 buildLevel() { tiles = new Tile[levelWidth][SCREEN_HEIGHT / TILE_SIZE + 2];