python challeng 22 解题总结

python challenge 22网址:www.pythonchallenge.com/pc/hex/copper.html,查看网页源代码会发现一个提示:maybe white.gif would be more bright,于是打开和下载www.pythonchallenge.com/pc/hex/white.gif,发现这个gif是多帧的,差不多在图像的正中间有比黑色稍微亮一点的颜色……那些颜色出现在小键盘一样布局的3*3的9个位置上。然后一次当作方向向量描点处理.

源代码如下:

 

import Image, ImageSequence

img = Image.open("white.gif") # http://www.pythonchallenge.com/pc/hex/white.gif
out = Image.new("P", (125, 125))
pix = out.load()
pos = [25, 25]

for x in [list(f.getdata()).index(8) for f in ImageSequence.Iterator(img)]:
    if x == 19698:
        pos[0] -= 1
        pos[1] -= 1
    elif x == 19700:
        pos[1] -= 1
    elif x == 19702:
        pos[0] += 1
        pos[1] -= 1
    elif x == 20098:
        pos[0] -= 1
    elif x == 20100:
        pos = [pos[0] + 10, pos[1] + 10]
    elif x == 20102:
        pos[0] += 1
    elif x == 20498:
        pos[0] -= 1
        pos[1] += 1
    elif x == 20500:
        pos[1] += 1
    elif x == 20502:
        pos[0] += 1
        pos[1] += 1

    pix[pos[0], pos[1]] = 200

out.save("solution.png")

 

下一关的网址:www.pythonchallenge.com/pc/hex/bonus.html(用户名:butter,密码:fly)