Giới thiệu sản phẩm

Tìm hiểu chủ đề: Các trò chơi kinh điển phần 2
Kiến thức, khái niệm: Sử dụng thư viện Pygame để hoàn thiện kịch bản đã thiết kế ở bài 5
Dự án Trò chơi rắn săn mồi: Lập trình cho nhân vật rắn di chuyển và ăn các ô gạch. Mỗi lần ăn ô gạch nhân vật rắn sẽ thay đổi kích thước.

import pygame
import random

pygame.init()
pygame.mixer.music.load('sound.mp3')
pygame.mixer.music.play(-1)

eat_sound = pygame.mixer.Sound('eat.mp3')

WIDTH, HEIGHT = 600, 400
BLOCK = 20
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

score = 0
font = pygame.font.SysFont(name="Arial", size=50)

snake = [(140,100), (120, 100), (100, 100)]
direction = 'RIGHT'

food_x = random.randint(0, (WIDTH - BLOCK) // BLOCK) * BLOCK
food_y = random.randint(0, (HEIGHT - BLOCK) // BLOCK) * BLOCK
food = (food_x, food_y)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                direction = 'RIGHT'
            elif event.key == pygame.K_LEFT:
                direction = 'LEFT'
            elif event.key == pygame.K_UP:
                direction = 'UP'
            elif event.key == pygame.K_DOWN:
                direction = 'DOWN'

    head_x, head_y = snake[0]

    if direction == 'RIGHT':
        head_x += BLOCK
    elif direction == 'LEFT':
        head_x -= BLOCK
    elif direction == 'UP':
        head_y -= BLOCK
    elif direction == 'DOWN':
        head_y += BLOCK

    snake.insert(0, (head_x, head_y))

    if (head_x, head_y) in snake[1:]:
        pygame.mixer.music.stop()
        game_over_text = font.render('Game Over', True, (255, 0, 0))
        screen.fill((0, 0, 0))
        screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - game_over_text.get_height() // 2))
        pygame.display.flip()
        pygame.time.delay(5000)
        running = False

    if head_x < 0 or head_x >= WIDTH or head_y < 0 or head_y >= HEIGHT:
        pygame.mixer.music.stop()
        game_over_text = font.render('Game Over', True, (255, 0, 0))
        screen.fill((0, 0, 0))
        screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - game_over_text.get_height() // 2))
        pygame.display.flip()
        pygame.time.delay(5000)
        running = False

    if (head_x, head_y) == food:
        score += 1
        eat_sound.play()
        food_x = random.randint(0, (WIDTH - BLOCK) // BLOCK) * BLOCK
        food_y = random.randint(0, (HEIGHT - BLOCK) // BLOCK) * BLOCK
        food = (food_x, food_y)
    else:
        snake.pop()

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 0, 0), (*food, BLOCK, BLOCK))

    score_text = font.render(f'Score: {score}', True, (255, 255, 255))
    screen.blit(score_text, (10, 10))

    for part in snake:
        pygame.draw.rect(screen, (0, 255, 0), (*part, BLOCK, BLOCK))
    pygame.display.flip()
    clock.tick(15)

pygame.quit()
Hình ảnh sản phẩm
Hãy bình luận để nhặt 100 thóc nhé

Đăng nhập để tham gia bình luận