سجاد سیدی
3 سال پیش توسط سجاد سیدی مطرح شد
1 پاسخ

کد پایتون که چت ببینگ نوشت

سلام. ببینید هوش مصنوعی (بینگ چت) چه کدی زده!
ببینید و نظر بدید.
اینم کد:

import pygame
import random
import sys

# تعریف متغیرهای اولیه
WIDTH = 600
HEIGHT = 600
BLOCK_SIZE = 20
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)

# تعریف کلاس مار
class Snake:
    def __init__(self):
        self.x = WIDTH / 2
        self.y = HEIGHT / 2
        self.x_change = 0
        self.y_change = BLOCK_SIZE
        self.body = [[self.x, self.y], [self.x - BLOCK_SIZE, self.y]]
        self.direction = "UP"

    def draw(self):
        for i, block in enumerate(self.body):
            x_pos, y_pos = block
            if i == len(self.body) - 1:
                pygame.draw.circle(screen, WHITE, (int(x_pos + BLOCK_SIZE / 2), int(y_pos + BLOCK_SIZE / 2)), int(BLOCK_SIZE / 2))
            elif i == 0:
                pygame.draw.circle(screen, RED, (int(x_pos + BLOCK_SIZE / 2), int(y_pos + BLOCK_SIZE / 2)), int(BLOCK_SIZE / 2))
                # اضافه کردن دهان به سر مار
                if self.direction == "UP":
                    pygame.draw.line(screen, BLACK, (int(x_pos + BLOCK_SIZE / 4), int(y_pos + BLOCK_SIZE / 4)), (int(x_pos + BLOCK_SIZE * 3 / 4), int(y_pos + BLOCK_SIZE / 4)), int(BLOCK_SIZE / 10))
                elif self.direction == "DOWN":
                    pygame.draw.line(screen, BLACK, (int(x_pos + BLOCK_SIZE / 4), int(y_pos + BLOCK_SIZE * 3 / 4)), (int(x_pos + BLOCK_SIZE * 3 / 4), int(y_pos + BLOCK_SIZE * 3 / 4)), int(BLOCK_SIZE / 10))
                elif self.direction == "LEFT":
                    pygame.draw.line(screen, BLACK, (int(x_pos + BLOCK_SIZE / 4), int(y_pos + BLOCK_SIZE / 4)), (int(x_pos + BLOCK_SIZE / 4), int(y_pos + BLOCK_SIZE * 3 / 4)), int(BLOCK_SIZE / 10))
                elif self.direction == "RIGHT":
                    pygame.draw.line(screen, BLACK, (int(x_pos + BLOCK_SIZE *3/4), int(y_pos + BLOCK_SIZE /4)), (int(x_pos + BLOCK_SIZE *3/4), int(y_pos + BLOCK_SIZE *3/4)), int(BLOCK_SIZE/10))
                # اضافه کردن دو عدد به عنوان دو عدد به سر مار
                if self.direction == "UP" or self.direction == "DOWN":
                    pygame.draw.circle(screen,BLACK,(int(x_pos+BLOCK_SIZE/3),int(y_pos+BLOCK_SIZE/2)),int(BLOCK_SIZE/8))
                    pygame.draw.circle(screen,BLACK,(int(x_pos+BLOCK_SIZE*2/3),int(y_pos+BLOCK_SIZE/2)),int(BLOCK_SIZE/8))
                else:
                    pygame.draw.circle(screen,BLACK,(int(x_pos+BLOCK_SIZE/2),int(y_pos+BLOCK_SIZE/3)),int(BLOCK_SIZE/8))
                    pygame.draw.circle(screen,BLACK,(int(x_pos+BLOCK_SIZE/2),int(y_pos+BLOCK_SIZE*2/3)),int(BLOCK_SIZE/8))
            else:
                pygame.draw.circle(screen, WHITE, (int(x_pos + BLOCK_SIZE / 2), int(y_pos + BLOCK_SIZE / 2)), int(BLOCK_SIZE / 2))

    def move(self):
        self.x += self.x_change
        self.y += self.y_change

        head = [self.x, self.y]
        self.body.insert(0, head)
        self.body.pop()

    def turn(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP] and self.direction != "DOWN":
            self.direction = "UP"
            self.x_change = 0
            self.y_change = -BLOCK_SIZE
        elif keys[pygame.K_DOWN] and self.direction != "UP":
            self.direction = "DOWN"
            self.x_change = 0
            self.y_change = BLOCK_SIZE
        elif keys[pygame.K_LEFT] and self.direction != "RIGHT":
            self.direction = "LEFT"
            self.x_change = -BLOCK_SIZE
            self.y_change = 0
        elif keys[pygame.K_RIGHT] and self.direction != "LEFT":
            self.direction = "RIGHT"
            self.x_change = BLOCK_SIZE
            self.y_change = 0

    def eat(self):
        if fruit.x == snake.x and fruit.y == snake.y:
            eat_sound.play()
            fruit.new_position()
            tail_block_x, tail_block_y = snake.body[-1]
            if snake.direction == "UP":
                new_block_y_pos = tail_block_y + BLOCK_SIZE
                snake.body.append([tail_block_x, new_block_y_pos])
            elif snake.direction == "DOWN":
                new_block_y_pos = tail_block_y - BLOCK_SIZE
                snake.body.append([tail_block_x, new_block_y_pos])
            elif snake.direction == "LEFT":
                new_block_x_pos = tail_block_x + BLOCK_SIZE
                snake.body.append([new_block_x_pos, tail_block_y])
            elif snake.direction == "RIGHT":
                new_block_x_pos = tail_block_x - BLOCK_SIZE
                snake.body.append([new_block_x_pos, tail_block_y])

    def collision(self):
        for block in snake.body[1:]:
            if block[0] == snake.x and block[1] == snake.y:
                return True

# تعریف کلاس غذا 
class Fruit:
    def __init__(self):
        x_blocks_num = WIDTH // BLOCK_SIZE
        y_blocks_num = HEIGHT // BLOCK_SIZE

        x_rand_num_blocks = random.randint(1, x_blocks_num - 1)
        y_rand_num_blocks = random.randint(1, y_blocks_num - 1)

        self.x = x_rand_num_blocks * BLOCK_SIZE
        self.y = y_rand_num_blocks * BLOCK_SIZE

    def draw(self):
        fruit_image = pygame.image.load("fruit.png")
        fruit_image = pygame.transform.scale(fruit_image, (BLOCK_SIZE, BLOCK_SIZE))
        screen.blit(fruit_image, (self.x, self.y))

    def new_position(self):
        x_blocks_num = WIDTH // BLOCK_SIZE
        y_blocks_num = HEIGHT // BLOCK_SIZE

        x_rand_num_blocks = random.randint(1, x_blocks_num - 1)
        y_rand_num_blocks = random.randint(1, y_blocks_num - 1)

        self.x = x_rand_num_blocks * BLOCK_SIZE
        self.y = y_rand_num_blocks * BLOCK_SIZE

# تعریف تابع game over
def game_over():
    global game_state
    game_over_sound.play()
    game_state = "game_over"

def play_again():
    global snake, game_state
    snake = Snake()
    game_state = "game"

# شروع بازی
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
background_image = pygame.image.load("background.png")
eat_sound = pygame.mixer.Sound("eat.wav")
game_over_sound = pygame.mixer.Sound("game_over.wav")
snake = Snake()
fruit = Fruit()
game_over_screen = False

game_state = "start"

# حلقه اصلی بازی
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if game_state == "start":
                mouse_x_pos, mouse_y_pos = pygame.mouse.get_pos()
                if play_button_x_pos <= mouse_x_pos <= play_button_x_pos + play_button_text.get_width() and \
                        play_button_y_pos <= mouse_y_pos <= play_button_y_pos + play_button_text.get_height():
                    game_state = "game"
            elif game_state == "game_over":
                if play_again_button_x_pos <= mouse_x_pos <= play_again_button_x_pos + play_again_button_text.get_width() and \
                        play_again_button_y_pos <= mouse_y_pos <= play_again_button_y_pos + play_again_button_text.get_height():
                    play_again()
    screen.blit(background_image, (0, 0))

    if game_state == "start":
        # Draw start screen
        play_button_font = pygame.font.Font(None, 36)
        play_button_text = play_button_font.render("Play", True, BLACK)
        play_button_x_pos = WIDTH / 2 - play_button_text.get_width() / 2
        play_button_y_pos = HEIGHT / 2
        # اضافه کردن مستطیل رنگی برای دکمه Play
        pygame.draw.rect(screen, BLUE, (play_button_x_pos - 10, play_button_y_pos - 10, play_button_text.get_width() + 20, play_button_text.get_height() + 20))
        screen.blit(play_button_text, (play_button_x_pos, play_button_y_pos))
    elif game_state == "game":
        # Draw game screen
        snake.draw()
        snake.move()
        snake.turn()
        snake.eat()
        fruit.draw()

        if snake.collision():
            game_over()

        if snake.x < 0 or snake.x >= WIDTH or snake.y < 0 or snake.y >= HEIGHT:
            game_over()
    elif game_state == "game_over":
        game_over_font = pygame.font.Font(None, 36)
        game_over_text = game_over_font.render("Game Over", True, BLACK)
        screen.blit(game_over_text, (WIDTH / 2 - game_over_text.get_width() / 2, HEIGHT / 2))

        play_again_button_font = pygame.font.Font(None, 24)
        play_again_button_text = play_again_button_font.render("Play Again", True, BLACK)
        play_again_button_x_pos = WIDTH / 2 - play_again_button_text.get_width() / 2
        play_again_button_y_pos = HEIGHT / 2 + 50
        # اضافه کردن مستطیل رنگی برای دکمه Play Again
        pygame.draw.rect(screen, BLUE, (play_again_button_x_pos - 10, play_again_button_y_pos - 10, play_again_button_text.get_width() + 20, play_again_button_text.get_height() + 20))
        screen.blit(play_again_button_text, (play_again_button_x_pos, play_again_button_y_pos))

        exit_button_font = pygame.font.Font(None, 24)
        exit_button_text = exit_button_font.render("Exit", True, BLACK)
        exit_button_x_pos = WIDTH / 2 - exit_button_text.get_width() / 2
        exit_button_y_pos = HEIGHT / 2 + 100
        # اضافه کردن مستطیل رنگی برای دکمه Exit
        pygame.draw.rect(screen, BLUE, (exit_button_x_pos - 10, exit_button_y_pos - 10, exit_button_text.get_width() + 20, exit_button_text.get_height() + 20))
        screen.blit(exit_button_text, (exit_button_x_pos, exit_button_y_pos))

        mouse_x_pos, mouse_y_pos = pygame.mouse.get_pos()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if play_again_button_x_pos <= mouse_x_pos <= play_again_button_x_pos + play_again_button_text.get_width() and \
                    play_again_button_y_pos <= mouse_y_pos <= play_again_button_y_pos + play_again_button_text.get_height():
                play_again()

            if exit_button_x_pos <= mouse_x_pos <= exit_button_x_pos + exit_button_text.get_width() and \
                    exit_button_y_pos <= mouse_y_pos <= exit_button_y_pos + exit_button_text.get_height():
                pygame.quit()
                sys.exit()

    pygame.display.update()
    clock.tick(10)