利用 ChatGPT 直接生成的贪吃蛇游戏
纯天然AI制作,不含任何人工成分。
命令它直接生成 游玩过程import pygame
import sys
import random
import time
check_errors = pygame.init()
if check_errors[1] > 0:
print(f"{check_errors[1]} errors found, exiting...")
sys.exit(-1)
else:
print("Pygame successfully initialized")
# Play Surface
play_surface = pygame.display.set_mode((720, 460))
pygame.display.set_caption("Snake Game")
# Colors
red = pygame.Color(255, 0, 0) # Game Over
green = pygame.Color(0, 255, 0) # Snake
black = pygame.Color(0, 0, 0) # Score
white = pygame.Color(255, 255, 255) # Background
brown = pygame.Color(165, 42, 42) # Food
# FPS controller
fps_controller = pygame.time.Clock()
# Important Variables
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
food_position = [random.randrange(1, (720 // 10)) * 10, random.randrange(1, (460 // 10)) * 10]
food_spawn = True
direction = 'RIGHT'
changeto = direction
score = 0
# Game Over Function
def game_over():
my_font = pygame.font.SysFont('times new roman', 50)
game_over_surface = my_font.render('Your Score: ' + str(score), True, red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (360, 15)
play_surface.blit(game_over_surface, game_over_rect)
pygame.display.flip()
time.sleep(2)
pygame.quit() # pygame exit
sys.exit() # console exit
# Main Logic of the Game
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
changeto = 'UP'
if event.key == pygame.K_DOWN:
changeto = 'DOWN'
if event.key == pygame.K_LEFT:
changeto = 'LEFT'
if event.key == pygame.K_RIGHT:
changeto = 'RIGHT'
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Validation of Direction
if changeto == 'UP' and direction != 'DOWN':
direction = 'UP'
if changeto == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if changeto == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if changeto == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] += 10
if direction == 'LEFT':
snake_position[0] -= 10
if direction == 'RIGHT':
snake_position[0] += 10
# Snake Body Growing Mechanism
snake_body.insert(0, list(snake_position))
if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
score += 1
food_spawn = False
else:
snake_body.pop()
# Food Spawn
if not food_spawn:
food_position = [random.randrange(1, (720 // 10)) * 10, random.randrange(1, (460 // 10)) * 10]
food_spawn = True
play_surface.fill(white)
# Draw Snake
for pos in snake_body:
pygame.draw.rect(play_surface, green,
pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(play_surface, brown,
pygame.Rect(food_position[0], food_position[1], 10, 10))
# Bound
if snake_position[0] < 0 or snake_position[0] > 720-10:
game_over()
if snake_position[1] < 0 or snake_position[1] > 460-10:
game_over()
# Self Hit
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over()
# Common stuff
pygame.display.set_caption("Snake Game | Score : " + str(score))
pygame.display.flip()
fps_controller.tick(24)
上面就是源码,copy下来就能用,ChatGPT似乎默认用Python3.0以上
pip install pygame后直接可用
无任何人工成分在其中,健康环保。