メニュー

pygame.examples

サンプルプログラムのモジュール

これらのサンプルプログラムは、pygameを使い始めるうえで助けになるでしょう。Here is a brief rundown of what you get.これらのサンプルプログラムのソースコードには、パブリックドメインが適用されています。プログラムの際にはご自由にお使いください。

サンプルプログラムを実行するには様々な方法があります。まず、これらのサンプルをプログラム単体として実行する方法があります。次に、インポートして各main()命令を実行する方法があります(詳しくは下記を参照してください)。 最後に、最も簡単なのはpython -m optionを使う方法です:

   python -m pygame.examples.<example name> <example arguments>

例:

   python -m pygame.examples.scaletest someimage.png

サンプルゲームで使用している素材は、pythonフォルダ/Lib/site-packages/pygame/examples/dataの階層に保存されています。

We're always on the lookout for more examples and/or example requests.こうしたソースコードは、pythonでゲーム開発を始めるうえで最も役立つものでしょう。

このページには、元となったサンプルプログラムのreadme.txtに記載された規約内容が適用されます。元サンプルの権利者は下記の方です。:

    Pete Shinners
    shredwheat@mediaone.net

加えて下記サイトも参照してください。

  pySDL    : http://pysdl.sourceforge.net
  SDL      : http://www.libsdl.org
  aliens.c : http://www.libsdl.org/projects/aliens

examplesモジュールはpygameのバージョン1.9.0で新たにパッケージに加えられました。

module of example programs

These examples should help get you started with pygame. Here is a brief rundown of what you get. The source code for these examples is in the public domain. Feel free to use for your own projects.

There are several ways to run the examples. First they can be run as stand-alone programs. Second they can be imported and their main() methods called (see below). Finally, the easiest way is to use the python -m option:

   python -m pygame.examples.<example name> <example arguments>

eg:

   python -m pygame.examples.scaletest someimage.png

Resources for the examples are found in the pygame/examples/data subdirectory.

We're always on the lookout for more examples and/or example requests. Code like this is probably the best way to start getting involved with python gaming.

This page is adapted from the original examples readme.txt by:

    Pete Shinners
    shredwheat@mediaone.net

Further references

  pySDL    : http://pysdl.sourceforge.net
  SDL      : http://www.libsdl.org
  aliens.c : http://www.libsdl.org/projects/aliens

examples as a package is new to pygame 1.9.0.


pygame.examples.aliens.main

インベーダーゲームの完成バージョンのサンプルをプレイします。
pygame.examples.aliens.main(): return None

このインベーダーゲームのサンプルは、元々SDLのデモストレーションのために作られました。今では単なるサンプルを超えて、十分に遊べるゲームにまで進化しています。このサンプルでは、スプライト機能や最適化したコピー描写処理の様々使用方法を豊富に見ることができます。また、透過処理、透明色、フォント、音声、音楽、ジョイスティック機能など、他にも様々な処理が取り入れされています。(追伸。私のハイスコアは117です!あなたも頑張ってください)

play the full aliens example
pygame.aliens.main(): return None

This started off as a port of the SDL demonstration, Aliens. Now it has evolved into something sort of resembling fun. This demonstrates a lot of different uses of sprites and optimized blitting. Also transparency, colorkeys, fonts, sound, music, joystick, and more. (PS, my high score is 117! goodluck)


pygame.examples.oldalien.main

インベーダーゲームのオリジナルバージョンのサンプルをプレイします。
pygame.examples.oldalien.main(): return None

このインベーダーゲームのサンプルは、元々のSDLのデモストレーションの原型に近いものです。このソースコードはとてもシンプルに書かれているので、初めてコードを見る人にとってはよりよい一歩となるでしょう。ここで使われているコピー描写処理は最適化されているものではありませんが、単純な処理しかしていないので十分な速度で動きます。

play the original aliens example
pygame.examples.oldalien.main(): return None

