神経すいじゃくのようなものを作る(1)

神経すいじゃく(漢字で書くと気が滅入るので平仮名で書いている)のようなものをPygameで作る。

  • 仕様のようなもの
    • 4×4の升目がある。
    • 好きなところをクリック。そこに絵が表示される。
    • 2回クリックして絵が合えばOK。
    • すべての絵が表示されれば終了。
  • 今回学ぶこと
    • ランダムに絵を配置する方法。
    • すべて絵が表示されたときの処理。

これぐらいか。

さわりだけコードを書いた。random.shuffle()を使ってみた。ランダムに配置できた。
実行すると終了できない。

#!/bin/env python
# coding: utf-8
# --------------------------------------------
#             ver.0.1
#             2008/11/13
import pygame
import sys
import random
from pygame.locals import *

WDTH = 100
TNUM = 4
SCR_W = WDTH * TNUM
SCR_H = WDTH * TNUM

""" initial setting """
pygame.init()
screen = pygame.display.set_mode( (SCR_W, SCR_H) )
image = []
ckey = [1,1,1,1,1,1,1,1]
imagerect = [1,1,1,1,1,1,1,1]

image.append(pygame.image.load('red.bmp'))
image.append(pygame.image.load('blue.bmp'))
image.append(pygame.image.load('green.bmp'))
image.append(pygame.image.load('pink.bmp'))
image.append(pygame.image.load('violet.bmp'))
image.append(pygame.image.load('yellow.bmp'))
image.append(pygame.image.load('orange.bmp'))
image.append(pygame.image.load('black.bmp'))

for i in range(TNUM*TNUM/2):
    image[i] = image[i].convert()
#    ckey[i] = image[i].get_at((0,0))
#    image[i].set_colorkey(ckey[i])
    imagerect[i] = image[i].get_rect()

alist = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
random.shuffle(alist)

def main():
    screen.fill((255,255,255))
    pygame.display.set_caption('Picture Matching')
    pygame.mouse.set_visible(True)
    screen.blit(image[0], imagerect[0].move(0,0))

    for i in xrange(TNUM):
        for j in xrange(TNUM):
            num = TNUM*i+j
            screen.blit(image[alist[num]/2],
                        imagerect[alist[num]/2].move(WDTH*i,WDTH*j))
    pygame.display.flip()

if __name__ == '__main__':
    main()

実行結果の図。