"""
宇宙类作品_星汉灿烂的行星与介绍类测试程序,
playnet.py模块。
,本程序单击地球,会从右到左出现一张介绍地球的图片,
再次单击,则图片又会从左到右慢慢消失。
"""
import pygame
from pygame.locals import *
class Intro(pygame.sprite.Sprite):
"""介绍对象,继承自pygame的角色类"""
def __init__(self,image,screen):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.sw = screen.get_width()
self.rect = image.get_rect(topleft=(self.sw,0))
self.visible = False
self.dx = -1
def update(self):
"""实时更新"""
if self.visible and self.dx == -1:
if self.rect.x > 0 :
self.rect.move_ip(self.dx,0)
else:
clicked = pygame.mouse.get_pressed()
if clicked[0] : self.dx = 1
else:
if self.rect.x < self.sw:
self.rect.move_ip(self.dx,0)
else:
print("运行了这")
self.visible = False
self.dx = -1
class Planet(pygame.sprite.Sprite):
"""行星类,继承自pygame的角色类"""
def __init__(self,image,pos,intro):
"""image:图形,pos:坐标,intro:介绍对象"""
pygame.sprite.Sprite.__init__(self)
self.intro = intro
self.image = image
w,h = image.get_size() # 得到宽度和高度
self.image_big = pygame.transform.scale(image,(w+10,h+10))
self.rect = image.get_rect(center=pos)
self.rect_big = self.image_big.get_rect(center=pos)
self.mask = pygame.mask.from_surface(self.image)
self.mask_big = pygame.mask.from_surface(self.image_big)
self.images = [self.image,self.image_big]
self.masks = [self.mask,self.mask_big]
self.rects = [self.rect,self.rect_big]
def update(self):
mp = pygame.mouse.get_pos()
dx = mp[0] - self.rect.x
dy = mp[1] - self.rect.y
if dx >= 0 and dx < self.rect.width and \
dy >=0 and dy < self.rect.height:
flag = self.mask.get_at((dx,dy))
clicked = pygame.mouse.get_pressed()
# 如果单击左键,把自己的介绍显示出来
if clicked[0] : self.intro.visible = True
else:
flag = 0
self.image = self.images[flag]
self.rect = self.rects[flag]
self.mask = self.masks[flag]
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
-
- 2026 年 1 月
- 2025 年 12 月
- 2025 年 11 月
- 2025 年 10 月
- 2025 年 9 月
- 2025 年 6 月
- 2025 年 5 月
- 2025 年 3 月
- 2025 年 2 月
- 2025 年 1 月
- 2024 年 12 月
- 2024 年 8 月
- 2024 年 6 月
- 2024 年 5 月
- 2024 年 4 月
- 2024 年 3 月
- 2024 年 2 月
- 2023 年 11 月
- 2023 年 9 月
- 2023 年 6 月
- 2023 年 5 月
- 2023 年 4 月
- 2023 年 3 月
- 2023 年 2 月
- 2023 年 1 月
- 2022 年 12 月
- 2022 年 11 月
- 2022 年 10 月
- 2022 年 9 月
- 2022 年 8 月
- 2022 年 7 月
- 2022 年 6 月
- 2022 年 5 月
- 2022 年 4 月
- 2022 年 3 月
- 2022 年 2 月
- 2022 年 1 月
- 2021 年 12 月
- 2021 年 11 月
- 2021 年 10 月
- 2021 年 9 月
- 2021 年 8 月
- 2021 年 7 月
- 2021 年 6 月
- 2021 年 5 月
- 2021 年 4 月
- 2021 年 3 月
- 2021 年 2 月
- 2021 年 1 月
- 2020 年 12 月
- 2020 年 11 月
- 2020 年 10 月
- 2020 年 9 月
- 2020 年 8 月
- 2020 年 7 月
- 2020 年 6 月
- 2020 年 5 月
- 2020 年 4 月
- 2020 年 3 月
- 2020 年 2 月
- 2020 年 1 月
- 2019 年 12 月
- 2019 年 11 月
- 2019 年 10 月
- 2019 年 9 月
- 2019 年 8 月
- 2019 年 7 月
- 2019 年 6 月
- 2019 年 5 月
- 2019 年 4 月
- 2019 年 3 月
- 2019 年 2 月
- 2018 年 3 月
- 2018 年 1 月
- 2017 年 9 月
- 2017 年 5 月
- 2017 年 1 月
