PyGame Zero:クラスの応用

Python

game_shooting1.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pgzrun
 
WIDTH = 320
HEIGHT = 480
TITLE = "PyGame Zeroでクラスを使う"
 
BACKGROUND = 50, 165, 165
 
# プレイヤークラス(Actorクラスを継承)
class Player(Actor):
    def __init__(self, name):
        super().__init__(name)
        self.pos = 140, 300
        self.isMoving = False
 
player = Player("kero")
dx, dy = 0, 0
 
def on_mouse_down(pos):
    global dx, dy
    player.isMoving = True
    x, y = pos
    dx = player.x - x
    dy = player.y - y
 
def on_mouse_up():
    #global isMoving
    player.isMoving = False
 
def on_mouse_move(pos):
    if player.isMoving:
        x, y = pos
        player.x = dx + x
        player.y = dy + y
 
def draw():
    screen.fill(BACKGROUND)
    player.draw()
 
def update():
    pass
 
pgzrun.go()

game_shooting2.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pgzrun
 
WIDTH = 320
HEIGHT = 480
TITLE = "PyGame Zeroでクラスを使う2"
 
BACKGROUND = 50, 165, 165
shots = []
 
# プレイヤークラス(Actorクラスを継承)
class Player(Actor):
    def __init__(self, name):
        super().__init__(name)
        self.pos = 140, 300
        self.isMoving = False
 
# プレイヤーの弾クラス
class Shot(Actor):
    def __init__(self, name, x, y):
        super().__init__(name)
        self.pos = x, y
        self.speed = 5
        self.isRunning = True
 
    def update(self):
        self.y -= self.speed
        if self.y < 0:
            self.isRunning = False
 
player = Player("kero")
dx, dy = 0, 0
 
def player_shot():
    shot = Shot("shot", player.x, player.y-16)
    shots.append(shot)
    print("shots: {}".format(len(shots)))
    clock.schedule_unique(player_shot, 0.5)
 
player_shot()
 
def on_mouse_down(pos):
    global dx, dy
    player.isMoving = True
    x, y = pos
    dx = player.x - x
    dy = player.y - y
 
def on_mouse_up():
    player.isMoving = False
 
def on_mouse_move(pos):
    if player.isMoving:
        x, y = pos
        player.x = dx + x
        player.y = dy + y
 
def draw():
    screen.fill(BACKGROUND)
    player.draw()
    for shot in shots:
        shot.draw()
 
def update():
    for shot in shots:
        if shot.isRunning == False:
            shots.remove(shot)
        else:
            shot.update()
 
pgzrun.go()

game_shooting3.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pgzrun
import random
 
WIDTH = 320
HEIGHT = 480
TITLE = "PyGame Zeroでクラスを使う3"
 
BACKGROUND = 50, 165, 165
shots = []
enemys = []
 
# プレイヤークラス(Actorクラスを継承)
class Player(Actor):
    def __init__(self, name):
        super().__init__(name)
        self.pos = 140, 300
        self.isMoving = False
 
# プレイヤーの弾クラス
class Shot(Actor):
    def __init__(self, name, x, y):
        super().__init__(name)
        self.pos = x, y
        self.speed = 5
        self.isRunning = True
 
    def update(self):
        self.y -= self.speed
        if self.y < 0:
            self.isRunning = False
 
# 敵クラス
class Enemy(Actor):
    def __init__(self, name, x, y):
        super().__init__(name)
        self.pos = x, y
        self.speed = 2
        self.isRunning = True
     
    def update(self):
        self.y += self.speed
        if self.y > HEIGHT:
            self.isRunning = False
 
player = Player("kero")
dx, dy = 0, 0
 
def player_shot():
    shot = Shot("shot", player.x, player.y-16)
    shots.append(shot)
    print("shots: {}発".format(len(shots)))
    clock.schedule_unique(player_shot, 0.5)
 
player_shot()
 
