对飞机大战项目进行封装
import pygame
import random
import time
import os
def get_pic(path):
pic_path = os.path.join("D:\\PythonCode\\PlaneWars\\img", path)
return pygame.image.load(pic_path)
class HeroBullet():
"""
英雄精灵子弹的类
"""
def __init__(self, x, y, screen):
"""
:param x: x坐标
:param y: y 坐标
:param screen: 窗口对象
"""
self.x = x
self.y = y
self.screen = screen
self.pic = get_pic("bullet.png")
def draw(self):
"""用来画子弹"""
self.screen.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y -= 5
class EnemyBullet():
"""敌机精灵子弹类"""
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.pic = get_pic("bullet1.png")
def draw(self):
"""用来画子弹"""
self.screen.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y += 5
class HeroPlane():
"""英雄战机类"""
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.normal_image_list = ["hero1.png", "hero2.png"]
self.normal_image_index = 0
self.bomb_image_list = ["hero_blowup_n1.png", "hero_blowup_n2.png", "hero_blowup_n3.png","hero_blowup_n4.png", ]
self.bomb_image_index = 0
self.isBomb = False
self.bullet_list = []
def draw(self):
if self.isBomb == False:
pic = get_pic(self.normal_image_list[self.normal_image_index])
self.screen.blit(pic, (self.x, self.y))
self.normal_image_index = (self.normal_image_index + 1) % len(self.normal_image_list)
else:
if self.bomb_image_index == len(self.bomb_image_list):
time.sleep(0.2)
exit()
enemy_bomb_img = get_pic(self.bomb_image_list[self.bomb_image_index])
screen.blit(enemy_bomb_img, (self.x, self.y))
self.bomb_image_index += 1
time.sleep(0.2)
def deal_event(self, event_list):
for event in event_list:
if event.type == pygame.QUIT:
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x = self.x - 5 if self.x >= 5 else 0
elif event.key == pygame.K_RIGHT:
self.x = self.x + 5 if self.x <= 480 - 100 - 5 else 480 - 100
elif event.key == pygame.K_DOWN:
self.y = self.y + 5 if self.y <= 650 - 124 - 5 else 0
elif event.key == pygame.K_UP:
self.y = self.y - 5 if self.y >= 5 else 0
elif event.key == pygame.K_SPACE:
one_bullet = HeroBullet(self.x + 39, self.y - 22, screen)
self.bullet_list.append(one_bullet)
for bullet in self.bullet_list:
bullet.draw()
self.bullet_list.remove(bullet) if bullet.y < 0 else ""
def check_collide(self,bullet_list):
"""碰撞检测"""
hero_rect = pygame.rect.Rect(self.x, self.y, 100, 124)
for bullet in bullet_list:
enemy_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y,9, 21)
flag = enemy_bullet_rect.colliderect(hero_rect)
if flag:
print("英雄机爆炸了......")
self.isBomb = True
bullet_list.remove(bullet)
class EnemyPlane():
"""敌机类"""
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.normal_image_list = ["enemy1.png"]
self.normal_image_index = 0
self.bomb_image_list = ["enemy1_down1.png", "enemy1_down2.png", "enemy1_down3.png", "enemy1_down4.png", ]
self.bomb_image_index = 0
self.isBomb = False
self.bullet_list = []
self.direct = "左"
def draw(self):
if self.isBomb == False:
pic = get_pic(self.normal_image_list[self.normal_image_index])
self.screen.blit(pic, (self.x, self.y))
self.normal_image_index = (self.normal_image_index + 1) % len(self.normal_image_list)
else:
if self.bomb_image_index == len(self.bomb_image_list):
time.sleep(0.2)
exit(0)
enemy_bomb_img = get_pic(self.bomb_image_list[self.bomb_image_index])
screen.blit(enemy_bomb_img, (self.x, self.y))
self.bomb_image_index += 1
time.sleep(0.2)
self.move()
self.fire()
def move(self):
"""让敌机移动"""
if self.direct == "左":
self.x -= 5
if self.x <= 0:
self.direct = "右"
elif self.direct == "右":
self.x += 5
if self.x >= 480 - 69:
self.direct = "左"
def fire(self):
"""敌机子弹"""
x = random.randint(0, 100)
if x == 5 or x == 78:
enemy_bullet = EnemyBullet(self.x + 69 // 2 - 9 // 2, self.y + 89, screen)
self.bullet_list.append(enemy_bullet)
for bullet in self.bullet_list:
bullet.draw()
self.bullet_list.remove(bullet) if bullet.y > 650 - 89 - 21 // 2 else ""
def check_collide(self,bullet_list):
"""碰撞检测"""
enemy_rect = pygame.rect.Rect(self.x, self.y, 50, 56)
print(enemy_rect)
for bullet in bullet_list:
hero_bullet_rect = pygame.rect.Rect(bullet.x, bullet.y, 22, 22)
flag = hero_bullet_rect.colliderect(enemy_rect)
if flag:
print("敌机爆炸了......")
self.isBomb = True
bullet_list.remove(bullet)
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((480, 650))
pygame.display.set_caption("飞机大战")
pygame.display.set_icon(get_pic("icon72x72.png"))
pygame.key.set_repeat(20, 30)
hero_plane = HeroPlane(480 // 2 - 100 // 2, 650 - 124, screen)
enemy_plane = EnemyPlane(480 // 2 - 69 // 2, 0, screen)
while True:
bg = get_pic("background.png")
screen.blit(bg, (0, 0))
enemy_plane.check_collide(hero_plane.bullet_list)
hero_plane.check_collide(enemy_plane.bullet_list)
hero_plane.draw()
enemy_plane.draw()
hero_plane.deal_event(pygame.event.get())
pygame.display.update()