gpt4 book ai didi

python - 在pygame中循环移动对象

转载 作者:行者123 更新时间:2023-12-01 09:11:32 26 4
gpt4 key购买 nike

正如您在这张图片中看到的,

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

最佳答案

您可以创建一个pygame.Rect具有附加 vel 的子类属性和 update移动矩形并进行边界检查的方法(或创建一个具有 rectvel 属性的类):

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),
]

在 while 循环中:

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)

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

<小时/>

如果您还不知道类是如何工作的,您可以定义一个 bool 标志( invert_direction )并将其设置为 True当其中一个矩形离开指定区域时。如果是True for之后循环,反转vel_left/vel_right变量。

vel_left = 4
vel_right = -4

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.left > 0:
player.x -= vel
if keys[pygame.K_RIGHT] and player.right < 1200:
player.x += vel
if keys[pygame.K_UP] and player.top > 0:
player.y -= vel
if keys[pygame.K_DOWN] and player.bottom < 600:
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")

# Set this variable to True if the direction should be inverted.
invert_left = False
for object in movingobjectsleft:
if player.colliderect(object):
print("Game over")

object.x += vel_left # Move the object.

# Check if the object has left the area.
if object.right > 600 or object.left < 320:
# Invert the direction after the loop.
invert_left = True

if invert_left: # Invert the direction.
vel_left = -vel_left

# Now do the same for the right objects.
invert_right = False
for object in movingobjectsright:
if player.colliderect(object):
print("Game over")

object.x += vel_right

if object.right > 600 or object.left < 320:
invert_right = True

if invert_right:
vel_right = -vel_right

关于python - 在pygame中循环移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51620956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com