在pygame中循环移动对象

时间:2018-07-31 19:44:00

标签: python pygame

您可以在此图片上看到

Picture有一个我正在控制的矩形(红色)和一个我未控制的绿色矩形。我希望这些绿色矩形不断地从左向右移动(每秒钟都向相反的方向移动)。我尝试这样做,但我只设法让他们走到一侧,然后停下来。我不知道如何让他们回到另一边,然后不断地这样做。这是if语句结束后的外观。

enter image description here

import pygame


pygame.init()
win = pygame.display.set_mode((1200, 600))
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)
GREEN = (13, 255, 0)

player = pygame.Rect(40, 45, 30, 30)
vel = 4


walls = [
pygame.Rect(0, 0, 1200, 20), pygame.Rect(0, 0, 20, 600),
pygame.Rect(0, 580, 1200, 20), pygame.Rect(1180, 0, 20, 600),
pygame.Rect(300, 0, 20, 530), pygame.Rect(20, 100, 230, 20),
pygame.Rect(70, 200, 230, 20), pygame.Rect(20, 300, 230, 20),
pygame.Rect(70, 400, 230, 20), pygame.Rect(600, 100, 20, 500),
]

movingobjectsleft = [
pygame.Rect(320, 120, 30, 30),
pygame.Rect(320, 240, 30, 30),
pygame.Rect(320, 360, 30, 30),
]

movingobjectsright = [
pygame.Rect(570, 180, 30, 30),
pygame.Rect(570, 300, 30, 30),
pygame.Rect(570, 420, 30, 30)
]

run = True
while run:
# Handle the events.
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

keys = pygame.key.get_pressed()

# Update the player coordinates.
if keys[pygame.K_LEFT] and player.x > 0:
    player.x -= vel
if keys[pygame.K_RIGHT] and player.x < 1200 - player.width:
    player.x += vel
if keys[pygame.K_UP] and player.y > 0:
    player.y -= vel
if keys[pygame.K_DOWN] and player.y < 600 - player.height:
    player.y += vel

# Game logic for walls and moving objects
for wall in walls:
    # Check if the player rect collides with a wall rect.
    if player.colliderect(wall):
        print("Game over")

for object in movingobjectsleft:
    if player.colliderect(object):
        print("Game over")
    if object.x < 570:
        object.x += vel


for object in movingobjectsright:
    if player.colliderect(object):
        print("Game over")
    if object.x > 320:
        object.x -= vel

# Draw everything.
win.fill(WHITE)
pygame.draw.rect(win, RED, player)
# Drawing walls and moving objects
for wall in walls:
    pygame.draw.rect(win, BLACK, wall)

for object in movingobjectsright:
    pygame.draw.rect(win, GREEN, object)

for object in movingobjectsleft:
    pygame.draw.rect(win, GREEN, object)

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

pygame.quit()

我认为这里需要做些事情

for object in movingobjectsleft:
    if player.colliderect(object):
        print("Game over")
    if object.x < 570:
        object.x += vel


for object in movingobjectsright:
    if player.colliderect(object):
        print("Game over")
    if object.x > 320:
        object.x -= vel

1 个答案:

答案 0 :(得分:1)

您可以使用额外的pygame.Rect属性和vel方法创建一个update子类,在其中您可以移动rect并进行边界检查(或创建一个具有{ {1}}和一个rect属性):

vel

在while循环中:

class MovingRect(pygame.Rect):

    def __init__(self, x, y, w, h, vel):
        # Call the __init__ method of the parent class.
        super().__init__(x, y, w, h)
        self.vel = vel

    def update(self):
        self.x += self.vel  # Move.
        if self.right > 600 or self.left < 320:  # If it's not in this area.
            self.vel = -self.vel  # Invert the direction.


vel_left = 4
vel_right = -4

movingrects = [
    MovingRect(320, 120, 30, 30, vel_left),
    MovingRect(320, 240, 30, 30, vel_left),
    MovingRect(320, 360, 30, 30, vel_left),
    MovingRect(570, 180, 30, 30, vel_right),
    MovingRect(570, 300, 30, 30, vel_right),
    MovingRect(570, 420, 30, 30, vel_right),
]

如果您需要计时器,请查看以下答案:Countdown timer in Pygame


如果您还不知道类如何工作,则可以定义一个布尔标志(for movingrect in movingrects: movingrect.update() # Movement and bounds checking. if player.colliderect(movingrect): print("Game over") # Draw everything. # ... for movingrect in movingrects: pygame.draw.rect(win, GREEN, movingrect) ),并在其中一个rect离开指定区域时将其设置为invert_direction。如果是True循环后的True,请反转for / vel_left变量。

vel_right