【问题标题】:Pygame image rectangle collides with everything except for the left wallPygame图像矩形与除了左墙之外的所有东西碰撞
【发布时间】:2020-05-03 23:07:55
【问题描述】:

试图为我的高中课程制作一个开始屏幕。图像应该在屏幕上移动,与每面墙碰撞并朝下一个方向移动。由于某种原因,图像不会与左墙发生碰撞,我不知道为什么。

class StartScreen:
    def __init__(self):
        screen.fill(WHITE)
        self.fontTitle = pygame.font.SysFont("arial", 30) #
        self.textTitle = self.fontTitle.render("Legend Of Zelda: ", True, WHITE) 
        self.textRect = self.textTitle.get_rect(center=(displayWidth / 2, 50)) 
        self.moveImage = pygame.image.load("images/link_down1.png")
        self.moveImageRect = self.moveImage.get_rect()
        self.dx = 3
        self.dy = 0
        self.moveImageRect.x = 1
        self.moveImageRect.y = 11
    def update(self):
        if self.moveImageRect.right >= displayWidth:
            self.dy = 3
            self.dx = 0
        if self.moveImageRect.left <= 0:
            self.dy = -1
            self.dx = 0
        if self.moveImageRect.top <= 0:
            self.dy = 0
            self.dx = -3
        elif self.moveImageRect.bottom >= displayHeight:
            self.dx = -3
            self.dy = 0
        self.moveImageRect.move_ip(self.dx, self.dy)

    def draw(self, surf):
        screen.blit(backgroundImage, (backgroundRect)) 
        screen.blit(self.textTitle, self.textRect) 
        screen.blit(backgroundImage, (backgroundRect)) 
        screen.blit(self.moveImage, self.moveImageRect)

【问题讨论】:

  • 嗯,唯一的区别是,对于左侧,self.dy = -1 而不是 3。您还可以将 dy 更改为向上、向左和向右,但将 dx 更改为向下。您是否想追求弹跳 dvd 播放器效果?
  • 不要试图在屏幕上顺时针移动它。

标签: python pygame collision-detection


【解决方案1】:
if self.moveImageRect.right > 500:
    dy = 1
    dx = 0
    self.moveImageRect.right = 500
elif self.moveImageRect.bottom > 500:
    dy = 0
    dx = -1
    self.moveImageRect.bottom = 500
elif self.moveImageRect.left < 0:
    dy = -1
    dx = 0
    self.moveImageRect.left = 0
elif self.moveImageRect.top < 0:
    dy = 0
    dx = 1
    self.moveImageRect.top = 0

这对我有用,只检查图像是否移出屏幕然后将其移回屏幕上,以确保它不会与墙壁发生碰撞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多