仿3D星空.py

"""仿3D星空.py,本程序定义了一个Dot类,点越大,移动的速度越快,所以有种3D效果"""

__author__ = "李兴球"
__date__  = "2018年6月"
__company__ = "风火轮少儿编程"

import pygame
from pygame.locals import *
from random import randint

class Dot():
    def __init__(self,r,screen):
        self.radius = r   # 半径
        self.screen = screen        
        self.sw = screen.get_width()
        self.sh = screen.get_height()
        self.image = pygame.Surface((2*r,2*r))     # 建立表面,长宽为2*r,2*r
        self.image.set_colorkey((0,0,0))           # 设置表面的透明色
        pygame.draw.circle(self.image,(255,255,255),(r,r),r,0)   # 在这个表面上画个白色的圆
        self.rect = self.image.get_rect()          # 得到self.image在screen上的坐标与长宽
        self.rect.x = self.sw+ r * 2
        self.rect.y = randint(0,self.sh-1)
        self.delete =0                             # 删除标志
    
    def move(self):
       self.rect.x = self.rect.x - self.radius     # self.radius被当成每帧移动的像素了
       if self.rect.right<=0:  self.delete = 1     # 标志为待删除
       
    def draw(self):
        self.screen.blit(self.image,self.rect)

def main():

    pygame.init()
    title = "3D星空_点类:作者:李兴球 www.scratch8.net"
    screen_width,screen_height = 480,360
    screen = pygame.display.set_mode((screen_width,screen_height)) 

    t = pygame.time.Clock()    
    dotList = []
    running = True
本文隐藏内容 登陆 后才可以浏览
pygame.quit() if __name__ =="__main__": main()