每次按下鼠标时,我都试图让pygame创建一个新的Bullet对象。它适用于第一个项目符号,但在第二个项目符号中我得到一个“'子弹'对象不可调用'错误。
class Bullet(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20,40))
self.image.fill(black)
self.rect = self.image.get_rect()
self.rect.x = player_1.rect.x
self.rect.y = player_1.rect.y
#self.image = pygame.image.load("C:/Users/Thomas/Desktop/rocket.png")
self.width = self.image.get_width()
self.height = self.image.get_height()
self.speed = 20
self.damage_radius = 0
self.damage = 0
self.angle = 0
def draw(self):
self.image.fill(black)
screen.blit(self.image, (self.rect.x, self.rect.y))
def delete(self):
pass
def fire(self, mouse):
"""Determine agnle of which rocket is fired"""
pass
def update(self):
self.rect.y -= self.speed
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(intro_bg_color)
large_text = pygame.font.Font("C:/Users/Thomas/Desktop/Python Sketches/Fonts/Quesat Black Demo.OTF", 100)
player_1 = Player((100,100), 20, blue)
while True:
pygame.display.update()
clock.tick(FPS)
mouse = pygame.mouse.get_pos()
gameEvent = pygame.event.get()
"""Draw"""
"""This wil be for advancing bullets in playing field"""
screen.fill(bg_color)
player_1.draw(mouse)
for Bullet in bullet_list:
Bullet.update()
if Bullet.rect.y < 0:
bullet_list.remove(Bullet)
Bullet.draw()
for event in gameEvent:
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
mouse_is_pressed = True # Used to determine if the mouse is held or not
print("Mouse pressed")
"""Generate new bullet"""
bullet = Bullet()
all_sprites_list.add(bullet)
bullet_list.add(bullet)
bullet.rect.x = player_1.rect.x
bullet.rect.y = player_1.rect.y
elif event.type == pygame.MOUSEBUTTONUP and pygame.mouse.get_rel()[0]:
mouse_is_pressed = False
print("Mouse button released")
按下鼠标时,生成新的Bullet,将其添加到bullet_list。这是我看到的一个例子是如何完成的,我无法解决问题。非常感谢帮助!
答案 0 :(得分:0)
我拿了这段代码:
for Bullet in bullet_list:
Bullet.update()
Bullet.draw()
并更改&#34;子弹&#34;到&#34;子弹&#34;并且代码终于开始工作了。
for bullet in bullet_list:
bullet.update()
bullet.draw()