星汉灿烂的planet模块最终代码

星汉灿烂的planet模块最终代码

python pygame star shinning星汉灿烂planet模块

python pygame star shinning星汉灿烂planet模块

python pygame star shinning星汉灿烂planet模块


下面是部分代码预览:

"""
   宇宙类作品_星汉灿烂的核心程序,
   playnet.py模块。
   ,本程序会显示太阳星空背景,八大行星会出现,等待单击.
   单击后,它们的介绍图片会从右侧出现,再次单击,
   则图片又会从左到右慢慢消失。如果某天本模块由于Python
   或操作系统升级原因导致挂了,请自行修改源码,这里提供
   的是基础思想。
"""

import pygame
from pygame.locals import *

class Intro(pygame.sprite.Sprite):
    """介绍页类,它是行星描述信息的包装,继承自pygame的角色类"""
    def __init__(self,image,screen,group,行星类):
        """image:图像,screen:所在屏幕,
           group:组,行星类:本模块的行星类"""

    def update(self):
        """实时更新,如果visible为真,并且dx是-1,则往左移"""
        
class Planet(pygame.sprite.Sprite):
    """行星类,继承自pygame的角色类"""
    show = False                 # 类变量,控制只能同时显示一个介绍
    def __init__(self,image,pos,intro,group):
        """image:图形,pos:坐标,intro:介绍对象,group:组"""
        
    def update(self):
        """更新,行星碰到鼠标指针并且按下了鼠标左键时,
           就把它的介绍页的可见性设为True。然后把类变量show
           的值也设为True,这样就阻止了再次单击其它行星时把
           其它介绍页也显示出来。
           
        """
def make_planet(intro_group,planet_group,screen):
    """
       加载资源,形成行星介绍页与行星角色,把它们加入到相应的组中去。
       intro_group:介绍组,planet_group:行星组,screen:屏幕

    """
    

def show_planet(screen,background):    
    """显示行星,等待单击各行星"""

    intro_group = pygame.sprite.Group()          # 介绍组
    planet_group = pygame.sprite.Group()         # 行星组

    make_planet(intro_group,planet_group,screen) # 生成若干行星
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:running=False
            
        planet_group.update()                    # 更新每个行星
        intro_group.update()                     # 更新每张介绍页
        
        screen.blit(background,(0,0))
        planet_group.draw(screen)
        # 如果介绍页是可见的才把它渲染出来
        [screen.blit(p.image,p.rect) for p in intro_group if p.visible]
        pygame.display.update()

    pygame.quit()
    
def main():
    """主调用函数"""
    size = width,height = 480,360    
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("星汉灿烂的核心程序by lixingqiu")

    background = pygame.image.load("images/太阳.png")
    show_planet(screen,background)
    
if __name__ == "__main__":

    main()        
        

 

下载完整源代码与素材,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。