pygame / pygame

🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
https://www.pygame.org
7.49k stars 3.33k forks source link

game con rắn #4387

Open anh0208-max opened 6 days ago

anh0208-max commented 6 days ago

import pygame import random

Khởi tạo Pygame

pygame.init()

Kích thước màn hình

SCREEN_WIDTH = 600 SCREEN_HEIGHT = 400 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Game Rắn Săn Mồi')

Màu sắc

WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) BLACK = (0, 0, 0)

Kích thước và tốc độ của rắn

snake_block = 20 snake_speed = 15

Thiết lập phông chữ

font_style = pygame.font.SysFont("bahnschrift", 25) score_font = pygame.font.SysFont("comicsansms", 35)

Hàm hiển thị điểm

def show_score(score): value = score_font.render("Score: " + str(score), True, BLACK) screen.blit(value, [0, 0])

Hàm tạo con rắn

def draw_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block, snake_block])

Hàm hiển thị thông báo

def message(msg, color): mesg = font_style.render(msg, True, color) screen.blit(mesg, [SCREEN_WIDTH / 6, SCREEN_HEIGHT / 3])

Vòng lặp chính của game

def game_loop(): game_over = False game_close = False

x1 = SCREEN_WIDTH / 2
y1 = SCREEN_HEIGHT / 2

x1_change = 0
y1_change = 0

snake_list = []
length_of_snake = 1

# Vị trí thức ăn ban đầu
foodx = round(random.randrange(0, SCREEN_WIDTH - snake_block) / 20.0) * 20.0
foody = round(random.randrange(0, SCREEN_HEIGHT - snake_block) / 20.0) * 20.0

clock = pygame.time.Clock()

while not game_over:

    while game_close:
        screen.fill(WHITE)
        message("You Lost! Press C to Play Again or Q to Quit", RED)
        show_score(length_of_snake - 1)
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    game_over = True
                    game_close = False
                if event.key == pygame.K_c:
                    game_loop()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x1_change = -snake_block
                y1_change = 0
            elif event.key == pygame.K_RIGHT:
                x1_change = snake_block
                y1_change = 0
            elif event.key == pygame.K_UP:
                y1_change = -snake_block
                x1_change = 0
            elif event.key == pygame.K_DOWN:
                y1_change = snake_block
                x1_change = 0

    if x1 >= SCREEN_WIDTH or x1 < 0 or y1 >= SCREEN_HEIGHT or y1 < 0:
        game_close = True

    x1 += x1_change
    y1 += y1_change
    screen.fill(WHITE)
    pygame.draw.rect(screen, RED, [foodx, foody, snake_block, snake_block])

    snake_head = [x1, y1]
    snake_list.append(snake_head)
    if len(snake_list) > length_of_snake:
        del snake_list[0]

    for x in snake_list[:-1]:
        if x == snake_head:
            game_close = True

    draw_snake(snake_block, snake_list)
    show_score(length_of_snake - 1)

    pygame.display.update()

    if x1 == foodx and y1 == foody:
        foodx = round(random.randrange(0, SCREEN_WIDTH - snake_block) / 20.0) * 20.0
        foody = round(random.randrange(0, SCREEN_HEIGHT - snake_block) / 20.0) * 20.0
        length_of_snake += 1

    clock.tick(snake_speed)

pygame.quit()
quit()

Chạy game

game_loop()

LuckyIntel commented 6 days ago

Is this a default template or something i've seen so much issues that gives an example code like this one.Maybe just bots?