回答編集履歴

3

補足

2018/05/26 18:09

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -134,6 +134,8 @@
134
134
 
135
135
  note_list = []
136
136
 
137
+ # 生成間隔:20、数値が小さいほど生成間隔が短くなります。
138
+
137
139
  t = cycle(list(range(20)))
138
140
 
139
141
  while True:

2

追記

2018/05/26 18:09

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -20,7 +20,9 @@
20
20
 
21
21
  ---
22
22
 
23
+ 結構悩んでらっしゃるみたいなので、サンプルコードを添付します。
24
+
23
- 次 x,y座標のどこに位置するかを求めればよいのではないでしょうか。
25
+ こんな感じで次 x,y座標のどこに位置するかを求めればよいのではないでしょうか。
24
26
 
25
27
 
26
28
 
@@ -28,11 +30,13 @@
28
30
 
29
31
  # -*- coding: utf-8 -*-
30
32
 
31
- import sys
33
+ from sys import exit
32
34
 
33
35
  import math
34
36
 
35
37
  from random import randint
38
+
39
+ from itertools import cycle
36
40
 
37
41
  import pygame
38
42
 
@@ -62,41 +66,31 @@
62
66
 
63
67
  self.rect = Rect(x, y, self.w, self.h)
64
68
 
69
+ # x方向の増分は固定値:5
70
+
65
71
  self.vx = 5
66
72
 
67
- self.time = 0
73
+ # 描画色
68
74
 
69
-
70
-
71
-
72
-
73
- def draw(self):
74
-
75
- pygame.draw.rect(screen, (255, 255, 255), self.rect)
75
+ self.color = (randint(0, 255), randint(0, 255), randint(0, 255))
76
76
 
77
77
 
78
78
 
79
79
  def move(self):
80
80
 
81
- self.time += 1
81
+ radians = self.rect.x % 360
82
82
 
83
- radians = self.time % 360
84
-
85
-
86
-
87
- if self.rect.y > 200:
88
-
89
- vy = math.sin(math.radians(radians)) * self.h
83
+ vy = 2 * math.sin(math.radians(radians)) * self.h
90
-
91
- else:
92
-
93
- vy = math.cos(math.radians(radians)) * self.h
94
-
95
-
96
84
 
97
85
  # 移動
98
86
 
99
87
  self.rect.move_ip(self.vx, vy)
88
+
89
+
90
+
91
+ def draw(self):
92
+
93
+ pygame.draw.rect(screen, self.color, self.rect)
100
94
 
101
95
 
102
96
 
@@ -130,7 +124,7 @@
130
124
 
131
125
  pygame.quit()
132
126
 
133
- sys.exit()
127
+ exit()
134
128
 
135
129
 
136
130
 
@@ -140,7 +134,7 @@
140
134
 
141
135
  note_list = []
142
136
 
143
- i = 0
137
+ t = cycle(list(range(20)))
144
138
 
145
139
  while True:
146
140
 
@@ -148,23 +142,23 @@
148
142
 
149
143
  screen.fill((0, 0, 0))
150
144
 
145
+ # 時間経過を元に生成
151
146
 
147
+ i = next(t)
152
148
 
153
- i += 1
154
-
155
- if i % 20 ==0:
149
+ if i == 0:
156
150
 
157
151
  note_list.append(Fire(0, 40))
158
152
 
159
- i = 0
160
153
 
161
154
 
155
+ #生成したオブジェクトに対して移動と描画を行う
162
156
 
163
- for bullet in note_list:
157
+ for note in note_list:
164
158
 
165
- bullet.move()
159
+ note.move()
166
160
 
167
- bullet.draw()
161
+ note.draw()
168
162
 
169
163
  #リストからスクリーン範囲外の物を削除
170
164
 
@@ -193,3 +187,9 @@
193
187
 
194
188
 
