-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhit_jump.py
More file actions
114 lines (90 loc) · 3.46 KB
/
Copy pathhit_jump.py
File metadata and controls
114 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import pyxel
import PyxelUniversalFont as puf
import random
from pygame import mixer
mixer.init()
try:
JUMP_SOUND = mixer.Sound("./sound/soundeffect/ジャンプ.wav")
GAMEOVER_SOUND = mixer.Sound("./sound/soundeffect/ゲームオーバー.mp3")
DAMAGE_SOUND = mixer.Sound("./sound/soundeffect/ダメージ.mp3")
except Exception as e:
print(f"サウンドファイルのロードに失敗しました: {e}")
JUMP_SOUND = None
GAMEOVER_SOUND = None
DAMAGE_SOUND = None
screen = {"yoko": 160, "tate": 120}
jump_box = {
"x": 20,
"y": 100,
"speed_x": 0,
"speed_y": 13,
"jump_status": False, # ジャンプの状態 ジャンプ中→True ジャンプしてないとき→False
}
enemy_box = {"x": 160, "y": 100, "speed_x": -2, "speed_y": 0}
writer = puf.Writer("misaki_gothic.ttf")
game_status = {"HP": 3, "GAME_OVER": False}
def update():
# ジャンプしていない状態でスペースが押されたとき または ジャンプ中のとき
if (not jump_box["jump_status"] and pyxel.btn(pyxel.KEY_SPACE)) or jump_box["jump_status"]:
if not jump_box["jump_status"] and pyxel.btn(pyxel.KEY_SPACE):
JUMP_SOUND.play()
jump_box["jump_status"] = True
jump_box["y"] -= jump_box["speed_y"]
jump_box["speed_y"] -= 1
# ジャンプ中の状態でy座標が100になったとき=着地したとき
if jump_box["y"] == 100 and jump_box["jump_status"]:
jump_box["speed_y"] = 13
jump_box["jump_status"] = False
# 障害物の動き
if enemy_box["x"] <= -10:
enemy_box["x"] = 160
enemy_box["speed_x"] = random.randint(-10, -2)
if game_status["GAME_OVER"] == False:
enemy_box["x"] += enemy_box["speed_x"]
# 当たり判定
x = enemy_box["x"] - jump_box["x"]
y = enemy_box["y"] - jump_box["y"]
distance = x**2 + y**2
if distance < 15**2:
# hpを1減らす
game_status["HP"] = game_status["HP"] - 1
DAMAGE_SOUND.play()
# gameover
if game_status["HP"] <= 0:
game_status["GAME_OVER"] = True
GAMEOVER_SOUND.play()
# 敵の場所をリセットする
enemy_box["x"] = 160
enemy_box["y"] = 100
def draw():
pyxel.cls(0)
# ジャンプキャラクターのイメージを描画
pyxel.blt(
jump_box["x"], # 描画する場所のx座標
jump_box["y"], # 描画する場所のx座標
0, # イメージバンクの番号
0, # 開始位置のx座標
0, # 開始位置のy座標
15, # 終了位置のx座標
15, # 終了位置のy座標
0, # 透明にしたい色番号
)
# これをコメントアウト→ pyxel.rect(enemy_box["x"], enemy_box["y"], 10, 10, 8)
pyxel.blt(
enemy_box["x"], # 描画する場所のx座標
enemy_box["y"], # 描画する場所のx座標
1, # イメージバンクの番号
0, # 開始位置のx座標
0, # 開始位置のy座標
15, # 終了位置のx座標
15, # 終了位置のy座標
2, # 透明にしたい色番号
)
# hpを表示
writer.draw(25, 5, f"hp {game_status["HP"]}:", 20, 15)
# hpが0以下ならgameoverを表示
if game_status["GAME_OVER"] == True:
writer.draw(25, 30, "GAME_OVER", 20, 15)
pyxel.init(screen["yoko"], screen["tate"])
pyxel.load("sample.pyxres")
pyxel.run(update, draw)