Initializing...
Liveweave
Python
expand_more
home
Home
Palette
Color Explorer
arrow_outward
Polyline
Graphics Editor
arrow_outward
code
Web Playground
Default
arrow_outward
build
Tools
expand_more
restart_alt
Load "Numpy" example
download
Download
cloud_upload
Save
account_circle
Login
settings
Settings
expand_more
14
px
Line number
Mini map
Word wrap
sync_alt
Reset Settings
smart_display
Run
code
Python editor
main.py
terminal
Output
delete
import pygame import random import sys # ============================================ # بخش 1: تنظیمات اولیه بازی # ============================================ # تعریف رنگها (RGB) # هر رنگ از ترکیب سه رنگ قرمز، سبز و آبی ساخته میشه BLACK = (0, 0, 0) # مشکی WHITE = (255, 255, 255) # سفید RED = (255, 0, 0) # قرمز GREEN = (0, 255, 0) # سبز BLUE = (0, 0, 255) # آبی # تنظیمات صفحه بازی WINDOW_WIDTH = 600 # عرض پنجره WINDOW_HEIGHT = 400 # ارتفاع پنجره BLOCK_SIZE = 10 # اندازه هر خانه مار (به پیکسل) GAME_SPEED = 4 # سرعت بازی (فریم در ثانیه) # راهاندازی pygame pygame.init() # ایجاد پنجره بازی screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("? Snake Game - Learnning Programming") clock = pygame.time.Clock() # ============================================ # بخش 2: توابع کمکی # ============================================ def show_message(text, color, size, x, y): """نمایش متن روی صفحه""" font = pygame.font.Font(None, size) message = font.render(text, True, color) screen.blit(message, (x, y)) def draw_snake(snake_body): """رسم مار روی صفحه""" for block in snake_body: # هر بخش مار یک مستطیل سبز رنگه pygame.draw.rect(screen, GREEN, [block[0], block[1], BLOCK_SIZE, BLOCK_SIZE]) # حاشیه تیره برای زیبایی pygame.draw.rect(screen, BLACK, [block[0], block[1], BLOCK_SIZE, BLOCK_SIZE], 2) def generate_food(snake_body): """تولید غذای جدید در مکان تصادفی""" while True: # محاسبه مختصات تصادفی با توجه به اندازه شبکه food_x = random.randrange(0, WINDOW_WIDTH - BLOCK_SIZE, BLOCK_SIZE) food_y = random.randrange(0, WINDOW_HEIGHT - BLOCK_SIZE, BLOCK_SIZE) # بررسی اینکه غذا روی مار ایجاد نشه if [food_x, food_y] not in snake_body: return [food_x, food_y] # ============================================ # بخش 3: تابع اصلی بازی # ============================================ def game_loop(): """حلقه اصلی بازی""" # وضعیت اولیه مار (وسط صفحه) snake_x = WINDOW_WIDTH // 2 snake_y = WINDOW_HEIGHT // 2 # جهت حرکت اولیه (بدون حرکت) change_x = 0 change_y = 0 # بدن مار (لیستی از مختصات) snake_body = [[snake_x, snake_y]] snake_length = 1 # ایجاد اولین غذا food_position = generate_food(snake_body) # امتیاز بازیکن score = 0 # وضعیت بازی game_over = False game_paused = False print("? Game Started!") print("⌨️ Controls:") print(" - Arrow Keys: Move the snake") print(" - Space Key: Stop/Continue The Game") print(" - ESC Key: Exit The Game") # ========================================== # حلقه اصلی بازی (تا وقتی بازی تمام نشده) # ========================================== while not game_over: # ---------------------------------- # مدیریت رویدادها (Events) # ---------------------------------- for event in pygame.event.get(): # بستن پنجره if event.type == pygame.QUIT: game_over = True # فشردن کلیدها if event.type == pygame.KEYDOWN: # کلیدهای جهتنما if event.key == pygame.K_LEFT and change_x == 0: change_x = -BLOCK_SIZE change_y = 0 elif event.key == pygame.K_RIGHT and change_x == 0: change_x = BLOCK_SIZE change_y = 0 elif event.key == pygame.K_UP and change_y == 0: change_y = -BLOCK_SIZE change_x = 0 elif event.key == pygame.K_DOWN and change_y == 0: change_y = BLOCK_SIZE change_x = 0 # توقف/ادامه بازی elif event.key == pygame.K_SPACE: game_paused = not game_paused if game_paused: print("⏸️ The Game is Stoped") else: print("▶️ The Game is continued") # خروج با ESC elif event.key == pygame.K_ESCAPE: game_over = True # اگر بازی متوقف شده، رویدادها رو بررسی کن ولی حرکت نده if game_paused: show_message("⏸️ Stop", RED, 50, WINDOW_WIDTH//2 - 80, WINDOW_HEIGHT//2) pygame.display.update() continue # ---------------------------------- # بهروزرسانی موقعیت مار # ---------------------------------- snake_x += change_x snake_y += change_y # ---------------------------------- # بررسی برخورد با دیوارها # ---------------------------------- if (snake_x >= WINDOW_WIDTH or snake_x < 0 or snake_y >= WINDOW_HEIGHT or snake_y < 0): print("? Hit the wall! Game is over.") game_over = True # اضافه کردن موقعیت جدید به بدن مار snake_body.append([snake_x, snake_y]) # حذف دم مار اگر غذا نخورده باشیم if len(snake_body) > snake_length: del snake_body[0] # ---------------------------------- # بررسی برخورد مار با خودش # ---------------------------------- for block in snake_body[:-1]: if block == [snake_x, snake_y]: print("? The snake ate itself! The game is over") game_over = True # ---------------------------------- # بررسی خوردن غذا # ---------------------------------- if [snake_x, snake_y] == food_position: food_position = generate_food(snake_body) snake_length += 1 score += 10 print(f"? New food! Score: {score}") # ---------------------------------- # رسم صحنه بازی # ---------------------------------- screen.fill(BLACK) # رسم غذا (دایره قرمز) pygame.draw.circle(screen, RED, [food_position[0] + BLOCK_SIZE//2, food_position[1] + BLOCK_SIZE//2], BLOCK_SIZE//2) # رسم مار draw_snake(snake_body) # نمایش امتیاز show_message(f"Score: {score}", WHITE, 30, 10, 10) show_message(f"Snake Length: {snake_length}", WHITE, 30, 10, 40) # راهنمای کنترلها show_message("ESC=Exit Space=Stop", WHITE, 20, WINDOW_WIDTH-200, 10) # بهروزرسانی صفحه pygame.display.update() # کنترل سرعت بازی clock.tick(GAME_SPEED) # ========================================== # پایان بازی # ========================================== print(f"\n? The game is over! Final score: {score}") print(f"? Final length of the snake: {snake_length}") # نمایش پیام پایان بازی screen.fill(BLACK) show_message("? The Game is over!", RED, 50, WINDOW_WIDTH//2 - 150, WINDOW_HEIGHT//2 - 40) show_message(f"Final score: {score}", WHITE, 40, WINDOW_WIDTH//2 - 120, WINDOW_HEIGHT//2 + 10) show_message("Click to exit", WHITE, 30, WINDOW_WIDTH//2 - 120, WINDOW_HEIGHT//2 + 60) pygame.display.update() # انتظار برای کلیک کاربر waiting = True while waiting: for event in pygame.event.get(): if event.type == pygame.QUIT or event.type == pygame.MOUSEBUTTONDOWN: waiting = False pygame.quit() sys.exit() # ============================================ # بخش 4: اجرای بازی # ============================================ if __name__ == "__main__": print("=" * 40) print("? Welcome to Snake game! ?") print("=" * 40) game_loop()