import pygame import sys # Initialize Pygame pygame.init() # Set up the screen WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("1v1 Shooter") # Define colors WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) # Player properties PLAYER_WIDTH, PLAYER_HEIGHT = 50, 50 PLAYER_SPEED = 5 PLAYER_HEALTH = 100 # Create players player1 = pygame.Rect(50, HEIGHT // 2 - PLAYER_HEIGHT // 2, PLAYER_WIDTH, PLAYER_HEIGHT) player2 = pygame.Rect(WIDTH - 50 - PLAYER_WIDTH, HEIGHT // 2 - PLAYER_HEIGHT // 2, PLAYER_WIDTH, PLAYER_HEIGHT) # Player colors player1_color = RED player2_color = BLUE # Define bullet properties BULLET_WIDTH, BULLET_HEIGHT = 10, 5 BULLET_SPEED = 7 # Create bullets player1_bullet = None player2_bullet = None # Game loop while True: screen.fill(WHITE) # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Player 1 controls if event.type == pygame.KEYDOWN: if event.key == pygame.K_w: player1.y -= PLAYER_SPEED elif event.key == pygame.K_s: player1.y += PLAYER_SPEED elif event.key == pygame.K_a: player1.x -= PLAYER_SPEED elif event.key == pygame.K_d: player1.x += PLAYER_SPEED elif event.key == pygame.K_SPACE and player1_bullet is None: player1_bullet = pygame.Rect(player1.x + PLAYER_WIDTH, player1.y + PLAYER_HEIGHT // 2 - BULLET_HEIGHT // 2, BULLET_WIDTH, BULLET_HEIGHT) # Player 2 controls if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player2.y -= PLAYER_SPEED elif event.key == pygame.K_DOWN: player2.y += PLAYER_SPEED elif event.key == pygame.K_LEFT: player2.x -= PLAYER_SPEED elif event.key == pygame.K_RIGHT: player2.x += PLAYER_SPEED elif event.key == pygame.K_RETURN and player2_bullet is None: player2_bullet = pygame.Rect(player2.x - BULLET_WIDTH, player2.y + PLAYER_HEIGHT // 2 - BULLET_HEIGHT // 2, BULLET_WIDTH, BULLET_HEIGHT) # Move bullets if player1_bullet: player1_bullet.x += BULLET_SPEED if player1_bullet.x > WIDTH: player1_bullet = None if player2_bullet: player2_bullet.x -= BULLET_SPEED if player2_bullet.x < 0: player2_bullet = None # Draw players pygame.draw.rect(screen, player1_color, player1) pygame.draw.rect(screen, player2_color, player2) # Draw bullets if player1_bullet: pygame.draw.rect(screen, RED, player1_bullet) if player2_bullet: pygame.draw.rect(screen, BLUE, player2_bullet) # Update display pygame.display.flip()