「you are dead」という画面になるコマンドをifを使って作ろうとしましたが上手くいきませんでした。
「上手くいかない」だけでは何も伝わりません。
やってみたことを提示してください(エラーになっていてもかまいません)
それがあって初めて「ここが間違っています」とか「こうしたらどうでしょう?」と回答できます。
これでは「丸投げの質問」といわれてしまいます。
ヘルプ | 推奨していない質問
キャラがある座標にいくと死ぬようにしたい。
前回回答でも言及しましたが、マップは2次元配列等で管理したほうが自由度は高いです。
迷路ゲームの当たり判定について
しかしマップ画像とデータ両方作るのは面倒なので、マップの色から判断するようにします。
マップ画像が不明なのでこちらで雑に作りました(Tiledで作成)
map.png

移動時にマップ画像のキャラ直下にあたる部分の色を取得します。
毒沼の色だったらゲームオーバー画面に遷移します。
ランダムで戦闘になるコマンドに至っては見当もつきません。
ランダムがわからないということですか!?
random() \ Language (API)
ある確率で遷移させるだけですが...(地形によってエンカウント率を変えてもいいですね)
マップに戻る際はTitle
やGameOver
のように新しいインスタンスを作ると初期位置に戻ってしまうので、同じインスタンスを使いまわすようにする必要があります。
戦闘画面の作り方が「見当もつきません」ということならわかります。
最初は「たたかう」と「にげる」だけで、戦ったら無条件で勝つくらいの簡単なものから始めてください(それでも結構なコード量になっています)
それができたら徐々に拡張していくようにします。
あるいは戦闘画面だけのプログラムをある程度作って、前回の質問のように合体させるほうが作りやすいかもしれません。
Python
1scene = None
2field = None
3
4def setup():
5 global scene, field
6 size(800, 480)
7 textFont(createFont(u'MS ゴシック', 30))
8
9 scene = Title()
10
11def draw():
12 scene.draw()
13
14def keyPressed():
15 scene.keyPressed()
16
17def mouseClicked(): # 色確認用
18 println(hex(get(mouseX, mouseY)))
19
20
21class Title(object):
22 def draw(self):
23 background(0)
24 fill(255)
25 textAlign(CENTER)
26 textSize(40)
27 text("DRAGON QUEST", width/2, height/2)
28 if frameCount / 60 % 2 == 0:
29 textSize(20)
30 text("> START", width/2, height/4*3)
31
32 def keyPressed(self):
33 global scene, field
34
35 # フィールド画面はバトル後も状態を覚えておかなければならない
36 # 1つのインスタンスを使いまわすためグローバル変数に入れておく
37 field = Field()
38 scene = field
39
40
41class GameOver(object):
42 def draw(self):
43 background(0)
44 fill(255)
45 textAlign(CENTER)
46 textSize(40)
47 text("you are dead", width/2, height/2)
48 if frameCount / 60 % 2 == 0:
49 textSize(20)
50 text("> Title", width/2, height/4*3)
51
52 def keyPressed(self):
53 global scene
54 scene = Title()
55
56
57class Field(object):
58 def __init__(self):
59 self.dir = 1
60 self.x = 32 * 21
61 self.y = 32 * 3
62 img = loadImage("pipo-charachip029d.png")
63 self.hero = [img.get(32, 96, 32, 32), img.get(32, 0, 32, 32),
64 img.get(32, 32, 32, 32), img.get(32, 64, 32, 32)]
65
66 self.field = loadImage("map.png")
67
68 def draw(self):
69 background(0xFF00A2E8)
70 translate(width/2, height/2)
71 imageMode(CORNER)
72 image(self.field, -self.x - 16, -self.y - 16)
73 imageMode(CENTER)
74 image(self.hero[self.dir], 0, 0)
75
76 def keyPressed(self):
77 global scene
78
79 # 移動前の座標を取っておく
80 oldX = self.x
81 oldY = self.y
82
83 if keyCode == UP:
84 self.dir = 0
85 self.y -= 32
86 elif keyCode == DOWN:
87 self.dir = 1
88 self.y += 32
89 elif keyCode == LEFT:
90 self.dir = 2
91 self.x -= 32
92 elif keyCode == RIGHT:
93 self.dir = 3
94 self.x += 32
95
96 # マップ画像のキャラ直下(のマップチップの左上)の色を取得
97 c = self.field.get(self.x, self.y)
98 println((self.x, self.y, hex(c)))
99
100 if c == 0xFFA349A4: # 毒沼
101 scene = GameOver() # ゲームオーバー画面に遷移
102
103 # 移動不可エリア
104 if (c == 0x00000000 or # マップ外
105 c == 0xFF00A2E8 or # 水
106 c == 0xFF7F7F7F): # 山
107 # 元に戻す(移動キャンセル)
108 self.x = oldX
109 self.y = oldY
110 else:
111 # エンカウント
112 if random(100) < 8: # エンカウント率8%
113 scene = Battle()
114
115
116class Battle(object):
117 def __init__(self):
118 self.menu = 0
119 self.message = None
120 self.finished = False
121
122 def draw(self):
123 background(0)
124 fill(255)
125 textAlign(CENTER)
126 textSize(40)
127 text("Battle", width/2, height/2)
128
129 textAlign(LEFT, TOP)
130 textSize(20)
131
132 if self.message:
133 self.drawMessage()
134 else:
135 self.drawMenu()
136
137 def drawMenu(self):
138 textAlign(LEFT, TOP)
139 textSize(20)
140
141 if self.menu == 0:
142 text(u">たたかう", 100, height/4*3)
143 text(u" にげる", 100, height/4*3 + 40)
144 elif self.menu == 1:
145 text(u" たたかう", 100, height/4*3)
146 text(u">にげる", 100, height/4*3 + 40)
147
148 noFill();
149 stroke(255)
150 strokeWeight(4)
151 rect(90, height/4*3 - 10, 120, 100, 10)
152
153 def drawMessage(self):
154 textAlign(LEFT, TOP)
155 textSize(20)
156 text(self.message, 250, height/4*3)
157
158 noFill();
159 stroke(255)
160 strokeWeight(4)
161 rect(240, height/4*3 - 10, 400, 100, 10)
162
163 def keyPressed(self):
164 global scene
165
166 if self.finished:
167 scene = field
168
169 # こうしてしまうと初期位置に戻ってしまう
170 # scene = Field()
171 return
172
173 if keyCode == UP:
174 self.menu = 0
175 elif keyCode == DOWN:
176 self.menu = 1
177 else:
178 if self.menu == 0:
179 self.message = u"スライムを倒した"
180 self.finished = True
181 if self.menu == 1:
182 self.message = u"にげだした"
183 self.finished = True
ぴぽや様の素材をお借りしました。
キャラクターチップ1 - ぴぽや倉庫
キャラチップ.zip in pipo-charachip029d.png
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/07/16 09:48