今回はプログラミング言語の「Python」を使って作成しました。
下記の本のサンプルの書き方だと自分の環境では滑らかにジャンプしなかったので
ジャンプの処理は自作しました。
■ プログラム
from game import * from crash import * import math import random image_player = [image('chicken.png'), image('chicken.png')] image_floor = image('floor.png') SW, SH = 1, 1.1 STATE = 'onFloor' def player(p): global STATE score(1) p.vy = max(-0.03, p.vy-0.001) p.x += p.vx p.y += p.vy if p.y < -SH: new_crash(p.x, p.y, 0.01, 20, 0.98) p.life = 0 on_floor = False if key(Z) and STATE == 'Jumping': STATE = 'Jumping' for f in group(floor): dx, dy = p.x-f.x, p.y-f.y ax, ay = abs(dx), abs(dy) sx, sy = p.sx+f.sx, p.sy+f.sy if ax < sx*0.7 and ay < sy*1.1: if p.y > f.y+f.sy and p.vy <= 0: p.y = f.y+sy p.vy = 0 on_floor = True if key(Z) and STATE == 'Jumping': STATE = 'Jumping' else: STATE = 'onFloor' if on_floor: #if key(Z) and not key_old(Z): # ジャンプ処理 if key(Z) and STATE == 'onFloor': STATE = 'Jumping' p.vy = 0.04 else: if not key(Z) and p.vy > 0: p.vy *= 0.8 vx, ax = 0.01, 0.001 if key(LSHIFT) or key(RSHIFT): vx, ax = 0.02, 0.002 if on_floor: if key(LEFT) and p.vx > 0 or key(RIGHT) and p.vx < 0: ax = 0.003 if not key(LEFT) and not key(RIGHT): p.vx *= 0.8 else: ax *= 0.5 if key(LEFT): p.vx = max(-vx, p.vx-ax) p.image = image_player[0] elif key(RIGHT): p.vx = min(vx, p.vx+ax) p.image = image_player[1] def floor(f): f.y -= 0.003 if f.y-f.sy > SH: f.life = 0 def stage(t): t.time -= 1 if t.time < 0: t.time = random.randint(80, 160) size = 0.05 n = random.randint(3, 5) w = SW*0.9 x = random.uniform(-w, w-n*size*2) for i in range(n): add(floor, image_floor, size, x+i*size*2, SH+size) def start(): score() add(stage) add(player, image_player[0], 0.05, 0, 0.6) add(floor, image_floor, 0.05, 0, 0.5) run(start, 600, 720, (0.1, 0.6, 0.8), False, (0.9, 0.9, 1)) |
■ 参考書
「Pythonでつくる ゲームプログラミング入門」
■ ゲーム用ライブラリ
「pyglet」
■ プログラミング言語
「Python」