简体   繁体   English

在 pygame python 中移动敌人

[英]Moving enemies in pygame python

I just added 5 lines of code, from line 86 to 91, in the "draw_enemy" method, but now I'm getting the following error: File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/random.py", line 243, in _randbelow_with_getrandbits k = n.bit_length() # don't use (n-1) here because n can be 1 RecursionError: maximum recursion depth exceeded while calling a Python object.我刚刚在“draw_enemy”方法中添加了 5 行代码,从第 86 行到第 91 行,但现在我收到以下错误:文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3. 9/random.py",第 243 行,在 _randbelow_with_getrandbits k = n.bit_length() # 此处不要使用 (n-1),因为 n 可以是 1 I don't understand the issue.我不明白这个问题。

import pygame, sys
import random
import time
pygame.init()

clock = pygame.time.Clock()
time0 = time.time()

has_passed = False

screen_width = 600
screen_height = 800

enemyWidth = 30
enemyHeight = 10

bg_color = (94, 50, 50)
enemy_color = (0, 0, 0)

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Space Race Game")

class ROCKET:
    def __init__(self):
        self.rocketImg = pygame.image.load("spaceship.png")
        self.rocket_x = screen_width/2 - 32
        self.rocket_y = screen_height/2 + 150

    def draw_rocket(self):
        screen.blit(self.rocketImg, (self.rocket_x, self.rocket_y))

    def move_rocket(self):
        key = pygame.key.get_pressed()

        if key[pygame.K_LEFT] and self.rocket_x + 15 > 0:
            self.rocket_x -= 5

        if key[pygame.K_RIGHT] and self.rocket_x < screen_width - 40:
            self.rocket_x += 5

class BULLET(ROCKET):
    def __init__(self):
        super().__init__()
        self.bullet_width = 10
        self.bullet_height = 20
        self.bullet_x = self.rocket_x + 25
        self.bullet_y = self.rocket_y
        self.move = [0, 0]
        self.bullet_speed = 7
        self.bullet_rect = pygame.Rect(self.bullet_x, self.bullet_y, self.bullet_width, self.bullet_height)

    def draw_bullet(self, rocket, bullet):
        key = pygame.key.get_pressed()

        if key[pygame.K_SPACE] and self.move[1] == 0:
            self.bullet_x = rocket.rocket_x + 25
            self.move[1] = -1

        self.bullet_y += self.move[1] * self.bullet_speed
        self.bullet_rect.topleft = (self.bullet_x, self.bullet_y)

        if self.bullet_y < self.rocket_y - 10:
            pygame.draw.rect(screen, (0, 0, 0), self.bullet_rect)

        if self.bullet_y < - 20:
            self.bullet_y = self.rocket_y
            self.move[1] = 0

class ENEMY(ROCKET):
    def __init__(self):
        super().__init__()
        self.enemy_width = enemyWidth
        self.enemy_height = enemyHeight
        self.enemy_x = random.randint(self.enemy_width, screen_width - self.enemy_width)
        self.enemy_y = 0
        self.enemy_speed = 1
        self.enemy_rect = pygame.Rect(self.enemy_x, self.enemy_y, self.enemy_width, self.enemy_height)
        self.next_enemy_time = 0
        self.enemies = []

    def draw_enemy(self, rocket, bullet):
        pygame.draw.rect(screen, enemy_color, self.enemy_rect)
        self.enemy_y += self.enemy_speed
        self.enemy_rect.topleft = (self.enemy_x, self.enemy_y)
        # Time Management and multiple enemies
        current_time = pygame.time.get_ticks()
        if current_time > self.next_enemy_time:
            self.next_enemy_time = current_time + 3000
            self.enemies.append(ENEMY())
        for enemies in self.enemies:
            enemies.draw_enemy(rocket, bullet)


rocket = ROCKET()
bullet = BULLET()
enemy = ENEMY()

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(bg_color)
    rocket.draw_rocket()
    rocket.move_rocket()
    bullet.draw_bullet(rocket, bullet)
    enemy.draw_enemy(rocket, bullet)


    pygame.display.flip()
    clock.tick(60)

The issue probably lies here:问题大概出在这里:

def draw_enemy(self, rocket, bullet):
        pygame.draw.rect(screen, enemy_color, self.enemy_rect)
        self.enemy_y += self.enemy_speed
        self.enemy_rect.topleft = (self.enemy_x, self.enemy_y)
        # Time Management and multiple enemies
        current_time = pygame.time.get_ticks()
        if current_time > self.next_enemy_time:
            self.next_enemy_time = current_time + 3000
            self.enemies.append(ENEMY())
        for enemies in self.enemies:
            enemies.draw_enemy(rocket, bullet)

Particularly, here:特别是,这里:

for enemies in self.enemies:
    enemies.draw_enemy(rocket, bullet)

You're calling draw_enemy again for every enemy you added to the list.您为添加到列表中的每个敌人再次调用draw_enemy This in turn adds enemies to each new enemy这反过来又为每个新敌人增加了敌人

if current_time > self.next_enemy_time:
    self.next_enemy_time = current_time + 3000
    self.enemies.append(ENEMY())

and calls draw_enemy on these again:并再次调用draw_enemy

for enemies in self.enemies:
    enemies.draw_enemy(rocket, bullet)

so this function recurses infinitely, ie, draw_enemy always calls itself, which results in a RecursionError when the recursion limit is reached.所以这个 function 无限递归,即draw_enemy总是调用自己,当达到递归限制时会导致RecursionError

You need to add a base case : at some point you either should stop adding enemies, or you should stop drawing them.您需要添加一个基本案例:在某些时候您应该停止添加敌人,或者您应该停止绘制它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM