生成AIで、以下、プロンプトをためす。ふるまい設定.txtをつくっておいて、くりかえし、くりかえしですが、検証です。命名が違ったり、勉強になります。
マウスクリックで生成 → 落下 → リセット → 地面追加
import pyxel
import pymunk
COLOR_RED = 8
COLOR_BLUE = 5
class PhysicsSimulation:
def __init__(self):
self.width = 640
self.height = 480
pyxel.init(self.width, self.height, title="Click to Spawn Box")
pyxel.mouse(True)
self.reset()
pyxel.run(self.update, self.draw)
def reset(self):
self.space = pymunk.Space()
self.space.gravity = (0, -900)
self.space.sleep_time_threshold = 0.5
ground_body = pymunk.Body(body_type=pymunk.Body.STATIC)
ground_body.position = (self.width / 2, 20)
ground_shape = pymunk.Poly.create_box(ground_body, (self.width, 40))
ground_shape.friction = 0.8
ground_shape.elasticity = 0.4
self.space.add(ground_body, ground_shape)
self.ground_shape = ground_shape
self.bodies = []
def update(self):
self.space.step(1 / 60.0)
if pyxel.btnp(pyxel.KEY_R):
self.reset()
if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
pos = pyxel.mouse_x, self.height - pyxel.mouse_y
size = (60, 30)
mass = 5
moment = pymunk.moment_for_box(mass, size)
body = pymunk.Body(mass, moment)
body.position = pos
shape = pymunk.Poly.create_box(body, size)
shape.friction = 0.5
shape.elasticity = 0.6
self.space.add(body, shape)
self.bodies.append((body, shape))
def draw(self):
pyxel.cls(0)
bb = self.ground_shape.bb
pyxel.rect(int(bb.left), int(self.height - bb.top), int(bb.right - bb.left), int(bb.top - bb.bottom), COLOR_BLUE)
for body, shape in self.bodies:
verts = [body.local_to_world(v) for v in shape.get_vertices()]
verts_pyxel = [(int(x), int(self.height - y)) for x, y in verts]
v0, v1, v2, v3 = verts_pyxel
pyxel.tri(v0[0], v0[1], v1[0], v1[1], v2[0], v2[1], COLOR_RED)
pyxel.tri(v2[0], v2[1], v3[0], v3[1], v0[0], v0[1], COLOR_RED)
if __name__ == "__main__":
PhysicsSimulation()
マウスで発生 → 落下 → リセット → 地面追加
import pyxel
import pymunk
COLOR_RED = 8
COLOR_BLUE = 5
class FallingBoxes:
def __init__(self):
self.width = 640
self.height = 480
pyxel.init(self.width, self.height, title="Falling Boxes")
pyxel.mouse(True)
self.reset()
pyxel.run(self.update, self.draw)
def reset(self):
self.space = pymunk.Space()
self.space.gravity = (0, -900)
self.space.sleep_time_threshold = 0.5
ground_body = pymunk.Body(body_type=pymunk.Body.STATIC)
ground_body.position = (self.width / 2, 20)
ground_shape = pymunk.Poly.create_box(ground_body, (self.width, 40))
ground_shape.friction = 0.8
ground_shape.elasticity = 0.4
self.space.add(ground_body, ground_shape)
self.ground_shape = ground_shape
self.boxes = []
def update(self):
self.space.step(1 / 60.0)
if pyxel.btnp(pyxel.KEY_R):
self.reset()
if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
x, y = pyxel.mouse_x, self.height - pyxel.mouse_y
size = (60, 30)
mass = 5
moment = pymunk.moment_for_box(mass, size)
body = pymunk.Body(mass, moment)
body.position = (x, y)
shape = pymunk.Poly.create_box(body, size)
shape.friction = 0.5
shape.elasticity = 0.6
self.space.add(body, shape)
self.boxes.append((body, shape))
def draw(self):
pyxel.cls(0)
bb = self.ground_shape.bb
pyxel.rect(int(bb.left), int(self.height - bb.top), int(bb.right - bb.left), int(bb.top - bb.bottom), COLOR_BLUE)
for body, shape in self.boxes:
verts = [body.local_to_world(v) for v in shape.get_vertices()]
verts_pyxel = [(int(x), int(self.height - y)) for x, y in verts]
v0, v1, v2, v3 = verts_pyxel
pyxel.tri(v0[0], v0[1], v1[0], v1[1], v2[0], v2[1], COLOR_RED)
pyxel.tri(v2[0], v2[1], v3[0], v3[1], v0[0], v0[1], COLOR_RED)
if __name__ == "__main__":
FallingBoxes()
プロンプト設計のポイント
-
主目的を最初に書く(例:「マウスクリックで発生させる」)
-
条件は「さらに」「加えて」でつなぐ
-
目的 → 動作 → 条件 → 環境(地面など)の順に書く
-
短文を並べず、ひとつのストーリーとして書く
-
「参照せずに独立して生成」と明記することで混乱を防ぐ
生成AIのfrequency penaltyを上げると、プロンプトの書き方によっては、制約条件を無視して、コメント、タブふえる挙動、コード生成がおかしくなるということがわかりました。デフォルトの0で安定しています。
ありがとうございます。