问题
I'm trying to make a top down shooter in Python using PyGame and I want the bullets to be drawn to the screen based on the player's orientation. I can get the bullets to draw but only when the player is in that specific direction (There is a direction variable but the bullets only show when the variable matches the state that the bullet was shot in).
Here is what I believe to be the relevant code
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()
I don't want to fire the bullet where the mouse is pointing which is what all other questions have an answer for. I just want each bullet to fire in the direction that the player was pointing (either up, down, left, right) and keep going in whatever direction it started off it. If anyone could help me out I'd really appreciate it. I'm also not using OOP.
回答1:
I suggest to redesign the bullets, so that they contain a rect style list (or better a pygame.Rect) consisting of the position and their size and another list, their velocity
vector. Now to update the positions of the bullets you just have to add the velocity to the position.
Here's the updated code with some comments. I highly recommend to use pygame.Rect
s instead of lists, since they're very useful for collision detection.
#!/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()
来源:https://stackoverflow.com/questions/45665784/why-wont-the-bullets-fire-in-the-direction-the-player-is-facing-and-stay-visibl