阻止rect移动moveip pygam

2024-04-19 15:30:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Pygame中的问题是我不明白如何在我的游戏循环中移动ip(+2.0)之后阻止Rect移动。我需要矩形在屏幕上的某个点停止。我试图通过在Rect下面放一个if语句来停止Rect;if time\u bar==pygame.rect文件(02000100,80):时间_吧台移动\u 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()

Tags: textrectipeventiftimedisplaybar
1条回答
网友
1楼 · 发布于 2024-04-19 15:30:11

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

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)

或者(或者另外),当矩形到达该位置时,可以将其位置设置为0:

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

相关问题 更多 >