横スクロールする背景を作ってみます。背景をスクロールさせる方法はいくつかあります。 ここでは、背景を2枚のスプライトで作り、この2枚を動かすことで、背景が横にスクロールしているように見せます。
ゲームのウィンドウの横幅を WIDTH とします。ウィンドウの横幅 WIDTH と同じ幅の背景画像を2枚、 background1 と background2 として用意します。background1 と background2 は、それぞれスプライトとして扱います。 それぞれの初期値の位置は
このように配置すると、2枚の背景画像が横に並びます。ゲームのウィンドウには、 最初は background1 だけが表示されます。ゲームが始まると、2枚の背景を同じ速度で左へ動かします。 すると、徐々に background1 が画面の左へ移動し、代わりに background2 が見えてきます。 下の図で灰色で囲ったところが現在見えているとことです。

やがて background1 の位置が -WIDTH /2 に達すると、background1 は画面から完全に見えなくなります。 このとき、background2 だけが画面に表示されています。 そこで、画面から見えなくなった background1 を、WIDTH * 3 /2の位置へ移動します。 これで、background1 は background2 の右側に移動します。
同じように、今度は background2 が左へ移動し、位置が -WIDTH /2 に達したら、 background2 を WIDTH * 3 /2の位置へ移動します。この動作を繰り返すことで、2枚の背景画像を使って、 背景が途切れることなく横スクロールしているように見せることができます。
# 横スクロール
import arcade
WIDTH = 640
# MyGame クラスの定義 ---
class MyGame(arcade.Window):
def __init__(self, width, height, title):
# 親クラスの初期化関数をコールする
super().__init__(width, height, title)
# 背景色
arcade.set_background_color(arcade.color.SKY_BLUE)
# 背景
self.background1 = arcade.Sprite("bg1.png")
self.background2 = arcade.Sprite("bg2.png")
self.wood = arcade.Sprite("wood1.png")
self.bk_list = arcade.SpriteList()
self.bk_list.append(self.background1)
self.background1.center_x=WIDTH/2
self.background1.center_y=60
self.background1.change_x=-1
self.bk_list.append(self.background2)
self.background2.center_x=WIDTH*3/2
self.background2.center_y=60
self.background2.change_x=-1
self.bk_list.append(self.wood)
self.wood.center_x=WIDTH
self.wood.center_y=100
self.wood.change_x=-2
# 1秒間に60回、この関数が呼び出され再描画します。
def on_draw(self):
self.clear()
self.bk_list.draw()
def on_update(self, delta_time):
self.bk_list.update(delta_time)
if(self.background1.center_x<=-WIDTH/2):
self.background1.center_x = WIDTH*3/2
if(self.background2.center_x<=-WIDTH/2):
self.background2.center_x = WIDTH*3/2
if(self.wood.center_x<-0):
self.wood.center_x += WIDTH
# --- クラスの定義終わり
# MyGame を作成
mywindow = MyGame(640, 480, "MyGame Example")
arcade.run()