Python Program Introduction Site

ゲームを作る前に②

# モジュールをインポート
import tkinter

# 初期値の設定
x = 0
ani = 0

# 関数を定義
def animation():
   # 関数の外側で宣言した変数を関数内で変化させるため
   # global宣言を行います
global x, ani
x = x + 4 # xを4増やす
if x == 960: # xは960になると再び0に戻る
x = 0
canvas.delete(“BG”) # 一旦背景画像を初期化
# 背景画像の描画
canvas.create_image(x-480, 300, image=img_bg, tag=“BG”) # 左側
canvas.create_image(x+480, 300, image=img_bg, tag=“BG”) # 右側
   # 余数により0~3の4パターンの繰り返し
ani = (ani+1)%4
canvas.create_image(520, 470, image=img_car[ani], tag=“BG”)
root.after(20, animation) # 20ミリ秒ごとに関数を実行

root = tkinter.Tk() # 基礎の基礎:ウインドウの部品を作成
root.title(“アニメ”) # タイトルを指定
canvas = tkinter.Canvas(width=960, height=600) # 実際に描くキャンバスの部品を作成
canvas.pack() # キャンバスを配置
img_bg = tkinter.PhotoImage(file=“kesiki.png”) # 背景画像を読み込む
img_car = [ #アニメーション用の4枚を指定
tkinter.PhotoImage(file=“car000.png”),
tkinter.PhotoImage(file=“car001.png”),
tkinter.PhotoImage(file=“car002.png”),
tkinter.PhotoImage(file=“car003.png”)
]
animation() # アニメーション表示の関数の実行
root.mainloop() #ウインドウを表示

関連記事

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

python3X