This more closely resembles a port of the SDL Aliens demo. The code is a lot simpler, so it makes a better starting point for people looking at code for the first times. These blitting routines are not as optimized as they should/could be, but the code is easier to follow, and it plays quick enough.


pygame.examples.stars.main

シンプルな宇宙空間の画面を表示するサンプルを実行します。
pygame.examples.stars.main(): return None

シンプルな宇宙空間の画面を表示するサンプルです。画面上を右クリックすることで、視点の中心を変更することができます。

run a simple starfield example
pygame.examples.stars.main(): return None

A simple starfield example. You can change the center of perspective by leftclicking the mouse on the screen.


pygame.examples.chimp.main

動き回るチンパンジーを叩くゲームのサンプル。
pygame.examples.chimp.main(): return None

このサンプルは、pygameホームページにある「Chimp Tutorial, Line by Line」の項目で使われているコードを流用しています。チュートリアルページの'有名な'ウェブバナーをクリックすると、元になったソースコードを見ることができます。ソースコードにはコメントが書かれていますが、詳しい説明についてはpygameの該当チュートリアルページにあるのでそちらを参照してください。

hit the moving chimp
pygame.examples.chimp.main(): return None

This simple example is derived from the line-by-line tutorial that comes with pygame. It is based on a 'popular' web banner. Note there are comments here, but for the full explanation, follow along in the tutorial.


pygame.examples.moveit.main

画面上にアニメーションを表示します。
pygame.examples.moveit.main(): return None

これはpygameのチュートリアルページにある "How Do I Make It Move"の項目で説明している内容が全て含まれた、最新バージョンのサンプルです。このサンプルでは被写体を10個用意してそれらを画面上で動かしています。

このサンプルコードはバグチェックまでは十分できていませんが、代わりにかなり読みやすいものになっています :幸いこれはpythonなので、エラーコードの山と格闘するようなことはないでしょう。

display animated objects on the screen
pygame.examples.moveit.main(): return None

This is the full and final example from the Pygame Tutorial, "How Do I Make It Move". It creates 10 objects and animates them on the screen.

Note it's a bit scant on error checking, but it's easy to read. :] Fortunately, this is python, and we needn't wrestle with a pile of error codes.


pygame.examples.fonty.main

文字を描写するサンプルコードを実行します。
pygame.examples.fonty.main(): return None

フォントモジュールを使って複数の方法で文字描写をする、とても軽くとてもシンプルなアプリケーションです。

run a font rendering example
pygame.examples.fonty.main(): return None

Super quick, super simple application demonstrating the different ways to render fonts with the font module


pygame.examples.vgrade.main

垂直向きのグラデーションを描写します。
pygame.examples.vgrade.main(): return None

NumPyモジュールを使って垂直方向のグラデーションを作る方法の例です。このサンプルでは0.5秒ごとに新しいグラデーションが作られ、その画像を作成して描写するまでにかかった時間が通知されます。NumPyモジュールを使う予定がないのであれば。このサンプルのことは気にしないで下さい。:]

補足
 
付属しているサンプルは現在のnumpyのバージョンに適応していないようです。
独自に動作確認したサンプルを下記に記載します。

import os, pygame
from pygame.locals import *

try:
    from numpy import *
    from numpy.oldnumeric import *
    from numpy.random import *
except ImportError:
    raise SystemExit('This example requires Numeric and the pygame surfarray module')

pygame.surfarray.use_arraytype('numpy')

timer = 0
def stopwatch(message = None):
    "simple routine to time python code"
    global timer
    if not message:
        timer = pygame.time.get_ticks()
        return
    now = pygame.time.get_ticks()
    runtime = (now - timer)/1000.0 + .001
    print ("%s %s %s" %
           (message, runtime, ('seconds	(%.2ffps)'%(1.0/runtime))))
    timer = now



