英文:
my drawing in pygame is covered by something and I want to animate it what does not work
问题
我创建了一个函数,可以使用我的3D线条函数制作一个三维曲线(我知道可能已经有一个版本,但我想自己学习如何做)。在尝试对这条曲线进行控制旋转动画后,我只能看到它的一小部分,并且它根本不移动。
import pygame
from pygame.locals import *
from math import sqrt, cos, sin, radians
def draw_3d_lines(screen, color, points_list, distance=1):
for i in range(len(points_list) - 1):
x1 = points_list[i][0]
y1 = points_list[i][1]
z1 = points_list[i][2]
x2 = points_list[i + 1][0]
y2 = points_list[i + 1][1]
z2 = points_list[i + 1][2]
# Apply translation and scaling
x1t = int(x1 / distance + screen.get_width() / 2)
y1t = int(y1 / distance + screen.get_height() / 2)
x2t = int(x2 / distance + screen.get_width() / 2)
y2t = int(y2 / distance + screen.get_height() / 2)
a= pygame.draw.line(screen, color, (x1t, y1t), (x2t, y2t),10)
return(a)
def rotate_3d_points(points_list, angle_x, angle_y, angle_z):
angle_x = radians(angle_x)
angle_y = radians(angle_y)
angle_z = radians(angle_z)
cos_angle_x = cos(angle_x)
sin_angle_x = sin(angle_x)
cos_angle_y = cos(angle_y)
sin_angle_y = sin(angle_y)
cos_angle_z = cos(angle_z)
sin_angle_z = sin(angle_z)
rotated_points = []
for point in points_list:
x = point[0]
y = point[1]
z = point[2]
# Apply rotation
x_rotated = x * cos_angle_y - z * sin_angle_y
y_rotated = y * cos_angle_x - z * sin_angle_x
z_rotated = x * sin_angle_y + z * cos_angle_y
x_final = y_rotated * sin_angle_z + x_rotated * cos_angle_z
y_final = y_rotated * cos_angle_z - x_rotated * sin_angle_z
z_final = z_rotated
rotated_points.append((x_final, y_final, z_final))
return rotated_points
def make_3d_curve(starting_point, ending_point, curve_point, step=0.01):
t = 0
points = []
while t <= 1:
# Calculate current point on the curve
x = (1 - t) ** 2 * starting_point[0] + 2 * (1 - t) * t * curve_point[0] + t ** 2 * ending_point[0]
y = (1 - t) ** 2 * starting_point[1] + 2 * (1 - t) * t * curve_point[1] + t ** 2 * ending_point[1]
z = (1 - t) ** 2 * starting_point[2] + 2 * (1 - t) * t * curve_point[2] + t ** 2 * ending_point[2]
points.append((x, y, z))
t += step
# Draw the curve
return points
# Example usage
pygame.init()
# Set up the screen
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("3D Curve")
# Starting point
start = [-300, -200, 0]
# Ending point
end = [250, 100, 0]
# Curve control point
curve = [300, 100, 200]
curve = make_3d_curve(start, end, curve)
# Main game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
rotate_3d_points(curve, 0,1,0)
if event.key == pygame.K_d:
rotate_3d_points(curve, 0,-1,0)
if event.key == pygame.K_w:
rotate_3d_points(curve, 1,0,0)
if event.key == pygame.K_s:
rotate_3d_points(curve, -1,0,0)
if event.key == pygame.K_q:
rotate_3d_points(curve, 0,0,1)
if event.key == pygame.K_e:
rotate_3d_points(curve, 0,0,-1)
screen.fill((0, 0, 0)) # Clear the screen
a = draw_3d_lines(screen, (0,255,0), curve)
pygame.display.flip()
clock.tick(60)
pygame.quit()
英文:
I made a function so I can make a 3 dimensional curve witch uses my 3d line function(I know that there may be already a version of this, but I want to learn how to do it myself) after trying to animate an controlled rotation of this curve I can only see a small part of it and it does not move at all.
import pygame
from pygame.locals import *
from math import sqrt, cos, sin, radians
def draw_3d_lines(screen, color, points_list, distance=1):
for i in range(len(points_list) - 1):
x1 = points_list[i][0]
y1 = points_list[i][1]
z1 = points_list[i][2]
x2 = points_list[i + 1][0]
y2 = points_list[i + 1][1]
z2 = points_list[i + 1][2]
# Apply translation and scaling
x1t = int(x1 / distance + screen.get_width() / 2)
y1t = int(y1 / distance + screen.get_height() / 2)
x2t = int(x2 / distance + screen.get_width() / 2)
y2t = int(y2 / distance + screen.get_height() / 2)
a= pygame.draw.line(screen, color, (x1t, y1t), (x2t, y2t),10)
return(a)
def rotate_3d_points(points_list, angle_x, angle_y, angle_z):
angle_x = radians(angle_x)
angle_y = radians(angle_y)
angle_z = radians(angle_z)
cos_angle_x = cos(angle_x)
sin_angle_x = sin(angle_x)
cos_angle_y = cos(angle_y)
sin_angle_y = sin(angle_y)
cos_angle_z = cos(angle_z)
sin_angle_z = sin(angle_z)
rotated_points = []
for point in points_list:
x = point[0]
y = point[1]
z = point[2]
# Apply rotation
x_rotated = x * cos_angle_y - z * sin_angle_y
y_rotated = y * cos_angle_x - z * sin_angle_x
z_rotated = x * sin_angle_y + z * cos_angle_y
x_final = y_rotated * sin_angle_z + x_rotated * cos_angle_z
y_final = y_rotated * cos_angle_z - x_rotated * sin_angle_z
z_final = z_rotated
rotated_points.append((x_final, y_final, z_final))
return rotated_points
def make_3d_curve(starting_point, ending_point, curve_point, step=0.01):
t = 0
points = []
while t <= 1:
# Calculate current point on the curve
x = (1 - t) ** 2 * starting_point[0] + 2 * (1 - t) * t * curve_point[0] + t ** 2 * ending_point[0]
y = (1 - t) ** 2 * starting_point[1] + 2 * (1 - t) * t * curve_point[1] + t ** 2 * ending_point[1]
z = (1 - t) ** 2 * starting_point[2] + 2 * (1 - t) * t * curve_point[2] + t ** 2 * ending_point[2]
points.append((x, y, z))
t += step
# Draw the curve
return(points)
# Example usage
pygame.init()
# Set up the screen
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("3D Curve")
# Starting point
start = [-300, -200, 0]
# Ending point
end = [250, 100, 0]
# Curve control point
curve = [300, 100, 200]
curve = make_3d_curve(start, end, curve)
# Main game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
rotate_3d_points(curve, 0,1,0)
if event.key == pygame.K_d:
rotate_3d_points(curve, 0,-1,0)
if event.key == pygame.K_w:
rotate_3d_points(curve, 1,0,0)
if event.key == pygame.K_s:
rotate_3d_points(curve, -1,0,0)
if event.key == pygame.K_q:
rotate_3d_points(curve, 0,0,1)
if event.key == pygame.K_e:
rotate_3d_points(curve, 0,0,-1)
screen.fill((0, 0, 0)) # Clear the screen
a = draw_3d_lines(screen, (0,255,0), curve)
pygame.display.flip()
clock.tick(60)
pygame.quit()
I added the key event part and afterwards i moved the fill around but always got a nearly black screen what did not happen before.
答案1
得分: 1
make_3d_curve
和rotate_3d_points
不会更新列表中的点,而是返回一个新的点列表。您需要将返回值赋回给变量。例如:
<s>rotate_3d_points(curve, 0,1,0)
</s>。
curve = rotate_3d_points(curve, 0, 1, 0)
英文:
make_3d_curve
and rotate_3d_points
don't update the points in the list, but return a new list of points. You need to assign the return value back to the variable. e.g.:
<s>rotate_3d_points(curve, 0,1,0)
</s>.
curve = rotate_3d_points(curve, 0, 1, 0)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论