美文网首页
Python学习笔记之【打飞机】2019-08-15

Python学习笔记之【打飞机】2019-08-15

作者: 平知 | 来源:发表于2019-08-15 21:53 被阅读0次
    章节号 内容            
    1图片格式(png) 宽度大于620px,保持高宽比减低为620px
    1-1 应用
    1-1-1 方法

      杀掉一个进程:1、ps aux 。2、kill -s 9 进程id号

    第1章节  新式类及保护对象的属性

      新式类的样式(括号中有object):

    # 1、导入模块(先pip3 install pygame)
    import pygame
    
    # 4、导入按键的检测
    from pygame.locals import *
    
    # 6、导入时间库,控制程序运行速度
    import time
    
    # 7、创建飞机类
    
    
    class PlayerPlane(object):
        def __init__(self):
            # 子弹列表
            self.bulletList = []
    
            planeImageName = "/home/li/Documents/image/hero1.png"
            self.image = pygame.image.load(planeImageName).convert_alpha()
            planeImageName1 = "/home/li/Documents/image/hero2.png"
            self.image1 = pygame.image.load(planeImageName1).convert_alpha()
            # 初始位置
            self.x = 230
            self.y = 600
            # 设置速度
            self.speed = 5
            # 设置名字
            self.planeName = "player"
    
        def draw(self, screen):
            screen.blit(self.image, (self.x, self.y))
            # screen.blit(self.image1, (self.x, self.y))
    
    # 2、打开一个窗口,加载背景
    if __name__ == '__main__':
        screen = pygame.display.set_mode((480, 852), 0, 32)
        bgImamgeFile = './image/background.png'
        background = pygame.image.load(bgImamgeFile).convert()
        pp1 = PlayerPlane()
    
    # 3、背景贴到screen上,然后刷新窗口。while True非常耗资源。
    while True:
        
        screen.blit(background, (0, 0))
        pp1.draw(screen)
    
    
    
    # 5、按键的检测。固定代码块。先执行blit,再看这里有没有按键,有就执行,没有就刷新,再循环一次。
        for event in pygame.event.get():
            if event.type == QUIT:
                print("exit")
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_a or event.key == K_LEFT:
                    pp1.x -= 10
                    print("left")
                elif event.key == K_d or event.key == K_RIGHT:
                    pp1.x += 10
                    print("right")
                elif event.key == K_w or event.key == K_UP:
                    pp1.y -= 10
                    print("up")
                elif event.key == K_s or event.key == K_DOWN:
                    pp1.y += 10
                    print("down")
                elif event.key == K_SPACE:
                    print("space")
    
        
    
        pygame.display.update()
    
    # 6.1 1秒会很卡。比较好的是0.01
        time.sleep(0.01)
    
    

      相对完整的版本

    # 1、导入模块(先pip3 install pygame)
    import pygame
    
    # 4、导入按键的检测
    from pygame.locals import *
    
    # 6、导入时间库,控制程序运行速度
    import time
    
    
    # 7、创建飞机类
    
    
    class PlayerPlane(object):
        def __init__(self):
            # 子弹列表
            self.bulletList = []
    
            planeImageName = "/home/li/Documents/image/hero1.png"
            self.image = pygame.image.load(planeImageName).convert_alpha()
            planeImageName1 = "/home/li/Documents/image/hero2.png"
            self.image1 = pygame.image.load(planeImageName1).convert_alpha()
            # 初始位置
            self.x = 230
            self.y = 600
            # 设置速度
            self.speed = 5
            # 设置名字
            self.planeName = "player"
    
        def draw(self, screen):
            screen.blit(self.image, (self.x, self.y))
            # screen.blit(self.image1, (self.x, self.y))
    
        def keyHandle(self, key):
            if key == K_a or key == K_LEFT:
                pp1.x -= 10
                print("left")
            elif event.key == K_d or event.key == K_RIGHT:
                pp1.x += 10
                print("right")
            elif event.key == K_w or event.key == K_UP:
                pp1.y -= 10
                print("up")
            elif event.key == K_s or event.key == K_DOWN:
                pp1.y += 10
                print("down")
            elif event.key == K_SPACE:
                # 按下空格,生成一个子弹对象
                b1 = Bullet()
                # b1.draw(screen)
                print("space")
    
        def position(self):
            return(self.x, self.y)
    
    
    # 8、创建子弹类
    # 8.1单颗子弹,如何把他画出来?考虑飞机的画法。飞因为只有一个,所以实例化一个飞机,直接在循环中pp1.draw。
    # 8.1那么也定义一个子弹集合类,这个集合类有个方法,draw所有子弹,是否可行?
    class Bullet(object):
        def __init__(self):
            print("Bullet init!")
    
            # bulletImageName = "/home/li/Documents/image/bullet1.png"
            # self.image = pygame.image.load(bulletImageName).convert_alpha()
            # 初始位置,和飞机的初始位置有关系hero1为100x124。这里要调用飞机的位置属性了。
            self.x = pp1.position()[0]+47
            self.y = pp1.position()[1]-15
            bc1.getbc().append(self)
    
            # 存储所有打出去的子弹
    
    
    class BulletCollection(object):
        def __init__(self):
            print("Collection init!")
            self.Collection = []
    
            bulletImageName = "/home/li/Documents/image/bullet1.png"
            self.image = pygame.image.load(bulletImageName).convert_alpha()
    
        def getbc(self):
            return self.Collection
    
        def draw(self, screen):
    
            for t in self.Collection:
                # 子弹飞出屏幕外就删掉
                if t.y <= 0:
                    self.Collection.remove(t)
                # 没有就继续往前飞
                else:
                    screen.blit(self.image, (t.x, t.y))
                    t.y -= 5
            # 打印子弹存量
            print(len(self.Collection))
    
    # 9、创建敌人飞机类=================================================
    
    
    class Enemy(object):
        def __init__(self):
            # 子弹列表
            self.bulletList = []
            self.turn = 0
    
            planeImageName2 = "/home/li/Documents/image/enemy1.png"
            self.image2 = pygame.image.load(planeImageName2).convert_alpha()
            # 初始位置
            self.x = 115
            self.y = 20
            # 设置速度
            self.speed = 5
            # 设置名字
            self.planeName = "enemy"
    
        def mov(self, turn):
            if turn == 0:
                self.x += 5
            else:
                self.x -= 5
    
        def draw(self, screen):
    
            if self.x <= 0:
                self.turn = 0
            elif self.x >= 420:
                self.turn = 1
    
            if self.turn == 0:
                self.x += 5
            else:
                self.x -= 5
    
            screen.blit(self.image2, (self.x, self.y))
            # screen.blit(self.image1, (self.x, self.y))
    
    
    # 2、打开一个窗口,加载背景
    if __name__ == '__main__':
        screen = pygame.display.set_mode((480, 852), 0, 32)
        bgImamgeFile = '/home/li/Documents/image/background.png'
        background = pygame.image.load(bgImamgeFile).convert()
        pp1 = PlayerPlane()
        bc1 = BulletCollection()
        em1 = Enemy()
    
        # b1 = Bullet()
    
    
    # 3、背景贴到screen上,然后刷新窗口。while True非常耗资源。
    
    while True:
    
        screen.blit(background, (0, 0))
        pp1.draw(screen)
        bc1.draw(screen)
        # b1.draw(screen)
        em1.draw(screen)
    
    
    # 5、按键的检测。固定代码块。先执行blit,再看这里有没有按键,有就执行,没有就刷新,再循环一次。
        for event in pygame.event.get():
            if event.type == QUIT:
                print("exit")
                exit()
            elif event.type == KEYDOWN:
                pp1.keyHandle(event.key)
                # if event.key == K_a or event.key == K_LEFT:
                #     pp1.x -= 10
                #     print("left")
                # elif event.key == K_d or event.key == K_RIGHT:
                #     pp1.x += 10
                #     print("right")
                # elif event.key == K_w or event.key == K_UP:
                #     pp1.y -= 10
                #     print("up")
                # elif event.key == K_s or event.key == K_DOWN:
                #     pp1.y += 10
                #     print("down")
                # elif event.key == K_SPACE:
                #     print("space")
    
        pygame.display.update()
    
    # 6.1 1秒会很卡。比较好的是0.01
        time.sleep(0.01)
    
    

    相关文章

      网友评论

          本文标题:Python学习笔记之【打飞机】2019-08-15

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