主文件:
from plane_sprites import *
import pygame
class PlaneGame:
"""主游戏类"""
grade = 0
def __init__(self):
print("游戏初始化")
# 创建游戏窗口对象
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
# 创建时钟对象
self.clock = pygame.time.Clock()
# 调用私有方法创建精灵和精灵组
self.__create_sprites()
# 添加创建敌机的定时器
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
# 添加发射子弹的定时器
pygame.time.set_timer(HERO_FIRE_EVENT, 300)
# 创建精灵和精灵组
def __create_sprites(self):
# 创建背景精灵组
background1 = BackGround()
background2 = BackGround(True)
self.back_group = pygame.sprite.Group(background1, background2)
# 创建敌机精灵组
self.enemy_group = pygame.sprite.Group()
# 创建英雄的精灵和精灵组
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):
print("游戏开始")
while True:
self.clock.tick(FRAME_PER_SEC)
self.__event_hand()
self.__check_collide()
self.__update_sprites()
pygame.display.update()
# 事件监听
def __event_hand(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("退出游戏...")
pygame.quit()
exit()
elif event.type == CREATE_ENEMY_EVENT:
enemy = Enemy()
self.enemy_group.add(enemy)
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
# 判断用户按下的按键
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT]:
self.hero.speed = 3
elif keys_pressed[pygame.K_LEFT]:
self.hero.speed = -3
else:
self.hero.speed = 0
# 碰撞检测
def __check_collide(self):
bullets = pygame.sprite.groupcollide(self.hero.bullet_group, self.enemy_group, True, True)
if len(bullets) > 0:
PlaneGame.grade += 1
enemis = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
if len(enemis) > 0:
self.hero.kill()
print("英雄阵亡。。。")
print("您的得分为:%d" % PlaneGame.grade)
pygame.quit()
exit()
# 更新精灵组/精灵
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
self.enemy_group.update()
self.enemy_group.draw(self.screen)
self.hero_group.update()
self.hero_group.draw(self.screen)
self.hero.bullet_group.update()
self.hero.bullet_group.draw(self.screen)
@staticmethod
def __game_over():
print("游戏结束")
pygame.quit()
exit()
if __name__ == '__main__':
game = PlaneGame()
# 启动游戏
game.start_game()
工具文件:
import random
import pygame
# 屏幕大小的常量
SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
# 刷新的帧率
FRAME_PER_SEC = 60
# 创建敌机定时器事件常量
CREATE_ENEMY_EVENT = pygame.USEREVENT
# 发射子弹定时器事件常量
HERO_FIRE_EVENT = pygame.USEREVENT + 1
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 BackGround(GameSprite):
def __init__(self, is_alt=False):
super().__init__("./images/background.png")
if is_alt:
self.rect.y = -self.rect.height
def update(self):
super().update()
# 判断是否超出屏幕
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy(GameSprite):
def __init__(self):
super().__init__("./images/enemy1.png")
# 指定敌机速度
self.speed = random.randint(1, 3)
# 指定敌机初始位置
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_x)
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.kill()
def __del__(self):
# print("%s 被销毁了" % self.rect)
pass
class Hero(GameSprite):
def __init__(self):
super().__init__("./images/me1.png", 0)
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 120
self.bullet_group = pygame.sprite.Group()
def update(self):
self.rect.x += self.speed
if self.rect.left < SCREEN_RECT.left:
self.rect.left = SCREEN_RECT.left
elif self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
def fire(self):
bullet = Bullet()
bullet.rect.y = self.rect.y
bullet.rect.centerx = self.rect.centerx
self.bullet_group.add(bullet)
class Bullet(GameSprite):
def __init__(self):
super().__init__("./images/bullet1.png", -2)
def update(self):
super().update()
if self.rect.y < 0:
self.kill()
项目中使用到的图片资源:
链接:https://pan.baidu.com/s/1Md0c80fxaa5hK9kHrcpvqA
提取码:1781
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享