def VertGrad3D(surf, topcolor, bottomcolor):
    "creates a new 3d vertical gradient array"
    topcolor = array(topcolor, copy=0)
    bottomcolor = array(bottomcolor, copy=0)
    diff = bottomcolor - topcolor
    width, height = surf.get_size()
    # create array from 0.0 to 1.0 triplets
    column = arange(height, dtype=Float)/height
    column = repeat(column[:, newaxis], [3], 1)
    # create a single column of gradient
    column = topcolor + (diff * column).astype(Int)
    # make the column a 3d image column by adding X
    column = column.astype(UnsignedInt8)[newaxis,:,:]
    #3d array into 2d array
    column = pygame.surfarray.map_array(surf, column)
    # stretch the column into a full image
    return resize(column, (width, height))



def DisplayGradient(surf):
    "choose random colors and show them"
    stopwatch()
    colors = randint(0, 255, (2, 3))
    grade = VertGrad3D(surf, colors[0], colors[1])
    pygame.surfarray.blit_array(surf, grade)
    pygame.display.flip()
    stopwatch('Gradient:')



def main():
    pygame.init()
    size = 600, 400
    os.environ['SDL_VIDEO_CENTERED'] = '1'
    screen = pygame.display.set_mode(size, NOFRAME, 0)

    pygame.event.set_blocked(MOUSEMOTION) #keep our queue cleaner
    pygame.time.set_timer(USEREVENT, 500)

    while 1:
        event = pygame.event.wait()
        if event.type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
            break
        elif event.type == USEREVENT:
            DisplayGradient(screen)



if __name__ == '__main__': main()

display a vertical gradient
pygame.examples.vgrade.main(): return None

Demonstrates creating a vertical gradient with NumPy python. The app will create a new gradient every half second and report the time needed to create and display the image. If you're not prepared to start working with the NumPy arrays, don't worry about the source for this one :]


pygame.examples.eventlist.main

pygameのイベントを表示します。
pygame.examples.eventlist.main(): return None

このサンプルプログラムの画面は大雑把な作りになっていますが、pygameのイベント入力について学ぶのに適したものになっています。画面の上部には各種入力装置の状態を表す値が表示され、画面下部には発生したイベントの一覧がスクロールされて表示されます。

このコードのユーザーインターフェイスは必ずしも質が高いとは言えませんが、固定の値を表示する方法や動的な値を文章に組み入れて表示する方法を見ることができます。

display pygame events
pygame.examples.eventlist.main(): return None

Eventlist is a sloppy style of pygame, but is a handy tool for learning about pygame events and input. At the top of the screen are the state of several device values, and a scrolling list of events are displayed on the bottom.

This is not quality 'ui' code at all, but you can see how to implement very non-interactive status displays, or even a crude text output control.


pygame.examples.arraydemo.main

surfarrayを使用して様々な画面を表示するエフェクトです。
pygame.examples.arraydemo.main(arraytype=None): return None

surfarrayモジュールを使って、様々な画面表示を切り替えるエフェクトの一例です。このサンプルを実行するにはsurfarrayのモジュールとimageのモジュールがインストールされている必要があります。ごく簡単なサンプルではありますが、surfarrayを試しに使ってみるためのスタートラインとして役に立ってくれるでしょう。

arraytype引数を設定する場合、指定した配列形式に合ったモジュールが使用されます。'numeric' か 'numpy'のいずれかの形式を指定できます。arraytype引数を指定しない場合はNumPyが既定で選択され、NumPyがインストールされていないとNumericが選択されます。この動きは surfarrayモジュールで'numeric'か'numpy'を配列形式として設定する時と同じです。(これはpygame ver1.9.0で新たに実装されました)

show various surfarray effects
pygame.examples.arraydemo.main(arraytype=None): return None

Another example filled with various surfarray effects. It requires the surfarray and image modules to be installed. This little demo can also make a good starting point for any of your own tests with surfarray

If arraytype is provided then use that array package. Valid values are 'numeric' or 'numpy'. Otherwise default to NumPy, or fall back on Numeric if NumPy is not installed. As a program surfarray.py accepts an optional --numeric or --numpy flag. (New pygame 1.9.0)


