Pacman Game in Python
Criado em: 3 de dezembro de 2024
Respondido usando O1 Preview por Chat01
Criado em: 3 de dezembro de 2024
Respondido usando O1 Preview por Chat01
Write the pacman game in python. Main character should be in yellow color and the enemy should be red or blue depending on different speed. I will use google colab to run it.
Here's a simplified version of the Pacman game written in Python that you can run in Google Colab. The main character (Pacman) is yellow, and the enemy is red. Pacman can be controlled using buttons for Up, Down, Left, and Right.
Step 1: Install Necessary Packages
First, install the required packages: xvfb
, python-opengl
, ffmpeg
, pyvirtualdisplay
, and pygame
.
python!apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1 !pip install pyvirtualdisplay pygame > /dev/null 2>&1
Step 2: Set Up Virtual Display
Set up a virtual display since Google Colab does not support a display environment by default.
pythonfrom pyvirtualdisplay import Display display = Display(visible=0, size=(800, 600)) display.start()
Step 3: Import Necessary Libraries
Import all the libraries you'll need for the game.
pythonimport pygame import sys import random import numpy as np from IPython.display import display, clear_output import ipywidgets as widgets import time import matplotlib.pyplot as plt
Step 4: Initialize Pygame and Set Up the Game Display
Initialize Pygame and set up the game window dimensions.
pythonpygame.init() screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Pacman in Colab")
Step 5: Define Colors
Define the colors that will be used in the game.
pythonBLACK = (0, 0, 0) YELLOW = (255, 255, 0) # Pacman RED = (255, 0, 0) # Enemy
Step 6: Create Control Buttons
Create interactive buttons to control Pacman's movement.
pythonup_button = widgets.Button(description="Up") down_button = widgets.Button(description="Down") left_button = widgets.Button(description="Left") right_button = widgets.Button(description="Right") direction = 'STAY' # Default direction
Step 7: Define Button Click Functions
Define functions that will update the direction based on button clicks.
pythondef on_up_button_clicked(b): global direction direction = 'UP' def on_down_button_clicked(b): global direction direction = 'DOWN' def on_left_button_clicked(b): global direction direction = 'LEFT' def on_right_button_clicked(b): global direction direction = 'RIGHT'
Step 8: Link Buttons to Functions and Display Them
Link the buttons to their corresponding functions and display them.
pythonup_button.on_click(on_up_button_clicked) down_button.on_click(on_down_button_clicked) left_button.on_click(on_left_button_clicked) right_button.on_click(on_right_button_clicked) display(widgets.HBox([up_button, down_button, left_button, right_button]))
Step 9: Initialize Game Objects
Set the initial positions and speeds of Pacman and the enemy.
pythonpacman_pos = [screen_width // 2, screen_height // 2] pacman_speed = 10 enemy_pos = [random.randint(0, screen_width), random.randint(0, screen_height)] enemy_speed = 5
Step 10: Main Game Loop
Run the game loop for a fixed number of iterations to prevent Colab from freezing.
pythonfor i in range(200): # Update game state based on direction if direction == 'UP': pacman_pos[1] -= pacman_speed elif direction == 'DOWN': pacman_pos[1] += pacman_speed elif direction == 'LEFT': pacman_pos[0] -= pacman_speed elif direction == 'RIGHT': pacman_pos[0] += pacman_speed # Reset direction direction = 'STAY' # Keep Pacman within the screen boundaries pacman_pos[0] = max(0, min(pacman_pos[0], screen_width)) pacman_pos[1] = max(0, min(pacman_pos[1], screen_height)) # Move enemy towards Pacman if enemy_pos[0] < pacman_pos[0]: enemy_pos[0] += enemy_speed elif enemy_pos[0] > pacman_pos[0]: enemy_pos[0] -= enemy_speed if enemy_pos[1] < pacman_pos[1]: enemy_pos[1] += enemy_speed elif enemy_pos[1] > pacman_pos[1]: enemy_pos[1] -= enemy_speed # Clear screen screen.fill(BLACK) # Draw Pacman pygame.draw.circle(screen, YELLOW, pacman_pos, 20) # Draw enemy pygame.draw.circle(screen, RED, enemy_pos, 20) # Update display pygame.display.flip() # Capture the frame as an array data = pygame.surfarray.array3d(pygame.display.get_surface()) # Transpose the array data = np.transpose(data, (1, 0, 2)) # Display the frame plt.imshow(data) plt.axis('off') clear_output(wait=True) # Clear previous output display(widgets.HBox([up_button, down_button, left_button, right_button])) plt.show() # Delay to control frame rate time.sleep(0.1)
Note: The game runs for 200 iterations to prevent Google Colab from timing out or freezing. You can adjust the number of iterations as needed.
Example Output:
After running the code, you should see the Pacman game displayed within the notebook. Use the buttons to control Pacman's movement.
This code sets up a simple Pacman game in Google Colab using Pygame and interactive widgets. The main character is yellow, and the enemy is red. Pacman is controlled using the provided buttons, and the game updates are displayed inline using Matplotlib.