0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PyxelとPymunkでサンプルプログラム その5

Last updated at Posted at 2025-11-05

古いchatgptは、コメント文を挿入すると、インデントが崩れるので、コメントなしで、出力してみました。

古いChatGPTの本質的な問題

古いバージョンのChatGPTは:

  • 「コメントを書きたい」という強い傾向がある
  • 「1行のみ」「簡潔に」などの制約を与えても、無視しがち
  • コメントを書き始めると、止まらなくなる
  • 完全禁止が唯一の確実な対策

画面中央上部から、赤い長方形が落下するサンプルプログラムを作成してください。

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="2D Physics Simulation")
        self.reset()
        pyxel.run(self.update, self.draw)
    
    def reset(self):
        self.space = pymunk.Space()
        self.space.gravity = (0, -900)
        
        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
        
        size = (60, 30)
        mass = 5
        moment = pymunk.moment_for_box(mass, size)
        self.rect_body = pymunk.Body(mass, moment)
        self.rect_body.position = (self.width / 2, self.height - 50)
        rect_shape = pymunk.Poly.create_box(self.rect_body, size)
        rect_shape.friction = 0.5
        rect_shape.elasticity = 0.6
        self.space.add(self.rect_body, rect_shape)
        self.rect_shape = rect_shape
    
    def update(self):
        if pyxel.btnp(pyxel.KEY_R):
            self.reset()
        self.space.step(1 / 60.0)
    
    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
        )
        
        verts = [self.rect_body.local_to_world(v) for v in self.rect_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 PhysicsSimulation:
    def __init__(self):
        self.width = 640
        self.height = 480
        pyxel.init(self.width, self.height, title="2D Physics Simulation")
        self.reset()
        pyxel.run(self.update, self.draw)
    
    def reset(self):
        self.space = pymunk.Space()
        self.space.gravity = (0, -900)
        
        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
        
        size = (60, 30)
        mass = 5
        moment = pymunk.moment_for_box(mass, size)
        self.rect_body = pymunk.Body(mass, moment)
        self.rect_body.position = (self.width / 2, self.height - 50)
        self.rect_body.velocity = (100, -200)
        self.rect_body.angular_velocity = 5
        rect_shape = pymunk.Poly.create_box(self.rect_body, size)
        rect_shape.friction = 0.5
        rect_shape.elasticity = 0.6
        self.space.add(self.rect_body, rect_shape)
        self.rect_shape = rect_shape
    
    def update(self):
        if pyxel.btnp(pyxel.KEY_R):
            self.reset()
        self.space.step(1 / 60.0)
    
    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
        )
        
        verts = [self.rect_body.local_to_world(v) for v in self.rect_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()

3つの長方形に角速度と初速度をつけて、落下させてください。高さは任意です。

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="2D Physics Simulation")
        self.reset()
        pyxel.run(self.update, self.draw)
    
    def reset(self):
        self.space = pymunk.Space()
        self.space.gravity = (0, -900)
        
        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
        
        size = (60, 30)
        mass = 5
        moment = pymunk.moment_for_box(mass, size)
        
        positions_velocities_angulars = [
            ((self.width / 4, self.height - 50), (100, -150), 5),
            ((self.width / 2, self.height - 100), (-80, -180), -4),
            ((3 * self.width / 4, self.height - 70), (50, -160), 6),
        ]
        
        self.rect_bodies = []
        self.rect_shapes = []
        
        for pos, vel, ang_vel in positions_velocities_angulars:
            body = pymunk.Body(mass, moment)
            body.position = pos
            body.velocity = vel
            body.angular_velocity = ang_vel
            shape = pymunk.Poly.create_box(body, size)
            shape.friction = 0.5
            shape.elasticity = 0.6
            self.space.add(body, shape)
            self.rect_bodies.append(body)
            self.rect_shapes.append(shape)
    
    def update(self):
        if pyxel.btnp(pyxel.KEY_R):
            self.reset()
        self.space.step(1 / 60.0)
    
    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 zip(self.rect_bodies, self.rect_shapes):
            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()