pygame.examples.sound.main

音声を読み込んで再生します。
pygame.examples.sound.main(file_path=None): return None

mixerモジュールの非常に基本的なテストを行います。音声を読み込んで再生します。全ての処理はコマンドシェル上で行われ、画面には何も表示されません。

file_path引数を設定していた場合はその音声ファイルが再生され、設定していない場合は既定の音声ファイルが再生されます。

このサンプルでは再生する音声ファイルを追加の引数として設定できます。

load and play a sound
pygame.examples.sound.main(file_path=None): return None

Extremely basic testing of the mixer module. Load a sound and play it. All from the command shell, no graphics.

If provided, use the audio file 'file_path', otherwise use a default file.

sound.py optional command line argument: an audio file


pygame.examples.sound_array_demos.main

surfarrayを使用して様々な効果音を再生します。
pygame.examples.sound_array_demos.main(arraytype=None): return None

arraytype引数を設定する場合、指定した配列形式に合ったモジュールが使用されます。'numeric' か 'numpy'のいずれかの形式を指定できます。arraytype引数を指定しない場合はNumPyが既定で選択され、NumPyがインストールされていないとNumericが選択されます。

SndarrayモジュールやNumPyモジュール (もしくは Numericモジュール)を使って作成した音声は、元の音声よりも劣化しています。エコー効果や時間差再生のための設定値は、今のところサンプルコードの中にベタ書きされています。必要に応じて数値を簡単に書き換えることができます。このサンプルプログラムを実行する場合は、numpy か numericを引数として設定することで、使用する配列形式モジュールを選ぶことができます。

play various sndarray effects
pygame.examples.sound_array_demos.main(arraytype=None): return None

If arraytype is provided then use that array package. Valid values are 'numeric' or 'numpy'. Otherwise default to NumPy, or fall back on Numeric if NumPy is not installed.

Uses sndarray and NumPy ( or Numeric) to create offset faded copies of the original sound. Currently it just uses hardcoded values for the number of echos and the delay. Easy for you to recreate as needed. Run as a program sound_array_demos.py takes an optional command line option, --numpy or --numeric, specifying which array package to use.


pygame.examples.liquid.main

水面のゆらぎ効果を表すアニメーションを画面に表示します。
pygame.examples.liquid.main(): return None

このサンプルはゲームプログラミング言語BlitzBasicと機能比較するために作られました。それでも8ビットSurfaceを使用した素早い処理に成功しています。(しかもカラーマップ付きで)。

display an animated liquid effect
pygame.examples.liquid.main(): return None

This example was created in a quick comparison with the BlitzBasic gaming language. Nonetheless, it demonstrates a quick 8-bit setup (with colormap).


pygame.examples.glcube.main

OpenGLを使った3Dの立体アニメーションを表示します。
pygame.examples.glcube.main(): return None

PyOpenGL と pygameを使い、回転するマルチカラーの立方体を作ります。

display an animated 3D cube using OpenGL
pygame.examples.glcube.main(): return None

Using PyOpenGL and pygame, this creates a spinning 3D multicolored cube.


pygame.examples.scrap_clipboard.main

クリップボード機能を操作します。
pygame.examples.scrap_clipboard.main(): return None

クリップボード機能を使うための簡単なサンプルプログラムです。

access the clipboard
pygame.examples.scrap_clipboard.main(): return None

A simple demonstration example for the clipboard support.


pygame.examples.mask.main

衝突判定処理を使って、複数の画像が画面上を跳ね回ります。
pygame.examples.mask.main(*args): return None

固定引数:

    1つ以上の画像ファイル名

このpygame.masksデモでは、互いにぶつかって跳ね返りながら移動する複数のスプライトが表示されます。スプライトの元となる画像は1つ以上設定することができます。

このサンプルプログラムを実行する場合は、画像ファイル名を引数として1つ以上指定する必要があります。

display multiple images bounce off each other using collision detection
pygame.examples.mask.main(*args): return None

