|
| 1 | +#Credit the Invent With Python book (http://inventwithpython.com) |
| 2 | +#for doRectsOverlap and isPointInsideRect functions |
| 3 | + |
| 4 | +#used to detect collisions in our game |
| 5 | +def doRectsOverlap(rect1, rect2): |
| 6 | + for a, b in [(rect1, rect2), (rect2, rect1)]: |
| 7 | + # Check if a's corners are inside b |
| 8 | + if ((isPointInsideRect(a.left, a.top, b)) or |
| 9 | + (isPointInsideRect(a.left, a.bottom, b)) or |
| 10 | + (isPointInsideRect(a.right, a.top, b)) or |
| 11 | + (isPointInsideRect(a.right, a.bottom, b))): |
| 12 | + return True |
| 13 | + |
| 14 | + return False |
| 15 | + |
| 16 | +#used the by the doRectsOverlap function (won't be called directly from game code) |
| 17 | +def isPointInsideRect(x, y, rect): |
| 18 | + if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom): |
| 19 | + return True |
| 20 | + else: |
| 21 | + return False |
| 22 | + |
| 23 | +import pygame, sys, random |
| 24 | +pygame.init() |
| 25 | + |
| 26 | +SCREEN_WIDTH = 640 |
| 27 | +SCREEN_HEIGHT = 480 |
| 28 | + |
| 29 | +screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT]) |
| 30 | + |
| 31 | +# List of missiles flying across the screen |
| 32 | +player_missiles = [] |
| 33 | +alien_missiles = [] |
| 34 | +black = [0, 0, 0] |
| 35 | +white = [255, 255, 255] |
| 36 | + |
| 37 | +class Spaceship: |
| 38 | + xpos = 0 |
| 39 | + ypos = 0 |
| 40 | + picture = pygame.image.load('spaceship.png') |
| 41 | + |
| 42 | + def __init__(self, x, y): |
| 43 | + self.xpos = x |
| 44 | + self.ypos = y |
| 45 | + |
| 46 | + def moveRight(self): |
| 47 | + self.xpos += 5 |
| 48 | + |
| 49 | + def moveLeft(self): |
| 50 | + self.xpos -= 5 |
| 51 | + |
| 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) |
| 58 | + |
| 59 | + def draw(self): |
| 60 | + screen.blit(self.picture, (self.xpos, self.ypos)) |
| 61 | + |
| 62 | +class Alien: |
| 63 | + xpos = 0 |
| 64 | + ypos = 0 |
| 65 | + xspeed = 2 |
| 66 | + picture1 = pygame.image.load('alien1.png') |
| 67 | + picture2 = pygame.image.load('alien2.png') |
| 68 | + |
| 69 | + def __init__(self, x, y): |
| 70 | + self.xpos = x |
| 71 | + self.ypos = y |
| 72 | + |
| 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 | + |
| 90 | + |
| 91 | +class Missile: |
| 92 | + xpos = 0 |
| 93 | + ypos = 0 |
| 94 | + speed = 0 |
| 95 | + spent = False |
| 96 | + |
| 97 | + def __init__(self, x, y, speed): |
| 98 | + self.xpos = x |
| 99 | + self.ypos = y |
| 100 | + self.speed = speed |
| 101 | + |
| 102 | + def draw(self): |
| 103 | + pygame.draw.rect(screen, white, [self.xpos, self.ypos, 2, 10], 2) |
| 104 | + |
| 105 | + def move(self): |
| 106 | + self.ypos += self.speed |
| 107 | + if self.ypos < 0 or self.ypos > SCREEN_HEIGHT: |
| 108 | + self.spent = True |
| 109 | + |
| 110 | + def get_rect(self): |
| 111 | + return pygame.Rect(self.xpos, self.ypos, 2, 10) |
| 112 | + |
| 113 | + def isSpent(self): |
| 114 | + return self.spent |
| 115 | + |
| 116 | +# Create all the objects and variables! |
| 117 | +spaceship = Spaceship(200, 440) |
| 118 | +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 | + |
| 123 | +running = True |
| 124 | +ticks = 0 |
| 125 | +alien_formation_x = 0 |
| 126 | +alien_formation_x_speed = 10 |
| 127 | + |
| 128 | +# Main event loop. Here we: |
| 129 | +# 1. Handle keyboard input for the player and move the player around |
| 130 | +# 2. Move the missiles around |
| 131 | +# 3. Move the aliens around |
| 132 | +# 4. Detect collisions and handle them |
| 133 | +while running: |
| 134 | + for event in pygame.event.get(): |
| 135 | + if event.type == pygame.QUIT: |
| 136 | + running = False |
| 137 | + |
| 138 | + keys = pygame.key.get_pressed() |
| 139 | + if keys[pygame.K_RIGHT]: |
| 140 | + spaceship.moveRight() |
| 141 | + if keys[pygame.K_LEFT]: |
| 142 | + spaceship.moveLeft() |
| 143 | + if keys[pygame.K_SPACE]: |
| 144 | + spaceship.shoot() |
| 145 | + |
| 146 | + screen.fill(black) |
| 147 | + |
| 148 | + # Move, detect collisions, and draw the player's missiles |
| 149 | + for missile in player_missiles: |
| 150 | + 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 | + |
| 156 | + if missile.isSpent(): |
| 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) |
| 171 | + missile.draw() |
| 172 | + |
| 173 | + # 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 |
| 205 | + |
| 206 | + # Draw the spaceship |
| 207 | + spaceship.draw() |
| 208 | + pygame.display.update() |
| 209 | + ticks = ticks + 1 |
| 210 | + |
| 211 | +pygame.quit() |
0 commit comments