diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f1e08aab --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +*# +*.pyc \ No newline at end of file diff --git a/main.py b/main.py index 6f962e33..a7ec6d9a 100644 --- a/main.py +++ b/main.py @@ -430,6 +430,20 @@ class Window(pyglet.window.Window): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) + self.walking_speed = WALKING_SPEED + + self.flying_speed = FLYING_SPEED + + self.gravity = GRAVITY + + self.max_jump_height = MAX_JUMP_HEIGHT + + self.jump_speed = JUMP_SPEED + + self.terminal_velocity = TERMINAL_VELOCITY + + self.player_height = PLAYER_HEIGHT + # Whether or not the window exclusively captures the mouse. self.exclusive = False @@ -495,6 +509,29 @@ def set_exclusive_mouse(self, exclusive): """ super(Window, self).set_exclusive_mouse(exclusive) self.exclusive = exclusive + + def set_walking_speed(self, speed): + self.walking_speed = speed + + def set_flying_speed(self, speed): + self.flying_speed = speed + + def set_gravity(self, g): + self.gravity = g + self.jump_speed = math.sqrt(2 * self.gravity * self.max_jump_height) + + def set_max_jump_height(self, height): + self.max_jump_height = height + self.jump_speed = math.sqrt(2 * self.gravity * self.max_jump_height) + + def set_jump_speed(self, speed): + self.jump_speed = speed + + def set_terminal_velocity(self, velocity): + self.terminal_velocity = velocity + + def set_player_height(self, height): + self.player_height = height def get_sight_vector(self): """ Returns the current line of sight vector indicating the direction @@ -585,7 +622,7 @@ def _update(self, dt): """ # walking - speed = FLYING_SPEED if self.flying else WALKING_SPEED + speed = self.flying_speed if self.flying else self.walking_speed d = dt * speed # distance covered this tick. dx, dy, dz = self.get_motion_vector() # New position in space, before accounting for gravity. @@ -595,12 +632,12 @@ def _update(self, dt): # Update your vertical speed: if you are falling, speed up until you # hit terminal velocity; if you are jumping, slow down until you # start falling. - self.dy -= dt * GRAVITY - self.dy = max(self.dy, -TERMINAL_VELOCITY) + self.dy -= dt * self.gravity + self.dy = max(self.dy, -(self.terminal_velocity)) dy += self.dy * dt # collisions x, y, z = self.position - x, y, z = self.collide((x + dx, y + dy, z + dz), PLAYER_HEIGHT) + x, y, z = self.collide((x + dx, y + dy, z + dz), self.player_height) self.position = (x, y, z) def collide(self, position, height): @@ -722,7 +759,7 @@ def on_key_press(self, symbol, modifiers): self.strafe[1] += 1 elif symbol == key.SPACE: if self.dy == 0: - self.dy = JUMP_SPEED + self.dy = self.jump_speed elif symbol == key.ESCAPE: self.set_exclusive_mouse(False) elif symbol == key.TAB: @@ -843,7 +880,6 @@ def draw_reticle(self): glColor3d(0, 0, 0) self.reticle.draw(GL_LINES) - def setup_fog(): """ Configure the OpenGL fog properties. @@ -889,6 +925,12 @@ def main(): setup() pyglet.app.run() +def main_window(window): + if isinstance (window, Window): + window.set_exclusive_mouse(True) + setup() + pyglet.app.run() + if __name__ == '__main__': main() diff --git a/mc.py b/mc.py new file mode 100644 index 00000000..77c7706f --- /dev/null +++ b/mc.py @@ -0,0 +1,35 @@ +import main + +class World (main.Window): + + def __init__(self): + main.Window.__init__(self, width=800, height=600, caption='Pyglet', resizable=True) + + def add_block(self, x, y, z, texture): + position = (x, y - 1, z) + if isinstance(texture, (str, int, float, long)): + if isinstance(texture, str): + texture = texture.upper() + if texture == "SAND" or texture == 1: + texture = SAND + elif texture == "BRICK" or texture == 2: + texture = BRICK + elif texture == "STONE" or texture == 3: + texture = STONE + else: + texture = GRASS + self.model.add_block(position, texture) + + def remove_block(self, x, y, z): + position = (x, y - 1, z) + self.model.remove_block(position) + + def set_block(self, x, y, z, texture): + self.add_block(x, y, z, texture) + +def run(world): + if isinstance(world, main.Window): + main.main_window(world) + +def normal_world(): + main.main() diff --git a/test.py b/test.py new file mode 100644 index 00000000..cd617a73 --- /dev/null +++ b/test.py @@ -0,0 +1,4 @@ +import mc + +world = mc.World() +mc.run(world)