Positional arguments:

    one or more image file names.

This pygame.masks demo will display multiple moving sprites bouncing off each other. More than one sprite image can be provided.

If run as a program then mask.py takes one or more image files as command line arguments.


pygame.examples.testsprite.main

動き回る大量のスプライトを表示します。
pygame.examples.testsprite.main(update_rects = True, use_static = False, use_FastRenderGroup = False, screen_dims = [640, 480], use_alpha = False, flags = 0): return None

任意に指定できるキーワード引数:

    update_rects - use the RenderUpdate sprite group class
    use_static - include non-moving images
    use_FastRenderGroup - Use the FastRenderGroup sprite group
    screen_dims - pygame window dimensions
    use_alpha - use alpha blending
    flags - additional display mode flags

SDLに付属しているtestsprite.cのように、このpygameのサンプルでも動き回る大量のスプライトを表示します。

このサンプルプログラムを実行する場合は、引数を指定しなくても問題ありません。

show lots of sprites moving around
pygame.examples.testsprite.main(update_rects = True, use_static = False, use_FastRenderGroup = False, screen_dims = [640, 480], use_alpha = False, flags = 0): return None

Optional keyword arguments:

    update_rects - use the RenderUpdate sprite group class
    use_static - include non-moving images
    use_FastRenderGroup - Use the FastRenderGroup sprite group
    screen_dims - pygame window dimensions
    use_alpha - use alpha blending
    flags - additional display mode flags

Like the testsprite.c that comes with sdl, this pygame version shows lots of sprites moving around.

If run as a stand-alone program then no command line arguments are taken.


pygame.examples.headless_no_windows_needed.main

指定した画像ファイルの大きさを変更して出力します。
pygame.examples.headless_no_windows_needed.main(fin, fout, w, h): return None

引数:

    fin - 入力する画像ファイル名
    fout - 新規作成/上書き保存する際の出力ファイル名
    w, h - 画像を変更するサイズ。横幅と高さを数字で設定する

ヘッドレスサーバーのように、この処理ではウィンドウが表示されません。

pygameを使って、サイズを変更した画像を作成する処理のサンプルです。

注意:pygameの画像サイズ変更機能では可能な場合はmmx/sseを使用し、マルチスレッドで実行されます。

このサンプルプログラムを実行する場合は、下記の引数を設定する必要があります。:

    -scale 入力画像 出力画像 変更する横幅 変更する高さ
    
    例:
     -scale in.png outpng 50 50
write an image file that is smoothscaled copy of an input file
pygame.examples.headless_no_windows_needed.main(fin, fout, w, h): return None

arguments:

    fin - name of an input image file
    fout - name of the output file to create/overwrite
    w, h - size of the rescaled image, as integer width and height

How to use pygame with no windowing system, like on headless servers.

Thumbnail generation with scaling is an example of what you can do with pygame.

NOTE: the pygame scale function uses mmx/sse if available, and can be run in multiple threads.

If headless_no_windows_needed.py is run as a program it takes the following command line arguments:

    -scale inputimage outputimage new_width new_height
    eg. -scale in.png outpng 50 50

pygame.examples.fastevents.main

fasteventsモジュールのストレステストを実行します。
pygame.examples.fastevents.main(): return None

このサンプルではfasteventsモジュールのストレステストを実行します。

  • Fast eventsって全然速くないじゃないか!

現時点では、普通のpygame.eventのほうが最大で2倍速いように見えます。ですから、fasteventは全く速くないのでしょう。

athlon搭載のWindowsXP SP2およびFreeBSDでテストした結果、そうした結論に達しました。

ですが..。DebianがインストールされたDuron 850搭載のマシンでテストした時はfasteventsの方が速かったです。

stress test the fastevents module
pygame.examples.fastevents.main(): return None

This is a stress test for the fastevents module.

  • Fast events does not appear faster!

So far it looks like normal pygame.event is faster by up to two times. So maybe fastevent isn't fast at all.

