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


    12-1

    import sys
    import pygame
    def open_window():
        pygame.init()
        screen = pygame.display.set_mode((1200,700))
        pygame.display.set_caption("没有想好叫什么名字")
        #背景色
        bg_color = (135,206,235)
        while True:
            screen.fill(bg_color)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
            pygame.display.flip()
    open_window()

    12-2    放在正中央应该是center, 然后不和边缘接触不用bottom top left啥的。。

    写完后 引入模块 然后要在循环前创建引入图片,在循环里引入方法。 图片的背景颜色应该是说自己在PS之类的改吧,还是能用代码改?现在还不会,要改只能改背景。懒得改了

    blit函数

    做出blit这个动作的人是一个Surface类的实例,

    这个人即将在自己身上画图,

    他需要两个参数:要画的图片,和画的位置,即source和dest.

    import pygame 
    
    class Hudieren():
        
        def __init__(self,screen):
    
            self.screen =screen
            
            
            self.image = pygame.image.load('images/hudieren.bmp')
            self.rect = self.image.get_rect()
            self.screen_rect = screen.get_rect()
            
            self.rect.center = self.screen_rect.center
         
            
        def blitme(self):
            self.screen.blit(self.image,self.rect)

    12-3  在完成上下移动的时候,自己照葫芦画瓢,然后出了BUG  pygame.Rect' object has no attribute 'up' , 查了一会发现,原来是根据移动标志调整飞船位置这里, and 后面,self.rect.top bottom,我原来写的是up和down,主要对这里的什么模块函数啥的都不懂,感觉到了这节书里讲的不细致了

    if self.moving_up and self.rect.top > 0:
                self.centery -= self.ai_settings.ship_speed_factor  
    if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
                self.centery += self.ai_settings.ship_speed_factor

    12-4

    import sys  
    import pygame  
    def test():  
        pygame.init()  
        screen = pygame.display.set_mode((800,600))  
        pygame.display.set_caption("这是一个会动的屏幕")  
        bg_color = (255,255,255)  
        while True:  
            for event in pygame.event.get():  
                if event.type == pygame.QUIT:  
                    sys.exit()  
                elif event.type == pygame.KEYDOWN:  
                    if event.key == pygame.K_UP:  
                        screen = pygame.display.set_mode((100,600))   
                    elif event.key == pygame.K_DOWN:  
                        screen = pygame.display.set_mode((100,100)) 
                    elif event.key == pygame.K_LEFT:  
                        screen = pygame.display.set_mode((600,600)) 
                    elif event.key == pygame.K_RIGHT:  
                        screen = pygame.display.set_mode((800,800)) 
            screen.fill(bg_color)  
            pygame.display.flip()  
    test()      

     12-5  不写这个了  我多消化下

  • 相关阅读:
    mysql 统计新增每天数据
    Oracle dg下掉一个从库
    rman全备脚本
    Linux Shell 统计一(行列)数值的总和及行、列转换
    pt工具加字段脚本
    MySQL慢日志切割邮件发送脚本
    MySQL主从复制邮件报警脚本
    读书清单
    数据库学习笔记
    JAVAEE学习笔记
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12313407.html
Copyright © 2020-2023  润新知