1
1
#!/usr/bin/env python3
2
2
import pyxel
3
+ from copy import copy
3
4
4
5
5
6
class Bullet :
@@ -10,11 +11,7 @@ def __init__(self, cords, rotate):
10
11
11
12
@classmethod
12
13
def from_player (cls , player ):
13
- x , y = player .cords
14
- w = player .WIDTH_SPRITE
15
- h = player .HEIGHT_SPRITE
16
- cords = [x + w / 2 , y + h / 2 ]
17
- return cls (cords , player .rotate )
14
+ return cls (copy (player .cords ), player .rotate )
18
15
19
16
def update (self ):
20
17
if self .rotate == 'u' :
@@ -38,28 +35,29 @@ def draw(self):
38
35
pyxel .circ (x , y , 2 , 10 )
39
36
40
37
41
- class Player :
38
+ class Ship :
42
39
WIDTH_SPRITE = 32
43
40
HEIGHT_SPRITE = 32
44
-
45
41
ROTATE_INDEXES = {
46
42
'u' : 0 ,
47
43
'l' : 1 ,
48
44
'd' : 2 ,
49
45
'r' : 3 ,
50
46
}
51
47
52
- def __init__ (self , cords ):
53
- self .score = 0
48
+ def __init__ (self , cords , rotate , img_index ):
54
49
self .cords = cords
55
- self .rotate = 'u'
50
+ self .rotate = rotate
51
+ self .img_index = img_index
56
52
57
53
def draw (self ):
58
- x , y = self .cords
59
54
w = self .WIDTH_SPRITE
60
55
h = self .HEIGHT_SPRITE
61
56
u = self .ROTATE_INDEXES [self .rotate ] * w
62
- pyxel .blt (x , y , 0 , u , 0 , w , h , 11 )
57
+ v = self .img_index * h
58
+ x = self .cords [0 ] - w / 2
59
+ y = self .cords [1 ] - h / 2
60
+ pyxel .blt (x , y , 0 , u , v , w , h , 11 )
63
61
64
62
def move_up (self ):
65
63
self .cords [1 ] -= 5
@@ -78,6 +76,28 @@ def move_right(self):
78
76
self .rotate = 'r'
79
77
80
78
79
+ class Player (Ship ):
80
+
81
+ def __init__ (self , cords ):
82
+ super ().__init__ (cords = cords , rotate = 'u' , img_index = 0 )
83
+ self .score = 0
84
+
85
+
86
+ class EnemyShip (Ship ):
87
+
88
+ def __init__ (self , cords ):
89
+ super ().__init__ (cords = cords , rotate = 'u' , img_index = 3 )
90
+
91
+ def update (self ):
92
+ self .move_up ()
93
+
94
+ def is_alive (self ):
95
+ x , y = self .cords
96
+ return (
97
+ x > 0 and x < 256
98
+ and y > 0 and y < 256
99
+ )
100
+
81
101
class App :
82
102
83
103
def run (self ):
@@ -89,10 +109,15 @@ def setup(self):
89
109
90
110
self .player = Player ([128 , 128 ])
91
111
self .bullets = []
112
+ self .enemies = [
113
+ EnemyShip ([20 + 50 * i , 250 ])
114
+ for i in range (4 )
115
+ ]
92
116
93
117
def update (self ):
94
118
self .keyboard ()
95
119
self .bullets = self .update_objs (self .bullets )
120
+ self .enemies = self .update_objs (self .enemies )
96
121
97
122
def update_objs (self , objs ):
98
123
survived_objs = []
@@ -125,5 +150,9 @@ def draw(self):
125
150
pyxel .cls (0 )
126
151
pyxel .text (0 , 0 , "Score: %d" % self .player .score , 7 )
127
152
self .player .draw ()
128
- for bullet in self .bullets :
129
- bullet .draw ()
153
+ self .draw_objs (self .bullets )
154
+ self .draw_objs (self .enemies )
155
+
156
+ def draw_objs (self , objs ):
157
+ for obj in objs :
158
+ obj .draw ()
0 commit comments