Tested on windowsXP sp2 athlon, and freebsd.

However... on my debian duron 850 machine fastevents is faster.


pygame.examples.overlay.main

オーバーレイを使ってpgm形式の動画を再生します。
pygame.examples.overlay.main(fname): return None

fname引数に設定したpgm形式の動画を再生します。

このサンプルプログラムを実行する場合は、再生する動画ファイル名を引数として設定する必要があります。

play a .pgm video using overlays
pygame.examples.overlay.main(fname): return None

Play the .pgm video fila a path fname.

If run as a program overlay.py takes the file name as a command line argument.


pygame.examples.blend_fill.main

surface.fill命令で様々なブレンドオプションを組み合わせたサンプルです。
pygame.examples.blend_fill.main(): return None

surfaceに適用するBLEND_xxxオプションを選んで実行結果を表示することができます。

demonstrate the various surface.fill method blend options
pygame.examples.blend_fill.main(): return None

A interactive demo that lets one choose which BLEND_xxx option to apply to a surface.


pygame.examples.cursors.main

独自の変更を加えた二つのカーソルを表示します。
pygame.examples.cursors.main(): return None

矢印型カーソルか円型カーソルを表示します。

display two different custom cursors
pygame.examples.cursors.main(): return None

Display an arrow or circle with crossbar cursor.


pygame.examples.pixelarray.main

pixelarrayを使って作成した様々なエフェクトを表示します。
pygame.examples.pixelarray.main(): return None

pixelarrayを使って作成した様々なエフェクトを表示します。

display various pixelarray generated effects
pygame.examples.pixelarray.main(): return None

Display various pixelarray generated effects.


pygame.examples.scaletest.main

pygame.transform.smoothscale命令を使って、直接操作しながら画像のサイズを変更します。
pygame.examples.scaletest.main(imagefile, convert_alpha=False, run_speed_test=True): return None

引数:

    imagefile - 元画像のファイル名 (必須)
    convert_alpha - Sufaceでconvert_alpha()を使用するか指定 (既定ではFalse)
    run_speed_test - (既定ではFalse)

このサンプルでは画面に表示された画像の大きさを任意に変更します。↑キー、↓キー、→キー、←キーを入力することで、画面に表示された画像の縦幅や横幅を変更できます。 convert_alpha引数にTrueが設定されていた場合は、元画像には透明度が設定されます。これは元々透明度が設定されているいないに関わらず、必ず設定されます。run_speed_test引数にTrueを設定した場合は、画像のサイズを変更する操作画面は表示されず、変更にかかる処理速度のパフォーマンステストがバックグラウンドで行われます。

このサンプルプログラムを実行する場合は、下記の引数を設定する必要があります。:

    ImageFile [-t] [-convert_alpha]
    [-t] = Run Speed Test
    [-convert_alpha] = Use convert_alpha() on the surf.
interactively scale an image using smoothscale
pygame.examples.scaletest.main(imagefile, convert_alpha=False, run_speed_test=True): return None

arguments:

    imagefile - file name of source image (required)
    convert_alpha - use convert_alpha() on the surf (default False)
    run_speed_test - (default False)

A smoothscale example that resized an image on the screen. Vertical and horizontal arrow keys are used to change the width and height of the displayed image. If the convert_alpha option is True then the source image is forced to have source alpha, whether or not the original images does. If run_speed_test is True then a background timing test is performed instead of the interactive scaler.

If scaletest.py is run as a program then the command line options are:

    ImageFile [-t] [-convert_alpha]
    [-t] = Run Speed Test
    [-convert_alpha] = Use convert_alpha() on the surf.

pygame.examples.midi.main

midiを再生するサンプルプログラムを実行します。
pygame.examples.midi.main(mode='output', device_id=None): return None

