我正在为一个项目制作游戏,但是我几乎没有被教导甚至不能制作一个简单的游戏,因此即使这里有类似的问题,我也不确定如何进行更改以适合我的游戏。
我确实设法制造了子弹,但它只能向上射击,而我需要它可以在鼠标所在的任何地方射击。
我尝试按照此处的一些答案进行操作,但它会显示错误消息,如果我尝试编辑这些错误消息,则会弹出更多错误消息,我真的不知道为什么。
这是我到目前为止所拥有的:
Code is removed for now. Will re-upload in 1 to 2 months.
run = True
while run:
[...]
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprite_list.add(bullet)
这是我的整个代码(如果有帮助的话):
Code is removed for now. Will re-upload in 1 to 2 months.
我非常痛苦地意识到该游戏存在严重缺陷,即使与该问题无关,我也将不胜感激。 (例如,子弹与敌人的碰撞检测。玩家与敌人的碰撞检测)但是目前,这个问题是我最大的担忧。非常感谢您的帮助!
答案 0 :(得分:3)
您可以在事件pygame.MOUSEBUTTONDOWN中获取鼠标位置。
if event.type == pygame.MOUSEBUTTONDOWN:
aim_pos = event.pos
您可能还希望子弹遵循射击的方向。
player_position = player.rect.center
# Assume you have got where you're aiming by aim_pos.
bullet_vec = pygame.math.Vector2(aim_pos[0] - player_position[0],
aim_pos[1] - player_position[1]).normalize() * 10 #move speed
bullet = Bullet()
bullet.rect.center = player.rect.center
bullet.vec = bullet_vec
all_sprite_list.add(bullet)
并按照指示移动。
class Bullet(pygame.sprite.Sprite):
....your codes
def update(self):
self.rect.move_ip(self.vec.x, self.vec.y)
答案 1 :(得分:1)
def update(self):
self.rect.y -= 3
这是控制您所询问的功能(球移动的位置)的代码部分,但是我想您知道这是因为您编写了它。
它将显示错误消息,如果我尝试编辑这些错误消息,则会弹出更多错误消息,我真的不知道为什么。
嗯,这就是为您编程。继续修复它们。
我强烈建议您安装皮棉机。在您的代码上运行pylint
可以为我提供所有这些,这似乎是您要求我们提供的反馈:
$ pylint --errors-only your-game.py
************* Module game
your-game.py:8:0: E1101: Module 'pygame' has no 'init' member (no-member)
your-game.py:30:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:45:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:93:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:130:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:299:25: E1101: Module 'pygame' has no 'QUIT' member (no-member)
your-game.py:301:25: E1101: Module 'pygame' has no 'MOUSEBUTTONDOWN' member (no-member)
your-game.py:336:25: E1101: Module 'pygame' has no 'QUIT' member (no-member)
your-game.py:339:27: E1101: Module 'pygame' has no 'KEYDOWN' member (no-member)
your-game.py:340:28: E1101: Module 'pygame' has no 'K_LEFT' member (no-member)
your-game.py:342:30: E1101: Module 'pygame' has no 'K_RIGHT' member (no-member)
your-game.py:344:30: E1101: Module 'pygame' has no 'K_UP' member (no-member)
your-game.py:346:30: E1101: Module 'pygame' has no 'K_DOWN' member (no-member)
your-game.py:349:27: E1101: Module 'pygame' has no 'KEYUP' member (no-member)
your-game.py:350:28: E1101: Module 'pygame' has no 'K_LEFT' member (no-member)
your-game.py:352:30: E1101: Module 'pygame' has no 'K_RIGHT' member (no-member)
your-game.py:354:30: E1101: Module 'pygame' has no 'K_UP' member (no-member)
your-game.py:356:30: E1101: Module 'pygame' has no 'K_DOWN' member (no-member)
your-game.py:359:27: E1101: Module 'pygame' has no 'MOUSEBUTTONDOWN' member (no-member)
your-game.py:376:0: E1101: Module 'pygame' has no 'quit' member (no-member)
另外,让我们谈谈这一点:
wall = Wall(0, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)
用于数百行。您可以对计算机进行编程以为您生成所有这些位置。但是,即使您不想这样做,也不要这样重复自己。
walls = (
(0, 0, 10, 800),
(40, 40, 10, 75),
(50, 40, 190, 10),
# ...
)
for wall_coords in walls:
wall = Wall(*wall_coords)
wall_list.add(wall)
all_sprite_list.add(wall)
与144个功能完全相同的是43行-如果您控制大量代码,则易于阅读和编辑。
答案 2 :(得分:0)
因此,看着它,当MOUSEBUTTONDOWN
时,您将在播放器位置创建一个新的Bullet()。然后,当您更新项目符号时。您可以将其y
坐标向上移动3个单位。
我建议给每个bullet
一个velocity
和target
属性。
这样您就可以“发射并忘记”每颗子弹,而不必担心
每次手动更新。
def __init__(self, velocity: float, target: tuple or list):
# other stuff
self.velocity = velocity
self.target = target
def update(self):
# get the angle towards the target
x_delta = self.target[0] - self.rect.x
y_delta = self.target[1] - self.rect.y
rotation = -math.atan2(y_delta, x_delta)
# move towards the target
self.rect.x += math.cos(rotation) * self.velocity
self.rect.y -= math.sin(rotation) * self.velocity