可爱的金币天使

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

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

本文隐藏内容 登陆 后才可以浏览
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) def collide(self): """碰到angel的检测,以距离进行判断""" distance = self.distance(self.angel) if distance < 60 and self.isvisible() : return True else: return False if __name__ == "__main__": coin = "coin.gif" girl = "character.gif" screen = Screen() screen.bgcolor("black") screen.delay(0) screen.title("可爱的金币天使 www.lixingqiu.com") screen.addshape(coin) screen.addshape(girl) angel = Turtle(shape = girl) angel.penup() angel.counter = 0 angel.sety(-240) screen.onkeypress(lambda:angel.setx(angel.xcor() - 10),"Left") screen.onkeypress(lambda:angel.setx(angel.xcor() + 10),"Right") screen.onkeypress(lambda:angel.sety(angel.ycor() - 10),"Down") screen.onkeypress(lambda:angel.sety(angel.ycor() + 10),"Up") [Coin(angel,coin) for i in range(10)] screen.listen() screen.mainloop()

可爱的金币天使