Skip to content

Commit 4f12821

Browse files
committed
Complete game
1 parent fa3f470 commit 4f12821

File tree

2 files changed

+92
-10
lines changed

2 files changed

+92
-10
lines changed

Day2/alien2.png

4.86 KB
Loading

Day2/spaceinvaders.py

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def isPointInsideRect(x, y, rect):
2020
else:
2121
return False
2222

23-
import pygame, sys
23+
import pygame, sys, random
2424
pygame.init()
2525

2626
SCREEN_WIDTH = 640
@@ -29,7 +29,8 @@ def isPointInsideRect(x, y, rect):
2929
screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
3030

3131
# List of missiles flying across the screen
32-
missiles = []
32+
player_missiles = []
33+
alien_missiles = []
3334
black = [0, 0, 0]
3435
white = [255, 255, 255]
3536

@@ -48,9 +49,12 @@ def moveRight(self):
4849
def moveLeft(self):
4950
self.xpos -= 5
5051

51-
def fire(self):
52-
missile = Missile(self.xpos, self.ypos - 32, -10)
53-
missiles.append(missile)
52+
def shoot(self):
53+
missile = Missile(self.xpos + 16, self.ypos - 1, -10)
54+
player_missiles.append(missile)
55+
56+
def get_rect(self):
57+
return pygame.Rect(self.xpos, self.ypos, 32, 32)
5458

5559
def draw(self):
5660
screen.blit(self.picture, (self.xpos, self.ypos))
@@ -59,12 +63,30 @@ class Alien:
5963
xpos = 0
6064
ypos = 0
6165
xspeed = 2
62-
picture = pygame.image.load('alien1.png')
66+
picture1 = pygame.image.load('alien1.png')
67+
picture2 = pygame.image.load('alien2.png')
6368

6469
def __init__(self, x, y):
6570
self.xpos = x
6671
self.ypos = y
6772

73+
def move(self, x, y):
74+
self.xpos += x
75+
self.ypos += y
76+
self.picture3 = self.picture1
77+
self.picture1 = self.picture2
78+
self.picture2 = self.picture3
79+
80+
def shoot(self):
81+
missile = Missile(self.xpos + 16, self.ypos + 32, 10)
82+
alien_missiles.append(missile)
83+
84+
def get_rect(self):
85+
return pygame.Rect(self.xpos, self.ypos, 32, 32)
86+
87+
def draw(self):
88+
screen.blit(self.picture1, (self.xpos, self.ypos))
89+
6890

6991
class Missile:
7092
xpos = 0
@@ -85,13 +107,23 @@ def move(self):
85107
if self.ypos < 0 or self.ypos > SCREEN_HEIGHT:
86108
self.spent = True
87109

110+
def get_rect(self):
111+
return pygame.Rect(self.xpos, self.ypos, 2, 10)
112+
88113
def isSpent(self):
89114
return self.spent
90115

91116
# Create all the objects and variables!
92117
spaceship = Spaceship(200, 440)
93118
aliens = []
119+
for x in range(1,8):
120+
for y in range(1,4):
121+
aliens.append(Alien(x * 50 + 10, y * 50 + 10))
122+
94123
running = True
124+
ticks = 0
125+
alien_formation_x = 0
126+
alien_formation_x_speed = 10
95127

96128
# Main event loop. Here we:
97129
# 1. Handle keyboard input for the player and move the player around
@@ -109,21 +141,71 @@ def isSpent(self):
109141
if keys[pygame.K_LEFT]:
110142
spaceship.moveLeft()
111143
if keys[pygame.K_SPACE]:
112-
spaceship.fire()
144+
spaceship.shoot()
113145

114146
screen.fill(black)
115147

116-
# Move and draw the missiles
117-
for missile in missiles:
148+
# Move, detect collisions, and draw the player's missiles
149+
for missile in player_missiles:
118150
missile.move()
151+
for alien in aliens:
152+
if doRectsOverlap(alien.get_rect(), missile.get_rect()):
153+
aliens.remove(alien)
154+
missile.spent = True
155+
119156
if missile.isSpent():
120-
missiles.remove(missile)
157+
player_missiles.remove(missile)
158+
missile.draw()
159+
160+
# Move, detect collisions, and draw the player's missiles
161+
for missile in alien_missiles:
162+
missile.move()
163+
164+
if doRectsOverlap(spaceship.get_rect(), missile.get_rect()):
165+
print "YOU LOSE"
166+
running = False
167+
missile.spent = True
168+
169+
if missile.isSpent():
170+
alien_missiles.remove(missile)
121171
missile.draw()
122172

123173
# Move all the aliens
174+
# The more aliens there are, the slower they move
175+
if ticks % (len(aliens) + 1) == 0:
176+
# The alien formation moves left and right
177+
alien_formation_x += alien_formation_x_speed
178+
for alien in aliens:
179+
alien.move(alien_formation_x_speed, 0)
180+
# Until the formation reaches a limit, after which it reverses
181+
# direction and moves down 5 pixels
182+
if alien_formation_x >= 200 or alien_formation_x <= 0:
183+
alien_formation_x_speed = -1 * alien_formation_x_speed
184+
for alien in aliens:
185+
alien.move(0, 5)
186+
187+
# Draw all the aliens and give them a chance to shoot!
188+
for alien in aliens:
189+
alien.draw()
190+
# Aliens shoot randomly
191+
# 0.05 percent change per frame that an alien shoots
192+
if (random.randint(1,1000) < 5):
193+
alien.shoot()
194+
195+
# If there are no more aliens, the player wins
196+
if len(aliens) == 0:
197+
print "YOU WIN!"
198+
running = False
199+
200+
# If any alien reaches the bottom of the screen, the player loses
201+
for alien in aliens:
202+
if alien.ypos > SCREEN_HEIGHT - 64:
203+
print "YOU LOSE"
204+
running = False
124205

125206
# Draw the spaceship
126207
spaceship.draw()
127208
pygame.display.update()
209+
ticks = ticks + 1
128210

129211
pygame.quit()

0 commit comments

Comments
 (0)