my drawing in pygame is covered by something and I want to animate it what does not work

huangapple go评论98阅读模式
英文:

my drawing in pygame is covered by something and I want to animate it what does not work

问题

我创建了一个函数,可以使用我的3D线条函数制作一个三维曲线(我知道可能已经有一个版本,但我想自己学习如何做)。在尝试对这条曲线进行控制旋转动画后,我只能看到它的一小部分,并且它根本不移动。

  1. import pygame
  2. from pygame.locals import *
  3. from math import sqrt, cos, sin, radians
  4. def draw_3d_lines(screen, color, points_list, distance=1):
  5. for i in range(len(points_list) - 1):
  6. x1 = points_list[i][0]
  7. y1 = points_list[i][1]
  8. z1 = points_list[i][2]
  9. x2 = points_list[i + 1][0]
  10. y2 = points_list[i + 1][1]
  11. z2 = points_list[i + 1][2]
  12. # Apply translation and scaling
  13. x1t = int(x1 / distance + screen.get_width() / 2)
  14. y1t = int(y1 / distance + screen.get_height() / 2)
  15. x2t = int(x2 / distance + screen.get_width() / 2)
  16. y2t = int(y2 / distance + screen.get_height() / 2)
  17. a= pygame.draw.line(screen, color, (x1t, y1t), (x2t, y2t),10)
  18. return(a)
  19. def rotate_3d_points(points_list, angle_x, angle_y, angle_z):
  20. angle_x = radians(angle_x)
  21. angle_y = radians(angle_y)
  22. angle_z = radians(angle_z)
  23. cos_angle_x = cos(angle_x)
  24. sin_angle_x = sin(angle_x)
  25. cos_angle_y = cos(angle_y)
  26. sin_angle_y = sin(angle_y)
  27. cos_angle_z = cos(angle_z)
  28. sin_angle_z = sin(angle_z)
  29. rotated_points = []
  30. for point in points_list:
  31. x = point[0]
  32. y = point[1]
  33. z = point[2]
  34. # Apply rotation
  35. x_rotated = x * cos_angle_y - z * sin_angle_y
  36. y_rotated = y * cos_angle_x - z * sin_angle_x
  37. z_rotated = x * sin_angle_y + z * cos_angle_y
  38. x_final = y_rotated * sin_angle_z + x_rotated * cos_angle_z
  39. y_final = y_rotated * cos_angle_z - x_rotated * sin_angle_z
  40. z_final = z_rotated
  41. rotated_points.append((x_final, y_final, z_final))
  42. return rotated_points
  43. def make_3d_curve(starting_point, ending_point, curve_point, step=0.01):
  44. t = 0
  45. points = []
  46. while t <= 1:
  47. # Calculate current point on the curve
  48. x = (1 - t) ** 2 * starting_point[0] + 2 * (1 - t) * t * curve_point[0] + t ** 2 * ending_point[0]
  49. y = (1 - t) ** 2 * starting_point[1] + 2 * (1 - t) * t * curve_point[1] + t ** 2 * ending_point[1]
  50. z = (1 - t) ** 2 * starting_point[2] + 2 * (1 - t) * t * curve_point[2] + t ** 2 * ending_point[2]
  51. points.append((x, y, z))
  52. t += step
  53. # Draw the curve
  54. return points
  55. # Example usage
  56. pygame.init()
  57. # Set up the screen
  58. width, height = 640, 480
  59. screen = pygame.display.set_mode((width, height))
  60. pygame.display.set_caption("3D Curve")
  61. # Starting point
  62. start = [-300, -200, 0]
  63. # Ending point
  64. end = [250, 100, 0]
  65. # Curve control point
  66. curve = [300, 100, 200]
  67. curve = make_3d_curve(start, end, curve)
  68. # Main game loop
  69. running = True
  70. clock = pygame.time.Clock()
  71. while running:
  72. for event in pygame.event.get():
  73. if event.type == QUIT:
  74. running = False
  75. if event.type == pygame.KEYDOWN:
  76. if event.key == pygame.K_a:
  77. rotate_3d_points(curve, 0,1,0)
  78. if event.key == pygame.K_d:
  79. rotate_3d_points(curve, 0,-1,0)
  80. if event.key == pygame.K_w:
  81. rotate_3d_points(curve, 1,0,0)
  82. if event.key == pygame.K_s:
  83. rotate_3d_points(curve, -1,0,0)
  84. if event.key == pygame.K_q:
  85. rotate_3d_points(curve, 0,0,1)
  86. if event.key == pygame.K_e:
  87. rotate_3d_points(curve, 0,0,-1)
  88. screen.fill((0, 0, 0)) # Clear the screen
  89. a = draw_3d_lines(screen, (0,255,0), curve)
  90. pygame.display.flip()
  91. clock.tick(60)
  92. 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.

  1. import pygame
  2. from pygame.locals import *
  3. from math import sqrt, cos, sin, radians
  4. def draw_3d_lines(screen, color, points_list, distance=1):
  5. for i in range(len(points_list) - 1):
  6. x1 = points_list[i][0]
  7. y1 = points_list[i][1]
  8. z1 = points_list[i][2]
  9. x2 = points_list[i + 1][0]
  10. y2 = points_list[i + 1][1]
  11. z2 = points_list[i + 1][2]
  12. # Apply translation and scaling
  13. x1t = int(x1 / distance + screen.get_width() / 2)
  14. y1t = int(y1 / distance + screen.get_height() / 2)
  15. x2t = int(x2 / distance + screen.get_width() / 2)
  16. y2t = int(y2 / distance + screen.get_height() / 2)
  17. a= pygame.draw.line(screen, color, (x1t, y1t), (x2t, y2t),10)
  18. return(a)
  19. def rotate_3d_points(points_list, angle_x, angle_y, angle_z):
  20. angle_x = radians(angle_x)
  21. angle_y = radians(angle_y)
  22. angle_z = radians(angle_z)
  23. cos_angle_x = cos(angle_x)
  24. sin_angle_x = sin(angle_x)
  25. cos_angle_y = cos(angle_y)
  26. sin_angle_y = sin(angle_y)
  27. cos_angle_z = cos(angle_z)
  28. sin_angle_z = sin(angle_z)
  29. rotated_points = []
  30. for point in points_list:
  31. x = point[0]
  32. y = point[1]
  33. z = point[2]
  34. # Apply rotation
  35. x_rotated = x * cos_angle_y - z * sin_angle_y
  36. y_rotated = y * cos_angle_x - z * sin_angle_x
  37. z_rotated = x * sin_angle_y + z * cos_angle_y
  38. x_final = y_rotated * sin_angle_z + x_rotated * cos_angle_z
  39. y_final = y_rotated * cos_angle_z - x_rotated * sin_angle_z
  40. z_final = z_rotated
  41. rotated_points.append((x_final, y_final, z_final))
  42. return rotated_points
  43. def make_3d_curve(starting_point, ending_point, curve_point, step=0.01):
  44. t = 0
  45. points = []
  46. while t &lt;= 1:
  47. # Calculate current point on the curve
  48. x = (1 - t) ** 2 * starting_point[0] + 2 * (1 - t) * t * curve_point[0] + t ** 2 * ending_point[0]
  49. y = (1 - t) ** 2 * starting_point[1] + 2 * (1 - t) * t * curve_point[1] + t ** 2 * ending_point[1]
  50. z = (1 - t) ** 2 * starting_point[2] + 2 * (1 - t) * t * curve_point[2] + t ** 2 * ending_point[2]
  51. points.append((x, y, z))
  52. t += step
  53. # Draw the curve
  54. return(points)
  55. # Example usage
  56. pygame.init()
  57. # Set up the screen
  58. width, height = 640, 480
  59. screen = pygame.display.set_mode((width, height))
  60. pygame.display.set_caption(&quot;3D Curve&quot;)
  61. # Starting point
  62. start = [-300, -200, 0]
  63. # Ending point
  64. end = [250, 100, 0]
  65. # Curve control point
  66. curve = [300, 100, 200]
  67. curve = make_3d_curve(start, end, curve)
  68. # Main game loop
  69. running = True
  70. clock = pygame.time.Clock()
  71. while running:
  72. for event in pygame.event.get():
  73. if event.type == QUIT:
  74. running = False
  75. if event.type == pygame.KEYDOWN:
  76. if event.key == pygame.K_a:
  77. rotate_3d_points(curve, 0,1,0)
  78. if event.key == pygame.K_d:
  79. rotate_3d_points(curve, 0,-1,0)
  80. if event.key == pygame.K_w:
  81. rotate_3d_points(curve, 1,0,0)
  82. if event.key == pygame.K_s:
  83. rotate_3d_points(curve, -1,0,0)
  84. if event.key == pygame.K_q:
  85. rotate_3d_points(curve, 0,0,1)
  86. if event.key == pygame.K_e:
  87. rotate_3d_points(curve, 0,0,-1)
  88. screen.fill((0, 0, 0)) # Clear the screen
  89. a = draw_3d_lines(screen, (0,255,0), curve)
  90. pygame.display.flip()
  91. clock.tick(60)
  92. 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_curverotate_3d_points不会更新列表中的点,而是返回一个新的点列表。您需要将返回值赋回给变量。例如:

<s>rotate_3d_points(curve, 0,1,0)</s>。

  1. 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>.

  1. curve = rotate_3d_points(curve, 0, 1, 0)

huangapple
  • 本文由 发表于 2023年7月27日 19:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定