Skip to content

Commit 315143a

Browse files
authored
Merge pull request #45 from xFrednet/mouse-movement
Mouse movement
2 parents b78327b + 6c561d3 commit 315143a

4 files changed

Lines changed: 21 additions & 12 deletions

File tree

a03_3d-maze/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
| Return to Home | `[H]` |
1111
| Debug information | `[P]` |
1212

13+
You can also use the mouse to rotate the view :)
14+
1315
## Installing and running the game
1416
This game uses:
1517
* PyOpenGL and pygame for rendering

a03_3d-maze/src/control_system.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,28 @@ def _wasd_movement(self, entity_id, speed, vertical_movement, vertical_speed):
102102
print(f"Transformation(Position: {tra.position}, Rotation: {tra.rotation})")
103103

104104
def _mouse_control(self, entity_id, enable_pitch=True):
105+
controls: res.GameControlState = self.world.controls
106+
screen_center = self.world.resolution / 2.0
107+
108+
# If python breaks on this try updating pygame :D
105109
(is_pressed, _, _, _, _) = pygame.mouse.get_pressed()
106-
transformation = self.world.component_for_entity(entity_id, com.Transformation)
107-
(rel_x, rel_y) = pygame.mouse.get_rel()
108110
if is_pressed:
111+
transformation = self.world.component_for_entity(entity_id, com.Transformation)
112+
#(rel_x, rel_y) = pygame.mouse.get_rel()
113+
(pos_x, pos_y) = pygame.mouse.get_pos()
114+
rel_x = screen_center.x - pos_x
115+
rel_y = screen_center.y - pos_y
116+
109117
if enable_pitch:
110-
pitch_change = 0.0
111-
if rel_y > 0:
112-
pitch_change += 0.01 # I think it is too fast with change 0.1
113-
if rel_y < 0:
114-
pitch_change -= 0.01
118+
pitch_change = rel_y * controls.mouse_sensitivity
115119
transformation.rotation.y = clamp(
116120
transformation.rotation.y + pitch_change,
117121
(math.pi - 0.2) / -2,
118122
(math.pi - 0.2) / 2)
119123

120-
if rel_x < 0:
121-
transformation.rotation.x += 0.1
122-
if rel_x > 0:
123-
transformation.rotation.x -= 0.1
124+
transformation.rotation.x += rel_x * controls.mouse_sensitivity
125+
126+
pygame.mouse.set_pos([screen_center.x, screen_center.y])
124127

125128
def _arrow_key_rotation(self, entity_id, enable_pitch=True):
126129
transformation = self.world.component_for_entity(entity_id, com.Transformation)

a03_3d-maze/src/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import glm
55
import pygame
6+
import pygame.display
67
from world import World
78

89
RESOLUTION = 1024, 720
@@ -41,7 +42,8 @@ def main():
4142
pygame.init()
4243
pygame.display.init()
4344
pygame.display.set_mode(RESOLUTION, pygame.DOUBLEBUF | pygame.OPENGL)
44-
pygame.display.set_caption("Le 3D maze of time")
45+
pygame.display.set_caption("Le maze: 3D-Packman - just a bit worse")
46+
pygame.mouse.set_visible(False)
4547
running = True
4648
world = World(glm.vec2(RESOLUTION), LEVEL)
4749

a03_3d-maze/src/resources.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def __init__(self):
2424
self.key_return_to_home = pygame.locals.K_h
2525
self.key_return_to_home_state = False
2626

27+
self.mouse_sensitivity = 0.0025
28+
2729
self.player_speed = 10.0
2830
self.player_jump_height = 15.0
2931
self.free_camera_speed = 10.0

0 commit comments

Comments
 (0)