Python: Sample Space Invaders

Return to Python resources

import pygame, sys, random
from pygame.locals import *

pygame.init()
fps = pygame.time.Clock()

# Create window
window = pygame.display.set_mode((700,700))
pygame.display.set_caption("Space invaders!")

# Create colours
colorRed = (255,0,0)
colorGreen = (0,255,0)
colorBlue = (0,0,255)
colorYellow = (255,255,0)
colorBlack = (0,0,0)
colorWhite = (255,255,255)

aliens = [
    [50, 50], [150, 50], [250, 50], [350, 50], [450, 50], [550, 50], [650, 50],
    [50, 150], [150, 150], [250, 150], [350, 150], [450, 150], [550, 150], [650, 150],
    [50, 250], [150, 250], [250, 250], [350, 250], [450, 250], [550, 250], [650, 250],
    [50, 350], [150, 350], [250, 350], [350, 350], [450, 350], [550, 350], [650, 350],
    [50, 450], [150, 450], [250, 450], [350, 450], [450, 450], [550, 450], [650, 450],
]
bullets = []
player = [50, 650]
alien_direction = 1
player_direction = 0
bullet_speed = -20
gameRunning = True

while gameRunning:
    # Events
    for event in pygame.event.get():
        if event.type == QUIT:
            gameRunning = False
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                player_direction = -10
            elif event.key == K_RIGHT:
                player_direction = 10
            elif event.key == K_SPACE:
                bulletFrom = player[0]+25
                bullets.append([bulletFrom, 650])

    # Clear window
    window.fill(colorBlack)

    ### MANAGE ALIENS
    for alien in aliens:
        # If we hit the edge, we need to swap direction and move down some
        if alien[0]+alien_direction >= 650 or alien[0]+alien_direction < 0:
            alien_direction = -1.1*alien_direction
            for a in aliens:
                a[1] = a[1] + 20

        # move this alien left/right
        alien[0] = alien[0] + alien_direction

        # Check for collisions between bullets and aliens
        alienX = alien[0]
        alienY = alien[1]
        for bullet in bullets:
            if bullet[0] > alienX and bullet[0] < (alienX+50) and bullet[1] > alienY and bullet[1] < (alienY+50):
                # repopulate the aliens list with all aliens except this one
                aliens = [x for x in aliens if not(x[0] == alienX and x[1] == alienY)]
                # repopulate the bullets list with all bullets except this one
                bullets = [x for x in bullets if not(x[0] == bullet[0] and x[1] == bullet[1])]

        # Draw the alien sprite
        pygame.draw.rect(window, colorWhite, (alien[0], alien[1], 50, 50))

    ### MANAGE PLAYER
    # Adjsut player locaiton
    player[0] = player[0] + player_direction
    # Make sure we don't move off edge of screen
    if player[0] < 0:
        player[0] = 0
    if player[0] > 650:
        player[0] = 650
    # Draw player
    pygame.draw.rect(window, colorBlue, (player[0], player[1], 50, 50))

    ### MANAGE BULLETS
    for bullet in bullets:
        # Adjust bullet location
        bullet[1] = bullet[1] + bullet_speed
        # repopulate the bullets list with all bullets that have property 1 greater than 0
        bullets = [x for x in bullets if x[1]>=0]
        # Draw bullet
        pygame.draw.rect(window, colorRed, (bullet[0]-1, bullet[1], 2, 20))

    # Update screen and wait for clock tick
    pygame.display.update()
    fps.tick(20)

    # End of loop

pygame.quit()
print("Good bye!")