""" 可爱的金币天使.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() # 屏幕高度 def goto_top(self): """到上面随机一个位置""" self.hideturtle() def move(self): """不断地移动对象""" x = self.xcor() y = self.ycor() self.setx(x + self.xspeed) # x坐标增加 self.sety(y + self.yspeed) # y坐标增加 def collide(self): """碰到angel的检测,以距离进行判断""" 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()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)