TGViewer
PyCoders (پایتون) PyCoders (پایتون) @python_rd · 4.1K subscribers
Post #2058 840
✨ کد کامل بازی Pac-Man ساده

اول پکیج زیر رو نصب کنید:
pip install pygame



حالا میریم سراغ کد:

import pygame
import random

pygame.init()

WIDTH, HEIGHT = 600, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pac-Man (Simple Version)")

FPS = 60
clock = pygame.time.Clock()

# Colors
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)

# Pac-man setup
pac_size = 20
pac_x, pac_y = WIDTH // 2, HEIGHT // 2
pac_speed = 4

# Ghost setup
ghost_size = 20
ghost_x, ghost_y = random.randint(0, WIDTH), random.randint(0, HEIGHT)
ghost_speed = 2

# Food
food_size = 8
foods = [(random.randint(10, WIDTH-10), random.randint(10, HEIGHT-10)) for _ in range(20)]
score = 0

# Movement variables
dx, dy = 0, 0

running = True
while running:
clock.tick(FPS)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dx, dy = -pac_speed, 0
elif event.key == pygame.K_RIGHT:
dx, dy = pac_speed, 0
elif event.key == pygame.K_UP:
dx, dy = 0, -pac_speed
elif event.key == pygame.K_DOWN:
dx, dy = 0, pac_speed

# Update pacman
pac_x += dx
pac_y += dy

# Boundary check
pac_x = max(0, min(WIDTH - pac_size, pac_x))
pac_y = max(0, min(HEIGHT - pac_size, pac_y))

# Move ghost
if pac_x > ghost_x: ghost_x += ghost_speed
if pac_x < ghost_x: ghost_x -= ghost_speed
if pac_y > ghost_y: ghost_y += ghost_speed
if pac_y < ghost_y: ghost_y -= ghost_speed

# Eating food
new_foods = []
for fx, fy in foods:
if abs(pac_x - fx) < pac_size and abs(pac_y - fy) < pac_size:
score += 1
else:
new_foods.append((fx, fy))
foods = new_foods

WIN.fill(BLACK)

# Draw pacman
pygame.draw.circle(WIN, YELLOW, (pac_x, pac_y), pac_size)

# Draw ghost
pygame.draw.rect(WIN, RED, (ghost_x, ghost_y, ghost_size, ghost_size))

# Draw food
for fx, fy in foods:
pygame.draw.circle(WIN, WHITE, (fx, fy), food_size)

# Display score
font = pygame.font.SysFont(None, 30)
img = font.render(f"Score: {score}", True, WHITE)
WIN.blit(img, (10, 10))

# Check collision with ghost
if abs(pac_x - ghost_x) < pac_size and abs(pac_y - ghost_y) < pac_size:
font = pygame.font.SysFont(None, 50)
over = font.render("GAME OVER!", True, WHITE)
WIN.blit(over, (WIDTH//2-120, HEIGHT//2-20))
pygame.display.update()
pygame.time.delay(2000)
running = False

pygame.display.update()

pygame.quit()



برای اجرا با ترمینال باید:
python pacman.py



🔸 با کلیدهای جهت‌دار حرکت می‌کنی
🔸 امتیاز جمع می‌کنی
🔸 اگر روح بهت بخوره Game Over میشی

🐍 @python_rd
  • ❤ 2
More from @python_rd
  1. Oct 16, 2026🔆 یک سایت خوب پر از پادکست‌های مرتبط با زبان پایتون 🔹 talkpython.fm 🐍 @python_rd
  2. Oct 15, 2026🌐 خروجی این کد پایتون چیست؟ x = [1, 2, 3] y = x x += [4, 5] print(x) print(y) 🔹 گزینه‌ها…
  3. Oct 15, 2026🔆 آیا enumerate() بهتر از index-based loop است؟ 🔹 خیلی‌ها هنوز این شکلی می‌نویسند: items…
  4. Oct 15, 2026🔆 یک تله عجیب در پایتون: bool در واقع زیرکلاس int است 🔹 خیلی‌ها می‌دانند True == 1 و Fal…
  5. Oct 13, 2026کوییز پایتون 🔆 خروجی اجرای کد زیر چیست؟ t = (1, 2, [3, 4]) try: t[2] += [5] except TypeEr…
  6. Oct 13, 2026🔆 راز مکانیزم پیاده‌سازی و تغییر جهت NaN در الگوریتم مرتب‌سازی پایتون (Timsort) 🔹 باگ ری…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →