【问题标题】:PyGame Rect Updates But Won't Move In My while Loop? [duplicate]PyGame Rect 更新但不会在我的 while 循环中移动? [复制]
【发布时间】:2021-03-04 22:58:58
【问题描述】:

我想模拟 Spring Physics 但我不知道如何使用 PyGame 来更新 Rect。

但是当我运行我的代码时,矩形不会移动,有人可以帮我吗??? 这是我的代码:

maxmag = 100
mag = 100

screen.fill((255, 255, 255))
ball = pygame.draw.circle(screen, (0, 0, 255), (250, 250 + mag), 75)
cube = pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(250,0,10,250 + mag))

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    mag -= 1
    if mag <= -maxmag:
        maxmag -= 10
        mag = maxmag

    cube = pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(250,0,10,250 + mag))
    cube.update(pygame.Rect(250,0,10,250 + mag))
    #cube = pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(250,0,10,250 + mag))

    print(mag)

    clock.tick(60)

    pygame.display.flip()

pygame.quit()

感谢您的阅读:D

【问题讨论】:

    标签: python pygame move rect


    【解决方案1】:

    这就是动画在 pygame 中的工作方式:

    import pygame
    
    pygame.init ()
    screen = pygame.display.set_mode ((600, 400))
    clock = pygame.time.Clock ()
    
    x_step = 2
    y_step = 2
    x = y = 100
    running = True
    while running :
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        screen.fill ((255, 255, 255))
        pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(x,y,50,50))
        x += x_step
        if x > 550  or x < 0 : x_step = -x_step
        y += y_step
        if y > 350 or y < 0 : y_step = -y_step
        pygame.display.flip()
        clock.tick (70)
    
    pygame.quit()
    

    【讨论】:

    • 但我说的是弹簧物理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    相关资源
    最近更新 更多