import pygame import random import time # https://pythonprogrammingsnippets.tumblr.com # Define some colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Set the width and height of the screen [width, height] SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # Set the title of the window pygame.display.set_caption("Raiden-like game") # Define the player's plane player_width = 50 player_height = 50 player_x = (SCREEN_WIDTH - player_width) / 2 player_y = SCREEN_HEIGHT - player_height player_speed = 5 # Define the bullet's size and speed bullet_width = 5 bullet_height = 10 bullet_speed = 10 # Define the enemy's size and speed enemy_width = 50 enemy_height = 50 enemy_speed = 2 # Create a list to hold the bullets bullet_list = [] # Create a list to hold the enemies enemy_list = [] # Create a clock to control the frame rate clock = pygame.time.Clock() # Function to create a new enemy at random intervals def create_enemy(): if random.randint(0, 100) < 2: enemy_x = random.randint(0, SCREEN_WIDTH - enemy_width) enemy_y = -enemy_height enemy_list.append([enemy_x, enemy_y]) # Function to move the player's plane def move_player(keys): global player_x, player_y if keys[pygame.K_a] and player_x > 0: player_x -= player_speed if keys[pygame.K_d] and player_x < SCREEN_WIDTH - player_width: player_x += player_speed if keys[pygame.K_w] and player_y > 0: player_y -= player_speed if keys[pygame.K_s] and player_y < SCREEN_HEIGHT - player_height: player_y += player_speed # Function to move the bullets def move_bullets(): for bullet in bullet_list: bullet[1] -= bullet_speed # Function to move the enemies def move_enemies(): for enemy in enemy_list: enemy[1] += enemy_speed # Function to draw the player's plane def draw_player(): pygame.draw.rect(screen, BLUE, [player_x, player_y, player_width, player_height]) # Function to draw the bullets def draw_bullets(): for bullet in bullet_list: pygame.draw.rect(screen, GREEN, [bullet[0], bullet[1], bullet_width, bullet_height]) # Function to draw the enemies def draw_enemies(): for enemy in enemy_list: pygame.draw.rect(screen, RED, [enemy[0], enemy[1], enemy_width, enemy_height]) # Function to check for collisions between the player and enemies or bullets and enemies def check_collisions(): global running for enemy in enemy_list: if enemy[1] + enemy_height >= player_y and enemy[0] + enemy_width >= player_x and enemy[0] <= player_x + player_width: running = False for bullet in bullet_list: if bullet[1] <= enemy[1] + enemy_height and bullet[0] + bullet_width >= enemy[0] and bullet[0] <= enemy[0] + enemy_width: enemy_list.remove(enemy) bullet_list.remove(bullet) # Function to remove bullets and enemies that have left the screen def remove_offscreen(): for bullet in bullet_list: if bullet[1] < 0: bullet_list.remove(bullet) for enemy in enemy_list: if enemy[1] > SCREEN_HEIGHT: enemy_list.remove(enemy) # Function to draw the score def draw_score(): pass # Function to draw the game over message def draw_game_over(): # switch background to red screen.fill(RED) # update canvas pygame.display.update() # draw game over message # wait a few seconds time.sleep(3) # quit pygame and program pygame.quit() quit() # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # -- main game loop -- running = True while not done: # --- Main event loop for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bullet_x = player_x + player_width / 2 - bullet_width / 2 bullet_y = player_y - bullet_height bullet_list.append([bullet_x, bullet_y]) # --- Game logic should go here create_enemy() keys = pygame.key.get_pressed() move_player(keys) move_bullets() move_enemies() check_collisions() remove_offscreen() # --- Drawing code should go here # First, clear the screen to white. Don't put other drawing commands # above this, or they will be erased with this command. screen.fill(WHITE) # Draw the player's plane draw_player() # Draw the bullets draw_bullets() # Draw the enemies draw_enemies() # Draw the score draw_score() # Check if the game is over if not running: draw_game_over() # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(60) # Close the window and quit. pygame.quit()