@@ -28,14 +28,33 @@ def isPointInsideRect(x, y, rect):
2828
2929screen = pygame .display .set_mode ([SCREEN_WIDTH ,SCREEN_HEIGHT ])
3030
31+ # List of missiles flying across the screen
32+ missiles = []
33+ black = [0 , 0 , 0 ]
34+ white = [255 , 255 , 255 ]
35+
3136class Spaceship :
3237 xpos = 0
3338 ypos = 0
39+ picture = pygame .image .load ('spaceship.png' )
3440
3541 def __init__ (self , x , y ):
3642 self .xpos = x
3743 self .ypos = y
3844
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+
3958class Alien :
4059 xpos = 0
4160 ypos = 0
@@ -50,16 +69,61 @@ def __init__(self, x, y):
5069class Missile :
5170 xpos = 0
5271 ypos = 0
53- xspeed = 2
54- yspeed = 2
72+ speed = 0
73+ spent = False
5574
56- def __init__ (self , x , y ):
75+ def __init__ (self , x , y , speed ):
5776 self .xpos = x
5877 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 )
5982
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
6095
6196# Main event loop. Here we:
6297# 1. Handle keyboard input for the player and move the player around
6398# 2. Move the missiles around
6499# 3. Move the aliens around
65100# 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