我已将代码简化为必需品,以便更容易理解。所有这一切都发生在点击某个区域时,一颗子弹射向该区域周围的白色方块。问题在于我不希望子弹击中被点击的区域,而是确切地点击它的位置。
这是我的代码:
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()
我真的需要帮助修改我的代码才能让子弹完全点击点击区域
答案 0 :(得分:0)
pygame.Rect
不能有浮点数,因为它们的x,y属性/坐标和pygame只是截断你分配给矩形的浮点数,所以方向变得不准确。
您需要将实际位置存储在单独的属性中(例如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。