【问题标题】:Pygame accurate shooting in gamePygame在游戏中精准射击
【发布时间】:2018-04-03 11:54:33
【问题描述】:

我已将我的代码简化为基本必需品,以使其更易于理解。所发生的一切都是当单击某个区域时,一颗子弹从一个白色方块向该区域周围发射。问题是我不希望子弹击中点击的区域,而是点击它的确切位置。

这是我的代码:

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

我真的需要帮助修改我的代码以使子弹准确地击中点击区域

【问题讨论】:

  • 似乎方向计算正确。怎么了?

标签: math vector pygame python-3.5


【解决方案1】:

pygame.Rects 不能将浮点数作为其 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

您也不需要每帧计算 bullet_vector,除非子弹应该能够改变它们的方向(如寻的导弹)。

【讨论】:

    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多