Math Pygame游戏中的精确射击

Math Pygame游戏中的精确射击,math,vector,pygame,python-3.5,Math,Vector,Pygame,Python 3.5,我已经简化了我的代码,使其更容易理解。所有发生的事情都是当某个区域被点击时,一颗子弹从一个白色的正方形射向该区域周围。问题是,我不希望子弹击中单击的区域周围,而是准确地击中单击的位置 这是我的密码: import pygame, sys, time, random, math from pygame.locals import * background = (0, 0, 0) entity_color = (255, 255, 255,255) listLaser = pygame.sprit

我已经简化了我的代码,使其更容易理解。所有发生的事情都是当某个区域被点击时,一颗子弹从一个白色的正方形射向该区域周围。问题是,我不希望子弹击中单击的区域周围,而是准确地击中单击的位置

这是我的密码:

import pygame, sys, time, random, math
from pygame.locals import *

background = (0, 0, 0)
entity_color = (255, 255, 255,255)
listLaser = pygame.sprite.Group()
player_x, player_y = 0, 0
move_player_x, move_player_y = 0, 0

all_sprites_list = pygame.sprite.Group()

class Entity(pygame.sprite.Sprite):
    """Inherited by any object in the game."""

    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)

        self.x = x
        self.y = y
        self.width = width
        self.height = height

        # This makes a rectangle around the entity, used for anything
        # from collision to moving around.
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

class User(Entity):
    """
    Player controlled or AI controlled, main interaction with
    the game
    """

    def __init__(self, x, y, width, height):
        super(User, self).__init__(x, y, width, height)

        self.image = pygame.Surface([20, 37])
        self.image.fill(entity_color)
        self.image.blit(self.image, (0, 0))

class Player(User):
    """The player controlled Character"""

    def __init__(self, x, y, width, height):
        super(Player, self).__init__(x, y, width, height)

        pass

class Bullet(pygame.sprite.Sprite):

    def __init__(self, mouse, player):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([4, 10])
        self.image.fill(entity_color)

        self.mouse_x, self.mouse_y = mouse[0], mouse[1]
        self.player = player

        self.rect = self.image.get_rect()
    def update(self):
        '''
        Gets vector from the two points and gets a direction and sends the bullet that way using player and clicked points
        '''
        speed = -4.
        range = 200
        distance = [self.mouse_x - self.player[0], self.mouse_y - self.player[1]]
        norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
        direction = [distance[0] / norm, distance[1 ] / norm]
        bullet_vector = [direction[0] * speed, direction[1] * speed]


        self.rect.x -= bullet_vector[0]
        self.rect.y -= bullet_vector[1]

pygame.init()

pygame.display.set_caption('Race')

window_width = 800
window_height = 700

screen = pygame.display.set_mode((window_width, window_height))

player = Player(20, window_height / 2, 40, 37)

all_sprites_list.add(player)

while True:  # the main game loop

    for event in pygame.event.get():

        if event.type == MOUSEBUTTONDOWN:
            bullet = Bullet(pygame.mouse.get_pos(), [player.rect.x, player.rect.y])

            bullet.rect.x = player.rect.x
            bullet.rect.y = player.rect.y

            all_sprites_list.add(bullet)
            listLaser.add(bullet)
    for ent in all_sprites_list:
        ent.update()

    screen.fill(background)
    all_sprites_list.draw(screen)

    pygame.display.flip()
    pygame.display.update()

我真的需要帮助修改我的代码,使子弹准确地击中单击区域。Rect不能有浮点数作为其x、y属性/坐标,pygame只是截断您指定给Rect的浮点,因此方向变得不准确

您需要将实际位置存储在单独的属性中(例如
self.posx
),然后首先更新这些属性,然后更新
self.rect

class Bullet(pygame.sprite.Sprite):

    def __init__(self, mouse, player):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([4, 10])
        self.image.fill(entity_color)

        self.mouse_x, self.mouse_y = mouse[0], mouse[1]
        self.player = player
        self.posx = self.player[0]
        self.posy = self.player[1]

        self.rect = self.image.get_rect()
        speed = -4.
        distance = [self.mouse_x - self.player[0], self.mouse_y - self.player[1]]
        norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
        direction = [distance[0] / norm, distance[1 ] / norm]
        self.bullet_vector = [direction[0] * speed, direction[1] * speed]

    def update(self):
        self.posx -= self.bullet_vector[0]
        self.posy -= self.bullet_vector[1]
        self.rect.topleft = self.posx, self.posy

你也不需要每一帧都计算子弹的矢量,除非子弹能够改变方向(就像一枚寻的导弹)

pygame.Rect不能将浮点数作为其x、y属性/坐标,pygame只会截断指定给Rect的浮点数,因此方向变得不准确

您需要将实际位置存储在单独的属性中(例如
self.posx
),然后首先更新这些属性,然后更新
self.rect

class Bullet(pygame.sprite.Sprite):

    def __init__(self, mouse, player):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([4, 10])
        self.image.fill(entity_color)

        self.mouse_x, self.mouse_y = mouse[0], mouse[1]
        self.player = player
        self.posx = self.player[0]
        self.posy = self.player[1]

        self.rect = self.image.get_rect()
        speed = -4.
        distance = [self.mouse_x - self.player[0], self.mouse_y - self.player[1]]
        norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
        direction = [distance[0] / norm, distance[1 ] / norm]
        self.bullet_vector = [direction[0] * speed, direction[1] * speed]

    def update(self):
        self.posx -= self.bullet_vector[0]
        self.posy -= self.bullet_vector[1]
        self.rect.topleft = self.posx, self.posy

你也不需要每一帧都计算子弹的矢量,除非子弹能够改变方向(就像一枚寻的导弹)

看来方向算对了。怎么了?看来方向算对了。发生了什么?