引数:

    mode - 'output'を設定すると、画面に表示されたキーボードをクリックしてmidiを再生するサンプルプログラムを実行します。
           'input'を設定すると、入力された音階を記録してイベントを発生させるサンプルプログラムを実行します。
           'list'を設定すると、使用可能なmidiデバイスの一覧を表示します。
           (default 'output')
    device_id - midiデバイスの識別番号;None値が設定されている場合は、
                使用環境の既定のmidi入力・出力デバイスが使用されます。

'output'モードでは、マウスのクリック操作やキーボード操作で発生したイベントを、midiで再生する音階に変換する方法を見ることができます。基本的なボタンウィジットと状態遷移処理が実装されています。

'input'モードでは、midiの入力操作をpygameのイベントに変換する方法を見ることができます。

With the use of a virtual midi patch cord the output and input examples can be run as separate processes and connected so the keyboard output is displayed on a console.

このサンプルはpygameのバージョン1.9.0から新たに追加されました。

run a midi example
pygame.examples.midi.main(mode='output', device_id=None): return None

Arguments:

    mode - if 'output' run a midi keyboard output example
              'input' run a midi event logger input example
              'list' list available midi devices
           (default 'output')
    device_id - midi device number; if None then use the default midi input or
                output device for the system

The output example shows how to translate mouse clicks or computer keyboard events into midi notes. It implements a rudimentary button widget and state machine.

The input example shows how to translate midi input to pygame events.

With the use of a virtual midi patch cord the output and input examples can be run as separate processes and connected so the keyboard output is displayed on a console.

new to pygame 1.9.0


pygame.examples.scroll.main

Surface.scroll命令を実行して拡大した画像を表示します。
pygame.examples.scroll.main(image_file=None): return None

このサンプルでは、8倍の大きさに拡大された画像をスクロール表示する例を示します。Surface.scroll命令を使って画面描写surface上の画像をスクロール表示させます。A clip rectangle protects a margin area.このサンプルを実行する際は、任意の画像ファイル名を指定することもできます。image_file引数に表示したい画像のパスを設定します。画像ファイルを設定しなかった場合は、既定の画像ファイルが使用されます。

画面に表示された黒い三角マークをクリックすると、三角マークの向きの方へ画像が1ピクセル分スクロールします。上下左右の各矢印キーを入力することでも同じ操作ができます。ウィンドウを閉じるか ESCキーを入力することでサンプルプログラムは終了します。

run a Surface.scroll example that shows a magnified image
pygame.examples.scroll.main(image_file=None): return None

This example shows a scrollable image that has a zoom factor of eight. It uses the Surface.scroll function to shift the image on the display surface. A clip rectangle protects a margin area. If called as a function, the example accepts an optional image file path. If run as a program it takes an optional file path command line argument. If no file is provided a default image file is used.

When running click on a black triangle to move one pixel in the direction the triangle points. Or use the arrow keys. Close the window or press ESC to quit.


pygame.examples.movieplayer.main

MPEG形式の動画を再生します。
pygame.examples.movieplayer.main(filepath): return None

PygameでMPEG形式の動画を再生する動画プレイヤーのサンプルです。ここではpygame.movieモジュールを使った例を示します。ウィンドウの画面サイズは動画の大きさに応じてが変更されます。サブウィンドウでも単体で動画が再生できることを見せるために、再生する動画には外枠をつけています。また、Pythonファイル以外の動画も再生できることを示すため、動画ファイルはpygameオブジェクトに変換されて再生します。

pygame.movieモジュールに不具合があると、全ての環境で一切動作しなくなります。その場合はpygame.movieモジュールではなく ffmpeg のベースバージョンを使った方がよいでしょう

play an MPEG movie
pygame.examples.moveplayer.main(filepath): return None

A simple movie player that plays an MPEG movie in a Pygame window. It showcases the pygame.movie module. The window adjusts to the size of the movie image. It is given a boarder to demonstrate that a movie can play autonomously in a sub- window. Also, the file is copied to a file like object to show that not just Python files can be used as a movie source.

The pygame.movie module is problematic and may not work on all systems. It is intended to replace it with an ffmpeg based version.