Mac で Pygame

とりあえず 1000speakers 後の Hackathon です

20080524205532.jpg
pygame というのを知ったのでさわってみます。

pygame のインストール

Mac のバイナリはここから DL 出来る
http://rene.f0o.com/~rene/stuff/macosx/

(The one downloaded from python.org not the system python installed with OSX).

http://rene.f0o.com/~rene/stuff/macosx/

との事なので Python を入れる

Pythonのインストール

http://python.org/download/mac/
このへんをたどっていって

http://www.pythonmac.org/packages/py25-fat/index.html
ここから DL する
http://www.python.org/download/
ここだった

エラーがでた

ImportError: PyObjC 1.2 or later is required to use pygame on Mac OS X. http://pygame.org/wiki/PyObjC

なので PyObjC をバージョンアップ
http://rene.f0o.com/~rene/stuff/macosx/pyobjc-1.4-py2.5-macosx10.4.mpkg.zip

CarbonEmacs の python-mode で /usr/local/bin/python を見にいくようにする

~/.MacOSX/environment.plist
を作って下記を記述

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
  <dict>
    <key>PATH</key>
    <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin</string>
  </dict>
</plist>

ログインしなおして完了

テュートリアルっぽいの

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, pygame
pygame.init()

size = width, height = 800, 600
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load('tksk.jpg')
ballrect = ball.get_rect()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            sys.exit()
        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = - speed[1]

        screen.fill(black)
        screen.blit(ball, ballrect)
        pygame.display.flip()

某画像がうごいた!!!
やった!!
2e19caeb44db4128de571cb7824184d1

id:nishiohirokazu を好きにうごかす

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *

SCR_W = 800
SCR_H = 600
black = 0, 0, 0

class Nishio(pygame.sprite.Sprite):
    def __init__(self, pos=(0,0)):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('nishio.jpg')
        self.image.convert()
        self.rect = self.image.get_rect()
        self.rect.topleft = pos
    def update(self, keyin, *args):
        x=0
        y=0
        if keyin[K_LEFT]:
            x = -2
        if keyin[K_RIGHT]:
            x = 2
        if keyin[K_UP]:
            y = -2
        if keyin[K_DOWN]:
            y = 2
        self.rect.move_ip(x, y)

def main():
    pygame.init()
    screen = pygame.display.set_mode( (SCR_W, SCR_H) )
    pygame.display.set_caption('nishio pygame')

    spritegroup = pygame.sprite.RenderClear()
    nishio = pygame.sprite.RenderClear(Nishio())

    clock = pygame.time.Clock()
    while 1:
        clock.tick(60)
        screen.fill(black)
        nishio.draw(screen)
        pygame.display.flip()
        nishio.update(pygame.key.get_pressed())
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            if (event.type == KEYDOWN and event.key == K_ESCAPE):
                return

if __name__ == '__main__':
    main()

できた。
スプライトとかつかってみた

ba7c44e65c468cc832c411146a6ff411