質問編集履歴
1
問題点が変わった
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
pygame.sprite.spritecollide
|
1
|
+
pygame.sprite.spritecollideを使ってスプライトの当たり判定をつけたい
|
test
CHANGED
@@ -1,227 +1,205 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
pythonのpygameを使ってスプライトの当たり判定をつけたいのです。
|
3
|
+
pythonのpygame.sprite.spritecollideを使ってスプライトの当たり判定をつけたいのです。
|
4
|
-
|
4
|
+
|
5
|
-
上からくるスプライト(human)と下からくるスプライト(
|
5
|
+
上からくるスプライト(human)と下からくるスプライト(human)が接触したらhumanの進む方向を逆にしたいです。
|
6
6
|
|
7
7
|
### 発生している問題・エラーメッセージ
|
8
8
|
|
9
|
+
スプライトが初期位置から動かない。vyの正負が交互に変わっているためだと思う。原因としてはpygame.sprite.spritecollideがスタート時から当たり判定が出ているためと思われる。
|
10
|
+
|
11
|
+
当たり判定を正しく機能させたい。
|
12
|
+
|
13
|
+
### 該当のソースコード
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```python
|
18
|
+
|
19
|
+
import pygame
|
20
|
+
|
21
|
+
from pygame.locals import *
|
22
|
+
|
23
|
+
import sys
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
SCR_RECT = Rect(0, 0, 600, 500)
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
class Player(pygame.sprite.Sprite):
|
32
|
+
|
33
|
+
def __init__(self, filename, x, y, vx, vy):
|
34
|
+
|
9
|
-
pygame.sprite.
|
35
|
+
pygame.sprite.Sprite.__init__(self,self.containers)
|
36
|
+
|
37
|
+
self.image = pygame.image.load(filename).convert_alpha()
|
38
|
+
|
39
|
+
width = self.image.get_width()
|
40
|
+
|
41
|
+
height = self.image.get_height()
|
42
|
+
|
43
|
+
self.rect = Rect(x, y, width, height)
|
44
|
+
|
45
|
+
self.vx = vx
|
46
|
+
|
47
|
+
self.vy = vy
|
48
|
+
|
49
|
+
self.x=x
|
50
|
+
|
51
|
+
self.y=y
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def update(self):
|
56
|
+
|
57
|
+
self.rect.move_ip(self.vx,self.vy)
|
58
|
+
|
59
|
+
pressed_key = pygame.key.get_pressed()
|
60
|
+
|
61
|
+
if pressed_key[K_LEFT]:
|
62
|
+
|
63
|
+
self.rect.move_ip(-2, 0)
|
64
|
+
|
65
|
+
self.x=self.x-2
|
66
|
+
|
67
|
+
if pressed_key[K_RIGHT]:
|
68
|
+
|
69
|
+
self.rect.move_ip(2, 0)
|
70
|
+
|
71
|
+
self.x=self.x+2
|
72
|
+
|
73
|
+
if pressed_key[K_UP]:
|
74
|
+
|
75
|
+
self.rect.move_ip(0, -2)
|
76
|
+
|
77
|
+
self.y=self.y-2
|
78
|
+
|
79
|
+
if pressed_key[K_DOWN]:
|
80
|
+
|
81
|
+
self.rect.move_ip(0, 2)
|
82
|
+
|
83
|
+
self.y=self.y+2
|
84
|
+
|
85
|
+
def draw(self, screen):
|
86
|
+
|
87
|
+
screen.blit(self.image, self.rect)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
class Human(pygame.sprite.Sprite):
|
92
|
+
|
93
|
+
def __init__(self, filename, x, y, vx, vy,blocks):
|
94
|
+
|
95
|
+
pygame.sprite.Sprite.__init__(self,self.containers)
|
96
|
+
|
97
|
+
self.image = pygame.image.load(filename).convert_alpha()
|
98
|
+
|
99
|
+
width = self.image.get_width()
|
100
|
+
|
101
|
+
height = self.image.get_height()
|
102
|
+
|
103
|
+
self.blocks=blocks
|
104
|
+
|
105
|
+
self.rect = Rect(x, y, width, height)
|
106
|
+
|
107
|
+
self.vx = vx
|
108
|
+
|
109
|
+
self.vy = vy
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def update(self):
|
114
|
+
|
115
|
+
self.rect.move_ip(self.vx, self.vy)
|
116
|
+
|
117
|
+
blocks_collided = pygame.sprite.spritecollide(self, self.blocks ,False)
|
118
|
+
|
119
|
+
if blocks_collided:
|
120
|
+
|
121
|
+
self.vy=-self.vy
|
122
|
+
|
123
|
+
def draw(self, screen):
|
124
|
+
|
125
|
+
screen.blit(self.image, self.rect)
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def main():
|
130
|
+
|
131
|
+
pygame.init()
|
132
|
+
|
133
|
+
screen = pygame.display.set_mode(SCR_RECT.size)
|
134
|
+
|
135
|
+
pygame.display.set_caption(u"スプライトの使い方")
|
136
|
+
|
137
|
+
# スプライトグループの作成
|
138
|
+
|
139
|
+
group = pygame.sprite.RenderUpdates()
|
140
|
+
|
141
|
+
blocks=pygame.sprite.Group()
|
142
|
+
|
143
|
+
# スプライトの追加
|
144
|
+
|
145
|
+
Player.containers=group
|
146
|
+
|
147
|
+
Human.containers=group,blocks
|
148
|
+
|
149
|
+
# スプライトを作成
|
150
|
+
|
151
|
+
for i in range(5):
|
152
|
+
|
153
|
+
Human("image/human.png", 300+(60*i), 0, 0, 1,blocks)
|
154
|
+
|
155
|
+
Human("image/human.png", 330+(60*i), 300, 0, -1,blocks)
|
156
|
+
|
157
|
+
Player("image/player.png", 300, 400, 0, 0)
|
158
|
+
|
159
|
+
clock = pygame.time.Clock()
|
160
|
+
|
161
|
+
while True:
|
162
|
+
|
163
|
+
clock.tick(60) # 60fps
|
164
|
+
|
165
|
+
screen.fill((255,255,255))
|
166
|
+
|
167
|
+
# スプライトグループを更新
|
168
|
+
|
169
|
+
group.update()
|
170
|
+
|
171
|
+
# スプライトを描画
|
172
|
+
|
173
|
+
group.draw(screen)
|
174
|
+
|
175
|
+
pygame.display.update()
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
for event in pygame.event.get():
|
180
|
+
|
181
|
+
if event.type == QUIT:
|
182
|
+
|
183
|
+
pygame.quit()
|
184
|
+
|
185
|
+
sys.exit()
|
186
|
+
|
187
|
+
if event.type == KEYDOWN: # キーを押したとき
|
188
|
+
|
189
|
+
if event.key == K_ESCAPE: # Escキーが押されたとき
|
190
|
+
|
191
|
+
pygame.quit()
|
192
|
+
|
193
|
+
sys.exit()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
if __name__ == "__main__":
|
198
|
+
|
199
|
+
main()
|
10
200
|
|
11
201
|
```
|
12
202
|
|
13
|
-
NameError Traceback (most recent call last)
|
14
|
-
|
15
|
-
<ipython-input-1-05ff0019ad46> in <module>()
|
16
|
-
|
17
|
-
69
|
18
|
-
|
19
|
-
70 if __name__ == "__main__":
|
20
|
-
|
21
|
-
---> 71 main()
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
<ipython-input-1-05ff0019ad46> in main()
|
26
|
-
|
27
|
-
54 screen.fill((255,255,255))
|
28
|
-
|
29
|
-
55 # スプライトグループを更新
|
30
|
-
|
31
|
-
---> 56 group.update()
|
32
|
-
|
33
|
-
57 # スプライトを描画
|
34
|
-
|
35
|
-
58 group.draw(screen)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
~\Anaconda3\lib\site-packages\pygame\sprite.py in update(self, *args)
|
40
|
-
|
41
|
-
460 """
|
42
|
-
|
43
|
-
461 for s in self.sprites():
|
44
|
-
|
45
|
-
--> 462 s.update(*args)
|
46
|
-
|
47
|
-
463
|
48
|
-
|
49
|
-
464 def draw(self, surface):
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
<ipython-input-1-05ff0019ad46> in update(self)
|
54
|
-
|
55
|
-
32 def update(self):
|
56
|
-
|
57
|
-
33 self.rect.move_ip(self.vx, self.vy)
|
58
|
-
|
59
|
-
---> 34 if pygame.sprite.spritecollideany(human, group):
|
60
|
-
|
61
|
-
35 self.vy=-self.vy
|
62
|
-
|
63
|
-
36 def draw(self, screen):
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
NameError: name 'human' is not defined
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
```
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
### 該当のソースコード
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
```python
|
80
|
-
|
81
|
-
import pygame
|
82
|
-
|
83
|
-
from pygame.locals import *
|
84
|
-
|
85
|
-
import sys
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
SCR_RECT = Rect(0, 0, 600, 500)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
class Player(pygame.sprite.Sprite):
|
94
|
-
|
95
|
-
def __init__(self, filename, x, y, vx, vy):
|
96
|
-
|
97
|
-
pygame.sprite.Sprite.__init__(self)
|
98
|
-
|
99
|
-
self.image = pygame.image.load(filename).convert_alpha()
|
100
|
-
|
101
|
-
width = self.image.get_width()
|
102
|
-
|
103
|
-
height = self.image.get_height()
|
104
|
-
|
105
|
-
self.rect = Rect(x, y, width, height)
|
106
|
-
|
107
|
-
self.vx = vx
|
108
|
-
|
109
|
-
self.vy = vy
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
def update(self):
|
114
|
-
|
115
|
-
self.rect.move_ip(self.vx,self.vy)
|
116
|
-
|
117
|
-
def draw(self, screen):
|
118
|
-
|
119
|
-
screen.blit(self.image, self.rect)
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
class Human(pygame.sprite.Sprite):
|
124
|
-
|
125
|
-
def __init__(self, filename, x, y, vx, vy):
|
126
|
-
|
127
|
-
pygame.sprite.Sprite.__init__(self)
|
128
|
-
|
129
|
-
self.image = pygame.image.load(filename).convert_alpha()
|
130
|
-
|
131
|
-
width = self.image.get_width()
|
132
|
-
|
133
|
-
height = self.image.get_height()
|
134
|
-
|
135
|
-
self.rect = Rect(x, y, width, height)
|
136
|
-
|
137
|
-
self.vx = vx
|
138
|
-
|
139
|
-
self.vy = vy
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
def update(self):
|
144
|
-
|
145
|
-
self.rect.move_ip(self.vx, self.vy)
|
146
|
-
|
147
|
-
if pygame.sprite.spritecollideany(human, group):
|
148
|
-
|
149
|
-
self.vy=-self.vy
|
150
|
-
|
151
|
-
def draw(self, screen):
|
152
|
-
|
153
|
-
screen.blit(self.image, self.rect)
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
def main():
|
158
|
-
|
159
|
-
pygame.init()
|
160
|
-
|
161
|
-
screen = pygame.display.set_mode(SCR_RECT.size)
|
162
|
-
|
163
|
-
pygame.display.set_caption(u"スプライトの使い方")
|
164
|
-
|
165
|
-
# スプライトを作成
|
166
|
-
|
167
|
-
player=Player("image/player.png", 300, 500, 0, -1)
|
168
|
-
|
169
|
-
human = Human("image/human.png", 300, 0, 0, 1)
|
170
|
-
|
171
|
-
# スプライトグループの作成
|
172
|
-
|
173
|
-
group = pygame.sprite.RenderUpdates()
|
174
|
-
|
175
|
-
# スプライトの追加
|
176
|
-
|
177
|
-
group.add(player)
|
178
|
-
|
179
|
-
group.add(human)
|
180
|
-
|
181
|
-
clock = pygame.time.Clock()
|
182
|
-
|
183
|
-
while True:
|
184
|
-
|
185
|
-
clock.tick(60) # 60fps
|
186
|
-
|
187
|
-
screen.fill((255,255,255))
|
188
|
-
|
189
|
-
# スプライトグループを更新
|
190
|
-
|
191
|
-
group.update()
|
192
|
-
|
193
|
-
# スプライトを描画
|
194
|
-
|
195
|
-
group.draw(screen)
|
196
|
-
|
197
|
-
pygame.display.update()
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
for event in pygame.event.get():
|
202
|
-
|
203
|
-
if event.type == QUIT:
|
204
|
-
|
205
|
-
pygame.quit()
|
206
|
-
|
207
|
-
sys.exit()
|
208
|
-
|
209
|
-
if event.type == KEYDOWN: # キーを押したとき
|
210
|
-
|
211
|
-
if event.key == K_ESCAPE: # Escキーが押されたとき
|
212
|
-
|
213
|
-
pygame.quit()
|
214
|
-
|
215
|
-
sys.exit()
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
if __name__ == "__main__":
|
220
|
-
|
221
|
-
main()
|
222
|
-
|
223
|
-
```
|
224
|
-
|
225
203
|
|
226
204
|
|
227
205
|
### 試したこと
|