可爱的金币天使

"""
   可爱的金币天使.py
   天下掉来下很多金币,通过按上下左右方向箭头去按住它们。   
"""
from random import randint
from turtle import Turtle,Screen

class Coin(Turtle):
    """定义Coin类,继承自Turtle类"""
    def __init__(self,angel,image):

        """初始化函数,
        参数:
        angel:引用的另一个对象
        image:造型
        """
        Turtle.__init__(self,shape = image,visible= False)
        self.angel = angel
        self.color("white")
        self.penup()        
        self.xspeed = 0
        self.yspeed = randint(-5,-1)
        self.w = self.screen.window_width()   # 屏幕宽度
        self.h = self.screen.window_height()  # 屏幕高度        
        self.goto_top()        
        self.move()

        
    def goto_top(self):
        """到上面随机一个位置"""
        self.hideturtle()
        x = randint(-self.w//2,self.w//2)
        y = randint(self.h//2,self.h//2 + randint(0,self.h))
        self.goto(x,y)
        self.showturtle()
        
    def move(self):
        """不断地移动对象"""
        x = self.xcor()
        y = self.ycor()
        self.setx(x + self.xspeed)           # x坐标增加
        self.sety(y + self.yspeed)           # y坐标增加
        if self.ycor() < -self.h//2:         # 到了最下面
            self.goto_top()                  # 就移到上面
        if self.collide():                   # 碰到 天 使
            self.angel.counter += 1         # 计数器加1
            self.goto_top()                  # 移到最上面
            self.move()
            info = "可爱的金币天使 www.lixingqiu.com"
            self.screen.title(info + "金币数:" + str(self.angel.counter))
        else:
            self.screen.ontimer(self.move,10)

You must log in to read the rest of this article. Please log in or register as a user.