Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame曲线移动问题如何解决?_Python_Pygame - Fatal编程技术网

Python Pygame曲线移动问题如何解决?

Python Pygame曲线移动问题如何解决?,python,pygame,Python,Pygame,0: 方向向量比例到长度(自重力) 自速度=(自速度+方向向量)*自摩擦 self.pos+=自速度 最简单的例子: 导入pygame pygame.init() 类曲线移动: 定义初始值(自、x、y、高度、宽度、颜色): self.pos=pygame.math.Vector2(x,y) self.color=颜色 self.rect=pygame.rect(x,y,高度,宽度) self.speed=pygame.math.Vector2(-5.0,0) 自重=0.5 自摩擦=0.99

<我正试图使白色矩形缓慢平滑地向红色矩形弯曲,然后停止,但由于某些原因,白色矩形没有正确弯曲,其移动方式也很快。我希望它平滑地移动,平滑地弯曲,以便眼睛可以看到,同时也可以向红色矩形弯曲。我有办法更改代码吗让它像那样工作? 在我的curvemove rect类中,我有一个
self.yspeed=0.05 self.xspeed=-0.5 self.gravity=-0.01矩形移动的x和y速度以及影响它的重力 然后我在curvemove课上重画 这就是我的rect是如何移动的如果按下V键,那么它应该mave self.move true,然后它应该运行使对象弯曲的代码

        if keys[pygame.K_v]:
            curve_move1.move = True
        if self.move:
            curve_move1.x += curve_move1.xspeed
            curve_move1.y += curve_move1.yspeed
            curve_move1.yspeed += curve_move1.gravity
            # curve it towards the red rectangle

        else:
            curve_move1.move2 = True
完整的代码,它的所有矩形,所以你可以测试出来

import pygame
pygame.init()


# our window
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("test map")


# our class
class curvemove:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
        self.yspeed = 0.05
        self.xspeed = -0.5
        self.gravity = -0.01
        self.move = False
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)
        # -------------- curve_move1 is our curving rectangle
        if keys[pygame.K_v]:
            curve_move1.move = True
        if self.move:
            curve_move1.x += curve_move1.xspeed
            curve_move1.y += curve_move1.yspeed
            curve_move1.yspeed += curve_move1.gravity
            # curve it towards the red rectangle

        else:
            curve_move1.move2 = True


white = 255,255,255
red = 205,0,10
curve_move1 = curvemove(250,400,50,50,white)

touched = curvemove(250,200,50,50,red)

# our game fps
fps = 60
clock = pygame.time.Clock()

# d redraw()
def redraw():
    window.fill((0,0,0))
    curve_move1.draw()
    touched.draw()

# our main loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if curve_move1.rect.colliderect(touched.rect):
        curve_move1.move = False
        

    redraw()
    pygame.display.update()
pygame.quit()




我想要一种方法,使这条矩形平滑曲线朝向红色矩形,不要太快,但正常这是我最好的尝试

重力必须作用在目标的方向上

我建议在计算中使用。计算从物体到目标的方向向量,并将其缩放到重力的大小。根据每个帧中的重力更改运动矢量:

dir\u vec=pygame.math.Vector2(target\u pos)-self.rect.center
v_len_sq=方向向量长度的平方()
如果v_len_sq>0:
方向向量比例到长度(自重力)
自速度=(自速度+方向向量)*自摩擦
self.pos+=自速度

最简单的例子:

导入pygame
pygame.init()
类曲线移动:
定义初始值(自、x、y、高度、宽度、颜色):
self.pos=pygame.math.Vector2(x,y)
self.color=颜色
self.rect=pygame.rect(x,y,高度,宽度)
self.speed=pygame.math.Vector2(-5.0,0)
自重=0.5
自摩擦=0.99
def牵引(自):
self.rect.center=(self.pos.x,self.pos.y)
pygame.draw.circle(窗口,self.color,(self.pos.x,self.pos.y),self.rect.width//2)
def更新(自我、目标位置):
dir\u vec=pygame.math.Vector2(target\u pos)-self.rect.center
v_len_sq=方向向量长度的平方()
如果v_len_sq>0:
方向向量比例到长度(自重力)
自速度=(自速度+方向向量)*自摩擦
self.pos+=自速度
window=pygame.display.set_模式((500500))
pygame.display.set_标题(“测试地图”)
clock=pygame.time.clock()
白色=255,255,255
红色=205,0,10
曲线移动1=曲线移动(250、400、20、20、白色)
触摸=曲线移动(250、200、20、20、红色)
fps=60
move=False
def redraw():
窗口填充((0,0,0))
曲线_move1.draw()
toucted.draw()
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_v:
move=True
如果event.key==pygame.K\r:
move=False
曲线移动1=曲线移动(250、400、20、20、白色)
如果(曲线移动1.pos-接触.pos).length()<10:
move=False
如果移动:
曲线移动1.更新(接触直线中心)
重画
pygame.display.update()
时钟滴答声(fps)
pygame.quit()
退出()
使用
速度
重力
摩擦力
的值来获得不同的效果

e、 g:

self.speed=pygame.math.Vector2(-10.0,0)
自重=1
自摩擦=0.95

e、 g:

self.speed=pygame.math.Vector2(-10.0,0)
自重=1
自摩擦=0.91

首先,您可以使用
print()
查看变量中的值以及执行代码的哪一部分。这叫做
print debuging
。一切正常。我只是想问我如何改进这一点,因为这是我第一次尝试这样做。你仍然可以从
print()开始
查看值是如何更改的。我试图阻止它在屏幕上滚动,我注意到如果我将此部分self.rect.center=(self.pos.x,self.pos.y)<更改为self.rect.center=(self.x,self.y),它可以工作,但用于旋转的代码不能工作*
import pygame
pygame.init()


# our window
window = pygame.display.set_mode((500,500))
pygame.display.set_caption("test map")


# our class
class curvemove:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
        self.yspeed = 0.05
        self.xspeed = -0.5
        self.gravity = -0.01
        self.move = False
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)
        # -------------- curve_move1 is our curving rectangle
        if keys[pygame.K_v]:
            curve_move1.move = True
        if self.move:
            curve_move1.x += curve_move1.xspeed
            curve_move1.y += curve_move1.yspeed
            curve_move1.yspeed += curve_move1.gravity
            # curve it towards the red rectangle

        else:
            curve_move1.move2 = True


white = 255,255,255
red = 205,0,10
curve_move1 = curvemove(250,400,50,50,white)

touched = curvemove(250,200,50,50,red)

# our game fps
fps = 60
clock = pygame.time.Clock()

# d redraw()
def redraw():
    window.fill((0,0,0))
    curve_move1.draw()
    touched.draw()

# our main loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if curve_move1.rect.colliderect(touched.rect):
        curve_move1.move = False
        

    redraw()
    pygame.display.update()
pygame.quit()