• python编程从入门到实践第13章


    13-1

    import pygame
    import sys
    from pygame.sprite import Group
    from pygame.sprite import Sprite
    from random import randint
     
    class Settings():
        def __init__(self):
            self.screen_width = 1200
            self.screen_height = 800
            self.bg_color = (255,255,255)
     
     
    class Huaji(Sprite):
        def __init__(self, settings, screen):
            super(Huaji,self).__init__()
            self.settings = settings
            self.screen = screen
            self.image = pygame.image.load('huaji.bmp')
            self.rect = self.image.get_rect()
            self.rect.x = self.rect.width
            self.rect.y = self.rect.height
            self.x = float(self.rect.x)
     
        def blitme(self):
            self.screen.blit(self.image, self.rect)
     
     
    def get_number_huajis_x(settings, huaji_width):
        available_space_x =settings.screen_width -  huaji_width
        number_huajis_x = int(available_space_x / (2 * huaji_width))
        return number_huajis_x
     
     
    def get_number_rows(settings, huaji_height):
        available_space_y = (settings.screen_height - huaji_height )
        number_rows = int(available_space_y / (2 * huaji_height))
        return number_rows
     
     
    def create_huaji(settings, screen, huajis, huaji_number, row_number):
        huaji = Huaji(settings, screen)
        huaji_width = huaji.rect.width
        huaji.x = randint(0,1200) 
        huaji.rect.x = huaji.x
        huaji.rect.y = randint(0,800) 
        huajis.add(huaji)
     
    def create_huaji_fleet(settings, screen, huajis):
        huaji = Huaji(settings, screen)
        number_huajis_x = get_number_huajis_x(settings, huaji.rect.width)
        number_rows = get_number_rows(settings, huaji.rect.height)
     
        for row_number in range(number_rows):
            for huaji_number in range(number_huajis_x):
                create_huaji(settings, screen, huajis, huaji_number, row_number)
     
    import pygame
    import sys
    from pygame.sprite import Group
    from pygame.sprite import Sprite
    from xingxing import Huaji
    from xingxing import Settings
    import xingxing as xx
    def run_huaji():
        pygame.init()
        settings = Settings()
        screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
        pygame.display.set_caption('滑稽滑稽')
        huajis = Group()
        xx.create_huaji_fleet(settings, screen, huajis)
        huaji = Huaji(settings,screen)
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
     
            screen.fill(settings.bg_color)
            huajis.draw(screen)
            pygame.display.flip()
     
     
    run_huaji()

    输出:

     13-2 

    def create_huaji(settings, screen, huajis, huaji_number, row_number):
        huaji = Huaji(settings, screen)
        huaji_width = huaji.rect.width
        huaji.x = randint(0,1200) 
        huaji.rect.x = huaji.x
        huaji.rect.y = randint(0,800) 
        huajis.add(huaji)

      就是很简单的把图片xy坐标用randint变成随机的,不过我的结果有重叠的,不知道有没有不重叠的办法

    输出:

     13-3  咋回事,明明带了行循环,为啥只显示一行  看着没毛病。。

    import pygame
    import sys
    from pygame.sprite import Sprite
    from pygame.sprite import Group
    
    
    class Settings():
        def __init__(self):
            self.screen_width = 1200
            self.screen_height = 800
            self.bg_color  = (255,255,255)
            
            self.qianbenying_speed_factor = 1
    class Qianbenying(Sprite):
        def __init__(self,settings,screen):
            super(Qianbenying,self).__init__()
            self.screen = screen
            self.settings =settings
            self.image = pygame.image.load('qianbenying.bmp')
            self.rect = self.image.get_rect()
            self.rect.y = self.rect.height
            self.rect.x = self.rect.width
            self.qianbenying_speed_factor = 1
            self.x = float(self.rect.x)
            self.y = float(self.rect.y)
        def update(self):
            '''向下移动子弹'''
            self.y += self.qianbenying_speed_factor 
            self.rect.y = self.y
            
        def blitme(self):
            self.screen.blit(self.image,self.rect)
    
    
    def get_number_qianbenyings_x(settings, qianbenying_width):
        available_space_x =settings.screen_width -  qianbenying_width * 2
        number_qianbenyings_x = int(available_space_x / (2 * qianbenying_width))
        return number_qianbenyings_x
        
    def get_number_rows(settings, qianbenying_height):
        available_space_y = (settings.screen_height - qianbenying_height )
        number_rows = int(available_space_y / (2 * qianbenying_height))
        return number_rows
        
    def create_qianbenying(settings, screen, qianbenyings, 
                                    qianbenying_number, row_number):
        qianbenying = Qianbenying(settings, screen)
        qianbenying_width = qianbenying.rect.width
        qianbenying.x = (qianbenying_width + 2 * qianbenying_width * 
                                                    qianbenying_number)
        qianbenying.rect.x = qianbenying.x
        qianbenying.rect.y = qianbenying.rect.height + (2  *qianbenying.rect.height * row_number)
        qianbenyings.add(qianbenying)
        
    def create_qianbenying_fleet(settings, screen, qianbenyings):
        qianbenying = Qianbenying(settings, screen)
        number_qianbenyings_x = get_number_qianbenyings_x(settings,qianbenying.rect.width)
        number_rows = get_number_rows(settings, qianbenying.rect.height)
     
        for row_number in range(number_rows):
            for qianbenying_number in range(number_qianbenyings_x):
                create_qianbenying(settings, screen, qianbenyings, qianbenying_number, row_number)
    
    def update_qianbenyings(settings,qianbenyings):
        qianbenyings.update()
        for qianbenying in qianbenyings.copy():
            if qianbenying.rect.top >= settings.screen_height:
                qianbenyings.remove(qianbenying)
    def runq():
        pygame.init()
        settings = Settings()
        screen = pygame.display.set_mode((settings.screen_width,settings.screen_height))
        #游戏窗口标题
        pygame.display.set_caption("千本樱景严")
        #创建一艘飞船,一个子弹编组和一个外星人编组
        
        qianbenyings = Group()
        #创建外星人群
        create_qianbenying_fleet(settings,screen,qianbenyings)
        #设置背景色
        
        #开始游戏的主循环
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
            screen.fill(settings.bg_color)
            qianbenyings.draw(screen)
            update_qianbenyings(settings,qianbenyings)
            pygame.display.flip()
    runq()

    13-4 update_qianbengyings里加一句这个,更新下参数就行了, 然后发现删除掉update有关的就是移动的函数, 运行不动的程序时是多行的,可是运行起来就是单行的,这未免有点奇怪了,按理说移动的函数规定的是单个图片的属性,不会影响行之类的啊

    if qianbenying.rect.top >= settings.screen_height:
            create_qianbenying_fleet(settings,screen,qianbenyings)

     13-5

  • 相关阅读:
    剑指offer-翻转单词序列
    剑指offer-丑数
    剑指offer-把数组排成最小的数
    mysql笔记(13)-视图的概念和使用
    mysql笔记(12)-外键约束的添加和删除
    mysql笔记(11)-约束的添加、修改和删除
    mysql笔记(10)-数据的插入和更新(insert/update/case)
    mysql笔记(9)-表的创建和删除(drop/truncate/delete)
    mysql笔记(8)-嵌套查询之in、exists
    mysql笔记(7)-多表查询之自然连接、外连接
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12354524.html
Copyright © 2020-2023  润新知