problem description
python encountered the following errors during the development of aircraft wars:
Traceback (most recent call last):
File "D:/python/ aircraft wars .py", line 53, in < module >
main()
File "D:/python/ aircraft Wars. Py", line 33, in main
bomb.draw(screen)
AttributeError: "Bomb" object has no attribute" draw"
when programming enemy planes explode
related codes
/ / Please paste the code text below (do not replace the code with pictures)
import pygame
import sys
from plane_sprites import *
bomb_flag = 0
import time
pygame.init ()
pygame.mixer.init ()
def main ():
SCREEN = (1024,768)
screen = pygame.display.set_mode(SCREEN,0,32)
timg = pygame.image.load("./feiji/timg.png")
enemy_group = pygame.sprite.Group()
hero = Hero()
hero_group = pygame.sprite.Group(hero)
bomb = Bomb()
pygame.time.set_timer(CREAT_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE_EVENT,200)
pygame.mixer.music.load("./feiji/bgm.mp3")
-sharpbombs = [Bomb() for _ in range(5)]
while True:
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play()
keys_pressed = pygame.key.get_pressed()
screen.blit(timg,(0,0))
enemy_group.update()
enemy_group.draw(screen)
hero_group.update()
hero_group.draw(screen)
hero.bullets.update()
hero.bullets.draw(screen)
bomb.update()
bomb.draw(screen)
for event in pygame.event.get():
if event.type == QUIT:
print("exit")
exit()
elif event.type == CREAT_ENEMY_EVENT:
enemy = Enemy()
enemy_group.add(enemy)
elif event.type == HERO_FIRE_EVENT:
if keys_pressed[pygame.K_SPACE]:
hero.fire()
pygame.display.update()
enemies1 = pygame.sprite.groupcollide(hero.bullets, enemy_group, 1,1)
enemies = pygame.sprite.spritecollide(hero,enemy_group,1)
enemies2 = tuple(enemies1)
if len(enemies) > 0:
hero.kill()
exit()
time.sleep(0.01) -sharp
if _ name__=="__main__":
main()
import random
import pygame
from pygame.locals import *
import time
CREAT_ENEMY_EVENT = pygame.USEREVENT
HERO_FIRE_EVENT = pygame.USEREVENT + 1
SCREEN = (1024768)
class GameSprite (pygame.sprite.Sprite):
def __init__(self,image_name,speed=1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
class Enemy (GameSprite):
def __init__(self):
super(Enemy, self).__init__("./feiji/enemy.png")
self.speed = random.randint(1,4)
self.rect.bottom = 0
max_x = 929
self.rect.x = random.randint(0,max_x)
def update(self):
super(Enemy, self).update()
if self.rect.y >= 863:
self.kill()
def __del__(self):
pass
class Hero (GameSprite):
def __init__(self):
super(Hero, self).__init__("./feiji/hero.png",0)
self.rect.x = 462
self.rect.y = 610
self.bullets = pygame.sprite.Group()
def update(self):
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
self.rect.x += 5
if keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
self.rect.y -= 5
elif keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
self.rect.y += 5
elif keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
self.rect.x -= 5
if keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
self.rect.y -= 5
elif keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
self.rect.y += 5
elif keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
self.rect.y -= 5
elif keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
self.rect.y += 5
if self.rect.x < -15:
self.rect.x = -15
elif self.rect.x > 940:
self.rect.x = 940
elif self.rect.y < -20:
self.rect.y = -20
elif self.rect.y > 685:
self.rect.y = 685
def fire(self):
bullet = Bullet()
bullet.rect.bottom = self.rect.y + 20
bullet.rect.x = self.rect.x + 47
self.bullets.add(bullet)
class Bullet (GameSprite):
def __init__(self):
super(Bullet, self).__init__("./feiji/bullet.png",-5)
def update(self):
super(Bullet, self).update()
if self.rect.bottom < 0:
self.kill()
def __del__(self):
pass
class Bomb (GameSprite):
def __init__(self):
super(Bomb, self).__init__("./feiji/res/boom1.png")
self.rect .x = 0
self.rect.y = 0
def update(self):
super(Bomb,self).update()