python飞机大战09-玩家飞机添加血条
1 添加血条
现在,玩家被一次流星击中后摧毁。这不是很有趣,所以要为玩家添加一个属性shield,数值类型,范围0 - 100。
class Player(pygame.sprite.Sprite):
def __init__(self):
self.speedx = 0
self.speedy = 0
self.shield = 100
现在,每次玩家被流星击中时,都可以从血条中减去一点。当血条达到0时,玩家将被摧毁。为了让事情更有趣,可以通过使用流星的radius属性使大型流星比小型流星造成更大的伤害。
2 破坏玩家
修改碰撞检测代码
# check to see if a mob hit the player
hits = pygame.sprite.spritecollide(player, mobs, True,pygame.sprite.collide_circle)
for hit in hits:
player.shield -= hit.leng/5
if player.shield <= 0:
running = False
改变了spritecollide函数的第三个参数(False改为了True),希望流星碰到后自动删除,因为如果不这样做,那么流星将继续存在,当它移动时,下一帧会和玩家发生另一次碰撞,依此类推。
此外,玩家可能同时碰到多个流星,因此hits可以有多个项目。循环hits,根据每个流星半径扣除玩家每次被碰到后失去的血条。最后,如果玩家的血条达到0,游戏将结束。
由于正在删除任何击中玩家的怪物,正在减少游戏中的怪物总数。希望保持不变,所以每当移除一个时,需要产生一个新的暴徒。初始化时这样产生了怪物集合:
for i in range(8):
m = Mob()
all_sprites.add(m)
mobs.add(m)
每当移除一个怪物时,需要做同样的事情,这是重复的代码,定义一个函数:
def newmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)
现在可以在游戏开始时调用该功能,当子弹击中暴徒时调用该功能,以及当需要更换击中玩家的暴徒时调用该功能:
for i in range(8):
newmob()
# check to see if a bullet hit a mob
hits = pygame.sprite.groupcollide(mobs, bullets, True, True, pygame.sprite.collide_rect_ratio(0.6))
for hit in hits:
score += 100 - hit.leng
random.choice(expl_sounds).play()
newmob()
# check to see if a mob hit the player
hits = pygame.sprite.spritecollide(player, mobs, True,pygame.sprite.collide_circle)
for hit in hits:
player.shield -= hit.leng/5
newmob()
if player.shield <= 0:
running = False
有血条变量的值,而且它有效,但除非玩家能看到这个值,否则它不是很有用。需要创建一个显示,但不是只显示数字,将制作一个条形图,显示血条的满/空:
def draw_shield_bar(surf,x,y,pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (pct / 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,GREEN,fill_rect)
pygame.draw.rect(surf,WHITE,outline_rect,2)
此函数与draw_text之前定义的函数类似。它接受位置参数(x, y)和填充量参数pct。将绘制两个矩形:第一个是白色轮廓,第二个是绿色填充部分。在游戏循环的绘图部分添加了对此函数的调用:
# Shmup game - part 9
# shmup.py
# Player Shield
import pygame
import random
from os import path
img_dir = path.join(path.dirname(__file__), 'img')
mp3_dir = path.join(path.dirname(__file__), 'mp3')
WIDTH = 512 # width of our game window
HEIGHT = 768 # height of our game window
FPS = 60 # 60 frames per second
# Colors(R,G,B),define color
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# initialize pygame and create windw
pygame.init() # 启动pygame并初始化
pygame.mixer.init() # 声音初始化
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # 游戏屏幕,按照在配置常量中设置的大小创建
pygame.display.set_caption("Shmup!")
icon = pygame.image.load("img/alien.ico")
pygame.display.set_icon(icon)
clock = pygame.time.Clock() # 创建一个时钟以便于确保游戏能以指定的FPS运行
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,WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x,y)
surf.blit(text_surface,text_rect)
def newmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)
def draw_shield_bar(surf,x,y,pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (pct / 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,GREEN,fill_rect)
pygame.draw.rect(surf,WHITE,outline_rect,2) # 第4个参数默认值为0,表示填充矩形内部
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(player_img, (60, 40))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.radius = 20
# pygame.draw.circle(self.image, RED, self.rect.center, self.radius)
self.rect.centerx = WIDTH/2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
self.speedy = 0
self.shield = 100
def update(self):
# any code here will happen every time the game loop updates
self.speedx = 0
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -5
if keystate[pygame.K_RIGHT]:
self.speedx = 5
self.rect.x += self.speedx
if keystate[pygame.K_UP]:
self.speedy = -5
if keystate[pygame.K_DOWN]:
self.speedy = 5
self.rect.y += self.speedy
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
if self.rect.top < 0:
self.rect.top = 0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
shoot_sound.play()
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.leng = random.randrange(30, 70, 10)
self.image_orig = pygame.transform.scale(random.choice(meteor_img), (self.leng, self.leng))
self.image_orig.set_colorkey(BLACK)
self.image = self.image_orig.copy()
self.rect = self.image.get_rect()
self.radius = int(self.rect.width / 2)
# pygame.draw.circle(self.image, RED, self.rect.center, self.radius)
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-150, -100)
self.speedx = random.randrange(-3, 3)
self.speedy = random.randrange(1, 5)
self.rot = 0
self.rot_speed = random.randrange(-8, 8)
self.last_update = pygame.time.get_ticks()
def update(self):
self.rotate()
self.rect.x += self.speedx
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10 or self.rect.left < -50 or self.rect.right > WIDTH + 20:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 5)
def rotate(self):
now = pygame.time.get_ticks()
if now - self.last_update > 50:
self.last_update = now
# do rotate here
self.rot = (self.rot + self.rot_speed) % 360
new_image = pygame.transform.rotate(self.image_orig, self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(bullet_img, (48, 48))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -8
def update(self):
self.rect.y += self.speedy
# kill if it moves off the top of the screen
if self.rect.bottom < 0:
self.kill()
# load all game graphics
background = pygame.image.load(path.join(img_dir, 'background2.jpg')).convert()
background_rect = background.get_rect()
player_img = pygame.image.load(path.join(img_dir, 'my_plane.png')).convert()
bullet_img = pygame.image.load(path.join(img_dir, 'bullet.png')).convert()
meteor_img = []
meteor_list = ['enemy1.png']
for img in meteor_list:
meteor_img.append(pygame.image.load(path.join(img_dir, img)).convert())
# load all game sound
shoot_sound = pygame.mixer.Sound(path.join(mp3_dir, 'hero_fire.wav'))
expl_sounds = []
for expl in ['explosion2.wav','explosion3.wav']:
expl_sounds.append(pygame.mixer.Sound(path.join(mp3_dir, expl)))
pygame.mixer.music.load(path.join(mp3_dir, 'bgm.mp3'))
pygame.mixer.music.set_volume(0.2)
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
newmob()
score = 0
pygame.mixer.music.play(loops=-1)
# Game Loop
running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
# Process input(events) # 这是游戏主循环,通过变量running控制,如果需要
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
# Update # 游戏结束的话直接将running设为False即可
all_sprites.update()
# check to see if a bullet hit a mob
hits = pygame.sprite.groupcollide(mobs, bullets, True, True, pygame.sprite.collide_rect_ratio(0.6))
for hit in hits:
score += 100 - hit.leng
random.choice(expl_sounds).play()
newmob()
# check to see if a mob hit the player
hits = pygame.sprite.spritecollide(player, mobs, True,pygame.sprite.collide_circle)
for hit in hits:
player.shield -= hit.leng/5
newmob()
if player.shield <= 0:
running = False
# Render(draw) # 现在还没有确定具体的代码,先用一些基本代码填充,后续再补充
screen.fill(BLACK)
screen.blit(background, background_rect)
all_sprites.draw(screen)
draw_text(screen, str(score), 18, WIDTH/2, 10)
draw_text(screen, str(player.shield), 15, 20, 20)
draw_shield_bar(screen, 5, 5, player.shield)
# *after* drawing everything,flip the display
pygame.display.flip()
pygame.quit()
项目代码可以查看我的github,网址为
alien-invasion