用chatgpt写程序,程序员要失业了

我想用chatgpt写一个贪吃蛇的游戏,用python语言,结果真写出来了,还可以运行,还可以每行注释。太强了,程序员看来要大量失业了!

用chatgpt写程序,程序员要失业了

以下是使用Python和Pygame库编写的简单贪吃蛇游戏代码:

“`python

import pygame

import random

# 初始化pygame

pygame.init()

# 游戏窗口大小

width = 500

height = 500

# 创建窗口

window = pygame.display.set_mode((width, height))

pygame.display.set_caption("贪吃蛇")

# 定义颜色

white = (255, 255, 255)

black = (0, 0, 0)

red = (255, 0, 0)

# 设置计时器

clock = pygame.time.Clock()

# 贪吃蛇头部坐标,初始位置在屏幕中央

snake_head_x = width // 2

snake_head_y = height // 2

# 贪吃蛇身体坐标列表,初始长度为3

snake_body = []

for i in range(3):

snake_body.append([snake_head_x – i * 10, snake_head_y])

# 食物坐标,随机生成

food_x = round(random.randrange(0, width – 10) / 10.0) * 10.0

food_y = round(random.randrange(0, height – 10) / 10.0) * 10.0

# 初始方向向右

direction = "right"

# 分数

score = 0

# 游戏结束标志

game_over = False

while not game_over:

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 direction != "right":

direction = "left"

elif event.key == pygame.K_RIGHT and direction != "left":

direction = "right"

elif event.key == pygame.K_UP and direction != "down":

direction = "up"

elif event.key == pygame.K_DOWN and direction != "up":

direction = "down"

# 移动贪吃蛇身体

for i in range(len(snake_body) – 1, 0, -1):

snake_body[i][0] = snake_body[i-1][0]

snake_body[i][1] = snake_body[i-1][1]

# 移动贪吃蛇头部

if direction == "left":

snake_head_x -= 10

elif direction == "right":

snake_head_x += 10

elif direction == "up":

snake_head_y -= 10

elif direction == "down":

snake_head_y += 10

# 判断贪吃蛇是否吃到食物

if snake_head_x == food_x and snake_head_y == food_y:

food_x = round(random.randrange(0, width – 10) / 10.0) * 10.0

food_y = round(random.randrange(0, height – 10) / 10.0) * 10.0

score += 10

# 吃到食物后,在贪吃蛇尾部添加一个身体点

snake_body.append([snake_body[-1][0], snake_body[-1][1]])

# 判断是否碰到边界或自身,游戏结束

if snake_head_x 0 or snake_head_x > width-10 or snake_head_y 0 or snake_head_y > height-10:

game_over = True

for i in range(1, len(snake_body)):

if snake_head_x == snake_body[i][0] and snake_head_y == snake_body[i][1]:

game_over = True

# 绘制窗口,包括贪吃蛇、食物和分数

window.fill(white)

pygame.draw.rect(window, red, [food_x, food_y, 10, 10])

for i in range(len(snake_body)):

pygame.draw.rect(window, black, [snake_body[i][0], snake_body[i][1], 10, 10])

font = pygame.font.SysFont(None, 25)

text = font.render("Score: " + str(score), True, black)

window.blit(text, (10, 10))

pygame.display.update()

# 控制

生意营销3大宝:彩铃、定位、认证,一个也不能少,如有需要,添加 微信:xnc528  备注:3

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 820277912@qq.com 举报,一经查实,本站将立刻删除。
如若转载,请注明出处:https://www.clzz8.com/46401.html