質問編集履歴
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -118,6 +118,290 @@
|
|
118
118
|
|
119
119
|
|
120
120
|
|
121
|
+
コード全体
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
```python
|
126
|
+
|
127
|
+
import pgzrun
|
128
|
+
|
129
|
+
import random
|
130
|
+
|
131
|
+
import time
|
132
|
+
|
133
|
+
import math
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
WIDTH = 500
|
138
|
+
|
139
|
+
HEIGHT = 500
|
140
|
+
|
141
|
+
score = 0
|
142
|
+
|
143
|
+
OUTSIDE = 1000
|
144
|
+
|
145
|
+
gameover = False
|
146
|
+
|
147
|
+
titlemode = True
|
148
|
+
|
149
|
+
start = 0
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
class Charclass:
|
156
|
+
|
157
|
+
def __init__(self, filename, hp):
|
158
|
+
|
159
|
+
self.imagename = filename
|
160
|
+
|
161
|
+
self.hp = hp
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
char = []
|
168
|
+
|
169
|
+
# キャラの追加
|
170
|
+
|
171
|
+
char.append(Charclass("human.png", 999))
|
172
|
+
|
173
|
+
char.append(Charclass("dog.png", 1))
|
174
|
+
|
175
|
+
char.append(Charclass("bone.png", 1))
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
class Obj(Actor):
|
182
|
+
|
183
|
+
# objectの基本クラス
|
184
|
+
|
185
|
+
def __init__(self, x, y, angle, num):
|
186
|
+
|
187
|
+
Actor.__init__(self, char[num].imagename, (x, y))
|
188
|
+
|
189
|
+
self.hp = char[num].hp
|
190
|
+
|
191
|
+
self.num = num
|
192
|
+
|
193
|
+
self.count = 0
|
194
|
+
|
195
|
+
self.angle = angle
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
class Dog(Obj):
|
202
|
+
|
203
|
+
#犬クラス
|
204
|
+
|
205
|
+
def update(self):
|
206
|
+
|
207
|
+
# 円形に動く
|
208
|
+
|
209
|
+
rad = math.radians(self.count - 100)
|
210
|
+
|
211
|
+
self.y = (HEIGHT / 2) + (math.sin(rad) * 200)
|
212
|
+
|
213
|
+
self.x = (HEIGHT / 2) + (math.cos(rad) * 200)
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
class Bone(Obj):
|
220
|
+
|
221
|
+
# 骨クラス
|
222
|
+
|
223
|
+
def update(self):
|
224
|
+
|
225
|
+
self.x, self.y = shootBone(self.pos, self.angle, 8)
|
226
|
+
|
227
|
+
hitbox = Rect((self.x-15, self.y-15), (30, 30))
|
228
|
+
|
229
|
+
for sp in objects:
|
230
|
+
|
231
|
+
if sp.hp == 999: # playerなら無視
|
232
|
+
|
233
|
+
continue
|
234
|
+
|
235
|
+
if sp.colliderect(hitbox):# player以外で当たっていれば
|
236
|
+
|
237
|
+
self.x = OUTSIDE
|
238
|
+
|
239
|
+
sp.hp -= 1
|
240
|
+
|
241
|
+
break
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
class Player(Obj):
|
248
|
+
|
249
|
+
def update(self):
|
250
|
+
|
251
|
+
# キーボード操作
|
252
|
+
|
253
|
+
if keyboard.up:
|
254
|
+
|
255
|
+
self.y -= 4
|
256
|
+
|
257
|
+
if keyboard.down:
|
258
|
+
|
259
|
+
self.y += 4
|
260
|
+
|
261
|
+
if keyboard.left:
|
262
|
+
|
263
|
+
self.x -= 4
|
264
|
+
|
265
|
+
if keyboard.right:
|
266
|
+
|
267
|
+
self.x += 4
|
268
|
+
|
269
|
+
if self.x < 35:
|
270
|
+
|
271
|
+
self.x = 35
|
272
|
+
|
273
|
+
if self.y < 35:
|
274
|
+
|
275
|
+
self.y = 35
|
276
|
+
|
277
|
+
if self.x > (WIDTH-35):
|
278
|
+
|
279
|
+
self.x = WIDTH-35
|
280
|
+
|
281
|
+
if self.y > (HEIGHT-35):
|
282
|
+
|
283
|
+
self.y = HEIGHT-35
|
284
|
+
|
285
|
+
if keyboard.space and (self.count % 16) == 0:
|
286
|
+
|
287
|
+
# 骨を投げる
|
288
|
+
|
289
|
+
objects.append(Bone(self.x, self.y, 180, 2))
|
290
|
+
|
291
|
+
objects.append(Bone(self.x, self.y, 0, 2))
|
292
|
+
|
293
|
+
objects.append(Bone(self.x, self.y, -90, 2))
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
def shootBone(pos, angle, speed):
|
300
|
+
|
301
|
+
x, y = pos
|
302
|
+
|
303
|
+
rad = math.radians(angle)
|
304
|
+
|
305
|
+
x += speed * (math.cos(rad))
|
306
|
+
|
307
|
+
y += speed * (math.sin(rad))
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
return int(x), int(y)
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
objects = [Player(400, 400, 0, 0),
|
318
|
+
|
319
|
+
Dog(20, 20, 0, 1)]
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
def draw():
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
screen.clear()
|
330
|
+
|
331
|
+
# screen.draw.text("Time:" + str(start), (10, 10), color="white")
|
332
|
+
|
333
|
+
if titlemode == True:
|
334
|
+
|
335
|
+
screen.draw.text("dog game",
|
336
|
+
|
337
|
+
left=150, top=240, fontsize=64, color="YELLOW")
|
338
|
+
|
339
|
+
else:
|
340
|
+
|
341
|
+
for sp in objects:
|
342
|
+
|
343
|
+
sp.draw()
|
344
|
+
|
345
|
+
if gameover:
|
346
|
+
|
347
|
+
screen.draw.text("game over",
|
348
|
+
|
349
|
+
left=150, top=240, fontsize=64, color="YELLOW")
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
def update():
|
356
|
+
|
357
|
+
global titlemode, start, objects, gameover
|
358
|
+
|
359
|
+
start += 1
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
if titlemode:
|
364
|
+
|
365
|
+
if keyboard.space:
|
366
|
+
|
367
|
+
titlemode = False
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
if gameover:
|
372
|
+
|
373
|
+
return
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
for sp in objects:
|
378
|
+
|
379
|
+
if start == 1000:
|
380
|
+
|
381
|
+
gameover = True
|
382
|
+
|
383
|
+
sp.update()
|
384
|
+
|
385
|
+
sp.count += 1
|
386
|
+
|
387
|
+
if sp.hp <= 0:
|
388
|
+
|
389
|
+
sp.x = OUTSIDE
|
390
|
+
|
391
|
+
if sp.x < -35 or sp.x > (HEIGHT+35) or sp.y < -35 or sp.y > (WIDTH+35):
|
392
|
+
|
393
|
+
objects.remove(sp)
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
pgzrun.go()
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
```
|
404
|
+
|
121
405
|
### 試したこと
|
122
406
|
|
123
407
|
骨を投げた後の座標をprintしてみたのですが1回しか表示されませんでした。骨はobjectを入れるリストには入っていました。
|
1
タイトルを変更しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
pygame で
|
1
|
+
pygame でobjectが描画されない
|
test
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
骨をplayerから投げることを実現したいのですが座標が更新されず描画されません。
|
20
20
|
|
21
|
-
|
21
|
+
一度playerの座標に表示され、消えてしまいます。
|
22
22
|
|
23
23
|
### 該当のソースコード
|
24
24
|
|