質問編集履歴
2
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -58,6 +58,148 @@
|
|
58
58
|
|
59
59
|
```
|
60
60
|
|
61
|
+
コード全体
|
62
|
+
|
63
|
+
```python
|
64
|
+
import pgzrun
|
65
|
+
import random
|
66
|
+
import time
|
67
|
+
import math
|
68
|
+
|
69
|
+
WIDTH = 500
|
70
|
+
HEIGHT = 500
|
71
|
+
score = 0
|
72
|
+
OUTSIDE = 1000
|
73
|
+
gameover = False
|
74
|
+
titlemode = True
|
75
|
+
start = 0
|
76
|
+
|
77
|
+
|
78
|
+
class Charclass:
|
79
|
+
def __init__(self, filename, hp):
|
80
|
+
self.imagename = filename
|
81
|
+
self.hp = hp
|
82
|
+
|
83
|
+
|
84
|
+
char = []
|
85
|
+
# キャラの追加
|
86
|
+
char.append(Charclass("human.png", 999))
|
87
|
+
char.append(Charclass("dog.png", 1))
|
88
|
+
char.append(Charclass("bone.png", 1))
|
89
|
+
|
90
|
+
|
91
|
+
class Obj(Actor):
|
92
|
+
# objectの基本クラス
|
93
|
+
def __init__(self, x, y, angle, num):
|
94
|
+
Actor.__init__(self, char[num].imagename, (x, y))
|
95
|
+
self.hp = char[num].hp
|
96
|
+
self.num = num
|
97
|
+
self.count = 0
|
98
|
+
self.angle = angle
|
99
|
+
|
100
|
+
|
101
|
+
class Dog(Obj):
|
102
|
+
#犬クラス
|
103
|
+
def update(self):
|
104
|
+
# 円形に動く
|
105
|
+
rad = math.radians(self.count - 100)
|
106
|
+
self.y = (HEIGHT / 2) + (math.sin(rad) * 200)
|
107
|
+
self.x = (HEIGHT / 2) + (math.cos(rad) * 200)
|
108
|
+
|
109
|
+
|
110
|
+
class Bone(Obj):
|
111
|
+
# 骨クラス
|
112
|
+
def update(self):
|
113
|
+
self.x, self.y = shootBone(self.pos, self.angle, 8)
|
114
|
+
hitbox = Rect((self.x-15, self.y-15), (30, 30))
|
115
|
+
for sp in objects:
|
116
|
+
if sp.hp == 999: # playerなら無視
|
117
|
+
continue
|
118
|
+
if sp.colliderect(hitbox):# player以外で当たっていれば
|
119
|
+
self.x = OUTSIDE
|
120
|
+
sp.hp -= 1
|
121
|
+
break
|
122
|
+
|
123
|
+
|
124
|
+
class Player(Obj):
|
125
|
+
def update(self):
|
126
|
+
# キーボード操作
|
127
|
+
if keyboard.up:
|
128
|
+
self.y -= 4
|
129
|
+
if keyboard.down:
|
130
|
+
self.y += 4
|
131
|
+
if keyboard.left:
|
132
|
+
self.x -= 4
|
133
|
+
if keyboard.right:
|
134
|
+
self.x += 4
|
135
|
+
if self.x < 35:
|
136
|
+
self.x = 35
|
137
|
+
if self.y < 35:
|
138
|
+
self.y = 35
|
139
|
+
if self.x > (WIDTH-35):
|
140
|
+
self.x = WIDTH-35
|
141
|
+
if self.y > (HEIGHT-35):
|
142
|
+
self.y = HEIGHT-35
|
143
|
+
if keyboard.space and (self.count % 16) == 0:
|
144
|
+
# 骨を投げる
|
145
|
+
objects.append(Bone(self.x, self.y, 180, 2))
|
146
|
+
objects.append(Bone(self.x, self.y, 0, 2))
|
147
|
+
objects.append(Bone(self.x, self.y, -90, 2))
|
148
|
+
|
149
|
+
|
150
|
+
def shootBone(pos, angle, speed):
|
151
|
+
x, y = pos
|
152
|
+
rad = math.radians(angle)
|
153
|
+
x += speed * (math.cos(rad))
|
154
|
+
y += speed * (math.sin(rad))
|
155
|
+
|
156
|
+
return int(x), int(y)
|
157
|
+
|
158
|
+
|
159
|
+
objects = [Player(400, 400, 0, 0),
|
160
|
+
Dog(20, 20, 0, 1)]
|
161
|
+
|
162
|
+
|
163
|
+
def draw():
|
164
|
+
|
165
|
+
screen.clear()
|
166
|
+
# screen.draw.text("Time:" + str(start), (10, 10), color="white")
|
167
|
+
if titlemode == True:
|
168
|
+
screen.draw.text("dog game",
|
169
|
+
left=150, top=240, fontsize=64, color="YELLOW")
|
170
|
+
else:
|
171
|
+
for sp in objects:
|
172
|
+
sp.draw()
|
173
|
+
if gameover:
|
174
|
+
screen.draw.text("game over",
|
175
|
+
left=150, top=240, fontsize=64, color="YELLOW")
|
176
|
+
|
177
|
+
|
178
|
+
def update():
|
179
|
+
global titlemode, start, objects, gameover
|
180
|
+
start += 1
|
181
|
+
|
182
|
+
if titlemode:
|
183
|
+
if keyboard.space:
|
184
|
+
titlemode = False
|
185
|
+
|
186
|
+
if gameover:
|
187
|
+
return
|
188
|
+
|
189
|
+
for sp in objects:
|
190
|
+
if start == 1000:
|
191
|
+
gameover = True
|
192
|
+
sp.update()
|
193
|
+
sp.count += 1
|
194
|
+
if sp.hp <= 0:
|
195
|
+
sp.x = OUTSIDE
|
196
|
+
if sp.x < -35 or sp.x > (HEIGHT+35) or sp.y < -35 or sp.y > (WIDTH+35):
|
197
|
+
objects.remove(sp)
|
198
|
+
|
199
|
+
|
200
|
+
pgzrun.go()
|
201
|
+
|
202
|
+
```
|
61
203
|
### 試したこと
|
62
204
|
骨を投げた後の座標をprintしてみたのですが1回しか表示されませんでした。骨はobjectを入れるリストには入っていました。
|
63
205
|
|
1
タイトルを変更しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
pygame で
|
1
|
+
pygame でobjectが描画されない
|
body
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
### 発生している問題・エラーメッセージ
|
9
9
|
|
10
10
|
骨をplayerから投げることを実現したいのですが座標が更新されず描画されません。
|
11
|
-
|
11
|
+
一度playerの座標に表示され、消えてしまいます。
|
12
12
|
### 該当のソースコード
|
13
13
|
以下が該当コード部分です。
|
14
14
|
|