Skip to content

Instantly share code, notes, and snippets.

@MizunagiKB
Last active September 6, 2020 06:05
Show Gist options
  • Save MizunagiKB/edb4b3c6ddcf9c6d73330c9ad62f3c78 to your computer and use it in GitHub Desktop.
Save MizunagiKB/edb4b3c6ddcf9c6d73330c9ad62f3c78 to your computer and use it in GitHub Desktop.
プレイヤーの周囲を回転するオプションのサンプル
"""プレイヤーの周囲を回転するオプションのサンプル
"""
import math
import pyxel
SCREEN_W = 256
SCREEN_H = 240
GAME_FPS = 60
MIN_RADIUS = 10
MAX_RADIUS = 100
DEFAULT_RADIUS = 30
FORMATION_SPEED = 0.02
ROT_SPEED = 4
def rotation(x: float, y: float, p_by: float) -> (float, float):
"""原点を軸に x, y を p_by 回転させます。
Args:
x (float):
回転元のX座標
y (float):
回転元のY座標
p_by (float):
回転角度(ラジアン)
Retrurns:
x, y の回転結果をタプルで戻します。
"""
sv = math.sin(p_by)
cv = math.cos(p_by)
return (x * cv - y * sv, x * sv + y * cv)
class CPlayer:
def __init__(self):
self.x = 0
self.y = 0
class CPlayerOption:
def __init__(self, formation):
self.x = 0
self.y = 0
self.formation = formation
def change_formation(self, new_formation: int):
v = new_formation - self.formation
if v != 0:
v /= abs(v)
self.formation += v * FORMATION_SPEED
def main():
pyxel.init(SCREEN_W, SCREEN_H, fps=GAME_FPS)
pyxel.mouse(False)
player = CPlayer()
list_option = []
radius = DEFAULT_RADIUS
while True:
# プレイヤーキャラクターの更新
# (オプションの回転座標の中心となります)
player.x = pyxel.mouse_x
player.y = pyxel.mouse_y
# マウス左クリックでオプションの追加。
# マウス右クリックでオプションの削除。
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON) == 1:
list_option.append(CPlayerOption(len(list_option) + 1))
if pyxel.btnp(pyxel.MOUSE_RIGHT_BUTTON) == 1:
list_option = list_option[0:-1]
# マウスホイールで回転半径の変更。
radius = min(max(radius + pyxel.mouse_wheel, MIN_RADIUS), MAX_RADIUS)
pyxel.cls(0)
for n, o in enumerate(list_option):
# オプションの個数にあわせて配置場所を計算します。
deg = 360 / o.formation * n
# オプション位置の回転処理。
x, y = rotation(
radius, 0, math.radians(deg + pyxel.frame_count * ROT_SPEED)
)
# オプション数が急に変わってもカクつかないように、徐々に角度を変更するための処理。
o.change_formation(len(list_option))
# プレイヤーの位置にあわせて、オプションの描画を行います。
pyxel.circ(player.x + x, player.y + y, 2, 6)
pyxel.circ(player.x, player.y, 3, 7)
pyxel.text(8, 8, "Option(s): {:7.2f}".format(len(list_option)), 7)
pyxel.text(8, 16, "Radius(s): {:7.2f}".format(radius), 7)
pyxel.flip()
if __name__ == "__main__":
main()
# [EOF]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment