python - 无法在 pygame 中正确执行射弹操作

标签 python python-3.x pygame

尝试使用 pygame 启用射击弹丸。在我按下空格键后,它只发射 1 个射弹,fps 从 100 下降到 ~30,玩家角色无法移动并且对按下按钮没有反应。抛射物停止动画后,fps 返回到 100,我可以移动和射击最多 3 个抛射物(按计划),但 fps 再次下降,直到最后一个抛射物到达边界我才能移动。

我加载了一个子弹图像而不是之前绘制的子弹,并认为里面有箱子。但结果并没有改变。我还尝试重新排序 blit() 函数,但也没有任何改变。

class Game():
    # Some game stuff and functions
    def __init__(self):
        self.x_default = 800
        self.y_default = 600
        self.screen = pygame.display.set_mode((self.x_default, self.y_default))
        #self.background = pygame.image.load('IMGs/BCK2.bmp').convert()         
        self.title = 'Title'
        self.fps = 100

class Projectile():
    # This class is supposed to make projectiles
    def __init__(self, x, y, speed_x=5, direction=1, width=10, height=10):
#       x&y - starting positions of projectiles
#       speed_x & speed_y - starting moving speeds of projectiles in # oX & oY
#       a - speed increase over time (cancelled for now)
#       direction - stays for its' name
#       Directions: [2, 3, 1, 4, 5, 6, -1, 7] starting from 0:00 and  moving towards the right side of the clock

        self.x = x
        self.y = y

        self.speed_x = speed_x

        self.direction = direction

        self.width = width
        self.height = height
        self.img = pygame.Rect(self.x, self.y, self.width, self.height)

    def move_projectile(self, x, speed_x, direction):

#       Moves stuff
#       For now direction works only in right-left (1 for right, -1 for left)

        x += speed_x * direction
        return x

while 1:
    if pygame.event.poll().type == pygame.QUIT:
        sys.exit()

    for pr in projectiles:
        if (pr.x >= -pr.width) and (pr.x <= Game().x_default):
            pr.x = pr.move_projectile(x=pr.x, speed_x=pr.speed_x, direction=pr.direction)
        else:
            projectiles.remove(pr)

    keys = pygame.key.get_pressed()

# ------------ oX moving --------------

    if keys[pygame.K_LEFT]:
        P.x -= P.speed
        P.direction = -1

    if keys[pygame.K_RIGHT]:
        P.x += P.speed
        P.direction = 1

# ------------ Shooting ----------------

    if keys[pygame.K_SPACE]:
        if len(projectiles) < 3:
            if P.direction > 0:
                projectiles.append(Projectile(x=P.x + P.width, y=P.y + P.height//2, direction=1))
            else:
                projectiles.append(Projectile(x=P.x, y=P.y + P.height//2, direction=-1))

# ---------- The blitting process --------------

    G.screen.blit(G.background, (0, 0))
    G.screen.blit(Block1.img, (Block1.x, Block1.y))
    G.screen.blit(Block2.img, (Block2.x, Block2.y))
    for pr in projectiles:
        pygame.draw.rect(G.screen, (0, 0, 0), pr.img)
    G.screen.blit(P.img, (P.x, P.y))
    pygame.display.update()
    pygame.time.delay(1000//G.fps)

最佳答案

参见 How can i shoot a bullet with space bar?How do I stop more than 1 bullet firing at once?

keys[pygame.K_SPACE]True 只要 SPACE 被按住。这意味着只要按住该键,就会连续生成多个子弹。

如果你想创建一个项目符号,当 SPACE 被按下时,然后使用 KEYDOWN 事件(见 pygame.event )。
该事件仅发生一次,即每次按下该键时。例如:

while 1:
    
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            sys.exit()
        
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:

                if len(projectiles) < 3:
                    if P.direction > 0:
                        projectiles.append(Projectile(x=P.x + P.width, y=P.y + P.height//2, direction=1))
                    else:
                        projectiles.append(Projectile(x=P.x, y=P.y + P.height//2, direction=-1))

    keys = pygame.key.get_pressed()

    # [...]

关于python - 无法在 pygame 中正确执行射弹操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57342088/

相关文章:

Python 在解析表时处理 NoneType

Python:如何为多人游戏序列化对象?

python - __new__ = 类型错误 : object() takes no parameters

python - 如何删除字符串中除所选值之外的所有值

python - 为什么我不能 "deactivate"pyenv/virtualenv?如何安装 "fix"

Python 按键监听器 - Raspberry Pi

python - 从 "graph"(数据库)数据递归创建字典

python - 使用 swig 将不透明类型公开给 python

python - 在 PostgreSQL 上使用 SQLAlchemy 创建全文搜索索引

multithreading - 试图弄清楚如何跟踪Pygame事件和组织游戏功能