Here's a simple implementation of a Light Cycle game from Tron using Python and Pygame:
python
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up the game window
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tron Light Cycle Game")
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Define game objects
player1 = pygame.Rect(100, height // 2, 10, 10)
player2 = pygame.Rect(width - 110, height // 2, 10, 10)
player1_direction = (1, 0)
player2_direction = (-1, 0)
player1_trail = []
player2_trail = []
# Game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and player1_direction != (0, 1):
player1_direction = (0, -1)
elif event.key == pygame.K_DOWN and player1_direction != (0, -1):
player1_direction = (0, 1)
elif event.key == pygame.K_LEFT and player1_direction != (1, 0):
player1_direction = (-1, 0)
elif event.key == pygame.K_RIGHT and player1_direction != (-1, 0):
player1_direction = (1, 0)
elif event.key == pygame.K_w and player2_direction != (0, 1):
player2_direction = (0, -1)
elif event.key == pygame.K_s and player2_direction != (0, -1):
player2_direction = (0, 1)
elif event.key == pygame.K_a and player2_direction != (1, 0):
player2_direction = (-1, 0)
elif event.key == pygame.K_d and player2_direction != (-1, 0):
player2_direction = (1, 0)
# Move players
player1.move_ip(player1_direction)
player2.move_ip(player2_direction)
# Check for collisions
if player1.colliderect(player2) or player1.collidelist(player2_trail) != -1:
print("Player 2 wins!")
break
if player2.colliderect(player1) or player2.collidelist(player1_trail) != -1:
print("Player 1 wins!")
break
# Update trails
player1_trail.append(player1.copy())
player2_trail.append(player2.copy())
# Draw the game objects
screen.fill(BLACK)
pygame.draw.rect(screen, BLUE, player1)
pygame.draw.rect(screen, RED, player2)
for trail in player1_trail:
pygame.draw.rect(screen, BLUE, trail)
for trail in player2_trail:
pygame.draw.rect(screen, RED, trail)
# Update the display
pygame.display.flip()
pygame.time.delay(50)
This code sets up a basic Light Cycle game from Tron using Pygame. Here's how it works:
The game window is created with a specified width and height.
The game objects (players and their trails) are defined using Pygame's Rect objects.
The game loop runs continuously until the game is quit or a collision occurs.
Player movement is controlled using the arrow keys for Player 1 and WASD keys for Player 2.
The players' positions are updated based on their current direction.
Collisions are checked between players and their trails. If a collision occurs, the game ends and the winner is declared.
The players' trails are updated by appending their current positions to the respective trail lists.
The game objects are drawn on the screen using Pygame's drawing functions.
The display is updated, and a small delay is added to control the game speed.
Note that this is a basic implementation and can be further enhanced with additional features such as menus, sound effects, and more advanced graphics.