fogleman/minecraft

repository·master·Indexed 26 days ago

https://github.com/fogleman/minecraft

A Minecraft-inspired demo written in Python and Pyglet designed as an educational tool for learning programming. It features a sector-based world system managed by the Model class, player physics and state handled by the Window class, and utilities for block manipulation, collision detection, and texture coordinate generation.

Tokens
947
Snippets
2
Records
10
Agent score
40%

What's inside fogleman/minecraft

  1. Controls for moving and playing

    master

    Use the following keyboard and mouse controls to navigate the world:

    Movement:

    • W: forward
    • S: back
    • A: strafe left
    • D: strafe right
    • Space: jump
    • Tab: toggle flying mode
    • Mouse: look around

    Building:

    • 1: select brick block
    • 2: select grass block
    • 3: select sand block
    • Mouse left-click: remove block
    • Mouse right-click: create block

    Quitting:

    • ESC: release mouse, then close window
  2. Install and run Minecraft demo

    master

    To run the Minecraft demo, you need to install the pyglet dependency, clone the repository, and execute main.py using Python.

    npip install pyglet
    git clone https://github.com/fogleman/Minecraft.git
    cd Minecraft
    python main.py
  3. Troubleshoot Pyglet on macOS

    master
    If you encounter issues running Pyglet in 64-bit mode on macOS, you can try running Python in 32-bit mode using arch -i386. Alternatively, you can set the default Python to 32-bit mode or install a Pyglet alpha version that supports 64-bit.
  4. Proposed API usage pattern

    master

    The project aims to provide a Python package API for configuring worlds and running the simulation. The envisioned usage pattern involves importing mc, initializing a World, setting blocks, and calling mc.run().

    import mc
    
    world = mc.World(...) 
    world.set_block(x, y, z, mc.DIRT)
    mc.run(world)
  5. Control player movement and view with the Window class

    master

    The Window class (subclass of pyglet.window.Window) manages the player's state, including position, rotation, and physics.

    Key properties:

    • position: Current (x, y, z) position (floats).
    • rotation: Current (horizontal, vertical) rotation in degrees.
    • flying: Boolean indicating if the player is in flying mode (ignores gravity).
    • inventory: List of available block textures.
    • block: The currently selected block from the inventory.

    Key methods:

    • set_exclusive_mouse(exclusive): Captures or releases the mouse.
    • get_sight_vector(): Returns the (dx, dy, dz) unit vector representing the direction the player is looking.
    • get_motion_vector(): Returns the (dx, dy, dz) velocity vector based on current strafe input and rotation.
  6. Manage the world with the Model class

    master

    The Model class handles the storage, rendering, and visibility of blocks in the world. It uses a sector-based system to optimize block loading and rendering.

    Key methods:

    • add_block(position, texture, immediate=True): Adds a block at the specified (x, y, z) position with the given texture. Use tex_coords() to generate texture data.
    • remove_block(position, immediate=True): Removes a block from the world.
    • hit_test(position, vector, max_distance=8): Performs a line-of-sight search from position along vector. Returns (hit_block, previous_block) or (None, None) if no hit is found.
    • process_queue(): Processes pending block visibility updates (used when immediate=False was used in add/remove calls).
  7. Check for collisions with collide()

    master
    The collide(position, height) method checks if a player at a given position with a specific height is colliding with any blocks. It returns the adjusted (x, y, z) position that accounts for collisions.
  8. Generate texture coordinates with tex_coords()

    master

    Use tex_coords(top, bottom, side) to generate a list of texture squares for a block's faces. The arguments top, bottom, and side should be tuples of coordinates (e.g., (1, 0)).

    Predefined textures available in the module:

    • GRASS
    • SAND
    • BRICK
    • STONE