回答編集履歴
3
補足
answer
CHANGED
@@ -66,6 +66,7 @@
|
|
66
66
|
def main():
|
67
67
|
clock = pygame.time.Clock()
|
68
68
|
note_list = []
|
69
|
+
# 生成間隔:20、数値が小さいほど生成間隔が短くなります。
|
69
70
|
t = cycle(list(range(20)))
|
70
71
|
while True:
|
71
72
|
clock.tick(30)
|
2
追記
answer
CHANGED
@@ -9,13 +9,15 @@
|
|
9
9
|
0. [pygameでスペースを押すと、上に動く四角を表示させたい。](https://teratail.com/questions/115898)
|
10
10
|
|
11
11
|
---
|
12
|
+
結構悩んでらっしゃるみたいなので、サンプルコードを添付します。
|
12
|
-
次 x,y座標のどこに位置するかを求めればよいのではないでしょうか。
|
13
|
+
こんな感じで次 x,y座標のどこに位置するかを求めればよいのではないでしょうか。
|
13
14
|
|
14
15
|
```Python
|
15
16
|
# -*- coding: utf-8 -*-
|
16
|
-
|
17
|
+
from sys import exit
|
17
18
|
import math
|
18
19
|
from random import randint
|
20
|
+
from itertools import cycle
|
19
21
|
import pygame
|
20
22
|
from pygame.locals import *
|
21
23
|
|
@@ -30,24 +32,19 @@
|
|
30
32
|
self.w = 10
|
31
33
|
self.h = 10
|
32
34
|
self.rect = Rect(x, y, self.w, self.h)
|
35
|
+
# x方向の増分は固定値:5
|
33
36
|
self.vx = 5
|
34
|
-
|
37
|
+
# 描画色
|
38
|
+
self.color = (randint(0, 255), randint(0, 255), randint(0, 255))
|
35
39
|
|
36
|
-
|
37
|
-
def draw(self):
|
38
|
-
pygame.draw.rect(screen, (255, 255, 255), self.rect)
|
39
|
-
|
40
40
|
def move(self):
|
41
|
-
self.time += 1
|
42
|
-
radians = self.
|
41
|
+
radians = self.rect.x % 360
|
43
|
-
|
44
|
-
if self.rect.y > 200:
|
45
|
-
|
42
|
+
vy = 2 * math.sin(math.radians(radians)) * self.h
|
46
|
-
else:
|
47
|
-
vy = math.cos(math.radians(radians)) * self.h
|
48
|
-
|
49
43
|
# 移動
|
50
44
|
self.rect.move_ip(self.vx, vy)
|
45
|
+
|
46
|
+
def draw(self):
|
47
|
+
pygame.draw.rect(screen, self.color, self.rect)
|
51
48
|
|
52
49
|
@property
|
53
50
|
def is_destroy(self) -> bool:
|
@@ -64,24 +61,24 @@
|
|
64
61
|
|
65
62
|
def application_exit():
|
66
63
|
pygame.quit()
|
67
|
-
|
64
|
+
exit()
|
68
65
|
|
69
66
|
def main():
|
70
67
|
clock = pygame.time.Clock()
|
71
68
|
note_list = []
|
72
|
-
|
69
|
+
t = cycle(list(range(20)))
|
73
70
|
while True:
|
74
71
|
clock.tick(30)
|
75
72
|
screen.fill((0, 0, 0))
|
76
|
-
|
73
|
+
# 時間経過を元に生成
|
77
|
-
i
|
74
|
+
i = next(t)
|
78
|
-
if i
|
75
|
+
if i == 0:
|
79
76
|
note_list.append(Fire(0, 40))
|
80
|
-
i = 0
|
81
77
|
|
78
|
+
#生成したオブジェクトに対して移動と描画を行う
|
82
|
-
for
|
79
|
+
for note in note_list:
|
83
|
-
|
80
|
+
note.move()
|
84
|
-
|
81
|
+
note.draw()
|
85
82
|
#リストからスクリーン範囲外の物を削除
|
86
83
|
note_list = list(filter(lambda x: not x.is_destroy, note_list))
|
87
84
|
pygame.display.update()
|
@@ -95,4 +92,7 @@
|
|
95
92
|
if __name__ == "__main__":
|
96
93
|
main()
|
97
94
|
|
98
|
-
```
|
95
|
+
```
|
96
|
+
◇参考情報
|
97
|
+
0. [9.2.3. 三角関数](https://docs.python.jp/3/library/math.html#trigonometric-functions)
|
98
|
+
0. [itertools.cycle](https://docs.python.jp/3/library/itertools.html#itertools.cycle)
|
1
追記
answer
CHANGED
@@ -6,4 +6,93 @@
|
|
6
6
|
|
7
7
|
◇過去の質問で参考になりそうなもの。
|
8
8
|
0. [pygame spriteで画像ではなく図形を扱いたい(動かしたい)](https://teratail.com/questions/116828)
|
9
|
-
0. [pygameでスペースを押すと、上に動く四角を表示させたい。](https://teratail.com/questions/115898)
|
9
|
+
0. [pygameでスペースを押すと、上に動く四角を表示させたい。](https://teratail.com/questions/115898)
|
10
|
+
|
11
|
+
---
|
12
|
+
次 x,y座標のどこに位置するかを求めればよいのではないでしょうか。
|
13
|
+
|
14
|
+
```Python
|
15
|
+
# -*- coding: utf-8 -*-
|
16
|
+
import sys
|
17
|
+
import math
|
18
|
+
from random import randint
|
19
|
+
import pygame
|
20
|
+
from pygame.locals import *
|
21
|
+
|
22
|
+
pygame.init()
|
23
|
+
screen = pygame.display.set_mode((640, 480))
|
24
|
+
pygame.display.set_caption("A126871")
|
25
|
+
|
26
|
+
#クラス名は大文字で
|
27
|
+
class Fire(object):
|
28
|
+
def __init__(self, x, y):
|
29
|
+
super().__init__()
|
30
|
+
self.w = 10
|
31
|
+
self.h = 10
|
32
|
+
self.rect = Rect(x, y, self.w, self.h)
|
33
|
+
self.vx = 5
|
34
|
+
self.time = 0
|
35
|
+
|
36
|
+
|
37
|
+
def draw(self):
|
38
|
+
pygame.draw.rect(screen, (255, 255, 255), self.rect)
|
39
|
+
|
40
|
+
def move(self):
|
41
|
+
self.time += 1
|
42
|
+
radians = self.time % 360
|
43
|
+
|
44
|
+
if self.rect.y > 200:
|
45
|
+
vy = math.sin(math.radians(radians)) * self.h
|
46
|
+
else:
|
47
|
+
vy = math.cos(math.radians(radians)) * self.h
|
48
|
+
|
49
|
+
# 移動
|
50
|
+
self.rect.move_ip(self.vx, vy)
|
51
|
+
|
52
|
+
@property
|
53
|
+
def is_destroy(self) -> bool:
|
54
|
+
return any([self.rect.x < -self.w, self.rect.y < -self.h, self.rect.x > 640, self.rect.y > 480])
|
55
|
+
#以下の5行と判定は同じです。
|
56
|
+
#if self.rect.x < -self.w or self.rect.y < -self.h:
|
57
|
+
# return True
|
58
|
+
#if self.rect.x > 640 or self.rect.y > 480:
|
59
|
+
# return True
|
60
|
+
#return False
|
61
|
+
|
62
|
+
def __str__(self):
|
63
|
+
return ','.join(map(str, [self.rect.x, self.rect.y, self.rect.width, self.rect.height, self.is_destroy]))
|
64
|
+
|
65
|
+
def application_exit():
|
66
|
+
pygame.quit()
|
67
|
+
sys.exit()
|
68
|
+
|
69
|
+
def main():
|
70
|
+
clock = pygame.time.Clock()
|
71
|
+
note_list = []
|
72
|
+
i = 0
|
73
|
+
while True:
|
74
|
+
clock.tick(30)
|
75
|
+
screen.fill((0, 0, 0))
|
76
|
+
|
77
|
+
i += 1
|
78
|
+
if i % 20 ==0:
|
79
|
+
note_list.append(Fire(0, 40))
|
80
|
+
i = 0
|
81
|
+
|
82
|
+
for bullet in note_list:
|
83
|
+
bullet.move()
|
84
|
+
bullet.draw()
|
85
|
+
#リストからスクリーン範囲外の物を削除
|
86
|
+
note_list = list(filter(lambda x: not x.is_destroy, note_list))
|
87
|
+
pygame.display.update()
|
88
|
+
for event in pygame.event.get():
|
89
|
+
if event.type == QUIT:
|
90
|
+
application_exit()
|
91
|
+
if event.type == KEYDOWN:
|
92
|
+
if event.key == K_ESCAPE:
|
93
|
+
application_exit()
|
94
|
+
|
95
|
+
if __name__ == "__main__":
|
96
|
+
main()
|
97
|
+
|
98
|
+
```
|