停止rect从移动moveip pygame

时间:2018-06-21 03:41:24

标签: python pygame

我在Pygame中的问题是我不理解如何在我的game_loop中的move_ip(+2.0)之后阻止Rect移动。我需要矩形停止在屏幕上的某个点。我试图通过在其下面放置一个if语句来停止Rect;如果time_bar == pygame.rect(0,200,100,80):time_bar.move_ip(0,0)。但这没用。请帮忙!

import pygame

pygame.init()

display_height = 900
display_width = 600

black = (0,0,0)
white = (255,255,255)
orange = (255, 153, 0)
red = (255,0,0)

gamedisplay = pygame.display.set_mode((display_height,display_width))
pygame.display.set_caption('ye boy')
clock = pygame.time.Clock()
gamedisplay.fill(white)
pygame.display.flip()
time_bar = pygame.Rect(900,200,100,80)

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def game_loop():
  font = pygame.font.Font("freesansbold.ttf", 90)
  clock = pygame.time.Clock()
  text = ''
  intro = True

  while intro:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return

    # drawing the input box
    gamedisplay.fill(white)

    # time bar and its function
    pygame.draw.rect(gamedisplay, red, time_bar)
    time_bar.move_ip(-2,0)

    pygame.display.update()
    clock.tick(60)

game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

您可以定义一个speed_x变量并将其传递给move_ip。当x坐标小于0时,将speed_x设置为0以停止矩形。

speed_x = -2

while intro:
    # ...
    # Set the speed to 0 if the x-coordinate is <= 0.
    if time_bar.x <= 0:
        speed_x = 0

    time_bar.move_ip(speed_x, 0)

或者(或另外),您可以将rect的位置设置为0:

if time_bar.x <= 0:
    time_bar.x = 0