@@ -28,14 +28,33 @@ def isPointInsideRect(x, y, rect):
28
28
29
29
screen = pygame .display .set_mode ([SCREEN_WIDTH ,SCREEN_HEIGHT ])
30
30
31
+ # List of missiles flying across the screen
32
+ missiles = []
33
+ black = [0 , 0 , 0 ]
34
+ white = [255 , 255 , 255 ]
35
+
31
36
class Spaceship :
32
37
xpos = 0
33
38
ypos = 0
39
+ picture = pygame .image .load ('spaceship.png' )
34
40
35
41
def __init__ (self , x , y ):
36
42
self .xpos = x
37
43
self .ypos = y
38
44
45
+ def moveRight (self ):
46
+ self .xpos += 5
47
+
48
+ def moveLeft (self ):
49
+ self .xpos -= 5
50
+
51
+ def fire (self ):
52
+ missile = Missile (self .xpos , self .ypos - 32 , - 10 )
53
+ missiles .append (missile )
54
+
55
+ def draw (self ):
56
+ screen .blit (self .picture , (self .xpos , self .ypos ))
57
+
39
58
class Alien :
40
59
xpos = 0
41
60
ypos = 0
@@ -50,16 +69,61 @@ def __init__(self, x, y):
50
69
class Missile :
51
70
xpos = 0
52
71
ypos = 0
53
- xspeed = 2
54
- yspeed = 2
72
+ speed = 0
73
+ spent = False
55
74
56
- def __init__ (self , x , y ):
75
+ def __init__ (self , x , y , speed ):
57
76
self .xpos = x
58
77
self .ypos = y
78
+ self .speed = speed
79
+
80
+ def draw (self ):
81
+ pygame .draw .rect (screen , white , [self .xpos , self .ypos , 2 , 10 ], 2 )
59
82
83
+ def move (self ):
84
+ self .ypos += self .speed
85
+ if self .ypos < 0 or self .ypos > SCREEN_HEIGHT :
86
+ self .spent = True
87
+
88
+ def isSpent (self ):
89
+ return self .spent
90
+
91
+ # Create all the objects and variables!
92
+ spaceship = Spaceship (200 , 440 )
93
+ aliens = []
94
+ running = True
60
95
61
96
# Main event loop. Here we:
62
97
# 1. Handle keyboard input for the player and move the player around
63
98
# 2. Move the missiles around
64
99
# 3. Move the aliens around
65
100
# 4. Detect collisions and handle them
101
+ while running :
102
+ for event in pygame .event .get ():
103
+ if event .type == pygame .QUIT :
104
+ running = False
105
+
106
+ keys = pygame .key .get_pressed ()
107
+ if keys [pygame .K_RIGHT ]:
108
+ spaceship .moveRight ()
109
+ if keys [pygame .K_LEFT ]:
110
+ spaceship .moveLeft ()
111
+ if keys [pygame .K_SPACE ]:
112
+ spaceship .fire ()
113
+
114
+ screen .fill (black )
115
+
116
+ # Move and draw the missiles
117
+ for missile in missiles :
118
+ missile .move ()
119
+ if missile .isSpent ():
120
+ missiles .remove (missile )
121
+ missile .draw ()
122
+
123
+ # Move all the aliens
124
+
125
+ # Draw the spaceship
126
+ spaceship .draw ()
127
+ pygame .display .update ()
128
+
129
+ pygame .quit ()
0 commit comments