Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improved version of menu screen. #104

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- markdownlint-disable MD033 -->
# MCPY

Source code for each episode of my Minecraft clone in Python YouTube tutorial series.
Expand All @@ -20,10 +21,18 @@ And for Debian-based Linux distros:

[<img alt = "Setup: Linux" src = "https://i.imgur.com/9rZiv4B.png" width = 25% />](https://youtu.be/TtkTkfwwefA?list=PL6_bLxRDFzoKjaa3qCGkwR5L_ouSreaVP)

The `pyglet` module is a necessary dependency for all episodes, the `nbtlib` & `base36` modules are necessary dependencies for all episodes starting with 11, and the `pyglm` module is necessary for the `community` directory. You can install them with PIP by issuing:
The `pyglet` module is a necessary dependency for all episodes, the `nbtlib` & `base36` modules are necessary dependencies for all episodes starting with 11, the `pyglm` and PyImGUI modules are necessary for the `community` directory. You can install them with PIP by issuing:
ajh123 marked this conversation as resolved.
Show resolved Hide resolved

- Without community additions:

```console
pip install --user -r requirements.txt`.
ajh123 marked this conversation as resolved.
Show resolved Hide resolved
```

- With community additions:

```console
pip install --user pyglet nbtlib base36 pyglm
pip install --user -r community/requirements.txt`.
ajh123 marked this conversation as resolved.
Show resolved Hide resolved
```

## Running
Expand All @@ -44,6 +53,7 @@ Characteristic contributions are contributions which *add* something to the code
Contributions which *fix* something are still merged on the source of all episodes.

The community has several features and options that can be toggled in `options.py`:

- Render Distance: At what distance (in chunks) should chunks stop being rendered
- FOV: Camera field of view

Expand All @@ -59,12 +69,13 @@ The community has several features and options that can be toggled in `options.p
- Mipmap (minification filtering): Texture filtering used on higher distances. Default is `GL_NEAREST` (no filtering) (more info in `options.py`)
- Colored lighting: Uses an alternative shader program to achieve a more colored lighting; it aims to look similar to Beta 1.8+ (no performance loss should be incurred)
- Antialiasing: Experimental feature
- A main menu / UI system: Uses PyImGUI to create a menu system which appears when the game loads instead of loading the world straight away.

## List of projects based on this

- **Nim implementation:** https://github.com/phargobikcin/nim-minecraft-clone
- **Java implementation:** https://github.com/StartForKillerMC/JavaMinecraft
- **C++ implementation:** https://github.com/Jukitsu/CppMinecraft-clone
- **Odin implementation:** https://github.com/anthony-63/lvo
- **Lua implementation:** https://github.com/brennop/lunarcraft
- **Javascript implementation:** https://github.com/drakeerv/js-minecraft-clone ([Demo](https://drakeerv.github.io/js-minecraft-clone/))
- **Nim implementation:** <https://github.com/phargobikcin/nim-minecraft-clone>
- **Java implementation:** <https://github.com/StartForKillerMC/JavaMinecraft>
- **C++ implementation:** <https://github.com/Jukitsu/CppMinecraft-clone>
- **Odin implementation:** <https://github.com/anthony-63/lvo>
- **Lua implementation:** <https://github.com/brennop/lunarcraft>
- **Javascript implementation:** <https://github.com/drakeerv/js-minecraft-clone> ([Demo](https://drakeerv.github.io/js-minecraft-clone/))
6 changes: 3 additions & 3 deletions community/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def misc(self, mode):
elif mode == self.MiscMode.SAVE:
self.game.world.save.save()
elif mode == self.MiscMode.ESCAPE:
self.game.mouse_captured = False
self.game.set_exclusive_mouse(False)
self.game.window.mouse_captured = False
self.game.window.set_exclusive_mouse(False)
elif mode == self.MiscMode.SPEED_TIME:
self.game.world.speed_daytime()
elif mode == self.MiscMode.FULLSCREEN:
self.game.toggle_fullscreen()
self.game.window.toggle_fullscreen()
elif mode == self.MiscMode.FLY:
self.game.player.flying = not self.game.player.flying
elif mode == self.MiscMode.TELEPORT:
Expand Down
2 changes: 1 addition & 1 deletion community/joystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def init_joysticks(self, joysticks):
joystick.open(exclusive=True)

def update_controller(self):
if not self.game.mouse_captured or not self.joysticks:
if not self.game.window.mouse_captured or not self.joysticks:
return

self.game.player.rotation[0] += self.joystick_look[0] * self.camera_sensitivity
Expand Down
106 changes: 56 additions & 50 deletions community/keyboard_mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,79 @@

import controller
import math
import imgui

class Keyboard_Mouse(controller.Controller):
def __init__(self, game):
super().__init__(game)

self.game.on_mouse_press = self.on_mouse_press
self.game.on_mouse_motion = self.on_mouse_motion
self.game.on_mouse_drag = self.on_mouse_drag
self.game.window.on_mouse_press = self.on_mouse_press
self.game.window.on_mouse_motion = self.on_mouse_motion
self.game.window.on_mouse_drag = self.on_mouse_drag

self.game.on_key_press = self.on_key_press
self.game.on_key_release = self.on_key_release
self.game.window.on_key_press = self.on_key_press
self.game.window.on_key_release = self.on_key_release

def on_mouse_press(self, x, y, button, modifiers):
if not self.game.mouse_captured:
self.game.mouse_captured = True
self.game.set_exclusive_mouse(True)
if not imgui.get_io().want_capture_mouse:
if not self.game.window.mouse_captured:
self.game.window.mouse_captured = True
self.game.window.set_exclusive_mouse(True)

return
return

if button == pyglet.window.mouse.RIGHT: self.interact(self.InteractMode.PLACE)
elif button == pyglet.window.mouse.LEFT: self.interact(self.InteractMode.BREAK)
elif button == pyglet.window.mouse.MIDDLE: self.interact(self.InteractMode.PICK)
if button == pyglet.window.mouse.RIGHT: self.interact(self.InteractMode.PLACE)
elif button == pyglet.window.mouse.LEFT: self.interact(self.InteractMode.BREAK)
elif button == pyglet.window.mouse.MIDDLE: self.interact(self.InteractMode.PICK)

def on_mouse_motion(self, x, y, delta_x, delta_y):
if self.game.mouse_captured:
sensitivity = 0.004
if not imgui.get_io().want_capture_mouse:
if self.game.window.mouse_captured:
sensitivity = 0.004

self.game.player.rotation[0] += delta_x * sensitivity
self.game.player.rotation[1] += delta_y * sensitivity
self.game.player.rotation[0] += delta_x * sensitivity
self.game.player.rotation[1] += delta_y * sensitivity

self.game.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.game.player.rotation[1]))
self.game.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.game.player.rotation[1]))

def on_mouse_drag(self, x, y, delta_x, delta_y, buttons, modifiers):
self.on_mouse_motion(x, y, delta_x, delta_y)
if not imgui.get_io().want_capture_mouse:
self.on_mouse_motion(x, y, delta_x, delta_y)

def on_key_press(self, key, modifiers):
if not self.game.mouse_captured:
return

if key == pyglet.window.key.D: self.start_move(self.MoveMode.RIGHT)
elif key == pyglet.window.key.A: self.start_move(self.MoveMode.LEFT)
elif key == pyglet.window.key.W: self.start_move(self.MoveMode.FORWARD)
elif key == pyglet.window.key.S: self.start_move(self.MoveMode.BACKWARD)
elif key == pyglet.window.key.SPACE : self.start_move(self.MoveMode.UP)
elif key == pyglet.window.key.LSHIFT: self.start_move(self.MoveMode.DOWN)

elif key == pyglet.window.key.LCTRL : self.start_modifier(self.ModifierMode.SPRINT)

elif key == pyglet.window.key.F: self.misc(self.MiscMode.FLY)
elif key == pyglet.window.key.G: self.misc(self.MiscMode.RANDOM)
elif key == pyglet.window.key.O: self.misc(self.MiscMode.SAVE)
elif key == pyglet.window.key.R: self.misc(self.MiscMode.TELEPORT)
elif key == pyglet.window.key.ESCAPE: self.misc(self.MiscMode.ESCAPE)
elif key == pyglet.window.key.F6: self.misc(self.MiscMode.SPEED_TIME)
elif key == pyglet.window.key.F11: self.misc(self.MiscMode.FULLSCREEN)
elif key == pyglet.window.key.F3: self.misc(self.MiscMode.TOGGLE_F3)
elif key == pyglet.window.key.F10: self.misc(self.MiscMode.TOGGLE_AO)
if not imgui.get_io().want_capture_keyboard:
if not self.game.window.mouse_captured:
return

if key == pyglet.window.key.D: self.start_move(self.MoveMode.RIGHT)
elif key == pyglet.window.key.A: self.start_move(self.MoveMode.LEFT)
elif key == pyglet.window.key.W: self.start_move(self.MoveMode.FORWARD)
elif key == pyglet.window.key.S: self.start_move(self.MoveMode.BACKWARD)
elif key == pyglet.window.key.SPACE : self.start_move(self.MoveMode.UP)
elif key == pyglet.window.key.LSHIFT: self.start_move(self.MoveMode.DOWN)

elif key == pyglet.window.key.LCTRL : self.start_modifier(self.ModifierMode.SPRINT)

elif key == pyglet.window.key.F: self.misc(self.MiscMode.FLY)
elif key == pyglet.window.key.G: self.misc(self.MiscMode.RANDOM)
elif key == pyglet.window.key.O: self.misc(self.MiscMode.SAVE)
elif key == pyglet.window.key.R: self.misc(self.MiscMode.TELEPORT)
elif key == pyglet.window.key.ESCAPE: self.misc(self.MiscMode.ESCAPE)
elif key == pyglet.window.key.F6: self.misc(self.MiscMode.SPEED_TIME)
elif key == pyglet.window.key.F11: self.misc(self.MiscMode.FULLSCREEN)
elif key == pyglet.window.key.F3: self.misc(self.MiscMode.TOGGLE_F3)
elif key == pyglet.window.key.F10: self.misc(self.MiscMode.TOGGLE_AO)

def on_key_release(self, key, modifiers):
if not self.game.mouse_captured:
return

if key == pyglet.window.key.D: self.end_move(self.MoveMode.RIGHT)
elif key == pyglet.window.key.A: self.end_move(self.MoveMode.LEFT)
elif key == pyglet.window.key.W: self.end_move(self.MoveMode.FORWARD)
elif key == pyglet.window.key.S: self.end_move(self.MoveMode.BACKWARD)
elif key == pyglet.window.key.SPACE : self.end_move(self.MoveMode.UP)
elif key == pyglet.window.key.LSHIFT: self.end_move(self.MoveMode.DOWN)

elif key == pyglet.window.key.LCTRL : self.end_modifier(self.ModifierMode.SPRINT)
if not imgui.get_io().want_capture_keyboard:
if not self.game.window.mouse_captured:
return

if key == pyglet.window.key.D: self.end_move(self.MoveMode.RIGHT)
elif key == pyglet.window.key.A: self.end_move(self.MoveMode.LEFT)
elif key == pyglet.window.key.W: self.end_move(self.MoveMode.FORWARD)
elif key == pyglet.window.key.S: self.end_move(self.MoveMode.BACKWARD)
elif key == pyglet.window.key.SPACE : self.end_move(self.MoveMode.UP)
elif key == pyglet.window.key.LSHIFT: self.end_move(self.MoveMode.DOWN)

elif key == pyglet.window.key.LCTRL : self.end_modifier(self.ModifierMode.SPRINT)
Loading