为什么子弹不会朝着玩家面对的方向发射并在

为什么子弹不会朝着玩家面对的方向发射并在

本文介绍了为什么子弹不会朝着玩家面对的方向发射并在 PyGame 中保持可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PyGame 在 Python 中制作自上而下的射击游戏,我希望根据玩家的方向将子弹绘制到屏幕上.我可以让子弹画出来,但只有当玩家在那个特定的方向时(有一个方向变量,但子弹只在变量与子弹射中的状态匹配时才会显示).

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).

这是我认为的相关代码

    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.

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.

推荐答案

我建议重新设计项目符号,使它们包含一个 rect 样式列表(或者更好的 pygame.Rect) 由位置及其大小和另一个列表组成,它们的 velocity 矢量.现在要更新子弹的位置,您只需将速度添加到位置即可.

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.

这是带有一些注释的更新代码.我强烈建议使用 pygame.Rects 而不是列表,因为它们对于碰撞检测非常有用.

Here's the updated code with some comments. I highly recommend to use pygame.Rects 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()

这篇关于为什么子弹不会朝着玩家面对的方向发射并在 PyGame 中保持可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:59