美文网首页
花里胡哨的飞机大战滑稽怪周末作业(半成品)2018-08-04

花里胡哨的飞机大战滑稽怪周末作业(半成品)2018-08-04

作者: LPP27149 | 来源:发表于2018-08-04 17:04 被阅读0次
    # -*- coding: utf-8 -*-
    """
    -------------------------------------------------
       File Name:     plane
       Author :       LPP
       E-mail :       l.piaopiao@qq.com
       date:          2018/8/4
    -------------------------------------------------
       Change Activity:
                       2018/8/4:
    -------------------------------------------------
    """
    import pygame
    from pygame.locals import *
    from random import randint
    SCREEN_WIDTH, SCREEN_HEIGHT = 480, 852
    UP = 273
    DOWN = 274
    LEFT = 276
    RIGHT = 275
    
    
    class Bullet:
        def __init__(self, image):
            self.pos = [190, 849]
            self.speed = 15
            self.image = image
    
        def move(self):
            self.pos[1] -= self.speed
    
    
    class Bomb(Bullet):
        def __init__(self, bomb_img):
            self.speed = 8
            self.image = bomb_img
    
    
    class 滑稽怪:
        def __init__(self, image):
            self.life = 100  # 初始化图片所在的矩形
            self.pos = [randint(5, 450), 100]  # 初始化矩形的左上角坐标
            self.speed = 8  # 初始化玩家飞机速度,这里是一个确定的值                             # 玩家飞机图片索引
            self.image = pygame.image.load(image)
    
        def move(self):
            self.pos[1] += 1
    
    
    class Player:
        def __init__(self, image):
            self.life = 100                      # 初始化图片所在的矩形
            self.pos = [190, 700]                    # 初始化矩形的左上角坐标
            self.speed = 10                                  # 初始化玩家飞机速度,这里是一个确定的值                             # 玩家飞机图片索引
            self.image = image
            self.bullets = []
            self.bombs = []
    
        def shoot(self, image):
            bullet = Bullet(pygame.image.load(image))
            bullet.pos = [self.pos[0] + 48, self.pos[1] - 20]
            self.bullets.append(bullet)
    
        def shoot_bomb(self, image):
            bomb = Bomb(pygame.image.load(image))
            bomb.pos = [self.pos[0] + 18, self.pos[1] - 54]
            self.bombs.append(bomb)
    
        def move_up(self):
            if self.pos[1] <= -62:
                self.pos[1] = -62
                return
            self.pos[1] -= self.speed
    
        def move_down(self):
            if self.pos[1] >= SCREEN_HEIGHT-62:
                self.pos[1] = SCREEN_HEIGHT-62
                return
            self.pos[1] += self.speed
    
        def move_left(self):
            if self.pos[0] <= -45:
                self.pos[0] = -45
                return
            self.pos[0] -= self.speed
    
        def move_right(self):
            if self.pos[0] >= SCREEN_WIDTH - 55:
                self.pos[0] = SCREEN_WIDTH - 55
                return
            self.pos[0] += self.speed
    
    
    if __name__ == '__main__':
        # 初始化 pygame
        pygame.init()
    
        # 设置游戏界面大小、背景图片及标题
        # 游戏界面像素大小
        screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    
        # 游戏界面标题
        pygame.display.set_caption('飞机大战滑稽怪')
    
        # 背景图
        background = pygame.image.load('image/background.png')
        player_img = pygame.image.load('image/hero1.png')
        player = Player(player_img)
        bullet_img = 'image/bullet1.png'
        滑稽图 = 'image/小小滑稽.png'
        bomb_img = 'image/bomb.png'
        # # Game Over 的背景图
        game_over = pygame.image.load('image/game_over.png')
        clock = pygame.time.Clock()
        temp1 = 0
        temp2 = 1
        滑稽数量 = 0
        bullet_num = 0
        滑稽怪们 = []
        while True:
            if temp1 > temp2:
                bullet_img = 'image/bullet1.png'
                temp2 += 2
            else:
                bullet_img = 'image/bullet2.png'
                temp1 +=2
            clock.tick(60)
            screen.fill(0)
            screen.blit(background, (0, 0))
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
            # if event.type == pygame.KEYDOWN:
                # 获取键盘事件(上下左右按键)
            key_pressed = pygame.key.get_pressed()
    
            # 处理键盘事件(移动飞机的位置)
            if key_pressed[K_w] or key_pressed[K_UP]:
                player.move_up()
            if key_pressed[K_s] or key_pressed[K_DOWN]:
                player.move_down()
            if key_pressed[K_a] or key_pressed[K_LEFT]:
                player.move_left()
            if key_pressed[K_d] or key_pressed[K_RIGHT]:
                player.move_right()
            if key_pressed[K_SPACE]:
                if bullet_num % 15 == 0:
                    player.shoot_bomb(bomb_img)
                bullet_num += 1
                if bullet_num > 100:
                    bullet_num = 0
    
            for bomb in player.bombs:
                screen.blit(bomb.image, bomb.pos)
                bomb.move()
                if bomb.pos[1] < 0:
                    player.bombs.remove(bomb)
    
            if bullet_num % 15 == 0:
                player.shoot(bullet_img)
            bullet_num += 1
            if bullet_num > 100:
                bullet_num = 0
    
            for bullet in player.bullets:
                screen.blit(bullet.image, bullet.pos)
                bullet.move()
                if bullet.pos[1] < 0:
                    player.bullets.remove(bullet)
            if 滑稽数量 % 100 == 0:
                滑稽 = 滑稽怪(滑稽图)
                滑稽怪们.append(滑稽)
            滑稽数量 += 1
            if 滑稽数量 >= 150:
                滑稽数量 = 0
    
            for 滑稽怪1 in 滑稽怪们:
                screen.blit(滑稽怪1.image, 滑稽怪1.pos)
                滑稽怪1.move()
                if 滑稽怪1.pos[1] > 850:
                    滑稽怪们.remove(滑稽怪1)
    
            for bullet in player.bullets:
                for 滑稽2 in 滑稽怪们:
                    w = -9 <= (bullet.pos[0] - 滑稽2.pos[0]) <= 96
                    h = -21 <= (bullet.pos[1] - 滑稽2.pos[1]) <= 96
                    if w and h:
                        滑稽怪们.remove(滑稽2)
                        player.bullets.remove(bullet)
    
            for 滑稽3 in 滑稽怪们:
                w = -100 <= (player.pos[0] - 滑稽3.pos[0]) <= 96
                h = -124 <= (player.pos[1] - 滑稽3.pos[1]) <= 96
                if w and h:
                    滑稽怪们.remove(滑稽3)
    
            screen.blit(player.image, player.pos)
            pygame.display.update()
    
    

    相关文章

      网友评论

          本文标题:花里胡哨的飞机大战滑稽怪周末作业(半成品)2018-08-04

          本文链接:https://www.haomeiwen.com/subject/crpmvftx.html