为什么子弹不会向玩家面向的方向射击并在PyGame中保持可见?

时间:2017-08-13 23:34:24

标签: python pygame 2d-games

我试图使用PyGame在Python中制作一个自上而下的射手,我希望根据玩家的方向将子弹绘制到屏幕上。我可以得到子弹来绘制,但只有当玩家处于特定方向时(有一个方向变量,但子弹仅在变量与子弹射入的状态匹配时显示)。

以下是我认为的相关代码

    global direction
    global bullets
    global bullet_speed
    direction = None
    bullets = []
    bullet_speed = []

    player_x = 100
    player_y = 100

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_LEFT:
              direction = 'left'
           if event.key == pygame.K_SPACE:
              bullets.append([player_x,player_y])

    gameDisplay.fill(BLACK)

    for draw_bullets in bullets:
        if direction = 'up':
           pygame.draw.rect(gameDisplay,WHITE,(draw_bullet[0] + (player_x / 2), draw_bullet[1] + 5, bullet_width, bullet_height))
           bullet_speed.append(speed)
        if direction = 'down':
           pygame.draw.rect(gameDisplay,WHITE,(draw_bullet[0] + (player_x / 2),draw_bullet[1] + (player_height + 5), bullet_width, bullet_height))

    pygame.display.update()

我不想发射鼠标指向的子弹,这是所有其他问题都有答案的。我只是希望每个子弹按照玩家指向的方向(向上,向下,向左,向右)射击,并继续沿着它开始的任何方向前进。如果有人能帮助我,我真的很感激。我也没有使用OOP。

1 个答案:

答案 0 :(得分:0)

我建议重新设计项目符号,以便它们包含一个矩形样式列表(或更好的pygame.Rect),其中包含位置及其大小和另一个列表,velocity vector 。现在要更新子弹的位置,你只需要将速度添加到位置。

这里有更新的代码和一些评论。我强烈建议使用pygame.Rect而不是列表,因为它们对于碰撞检测非常有用。

#!/usr/bin/env python3

import pygame as pg
from pygame.math import Vector2


pg.init()

BACKGROUND_COLOR = pg.Color(30, 30, 30)
BLUE = pg.Color('dodgerblue1')
ORANGE = pg.Color('sienna1')


def game_loop():
    screen = pg.display.set_mode((800, 600))
    screen_rect = screen.get_rect()
    clock = pg.time.Clock()
    # Use a pg.Rect for the player (helps with collision detection):
    player = pg.Rect(50, 50, 30, 50)
    player_velocity = Vector2(0, 0)

    bullets = []
    bullet_speed = 7
    bullet_velocity = Vector2(bullet_speed, 0)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_LEFT:
                    player_velocity.x = -5
                    bullet_velocity = Vector2(-bullet_speed, 0)
                elif event.key == pg.K_RIGHT:
                    player_velocity.x = 5
                    bullet_velocity = Vector2(bullet_speed, 0)
                elif event.key == pg.K_UP:
                    player_velocity.y = -5
                    bullet_velocity = Vector2(0, -bullet_speed)
                elif event.key == pg.K_DOWN:
                    player_velocity.y = 5
                    bullet_velocity = Vector2(0, bullet_speed)
                elif event.key == pg.K_SPACE:
                    # A bullet is a list consisting of a rect and the
                    # velocity vector. The rect contains the position
                    # and size and the velocity vector is the
                    # x- and y-speed of the bullet.
                    bullet_rect = pg.Rect(0, 0, 10, 10)
                    bullet_rect.center = player.center
                    bullets.append([bullet_rect, bullet_velocity])
            if event.type == pg.KEYUP:
                if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                    player_velocity.x = 0
                elif event.key == pg.K_UP or event.key == pg.K_DOWN:
                    player_velocity.y = 0

        # Move the player.
        player.x += player_velocity.x
        player.y += player_velocity.y

        # To move the bullets, add their velocities to their positions.
        for bullet in bullets:
            bullet[0].x += bullet[1].x  # x-coord += x-velocity
            bullet[0].y += bullet[1].y  # y-coord += y-velocity

        # Stop the player at the borders of the screen.
        player.clamp_ip(screen_rect)

        screen.fill(BACKGROUND_COLOR)
        pg.draw.rect(screen, BLUE, player)
        # Draw the bullets.
        for bullet in bullets:
            pg.draw.rect(screen, ORANGE, bullet[0])

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

game_loop()
pg.quit()