195
189
  ```
190
+
191
+ ◇参考情報
192
+
193
+ 0. [9.2.3. 三角関数](https://docs.python.jp/3/library/math.html#trigonometric-functions)
194
+
195
+ 0. [itertools.cycle](https://docs.python.jp/3/library/itertools.html#itertools.cycle)

1

追記

2018/05/26 17:59

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -15,3 +15,181 @@
15
15
  0. [pygame spriteで画像ではなく図形を扱いたい(動かしたい)](https://teratail.com/questions/116828)
16
16
 
17
17
  0. [pygameでスペースを押すと、上に動く四角を表示させたい。](https://teratail.com/questions/115898)
18
+
19
+
20
+
21
+ ---
22
+
23
+ 次 x,y座標のどこに位置するかを求めればよいのではないでしょうか。
24
+
25
+
26
+
27
+ ```Python
28
+
29
+ # -*- coding: utf-8 -*-
30
+
31
+ import sys
32
+
33
+ import math
34
+
35
+ from random import randint
36
+
37
+ import pygame
38
+
39
+ from pygame.locals import *
40
+
41
+
42
+
43
+ pygame.init()
44
+
45
+ screen = pygame.display.set_mode((640, 480))
46
+
47
+ pygame.display.set_caption("A126871")
48
+
49
+
50
+
51
+ #クラス名は大文字で
52
+
53
+ class Fire(object):
54
+
55
+ def __init__(self, x, y):
56
+
57
+ super().__init__()
58
+
59
+ self.w = 10
60
+
61
+ self.h = 10
62
+
63
+ self.rect = Rect(x, y, self.w, self.h)
64
+
65
+ self.vx = 5
66
+
67
+ self.time = 0
68
+
69
+
70
+
71
+
72
+
73
+ def draw(self):
74
+
75
+ pygame.draw.rect(screen, (255, 255, 255), self.rect)
76
+
77
+
78
+
79
+ def move(self):
80
+
81
+ self.time += 1
82
+
83
+ radians = self.time % 360
84
+
85
+
86
+
87
+ if self.rect.y > 200:
88
+
89
+ vy = math.sin(math.radians(radians)) * self.h
90
+
91
+ else:
92
+
93
+ vy = math.cos(math.radians(radians)) * self.h
94
+
95
+
96
+
97
+ # 移動
98
+
99
+ self.rect.move_ip(self.vx, vy)
100
+
101
+
102
+
103
+ @property
104
+
105
+ def is_destroy(self) -> bool:
106
+
107
+ return any([self.rect.x < -self.w, self.rect.y < -self.h, self.rect.x > 640, self.rect.y > 480])
108
+
109
+ #以下の5行と判定は同じです。
110
+
111
+ #if self.rect.x < -self.w or self.rect.y < -self.h:
112
+
113
+ # return True
114
+
115
+ #if self.rect.x > 640 or self.rect.y > 480:
116
+
117
+ # return True
118
+
119
+ #return False
120
+
121
+
122
+
123
+ def __str__(self):
124
+
125
+ return ','.join(map(str, [self.rect.x, self.rect.y, self.rect.width, self.rect.height, self.is_destroy]))
126
+
127
+
128
+
129
+ def application_exit():
130
+
131
+ pygame.quit()
132
+
133
+ sys.exit()
134
+
135
+
136
+
137
+ def main():
138
+
139
+ clock = pygame.time.Clock()
140
+
141
+ note_list = []
142
+
143
+ i = 0
144
+
145
+ while True:
146
+
147
+ clock.tick(30)
148
+
149
+ screen.fill((0, 0, 0))
150
+
151
+
152
+
153
+ i += 1
154
+
155
+ if i % 20 ==0:
156
+
157
+ note_list.append(Fire(0, 40))
158
+
159
+ i = 0
160
+
161
+
162
+
163
+ for bullet in note_list:
164
+
165
+ bullet.move()
166
+
167
+ bullet.draw()
168
+
169
+ #リストからスクリーン範囲外の物を削除
170
+
171
+ note_list = list(filter(lambda x: not x.is_destroy, note_list))
172
+
173
+ pygame.display.update()
174
+
175
+ for event in pygame.event.get():
176
+
177
+ if event.type == QUIT:
178
+
179
+ application_exit()
180
+
181
+ if event.type == KEYDOWN:
182
+
183
+ if event.key == K_ESCAPE:
184
+
185
+ application_exit()
186
+
187
+
188
+
189
+ if __name__ == "__main__":
190
+
191
+ main()
192
+
193
+
194
+
195
+ ```