Pygame-画线.双摆的轨迹被摆本身所覆盖——我该如何防止这种情况发生

  • 本文关键字:何防止 情况 覆盖 画线 轨迹 Pygame- python
  • 更新时间 :
  • 英文 :


我想创建一个双摆,第二个质量的路径用红色描绘。为了给人一种钟摆运动的错觉,我用白色(与背景相同的颜色(重新绘制了钟摆之前的位置。然而,绘制钟摆会覆盖红色轨迹的一部分,使其不连续且不完整。在绘制第二个质量所经过的路径时,我怎么能给人一种钟摆运动的错觉呢?

import pygame
import math
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
start_x = 400
start_y = 250
Display = pygame.display.set_mode((800,500))
Display.fill(white)
pygame.display.set_caption('Double Pendulum')
clock = pygame.time.Clock()
def game_loop():
crashed = False
length = 150
length2 = 100
mass = 8
mass2 = 3
theta = math.pi
theta_v = 0
theta2 = math.pi-0.0001
theta2_v = 0
damping = 0.002
g = 1
x = length*math.sin(theta)
y = length*math.cos(theta)
x2 = length2*math.sin(theta2)
y2 = length2*math.cos(theta2)
xp = length*math.sin(theta)
yp = length*math.cos(theta)
x2p = length2*math.sin(theta2)
y2p = length2*math.cos(theta2)
last_position1 = None
last_position2 = None
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
theta_a = (((-g*(2*mass+mass2)*math.sin(theta))-(mass2*g*math.sin(theta-2*theta2))-(2*math.sin(theta-theta2)*mass2*((theta2_v**2)*length2 + (theta_v**2)*length*math.cos(theta-theta2))))/(length*(2*mass+mass2-mass2*math.cos(2*theta-2*theta2))))-(damping*theta_v)
theta2_a = ((((2*math.sin(theta-theta2))*(((theta_v**2)*length*(mass+mass2))+(g*(mass+mass2)*math.cos(theta))+((theta2_v**2)*length2*mass2*math.cos(theta-theta2)))))/(length2*(2*mass+mass2-mass2*math.cos(2*theta-2*theta2)))) - (damping*theta2_v)
theta_v += theta_a
theta2_v += theta2_a
theta += theta_v
theta2 += theta2_v
x = length*math.sin(theta)
y = length*math.cos(theta)
x2 = length2*math.sin(theta2)
y2 = length2*math.cos(theta2)
pygame.draw.line(Display, white, (start_x,start_y), (start_x+xp, start_y+yp), 1)
pygame.draw.circle(Display, white, (start_x + int(xp),start_y + int(yp)), mass, 0)
pygame.draw.line(Display, white, (start_x+xp, start_y+yp), (start_x+xp+x2p, start_y+yp+y2p), 1)
pygame.draw.circle(Display, white, (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p)), mass2, 0)
pygame.draw.line(Display, black, (start_x,start_y), (start_x+x, start_y+y), 1)
pygame.draw.circle(Display, black, (start_x + int(x),start_y + int(y)), mass, 0)
pygame.draw.line(Display, black, (start_x+x, start_y+y), (start_x+x+x2, start_y+y+y2), 1)
pygame.draw.circle(Display, black, (start_x + int(x) + int(x2),start_y + int(y) + int(y2)), mass2, 0)
if last_position2 is not None:
pygame.draw.line(Display, red, last_position2, (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p)), 1)
last_position2 = (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p))
xp = x
yp = y
x2p = x2
y2p = y2
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()

解决了!我找到了解决问题的办法。我创建了一个完全透明的曲面,并在该透明曲面上绘制了红色轨迹。然后,我使用blit将跟踪的路径叠加到显示器上。

background = pygame.Surface([800,500], pygame.SRCALPHA, 32)
background = background.convert_alpha()
if last_position2 is not None:
pygame.draw.line(background, red, last_position2, (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p)), 1)
last_position2 = (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p))
Display.blit(background,(0,0))

喜欢双摆。感谢您的分享。因此,为了避免在迹线上重新绘制,可以在不同的表面上绘制迹线,并将该表面闪电式绘制到显示器(主表面(上。请参见下文。我已对添加更改的位置进行了评论。

import pygame
import math
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
start_x = 400
start_y = 250
Display = pygame.display.set_mode((800,500))
Display.fill(white)
# Add surface
Surface = pygame.Surface((800,500))
Surface.fill(white)
pygame.display.set_caption('Double Pendulum')
clock = pygame.time.Clock()
def game_loop():
crashed = False
length = 150
length2 = 100
mass = 8
mass2 = 3
theta = math.pi
theta_v = 0
theta2 = math.pi-0.0001
theta2_v = 0
damping = 0.002
g = 1
x = length*math.sin(theta)
y = length*math.cos(theta)
x2 = length2*math.sin(theta2)
y2 = length2*math.cos(theta2)
xp = length*math.sin(theta)
yp = length*math.cos(theta)
x2p = length2*math.sin(theta2)
y2p = length2*math.cos(theta2)
last_position1 = None
last_position2 = None
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
theta_a = (((-g*(2*mass+mass2)*math.sin(theta))-(mass2*g*math.sin(theta-2*theta2))-(2*math.sin(theta-theta2)*mass2*((theta2_v**2)*length2 + (theta_v**2)*length*math.cos(theta-theta2))))/(length*(2*mass+mass2-mass2*math.cos(2*theta-2*theta2))))-(damping*theta_v)
theta2_a = ((((2*math.sin(theta-theta2))*(((theta_v**2)*length*(mass+mass2))+(g*(mass+mass2)*math.cos(theta))+((theta2_v**2)*length2*mass2*math.cos(theta-theta2)))))/(length2*(2*mass+mass2-mass2*math.cos(2*theta-2*theta2)))) - (damping*theta2_v)
theta_v += theta_a
theta2_v += theta2_a
theta += theta_v
theta2 += theta2_v
x = length*math.sin(theta)
y = length*math.cos(theta)
x2 = length2*math.sin(theta2)
y2 = length2*math.cos(theta2)
if last_position2 is not None:
#draw on surface
pygame.draw.line(Surface, red, last_position2, (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p)), 3)
#blit surface to display (main surface)
Display.blit(Surface,((0,0)) )
last_position2 = (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p))
pygame.draw.line(Display, white, (start_x,start_y), (start_x+xp, start_y+yp), 1)
pygame.draw.circle(Display, white, (start_x + int(xp),start_y + int(yp)), mass, 0)
pygame.draw.line(Display, white, (start_x+xp, start_y+yp), (start_x+xp+x2p, start_y+yp+y2p), 1)
pygame.draw.circle(Display, white, (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p)), mass2, 0)
pygame.draw.line(Display, black, (start_x,start_y), (start_x+x, start_y+y), 1)
pygame.draw.circle(Display, black, (start_x + int(x),start_y + int(y)), mass, 0)
pygame.draw.line(Display, black, (start_x+x, start_y+y), (start_x+x+x2, start_y+y+y2), 1)
pygame.draw.circle(Display, black, (start_x + int(x) + int(x2),start_y + int(y) + int(y2)), mass2, 0)
if last_position2 is not None:
pygame.draw.line(Display, red, last_position2, (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p)), 1)
last_position2 = (start_x + int(xp) + int(x2p),start_y + int(yp) + int(y2p))
xp = x
yp = y
x2p = x2
y2p = y2
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()

相关内容

最新更新