Bros Java Game 240x320 | Super Mario
// --- 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); } }
// 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;
// flag at end flag = new Flag((levelWidth - 3) * TILE_SIZE, 18 * TILE_SIZE - TILE_SIZE); }
// 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
// Game state private boolean gameRunning = true; private boolean gameWin = false; private int score = 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; }
// 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)); // --- Goomba --- class Goomba { int
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; } } } } } } }
private void buildLevel() { tiles = new Tile[levelWidth][SCREEN_HEIGHT / TILE_SIZE + 2];
Mario(int startX, int groundY) { x = startX; y = groundY - height; } int vx = -1
private Timer timer;
Rectangle getBounds() { return new Rectangle(x, y, width, height); }
// 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); }
@Override public void keyTyped(KeyEvent e) {}
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Iterator; public class MarioGame240x320 extends JPanel implements ActionListener, KeyListener { // Screen settings (240x320) private static final int SCREEN_WIDTH = 240; private static final int SCREEN_HEIGHT = 320; private static final int TILE_SIZE = 16;