マウス左クリック位置に、長方形を発生させ、回転落下する。リセットで、物理space内も削除する。

コメント禁止でもコメントしてきて、たくさん改行して混乱していました。
プロンプトに貼り付けて修正してもらう。

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="2D Physics Simulation")
        pyxel.mouse(True)
        self.reset()
        pyxel.run(self.update, self.draw)
    
    def reset(self):
        self.space = pymunk.Space()
        self.space.gravity = (0, -900)
        
        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.rect_bodies = []
        self.rect_shapes = []
    
    def add_rectangle(self, x, y):
        size = (60, 30)
        mass = 5
        moment = pymunk.moment_for_box(mass, size)
        
        body = pymunk.Body(mass, moment)
        body.position = (x, self.height - y)
        body.velocity = (0, 0)
        body.angular_velocity = 3
        
        shape = pymunk.Poly.create_box(body, size)
        shape.friction = 0.5
        shape.elasticity = 0.6
        
        self.space.add(body, shape)
        
        self.rect_bodies.append(body)
        self.rect_shapes.append(shape)

    def update(self):
        if pyxel.btnp(pyxel.KEY_R):
            for body in self.rect_bodies:
                for shape in body.shapes:
                    if shape in self.space.shapes:
                        self.space.remove(shape)
                if body in self.space.bodies:
                    self.space.remove(body)
            self.rect_bodies.clear()
            self.rect_shapes.clear()
            # reset space and ground properly by calling reset()
            # btnp triggers once per press so safe to call here without infinite recursion
            self.reset()
            return
        
        if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
            x,y=pyxel.mouse_x,pyxel.mouse_y
            if y < 40:
                y=40
            # add rectangle at mouse position with rotation and zero initial velocity
            self.add_rectangle(x,y)

        self.space.step(1 / 60.0)

    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 zip(self.rect_bodies,self.rect_shapes):
           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()

プロンプトの書き方に問題

次のプロンプトは、コメントの暴走、空行が大量発生しました。

1. 「マウス左クリック位置に、長方形を発生させ、回転落下する。リセットで、物理space内も削除する。」

1のプロンプトでは、update()に複数のコメント行が挿入され、最後まで作成されず、大量に改行されました。その後、コードをコピーして、修正をお願いしたら、正常に動作しました。

次のプロンプトは、問題なく作成されました。

2. 「マウス左クリックした位置に、長方形を発生させ、回転落下するプログラムを作成してください。なおリセット時に、古い長方形は、spaceから削除してください。」

失敗プロンプトの問題点

箇条書き風の文体
「〜する。」「〜する。」という連続
ChatGPTが「これは仕様書」と認識
仕様書 → 各項目に説明コメントを付けたくなる

短い文の連続
「発生させ、回転落下する。」
「削除する。」
各文がタスクリストに見える → コメントで説明したくなる

「物理space内」という専門用語
ChatGPTが「これは重要な技術的ポイント」と認識
詳細な説明コメントを書きたくなる

問題:
✗ 「〜する。」「〜する。」という箇条書き風
✗ 2つの短文
✗ 「物理space内」という専門用語が浮いている

ChatGPTの反応:
「これは仕様書だ」と認識
各項目を説明するコメントを生成
update()に説明コメントを大量挿入
空行も大量に挿入

成功プロンプトの良い点

自然な依頼文
「プログラムを作成してください」
命令形ではなく依頼形
ChatGPTが「コード生成タスク」と明確に認識

接続詞の使用
「なお〜してください」
文章が流れるように繋がる
箇条書き感が薄れる

具体的な説明
「マウス左クリックした位置に」(過去形で具体的)
「古い長方形は」(対象が明確)
「spaceから削除」(専門用語を自然に組み込む)

ChatGPTの反応:
「これはコード生成タスク」と明確に認識
コメント生成の衝動が抑えられる
素直にコードだけを生成

一歩一歩です。ありがとうございます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?