def on_mouse_down(pos):
    global dx, dy
    player.isMoving = True
    x, y = pos
    dx = player.x - x
    dy = player.y - y
 
def on_mouse_up():
    player.isMoving = False
 
def on_mouse_move(pos):
    if player.isMoving:
        x, y = pos
        player.x = dx + x
        player.y = dy + y
 
def mydraw():
    screen.fill(BACKGROUND)
    player.draw()
    for shot in shots:
        shot.draw()
    for enemy in enemys:
        enemy.draw()
 
def update():
    # 敵の発生
    if random.randint(1, 100) == 1:
        x = random.randint(0, WIDTH)
        y = 0
        enemy = Enemy("teki", x, y)
        enemys.append(enemy)
        print("enemys: {}体".format(len(enemys)))
     
    # プレイヤーのショット処理
    for shot in shots:
        if shot.isRunning == False:
            shots.remove(shot)
        else:
            shot.update()
     
    # 敵の処理
    for enemy in enemys:
        if enemy.isRunning == False:
            enemys.remove(enemy)
        else:
            enemy.update()
     
    # 画面描画
    mydraw()
 
pgzrun.go()

game_shooting4.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pgzrun
import random
 
WIDTH = 320
HEIGHT = 480
TITLE = "PyGame Zeroでクラスを使う3"
 
BACKGROUND = 50, 165, 165
shots = []
enemys = []
 
# プレイヤークラス(Actorクラスを継承)
class Player(Actor):
    def __init__(self, name):
        super().__init__(name)
        self.pos = 140, 300
        self.isMoving = False
        self.isRunning = True
     
    def update(self):
        if self.isRunning == False:
            player.pos = -1000, -1000
 
# プレイヤーの弾クラス
class Shot(Actor):
    def __init__(self, name, x, y):
        super().__init__(name)
        self.pos = x, y
        self.speed = 5
        self.isRunning = True
 
    def update(self):
        self.y -= self.speed
        if self.y < 0:
            self.isRunning = False
 
# 敵クラス
class Enemy(Actor):
    def __init__(self, name, x, y):
        super().__init__(name)
        self.pos = x, y
        self.speed = 2
        self.isRunning = True
     
    def update(self):
        # プレイヤー弾との当たり判定
        for shot in shots:
            if shot.colliderect(self):
                self.isRunning = False
                 
        self.y += self.speed
        if self.y > HEIGHT:
            self.isRunning = False
 
player = Player("kero")
dx, dy = 0, 0
 
def player_shot():
    shot = Shot("shot", player.x, player.y-16)
    shots.append(shot)
    print("shots: {}発".format(len(shots)))
    clock.schedule_unique(player_shot, 0.5)
 
player_shot()
 
def on_mouse_down(pos):
    global dx, dy
    player.isMoving = True
    x, y = pos
    dx = player.x - x
    dy = player.y - y
 
def on_mouse_up():
    player.isMoving = False
 
def on_mouse_move(pos):
    if player.isMoving:
        x, y = pos
        player.x = dx + x
        player.y = dy + y
 
def mydraw():
    screen.fill(BACKGROUND)
    player.draw()
    for shot in shots:
        shot.draw()
    for enemy in enemys:
        enemy.draw()
 
def update():
    # 敵の発生
    if random.randint(1, 100) == 1:
        x = random.randint(0, WIDTH)
        y = 0
        enemy = Enemy("teki", x, y)
        enemys.append(enemy)
        print("enemys: {}体".format(len(enemys)))
     
    # プレイヤーのショット処理
    for shot in shots:
        if shot.isRunning == False:
            shots.remove(shot)
        else:
            shot.update()
     
    # 敵の処理
    for enemy in enemys:
        # プレイヤーとの当たり判定
        if player.colliderect(enemy):
            player.isRunning = False
 
        if enemy.isRunning == False:
            enemys.remove(enemy)
        else:
            enemy.update()
     
    # プレイヤーの処理
    player.update()
 
    # 画面描画
    mydraw()
 
pgzrun.go()

コメント