爆炸與寶箱,從零開始用Python制作飛機大戰(zhàn) P9

爆炸做好后提示
Traceback (most recent call last):
?File "d:\Python_cs\fjdz\fjdz.py", line 233, in <module>
??all_sprites.add(exception)
?File "C:\Program Files\Python311\Lib\site-packages\pygame\sprite.py", line 478, in add
??sprite.add_internal(self)
??^^^^^^^^^^^^^^^^^^^
代碼
import pygame
import random
import os
FPS = 60
WIDTH = 500
HEIGHT = 600
COLOR1 = (0,0,0)
COLOR2 = (250,230,100)
COLOR3 = (200,130,150)
COLOR4 = (160,170,210)
COLOR5 = (255,255,255)
COLOR6 = (0,255,0)
# 游戲初始化
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('飛機大戰(zhàn)')
clock = pygame.time.Clock()
background_img = pygame.image.load(os.path.join("img","background.png")).convert()
player_img = pygame.image.load(os.path.join("img","player.png")).convert()
player_mini_img = pygame.transform.scale(player_img,(25,20))
player_mini_img.set_colorkey(COLOR1)
rock_img = pygame.image.load(os.path.join("img","rock.png")).convert()
Bullet_img = pygame.image.load(os.path.join("img","bullet.png")).convert()
rock_images = []
for i in range(7):
? ? rock_images.append(pygame.image.load(os.path.join("img",f"rock{i}.png")).convert())
expl_anim = {}
expl_anim['ig'] = []
expl_anim['sm'] = []
for i in range(9):
? ? expl_img = pygame.image.load(os.path.join("img",f"expl{i}.png")).convert()
? ? expl_img.set_colorkey(COLOR1)
? ? expl_anim["ig"].append(pygame.transform.scale(expl_img,(75,75)))
? ? expl_anim["sm"].append(pygame.transform.scale(expl_img,(30,30)))
shoot_sound = pygame.mixer.Sound(os.path.join("sound","shoot.wav"))
expl_sounds = [
? ? pygame.mixer.Sound(os.path.join("sound","expl0.wav")),
? ? pygame.mixer.Sound(os.path.join("sound","expl1.wav"))
]
pygame.mixer.music.load(os.path.join("sound","background.ogg"))
pygame.mixer.music.set_volume(0.4)
font_name = pygame.font.match_font('arial')
def draw_text(surf,text,size,x,y):
? ? font = pygame.font.Font(font_name,size)
? ? text_surface = font.render(text,True,COLOR5)
? ? text_rect ?= text_surface.get_rect()
? ? text_rect.centerx = x
? ? text_rect.top = y
? ? surf.blit(text_surface,text_rect)
def draw_health(surf,hp,x,y):
? ? if hp < 0:
? ? ? ? hp = 0
? ? BAR_LENGTH = 100
? ? BAR_HEIGHT = 10
? ? fill = (hp/100)*BAR_LENGTH
? ? outline_rect = pygame.Rect(x,y,BAR_LENGTH,BAR_HEIGHT)
? ? fill_rect = pygame.Rect(x,y,fill,BAR_HEIGHT)
? ? pygame.draw.rect(surf,COLOR6,fill_rect)
? ? pygame.draw.rect(surf,COLOR5,outline_rect,2)
def draw_lives(surf,lives,img,x,y):
? ? for i in range(lives):
? ? ? ? img_rect = img.get_rect()
? ? ? ? img_rect.x = x + 30 * i
? ? ? ? img_rect.y = y
? ? ? ? surf.blit(img,img_rect)
def new_rock():
? ? r = Rock()
? ? all_sprites.add(r)
? ? Rocks.add(r)
class Player(pygame.sprite.Sprite):
? ? def __init__(self):
? ? ? ? pygame.sprite.Sprite.__init__(self)
? ? ? ? self.image = pygame.transform.scale(player_img,(50,40))
? ? ? ? self.image.set_colorkey(COLOR1)
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? self.radius = 24
? ? ? ? #pygame.draw.circle(self.image,COLOR4,self.rect.center,self.radius)
?
? ? ? ? self.rect.centerx = WIDTH / 2
? ? ? ? self.rect.bottom = HEIGHT - 20
? ? ? ?
? ? ? ? self.speedx = 8
? ? ? ? self.health = 100
? ? ? ? self.lives = 3
? ? ? ? self.hidden = False
? ? def update(self):
? ? ? ? key_pressed = pygame.key.get_pressed()
? ? ? ? if key_pressed[pygame.K_RIGHT]:
? ? ? ? ? ? self.rect.x += self.speedx
? ? ? ? if key_pressed[pygame.K_LEFT]:
? ? ? ? ? ? self.rect.x -= self.speedx
? ? ? ?
? ? ? ? if self.rect.right > WIDTH:
? ? ? ? ? ? self.rect.right = WIDTH
? ? ? ? if self.rect.left < 0:
? ? ? ? ? ? self.rect.left = 0
? ? ? ?
? ? ? ? if self.hidden and pygame.time.get_ticks() - self.hide_time > 1000:
? ? ? ? ? ? self.hidden = False
? ? ? ? ? ? self.rect.centerx = WIDTH / 2
? ? ? ? ? ? self.rect.bottom = HEIGHT - 20
? ? def player_shoot(self):
? ? ? ? bullet = Bullet(self.rect.centerx,self.rect.centery)
? ? ? ? all_sprites.add(bullet)
? ? ? ? Bullets.add(bullet)
? ? ? ? shoot_sound.play
? ? def hide(self):
? ? ? ? self.hidden = True
? ? ? ? self.hide_time = pygame.time.get_ticks()
? ? ? ? self.rect.center = (WIDTH / 2,HEIGHT + 500)
class Rock(pygame.sprite.Sprite):
? ? def __init__(self):
? ? ? ? pygame.sprite.Sprite.__init__(self)
? ? ? ? self.image_origin = random.choice(rock_images)
? ? ? ? self.image_origin.set_colorkey(COLOR1)
? ? ? ? self.image = self.image_origin.copy()
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? self.radius = self.rect.width / 2.2
? ? ? ? #pygame.draw.circle(self.image,COLOR4,self.rect.center,self.radius)
? ? ? ? self.rect.x = random.randrange(0,WIDTH - self.rect.width)
? ? ? ? self.rect.y = random.randrange(-180,-100)
? ? ? ? self.speedy = random.randrange(2,10)
? ? ? ? self.speedx = random.randrange(-3,3)
? ? ? ? self.rot_degree = random.randrange(-3,3)
? ? ? ? self.total_degree = 0
? ? def rotate(self):
? ? ? ? self.total_degree = self.total_degree + self.rot_degree
? ? ? ? self.total_degree = self.total_degree % 360
? ? ? ? self.image = pygame.transform.rotate(self.image_origin,self.total_degree)
? ? ? ? center = self.rect.center
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? self.rect.center = center
? ? def update(self):
? ? ? ? self.rotate()
? ? ? ? self.rect.y += self.speedy
? ? ? ? self.rect.x += self.speedx
? ? ? ? if self.rect.top > HEIGHT or self.rect.left > WIDTH or self.rect.right < 0:
? ? ? ? ? ? self.rect.x = random.randrange(0,WIDTH - self.rect.width)
? ? ? ? ? ? self.rect.y = random.randrange(-100,-40)
? ? ? ? ? ? self.speedy = random.randrange(2,10)
? ? ? ? ? ? self.speedx = random.randrange(-3,3)
class Bullet(pygame.sprite.Sprite):
? ? def __init__(self,x,y):
? ? ? ? pygame.sprite.Sprite.__init__(self)
? ? ? ? self.image = Bullet_img
? ? ? ? self.image.set_colorkey(COLOR1)
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? self.rect.x = x
? ? ? ? self.rect.y = y
? ? ? ? self.speedy = -10
? ? def update(self):
? ? ? ? self.rect.y += self.speedy
? ? ? ? if self.rect.bottom < 0:
? ? ? ? ? ? self.kill()
class Explosion(pygame.sprite.Sprite):
? ? def __init__(self,center,size):
? ? ? ? pygame.sprite.Sprite.__init__(self)
? ? ? ? self.size = size
? ? ? ? self.image = expl_anim[self.size][0]
? ? ? ? self.rect = self.image.get_rect()
? ? ? ? self.rect.center = center
? ? ? ? self.frame = 0
? ? ? ? self.last_update = pygame.time.get_ticks
? ? def update(self):
? ? ? ? now = pygame.time.get_ticks
? ? ? ? if now - self.last_update > 50:
? ? ? ? ? ? self.last_update = now
? ? ? ? ? ? self.frame = self.frame + 1
? ? ? ? ? ? if self.frame == len(expl_anim[self.size]):
? ? ? ? ? ? ? ? self.kill()
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? self.image = expl_anim[self.size][self.frame]
? ? ? ? ? ? ? ? self.rect = self.image.get_rect()
? ? ? ? ? ? ? ? self.rect.center = center
all_sprites = pygame.sprite.Group()
Rocks = pygame.sprite.Group()
Bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
score = 0
pygame.mixer.music.play(-1)
for i in range(8):
? ? new_rock()
running=True
while running:
? ? clock.tick(FPS)
? ? for event in pygame.event.get():
? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? running = False
? ? ? ? elif event.type == pygame.KEYDOWN:
? ? ? ? ? ? if event.key == pygame.K_SPACE:
? ? ? ? ? ? ? ? player.player_shoot()
? ? # 更新游戲
? ? all_sprites.update()
? ? hits_rockandbullet = pygame.sprite.groupcollide(Rocks,Bullets,True,True)
? ? for hit in hits_rockandbullet:
? ? ? ? random.choice(expl_sounds).play()
? ? ? ? exception = Exception(hit.rect.center,'lg')
? ? ? ? all_sprites.add(exception)
? ? ? ? new_rock()
? ? ? ? score = score + int(hit.radius)
? ? hits_playerandrock = pygame.sprite.spritecollide(player,Rocks,True,pygame.sprite.collide_circle)
? ? for hit in hits_playerandrock:
? ? ? ? player.health = player.health - hit.radius
? ? ? ? new_rock()
? ? ? ? expl = Exception(hit.rect.center,'sm')
? ? ? ? all_sprites.add(expl)
? ? ? ? if player.health <= 0:
? ? ? ? ? ? player.lives = player.lives - 1
? ? ? ? ? ? player.health = 100
? ? ? ? ? ? player.hide()
? ? ? ? if player.lives == 0:
? ? ? ? ? ? ? ? running = False
? ? # 顯示畫面
? ? screen.fill(COLOR1)
? ? screen.blit(background_img,(0,0))
? ? all_sprites.draw(screen)
? ? draw_text(screen,str(score),18,WIDTH/2,0)
? ? draw_health(screen,player.health,10,30)
? ? draw_lives(screen,player.lives,player_mini_img,WIDTH - 100,15)
? ? pygame.display.update()
pygame.quit()