python invaders style concept game example
# invaders style concept using pygame and colored squares # enemy ship goes back and forth across the screen starting from top and working down as bullets hit them # player ship moves left and right and shoots bullets # player ship is destroyed when hit by enemy ship # player score is number of enemy ships destroyed # player score is number of shots taken # player score is time taken to destroy all enemy ships # sometimes my constants have a tendency to become variable. sorry. # https://pythonprogrammingsnippets.tumblr.com import pygame import random # Set up the game window WINDOW_WIDTH = 600 WINDOW_HEIGHT = 800 WINDOW_TITLE = "Invaders Style Clone" pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption(WINDOW_TITLE) # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # Define the player's spaceship PLAYER_WIDTH = 50 PLAYER_HEIGHT = 50 PLAYER_X = (WINDOW_WIDTH - PLAYER_WIDTH) / 2 PLAYER_Y = WINDOW_HEIGHT - PLAYER_HEIGHT - 50 player = pygame.Rect(PLAYER_X, PLAYER_Y, PLAYER_WIDTH, PLAYER_HEIGHT) # Define the player's movement PLAYER_MOVE_SPEED = 1 # Define the enemy's spaceship ENEMY_WIDTH = 50 ENEMY_HEIGHT = 50 ENEMY_X = 0 ENEMY_Y = 0 enemy = pygame.Rect(ENEMY_X, ENEMY_Y, ENEMY_WIDTH, ENEMY_HEIGHT) # Define the enemy's grid ENEMY_GRID_WIDTH = 10 ENEMY_GRID_HEIGHT = 5 ENEMY_GRID_X = 0 ENEMY_GRID_Y = 0 enemy_grid = pygame.Rect(ENEMY_GRID_X, ENEMY_GRID_Y, ENEMY_GRID_WIDTH, ENEMY_GRID_HEIGHT) # Define the enemy's movement ENEMY_MOVE_SPEED = 1 ENEMY_MOVE_DIRECTION = 1 # start enemy at top and work down as bullets hit them ENEMY_START_Y = 0 # Define the player's bullets BULLET_WIDTH = 10 BULLET_HEIGHT = 20 BULLET_X = 0 BULLET_Y = 0 bullet = pygame.Rect(BULLET_X, BULLET_Y, BULLET_WIDTH, BULLET_HEIGHT) # Define the player's bullets movement BULLET_MOVE_SPEED = 2 # Define the player's score shots_taken = 0 score = 0 # Define the game loop running = True while running: time_started = pygame.time.get_ticks() # Check for events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Check for key presses # if player presses left and player is already going left, then stop movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player.x > 0: player.x -= PLAYER_MOVE_SPEED if keys[pygame.K_RIGHT] and player.x + player.width < WINDOW_WIDTH: player.x += PLAYER_MOVE_SPEED if keys[pygame.K_SPACE]: bullet.x = player.x + player.width / 2 bullet.y = player.y shots_taken += 1 # Move the bullet bullet.y -= BULLET_MOVE_SPEED # Move the enemy enemy.x += ENEMY_MOVE_SPEED * ENEMY_MOVE_DIRECTION # Check if the enemy has hit the edge of the screen if enemy.x <= 0 or enemy.x + enemy.width >= WINDOW_WIDTH: ENEMY_MOVE_DIRECTION *= -1 # Check if the bullet has hit the enemy if bullet.colliderect(enemy): score += 1 # now start enemy at top and work down as bullets hit them and spawn new enemy # move down one tile size ENEMY_START_Y += ENEMY_HEIGHT enemy.x = ENEMY_START_Y enemy.y = ENEMY_START_Y bullet.x = 0 bullet.y = 0 # increase bullet speed BULLET_MOVE_SPEED += 1 # increase enemy speed ENEMY_MOVE_SPEED += 1 # Check if the enemy has hit the player if enemy.colliderect(player): running = False # Draw the game window.fill(BLACK) pygame.draw.rect(window, WHITE, player) pygame.draw.rect(window, RED, enemy) pygame.draw.rect(window, GREEN, bullet) # Update the game pygame.display.update() # limit the game to 60 frames per second clock = pygame.time.Clock() clock.tick(60) # output time and score print("Time: " + str(pygame.time.get_ticks() - time_started)) print("Score: " + str(score)) print("Shots Taken: " + str(shots_taken)) pygame.quit()













