我正在为一个项目制作一个游戏,但我的学习还不够,即使是一个简单的游戏,所以即使这里有类似的问题,我不确定如何改变它,以适应我的游戏。你知道吗
我确实设法制造了子弹,但它只能向上射击,而我需要它向老鼠所在的任何地方射击。你知道吗
我试图按照这里的一些答案,但它会出现错误消息,如果我试图编辑这些,甚至更多的错误消息弹出,我真的不知道为什么。你知道吗
到目前为止,我的情况是:
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.
我痛苦地意识到,这个游戏是高度缺陷,我会感谢任何帮助,我可以得到,即使它与这个问题无关。(例如,子弹与敌人的碰撞检测。碰撞检测的球员和敌人),但目前,这个问题是我最关心的。非常感谢你的帮助!你知道吗
您可以在事件中获得鼠标位置pygame.鼠标按钮. 你知道吗
你也可能希望子弹沿着你射击的方向。你知道吗
并沿着方向移动。你知道吗
这是代码的一部分,用于控制您询问的特性(球移动的位置),但我想您知道这一点是因为您编写了它。你知道吗
好吧,那是给你编的程序。继续修理。你知道吗
顺便说一下,我强烈建议安装一个过梁。在您的代码上运行
pylint
会给我所有这些,这似乎是您要求我们提供的反馈:另外,让我们谈谈这个:
成百上千行。你只要给电脑编程,就可以为你生成所有这些职位。但即使你不想那样做,也不要这样重复你自己。你知道吗
这是43行到144行完全相同的操作—如果您控制代码的绝对数量,那么阅读和编辑就容易多了。你知道吗
所以,看看它,当
MOUSEBUTTONDOWN
,你在播放器的位置创建一个新的Bullet()。然后,当你更新子弹的时候。将其y
坐标上移3个单位。你知道吗我建议给每个
bullet
一个velocity
和target
属性。 这将允许你“发射并忘记”每一颗子弹,而不必担心 每次手动更新。你知道吗相关问题 更多 >
编程相关推荐