Create a python script that blends 2 arcade games in one script fully functionall with…
- User
Create a python script that blends 2 arcade games in one script fully functionall with all the things a game should have
Assistant
Of course! This is a fantastic and creative request. I'll create a Python script that blends two iconic arcade games: Snake and Pong.
The result is "SNAKE-PONG FUSION".
Game Concept
- Player 1 (Left Side): Controls a Pong paddle with
WandSkeys. Their goal is to keep the "ball" in play and prevent it from going off their side of the screen. - Player 2 (Right Side): Controls a Snake with the Arrow Keys. Their goal is to "eat" the ball to score points and grow longer.
- The "Ball": Acts as both the Pong ball and the food for the snake. It bounces off walls and the paddle.
- Scoring & Winning/Losing:
- The Snake player scores a point by eating the ball.
- The Pong player scores a point if the Snake crashes into itself or the top/bottom/right walls.
- The Snake player gets a massive 5-point bonus if the Pong player misses the ball.
- The game ends when any of these "lose" conditions are met, and the player with the higher score wins the round.
Features Included
- Start Menu: A clean title screen with instructions.
- Two-Player Gameplay: Asymmetric gameplay where each player has a different role.
- Live Scoring: Scores are updated and displayed in real-time.
- Sound Effects: Simple, generated sounds for key events (hits, scores, game over) without needing external files.
- Game Over Screen: Clearly states who won and gives the option to play again.
- Increasing Difficulty: The ball's speed increases slightly each time it's hit, making the game more challenging over time.
- Fully Self-Contained: The script uses
pygamebut requires no external asset files (like images or sound files).
Prerequisites
You need to have the
pygamelibrary installed. If you don't have it, open your terminal or command prompt and run:pip install pygame
The Python Script:
snake_pong_fusion.pyCopy and paste the entire code block below into a single Python file named
snake_pong_fusion.pyand run it.import pygame import sys import random import math # --- Game Constants --- SCREEN_WIDTH = 1000 SCREEN_HEIGHT = 700 FPS = 60 # --- Colors --- BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (100, 100, 255) YELLOW = (255, 255, 0) # --- Game Object Settings --- PADDLE_WIDTH = 15 PADDLE_HEIGHT = 120 PADDLE_SPEED = 8 SNAKE_SEGMENT_SIZE = 20 SNAKE_SPEED_INITIAL = 5 # How many frames before the snake moves BALL_SIZE = 20 BALL_SPEED_INITIAL_X = 5 BALL_SPEED_INITIAL_Y = 5 class SoundManager: """A simple class to generate and play sounds without external files.""" def __init__(self): pygame.mixer.pre_init(44100, -16, 2, 512) pygame.mixer.init() self.sounds = { 'hit': self._create_sound(440, 0.1), 'score_snake': self._create_sound(880, 0.2), 'score_pong': self._create_sound(220, 0.3), 'game_over': self._create_sound(110, 0.8) } def _create_sound(self, frequency, duration_s): sample_rate = pygame.mixer.get_init()[0] num_samples = int(sample_rate * duration_s) bits = pygame.mixer.get_init()[1] num_channels = pygame.mixer.get_init()[2] max_amplitude = 2 ** (abs(bits) - 1) - 1 wave = [int(max_amplitude * math.sin(2 * math.pi * frequency * i / sample_rate)) for i in range(num_samples)] # Create a stereo sound by duplicating the mono wave if num_channels == 2: stereo_wave = [] for sample in wave: stereo_wave.extend([sample, sample]) # Left and right channels wave = stereo_wave sound_buffer = bytearray(wave) return pygame.mixer.Sound(buffer=sound_buffer) def play(self, sound_name): if sound_name in self.sounds: self.sounds[sound_name].play() class Game: """The main class that encapsulates the entire game.""" def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Snake-Pong Fusion") self.clock = pygame.time.Clock() self.font_large = pygame.font.Font(None, 74) self.font_small = pygame.font.Font(None, 36) self.sounds = SoundManager() self.game_state = "START" self.winner_text = "" def reset_game(self): """Initializes or resets all game variables for a new round.""" # Scores self.score_pong = 0 self.score_snake = 0 # Pong Paddle self.paddle = pygame.Rect(30, SCREEN_HEIGHT / 2 - PADDLE_HEIGHT / 2, PADDLE_WIDTH, PADDLE_HEIGHT) # Snake self.snake_pos = [SCREEN_WIDTH - 200, SCREEN_HEIGHT / 2] self.snake_body = [[self.snake_pos[0], self.snake_pos[1]], [self.snake_pos[0] - SNAKE_SEGMENT_SIZE, self.snake_pos[1]], [self.snake_pos[0] - (2 * SNAKE_SEGMENT_SIZE), self.snake_pos[1]]] self.snake_direction = 'LEFT' self.change_to = self.snake_direction self.snake_move_counter = 0 self.snake_speed = SNAKE_SPEED_INITIAL # Ball self.ball = pygame.Rect(SCREEN_WIDTH / 2 - BALL_SIZE / 2, SCREEN_HEIGHT / 2 - BALL_SIZE / 2, BALL_SIZE, BALL_SIZE) self.ball_speed_x = random.choice([BALL_SPEED_INITIAL_X, -BALL_SPEED_INITIAL_X]) self.ball_speed_y = random.choice([BALL_SPEED_INITIAL_Y, -BALL_SPEED_INITIAL_Y]) def handle_input(self): """Processes all user input.""" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if self.game_state == "PLAYING": if event.type == pygame.KEYDOWN: # Snake controls if event.key == pygame.K_UP and self.snake_direction != 'DOWN': self.change_to = 'UP' if event.key == pygame.K_DOWN and self.snake_direction != 'UP': self.change_to = 'DOWN' if event.key == pygame.K_LEFT and self.snake_direction != 'RIGHT': self.change_to = 'LEFT' if event.key == pygame.K_RIGHT and self.snake_direction != 'LEFT': self.change_to = 'RIGHT' elif self.game_state in ["START", "GAME_OVER"]: if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: self.game_state = "PLAYING" self.reset_game() # Continuous key presses for paddle if self.game_state == "PLAYING": keys = pygame.key.get_pressed() if keys[pygame.K_w]: self.paddle.y -= PADDLE_SPEED if keys[pygame.K_s]: self.paddle.y += PADDLE_SPEED def update_game_state(self): """Updates the positions and logic of all game elements.""" # --- Paddle Movement and Bounds --- self.paddle.y = max(0, min(self.paddle.y, SCREEN_HEIGHT - PADDLE_HEIGHT)) # --- Snake Movement --- self.snake_move_counter += 1 if self.snake_move_counter >= self.snake_speed: self.snake_move_counter = 0 # Validate direction change if self.change_to == 'UP' and self.snake_direction != 'DOWN': self.snake_direction = 'UP' if self.change_to == 'DOWN' and self.snake_direction != 'UP': self.snake_direction = 'DOWN' if self.change_to == 'LEFT' and self.snake_direction != 'RIGHT': self.snake_direction = 'LEFT' if self.change_to == 'RIGHT' and self.snake_direction != 'LEFT': self.snake_direction = 'RIGHT' # Update position if self.snake_direction == 'UP': self.snake_pos[1] -= SNAKE_SEGMENT_SIZE if self.snake_direction == 'DOWN': self.snake_pos[1] += SNAKE_SEGMENT_SIZE if self.snake_direction == 'LEFT': self.snake_pos[0] -= SNAKE_SEGMENT_SIZE if self.snake_direction == 'RIGHT': self.snake_pos[0] += SNAKE_SEGMENT_SIZE # Snake body mechanism self.snake_body.insert(0, list(self.snake_pos)) # --- Snake-Ball Collision (Eating) --- snake_head_rect = pygame.Rect(self.snake_pos[0], self.snake_pos[1], SNAKE_SEGMENT_SIZE, SNAKE_SEGMENT_SIZE) if snake_head_rect.colliderect(self.ball): self.score_snake += 1 self.sounds.play('score_snake') # Respawn ball in a new safe location self.ball.x = random.randint(100, SCREEN_WIDTH - 100) self.ball.y = random.randint(100, SCREEN_HEIGHT - 100) # Reverse ball direction and speed up slightly self.ball_speed_x = -self.ball_speed_x * 1.05 self.ball_speed_y = -self.ball_speed_y * 1.05 else: self.snake_body.pop() # --- Ball Movement and Bouncing --- self.ball.x += self.ball_speed_x self.ball.y += self.ball_speed_y # Ball collision with top/bottom walls if self.ball.top <= 0 or self.ball.bottom >= SCREEN_HEIGHT: self.ball_speed_y *= -1 self.sounds.play('hit') # Ball collision with right wall (Snake's side) if self.ball.right >= SCREEN_WIDTH: self.ball_speed_x *= -1 self.sounds.play('hit') # Ball collision with Pong paddle if self.ball.colliderect(self.paddle): self.ball_speed_x *= -1.1 # Speed up horizontally self.ball_speed_y *= 1.05 # Speed up vertically # Prevent ball from getting stuck in paddle self.ball.left = self.paddle.right self.sounds.play('hit') # --- Lose Conditions --- # 1. Pong player misses the ball if self.ball.left <= 0: self.score_snake += 5 # Bonus for snake self.end_game("Pong Player Missed!") # 2. Snake hits walls (top, bottom, right) if (self.snake_pos[0] < 0 or self.snake_pos[0] > SCREEN_WIDTH - SNAKE_SEGMENT_SIZE or self.snake_pos[1] < 0 or self.snake_pos[1] > SCREEN_HEIGHT - SNAKE_SEGMENT_SIZE): self.score_pong += 1 self.end_game("Snake Crashed into a Wall!") # 3. Snake hits itself for block in self.snake_body[1:]: if self.snake_pos[0] == block[0] and self.snake_pos[1] == block[1]: self.score_pong += 1 self.end_game("Snake Ate Itself!") def end_game(self, message): """Sets the game state to GAME_OVER and determines the winner.""" self.sounds.play('game_over') self.game_state = "GAME_OVER" if self.score_snake > self.score_pong: self.winner_text = f"Snake Player Wins! ({message})" elif self.score_pong > self.score_snake: self.winner_text = f"Pong Player Wins! ({message})" else: self.winner_text = "It's a Draw!" def draw_elements(self): """Draws all game elements to the screen.""" self.screen.fill(BLACK) # Draw dotted line for i in range(0, SCREEN_HEIGHT, 20): pygame.draw.rect(self.screen, WHITE, (SCREEN_WIDTH/2 - 1, i, 2, 10)) # Draw paddle pygame.draw.rect(self.screen, BLUE, self.paddle) # Draw snake for pos in self.snake_body: pygame.draw.rect(self.screen, GREEN, pygame.Rect(pos[0], pos[1], SNAKE_SEGMENT_SIZE, SNAKE_SEGMENT_SIZE)) # Draw ball pygame.draw.ellipse(self.screen, YELLOW, self.ball) # Draw scores pong_score_text = self.font_small.render(f"PONG: {self.score_pong}", True, WHITE) self.screen.blit(pong_score_text, (50, 20)) snake_score_text = self.font_small.render(f"SNAKE: {self.score_snake}", True, WHITE) self.screen.blit(snake_score_text, (SCREEN_WIDTH - snake_score_text.get_width() - 50, 20)) def draw_start_screen(self): self.screen.fill(BLACK) title = self.font_large.render("SNAKE-PONG FUSION", True, WHITE) p1_instr = self.font_small.render("P1 (PONG): Use W/S to move paddle", True, BLUE) p2_instr = self.font_small.render("P2 (SNAKE): Use Arrow Keys to move & eat the ball", True, GREEN) start_instr = self.font_small.render("Press SPACE to Start", True, YELLOW) self.screen.blit(title, (SCREEN_WIDTH/2 - title.get_width()/2, SCREEN_HEIGHT/4)) self.screen.blit(p1_instr, (SCREEN_WIDTH/2 - p1_instr.get_width()/2, SCREEN_HEIGHT/2 - 20)) self.screen.blit(p2_instr, (SCREEN_WIDTH/2 - p2_instr.get_width()/2, SCREEN_HEIGHT/2 + 30)) self.screen.blit(start_instr, (SCREEN_WIDTH/2 - start_instr.get_width()/2, SCREEN_HEIGHT * 3/4)) def draw_game_over_screen(self): self.screen.fill(BLACK) title = self.font_large.render("GAME OVER", True, RED) winner = self.font_small.render(self.winner_text, True, WHITE) restart_instr = self.font_small.render("Press SPACE to Play Again", True, YELLOW) self.screen.blit(title, (SCREEN_WIDTH/2 - title.get_width()/2, SCREEN_HEIGHT/4)) self.screen.blit(winner, (SCREEN_WIDTH/2 - winner.get_width()/2, SCREEN_HEIGHT/2)) self.screen.blit(restart_instr, (SCREEN_WIDTH/2 - restart_instr.get_width()/2, SCREEN_HEIGHT * 3/4)) def run(self): """The main game loop.""" while True: self.handle_input() if self.game_state == "PLAYING": self.update_game_state() self.draw_elements() elif self.game_state == "START": self.draw_start_screen() elif self.game_state == "GAME_OVER": self.draw_game_over_screen() pygame.display.flip() self.clock.tick(FPS) if __name__ == "__main__": game = Game() game.run()How to Play
- Run the script. You will see the Start Menu.
- Player 1 gets ready with the
WandSkeys. - Player 2 gets ready with the
Up,Down,Left,RightArrow keys. - Press the SPACEBAR to begin the game.
- Player 1 (Pong): Defend your goal! Keep the yellow ball from getting past your blue paddle.
- Player 2 (Snake): Hunt the ball! Intercept it to score points and make your snake grow longer. Be careful not to run into the walls or your own tail.
- The game ends when a player makes a mistake. The winner is declared on the Game Over screen.
- Press SPACEBAR to play another round.
- Player 1 (Left Side): Controls a Pong paddle with
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.