博客
关于我
Python源代码 ---飞机大战游戏
阅读量:493 次
发布时间:2019-03-06

本文共 3406 字,大约阅读时间需要 11 分钟。

?????????????????

?????????????????????????????????????????????????????

???????

  • ?????????????????????????????Pygame???????Sprite??????????????????????????????GameSprite????????????????
  • 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
    1. ?????????????????????????????????????????
    2. class Background(GameSprite):    def __init__(self, is_alt=False):        super().__init__("./images/background.png")        if is_alt:            self.rect.y = -self.rect.height
      1. ????????????????????????????
      2. class Enemy(GameSprite):    def __init__(self):        super().__init__("./images/enemy1.png")        self.speed = random.randint(1,3)        max_x = SCREEN_RECT.width - self.rect.width        self.rect.x = random.randint(0, max_x)
        1. ?????????????????????????????
        2. 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.bullets_group = pygame.sprite.Group()
          1. ???????????????????????
          2. class Bullet(GameSprite):    def __init__(self):        super().__init__("./images/bullet1.png", -2)

            ?????????

          3. ?????????????????????????????????????
          4. class PlaneGame:    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, 500)
            1. ?????????????????????????????????
            2. def start_game(self):        print("????...")        while True:            self.clock.tick(FRAME_PRE_SEC)            self.__event_handler()            self.__check_collide()            self.__update_sprites()            pygame.display.update()
              1. ???????????????????????????????
              2. def __event_handler(self):        for event in pygame.event.get():            if event.type == pygame.QUIT:                PlaneGame.__game__over()            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 = 2        elif keys_pressed[pygame.K_LEFT]:            self.hero.speed = -2        else:            self.hero.speed = 0
                1. ?????????????????????????
                2. def __check_collide(self):        pygame.sprite.groupcollide(self.hero.bullets_group, self.enemy_group, True, True)        enemies = pygame.sprite.spritecollide(self.hero, self(enemy_group), True)        if len(enemies) > 0:            self.hero.kill()            PlaneGame.__game__over()
                  1. ??????????????????????????
                  2. 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.bullets_group.update()        self.hero.bullets_group.draw(self.screen)

                    ??????????????????????

                    @staticmethod    def __game__over():        print("????")        pygame.quit()        exit()

                    ????????????????????????????????????????????????????????????????????????

    转载地址:http://dildz.baihongyu.com/

    你可能感兴趣的文章
    poj 2186 Popular Cows :求能被有多少点是能被所有点到达的点 tarjan O(E)
    查看>>
    POJ 2186:Popular Cows Tarjan模板题
    查看>>
    POJ 2229 Sumsets(递推,找规律)
    查看>>
    poj 2236
    查看>>
    POJ 2243 Knight Moves
    查看>>
    POJ 2262 Goldbach's Conjecture
    查看>>
    POJ 2362 Square DFS
    查看>>
    Qt笔记——解决添加Qt Designer Form Class时“allocation of incomplete type Ui::”
    查看>>
    poj 2386 Lake Counting(BFS解法)
    查看>>
    poj 2387 最短路模板题
    查看>>
    POJ 2391 多源多汇拆点最大流 +flody+二分答案
    查看>>
    POJ 2403
    查看>>
    poj 2406 还是KMP的简单应用
    查看>>
    POJ 2431 Expedition 优先队列
    查看>>
    Qt笔记——获取位置信息的相关函数
    查看>>
    POJ 2484 A Funny Game(神题!)
    查看>>
    POJ 2486 树形dp
    查看>>
    POJ 2488:A Knight's Journey
    查看>>
    SpringBoot为什么易学难精?
    查看>>
    poj 2545 Hamming Problem
    查看>>