在海龟画图屏幕上铺满了相同颜色的矩形块,但是有一个颜色稍有一点不一样,请把它找出来。下面是部分代码预览:
"""
找色块小游戏。本程序显示一些颜色块,其中有一个颜色有点不同,请迅速把它找出来!
这个游戏代码主要来源于翻转格子版本2的代码,不过把它做成游戏形式了。
读者可以把它继续做成多关卡游戏。如限定时间,如有三次机会等等。
"""
__author__ = '李兴球'
__date__ = '2019/10/5'
from turtle import *
from random import choice
class Rect:
"""定义矩形类"""
def __init__(self,x,y,width,height):
"""
x,y:左上角坐标
width,height:宽度和高度
"""
self.x = x
self.y = y
self.width = width
self.height = height
def collidepoint(self,point):
"""
point是一个坐标点,
本方法判断这个点是否在矩形内
"""
x,y = point
c1 = x >= self.x and x <=self.right
c2 = y >= self.bottom and y <= self.y
return c1 and c2
def draw(self,t,color):
"""用海龟t把自己画出来"""
self.c = color
t.goto(self.x,self.y) # 定位到左上角
def main():
"""主要执行函数"""
width,height = 600,600
screen = Screen()
t = Turtle(visible=False)
t.penup()
t.color('gray')
t.sety(height/2-10)
ft = ('黑体',20,'normal')
t.write("请找出不同颜色块",align='center',font=ft)
rects = make_rects(10,10,500,500) # 生成10行10列的矩形
normal_color = (0,255,0) # 正常矩形的填充颜色
diff_color = (100,255,100) # 不同样的那个矩形的颜色
[r.draw(t,normal_color) for r in rects] # 用海龟t把每个矩形画出来
r = choice(rects) # 随机挑一个矩形
r.draw(t,diff_color) # 把这个矩形换成不同的颜色
screen.mainloop()
if __name__ == "__main__":
main()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

