Python速成课程-外星人入侵-increase_speed()不起作用
我正在做外星人入侵。在升级一章中,我对increase_speed()方法有疑问。它什么都不做,所以在新一波外星人到来后游戏不会进展得更快。
这是我在 settings.py 中的代码:
def initialize_dynamic_settings(self):
self.velocity = 2.0
self.projectile_speed = 3.0
self.alien_speed = 1.0
# 1 = droite, -1 = gauche
self.fleet_direction = 1
def increase_speed(self):
self.velocity *= self.speed_scale
self.alien_speed *= self.speed_scale
self.projectile_speed *= self.speed_scale
在 main.py 中,
def check_collisions_bullet_alien(self):
# Gestion des collisions entre le groupe projectiles et le groupe aliens, résultant du kill des deux entités
collision = pygame.sprite.groupcollide(self.projectiles_gp, self.alien.aliens_gp, True, True)
if collision :
self.stats.score += self.settings.alien_points
if not self.alien.aliens_gp:
self.projectiles_gp.empty()
self.alien.create_fleet()
self.settings.increase_speed()
self.stats.score += self.settings.all_aliens_points
我在网络上检查了游戏的源代码,但我不明白我在哪里犯了错误。
感谢您的帮助 ;)
I'm doing Alien invasion. At the chapter Leveling up, i've an issue with the method increase_speed(). It does nothing, so the game won't go faster after a new wave of aliens arrives.
Here is my code in settings.py:
def initialize_dynamic_settings(self):
self.velocity = 2.0
self.projectile_speed = 3.0
self.alien_speed = 1.0
# 1 = droite, -1 = gauche
self.fleet_direction = 1
def increase_speed(self):
self.velocity *= self.speed_scale
self.alien_speed *= self.speed_scale
self.projectile_speed *= self.speed_scale
And in main.py
def check_collisions_bullet_alien(self):
# Gestion des collisions entre le groupe projectiles et le groupe aliens, résultant du kill des deux entités
collision = pygame.sprite.groupcollide(self.projectiles_gp, self.alien.aliens_gp, True, True)
if collision :
self.stats.score += self.settings.alien_points
if not self.alien.aliens_gp:
self.projectiles_gp.empty()
self.alien.create_fleet()
self.settings.increase_speed()
self.stats.score += self.settings.all_aliens_points
I've checked for the source code of the game on the web but I don't understand where I've made a mistake.
Thanks for your help ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决:我试图通过 Settings 类的实例更改静态变量。因此,Python 创建了一个动态变量,而不是更改静态变量。
解决方案:直接调用Class的静态变量在increase_speed()函数中改变它。
Solved : I was trying to change a static variable throught an instance of the Settings class. So, Python was creating a dynamic variable instead of changing the static one.
Solution : Call directly the static variable of the Class to change it in the increase_speed() fonction.