首页 > 解决方案 > 视差滚动弄乱了射弹的位置

问题描述

我每 3 秒将 lazers 附加到一个列表中,以便绘制它。

    if event.type == pygame.USEREVENT:
            timer += 1
            print(timer)
            if timer%3 == 0:
                for enemy in enemies:
                    lazers.append(Projectile(enemy.x-enemy.width//3, enemy.y-enemy.height//2, scroll))

在 pygame 事件循环之外,我绘制了所有的 lazers:

for lazer in lazers:
        lazer.draw_rect(screen)
        lazer.x += lazer.velocity

projectile 类很简单,它只在提供的 x,y 坐标处绘制矩形:

class Projectile():
    def __init__(self, x, y, scroll, color=(255,0,0)):
        self.scroll = scroll
        self.x = x-self.scroll[0]
        self.y = y-self.scroll[1]
        self.color = color
        self.velocity = -4
        self.width = 20
        self.height = 5
        
        
    def draw_rect(self, screen):
        pygame.draw.rect(screen, self.color, pygame.Rect(self.x, self.y, self.width, self.height))

它按照我的编程方式工作。在 x 和 y 处绘制矩形,但在屏幕向左、向右或向上滚动时无法正确绘制。我能想到的唯一解决方案是找到某种方法将屏幕上的激光绘制到敌人的 x 和 y 位置,包括滚动偏移。

问题是什么样的

标签: pythonpygamepygame-surface

解决方案


推荐阅读