-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement7.py
158 lines (128 loc) · 4.86 KB
/
movement7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pygame
import math
from pygame.math import Vector3
# Initialize pygame
pygame.init()
# Set up display dimensions and create display window
width, height = 800, 600
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("3D Wireframe")
# Define colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
# Initialize player position, angle, and jumping state
pos = Vector3(0, 0, 0)
angle = 0
vertical_speed = 0
is_jumping = False
# Set up field of view and draw distance for the 3D perspective
FOV = 90
draw_distance = 1000
# Define grid size and create grid points
grid_size = 10
grid = []
for x in range(-grid_size, grid_size + 1):
for z in range(-grid_size, grid_size + 1):
grid.append(Vector3(x * 100, 0, z * 100))
# Function to rotate a point around the Z-axis
def rotate2d(pos, rad):
x, y = pos
s, c = math.sin(rad), math.cos(rad)
return x * c - y * s, y * c + x * s
# Set up clock for managing frame rate
clock = pygame.time.Clock()
running = True
# Define movement speeds
normal_speed = 200
sprint_speed = 400
# Headbobbing variables
headbob_angle = 0
normal_headbob_amplitude = 5
sprint_headbob_amplitude = 15
normal_headbob_frequency = 5
sprint_headbob_frequency = 7
jump_headbob_time = 0
jump_headbob_duration = 0.3 # Duration of jump headbob in seconds
jump_headbob_amplitude = 30 # Maximum downward displacement for jump headbob
# Main game loop
while running:
# Calculate delta time in seconds
dt = clock.tick(60) / 1000
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not is_jumping:
vertical_speed = 300 # Initial jump velocity
is_jumping = True
jump_headbob_time = 0 # Reset jump headbob timer
# Handle key presses for movement and rotation
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
angle -= 2 * dt
if keys[pygame.K_RIGHT]:
angle += 2 * dt
# Determine movement speed and headbob parameters based on sprint key (left Shift)
is_sprinting = keys[pygame.K_LSHIFT]
movement_speed = sprint_speed if is_sprinting else normal_speed
headbob_amplitude = sprint_headbob_amplitude if is_sprinting else normal_headbob_amplitude
headbob_frequency = sprint_headbob_frequency if is_sprinting else normal_headbob_frequency
# Variable to track if the player is moving
is_moving = False
if keys[pygame.K_UP]:
pos.x += math.sin(angle) * movement_speed * dt
pos.z += math.cos(angle) * movement_speed * dt
is_moving = True
if keys[pygame.K_DOWN]:
pos.x -= math.sin(angle) * movement_speed * dt
pos.z -= math.cos(angle) * movement_speed * dt
is_moving = True
# Apply gravity and update vertical position
vertical_speed -= 600 * dt # Gravity
pos.y += vertical_speed * dt
# Handle ground collision
if pos.y < 0:
pos.y = 0
vertical_speed = 0
is_jumping = False
# Update headbobbing
if is_moving and not is_jumping:
headbob_angle += headbob_frequency * dt
headbob_offset = math.sin(headbob_angle) * headbob_amplitude
else:
headbob_offset = 0
headbob_angle = 0
# Update jump headbob
jump_headbob_offset = 0
if is_jumping:
jump_headbob_time += dt
if jump_headbob_time < jump_headbob_duration:
# Create a quick downward motion followed by a return to neutral
jump_progress = jump_headbob_time / jump_headbob_duration
jump_headbob_offset = jump_headbob_amplitude * math.sin(jump_progress * math.pi)
# Combine regular headbob and jump headbob
total_headbob_offset = headbob_offset - jump_headbob_offset
# Clear display with black background
display.fill((0, 0, 0))
# Draw the grid
for point in grid:
x, y, z = point - pos # Translate point relative to player position
x, z = rotate2d((x, z), angle) # Rotate point around player
if z > 0: # Only draw points in front of the player
f = FOV / z # Perspective projection factor
x, y = x * f, y * f
# Convert 3D coordinates to 2D screen coordinates
screen_x = int(x) + width // 2
screen_y = height // 2 - int(y)
# Apply total headbobbing offset to screen_y
screen_y += int(total_headbob_offset)
# Calculate top and bottom y-coordinates for vertical lines
top_y = screen_y - int(100 * f)
bottom_y = screen_y + int(100 * f)
# Draw vertical lines representing the grid
pygame.draw.line(display, WHITE, (screen_x, top_y), (screen_x, bottom_y), 1)
# Update the display
pygame.display.flip()
# Quit pygame
pygame.quit()