使用文心一言尝试编写一个小游戏,先完成
1.python中用pygame模块设计出一个显示区域720x540的屏幕,并绘制一个小球,可以完成小球在显示区域内自动随机直线移动,碰到显示区域的便捷并反弹
import pygame
import random
import sys# 初始化 Pygame
pygame.init()# 设置屏幕尺寸
screen_width, screen_height = 720, 540
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("小球反弹示例")# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)# 定义小球属性
ball_radius = 20
ball_color = WHITE
ball_x, ball_y = screen_width // 2, screen_height // 2 # 初始位置
ball_dx, ball_dy = random.choice([-5, 5]), random.choice([-5, 5]) # 初始速度# 游戏时钟
clock = pygame.time.Clock()# 游戏循环
running = True
while running:# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = False# 更新小球位置ball_x += ball_dxball_y += ball_dy# 检查并处理小球与边界的碰撞if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:ball_dx = -ball_dx # 水平方向反弹if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:ball_dy = -ball_dy # 垂直方向反弹# 绘制屏幕screen.fill(BLACK) # 用黑色填充屏幕pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius) # 绘制小球# 更新显示pygame.display.flip()# 控制帧率clock.tick(60)# 退出 Pygame
pygame.quit()
sys.exit()
2.python中用pygame模块设计出一个显示区域720x540的屏幕,并绘制一个小球,可以完成小球在显示区域内自动随机直线移动,碰到显示区域的便捷并反弹,反弹后小球的颜色变化一些
import pygame
import random
import sys# 初始化 Pygame
pygame.init()# 设置屏幕尺寸
screen_width, screen_height = 720, 540
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("小球反弹与变色示例")# 定义颜色
BLACK = (0, 0, 0)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0), (138, 43, 226)] # 多种颜色供选择# 定义小球属性
ball_radius = 20
ball_color = COLORS[0] # 初始颜色
ball_x, ball_y = random.randint(ball_radius, screen_width - ball_radius), random.randint(ball_radius, screen_height - ball_radius) # 随机初始位置
ball_dx, ball_dy = random.choice([-5, 5]), random.choice([-5, 5]) # 随机初始速度# 游戏时钟
clock = pygame.time.Clock()# 游戏循环
running = True
while running:# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = False# 更新小球位置ball_x += ball_dxball_y += ball_dy# 检查并处理小球与边界的碰撞if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:ball_dx = -ball_dx # 水平方向反弹ball_color = random.choice(COLORS) # 随机改变颜色if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:ball_dy = -ball_dy # 垂直方向反弹ball_color = random.choice(COLORS) # 随机改变颜色# 绘制屏幕screen.fill(BLACK) # 用黑色填充屏幕pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius) # 绘制小球# 更新显示pygame.display.flip()# 控制帧率clock.tick(60)# 退出 Pygame
pygame.quit()
sys.exit()
3.在运行过程中按下键盘的向上和向下箭头分别增加小球的运动速度和减小小球的运动速度
import pygame
import random
import sys# 初始化 Pygame
pygame.init()# 设置屏幕尺寸
screen_width, screen_height = 720, 540
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("小球反弹与变色及调速示例")# 定义颜色
BLACK = (0, 0, 0)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0), (138, 43, 226)] # 多种颜色供选择# 定义小球属性
ball_radius = 20
ball_color = COLORS[0] # 初始颜色
ball_x, ball_y = random.randint(ball_radius, screen_width - ball_radius), random.randint(ball_radius, screen_height - ball_radius) # 随机初始位置
ball_speed = 5 # 初始速度大小(绝对值),方向由 ball_dx 和 ball_dy 决定
ball_dx, ball_dy = random.choice([-ball_speed, ball_speed]), random.choice([-ball_speed, ball_speed]) # 随机初始速度方向# 游戏时钟
clock = pygame.time.Clock()# 游戏循环
running = True
while running:# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = False# 获取按键状态keys = pygame.key.get_pressed()if keys[pygame.K_UP] and ball_speed < 20: # 向上箭头增加速度,但不超过某个最大值ball_speed += 1ball_dx = random.choice([-ball_speed, ball_speed]) # 重新随机选择方向以保持一致性(可选)ball_dy = random.choice([-ball_speed, ball_speed]) # 同上elif keys[pygame.K_DOWN] and ball_speed > 1: # 向下箭头减小速度,但不小于某个最小值ball_speed -= 1ball_dx = random.choice([-ball_speed, ball_speed]) # 重新随机选择方向以保持一致性(可选)ball_dy = random.choice([-ball_speed, ball_speed]) # 同上# 更新小球位置ball_x += ball_dxball_y += ball_dy# 检查并处理小球与边界的碰撞if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:ball_dx = -ball_dx # 水平方向反弹ball_color = random.choice(COLORS) # 随机改变颜色if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:ball_dy = -ball_dy # 垂直方向反弹ball_color = random.choice(COLORS) # 随机改变颜色# 绘制屏幕screen.fill(BLACK) # 用黑色填充屏幕pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius) # 绘制小球# 更新显示pygame.display.flip()# 控制帧率clock.tick(60)# 退出 Pygame
pygame.quit()
sys.exit()
4.程序开始时界面增加一个game start按钮,鼠标单击松开后小球开始运动
import pygame
import random
import sys
import math# 初始化 Pygame
pygame.init()# 设置屏幕尺寸
screen_width, screen_height = 720, 540
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("小球反弹与变色及启动按钮示例")# 定义颜色
BLACK = (0, 0, 0)
GRAY = (169, 169, 169) # 按钮灰色
WHITE = (255, 255, 255) # 按钮文字白色
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0), (138, 43, 226)] # 多种颜色供选择# 定义按钮属性
button_width, button_height = 150, 50
button_x, button_y = (screen_width - button_width) // 2, (screen_height - button_height) // 2 + 100 # 居中位置,稍微偏上
button_rect = pygame.Rect(button_x, button_y, button_width, button_height)
button_font_size = 30
button_font = pygame.font.Font(None, button_font_size)
button_text = button_font.render("Game Start", True, WHITE)
button_text_rect = button_text.get_rect(center=button_rect.center)
show_button = True # 控制按钮是否显示的标志# 定义小球属性
ball_radius = 20
ball_color = COLORS[0] # 初始颜色
ball_x, ball_y = random.randint(ball_radius, screen_width - ball_radius), random.randint(ball_radius,screen_height - ball_radius) # 随机初始位置
ball_dx, ball_dy = 0, 0 # 初始速度
game_started = False # 游戏是否开始的标志# 游戏时钟
clock = pygame.time.Clock()# 游戏循环
running = True
while running:# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.MOUSEBUTTONDOWN:if show_button and button_rect.collidepoint(event.pos): # 检查鼠标是否点击在按钮上且按钮是否显示game_started = True # 游戏开始show_button = False # 隐藏按钮# 如果游戏已经开始,则更新小球位置和检查碰撞if game_started:if not (ball_dx or ball_dy): # 如果速度为0,则随机设置速度方向和大小ball_speed = random.randint(3, 10) # 随机速度大小angle = random.uniform(0, 2 * math.pi) # 随机角度ball_dx = int(ball_speed * math.cos(angle))ball_dy = int(ball_speed * math.sin(angle))ball_x += ball_dxball_y += ball_dy# 检查并处理小球与边界的碰撞if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:ball_dx = -ball_dx # 水平方向反弹ball_color = random.choice(COLORS) # 随机改变颜色if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:ball_dy = -ball_dy # 垂直方向反弹ball_color = random.choice(COLORS) # 随机改变颜色# 绘制屏幕screen.fill(BLACK) # 用黑色填充屏幕# 如果按钮应该显示,则绘制按钮if show_button:pygame.draw.rect(screen, GRAY, button_rect)screen.blit(button_text, button_text_rect)# 如果游戏已经开始,则绘制小球if game_started:pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius) # 绘制小球# 更新显示pygame.display.flip()# 控制帧率clock.tick(60)# 退出 Pygame
pygame.quit()
sys.exit()
5.在界面的右上角增加一个game over 结束按钮,为半透明,单击松开后结束此游戏
import pygame
import random
import sys
import math# 初始化 Pygame
pygame.init()# 设置屏幕尺寸
screen_width, screen_height = 720, 540
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("小球反弹与Game Over按钮")# 定义颜色
BLACK = (0, 0, 0)
GRAY = (169, 169, 169) # 按钮灰色
WHITE = (255, 255, 255) # 按钮文字白色
SEMI_TRANSPARENT_GRAY = (169, 169, 169, 128) # 半透明灰色
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 165, 0), (138, 43, 226)] # 多种颜色供选择# 定义按钮属性
button_width, button_height = 150, 50
start_button_x, start_button_y = (screen_width - button_width) // 2, (screen_height - button_height) // 2 + 100 # 居中位置,稍微偏上
start_button_rect = pygame.Rect(start_button_x, start_button_y, button_width, button_height)
game_over_button_x, game_over_button_y = screen_width - button_width - 10, 10 # 右上角位置
game_over_button_rect = pygame.Rect(game_over_button_x, game_over_button_y, button_width, button_height)
button_font_size = 30
button_font = pygame.font.Font(None, button_font_size)
start_button_text = button_font.render("Game Start", True, WHITE)
start_button_text_rect = start_button_text.get_rect(center=start_button_rect.center)
game_over_button_text = button_font.render("Game Over", True, WHITE)
game_over_button_text_rect = game_over_button_text.get_rect(center=game_over_button_rect.center)
show_start_button = True # 控制开始按钮是否显示的标志
game_started = False # 游戏是否开始的标志# 定义小球属性
ball_radius = 20
ball_color = COLORS[0] # 初始颜色
ball_x, ball_y = random.randint(ball_radius, screen_width - ball_radius), random.randint(ball_radius,screen_height - ball_radius) # 随机初始位置
ball_dx, ball_dy = 0, 0 # 初始速度# 游戏时钟
clock = pygame.time.Clock()# 游戏循环
running = True
while running:# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.MOUSEBUTTONDOWN:if show_start_button and start_button_rect.collidepoint(event.pos): # 检查鼠标是否点击在开始按钮上且按钮是否显示game_started = True # 游戏开始show_start_button = False # 隐藏开始按钮elif game_started and game_over_button_rect.collidepoint(event.pos): # 检查鼠标是否点击在游戏结束按钮上且游戏已开始running = False # 结束游戏# 如果游戏已经开始,则更新小球位置和检查碰撞if game_started:if not (ball_dx or ball_dy): # 如果速度为0,则随机设置速度方向和大小ball_speed = random.randint(3, 10) # 随机速度大小angle = random.uniform(0, 2 * math.pi) # 随机角度ball_dx = int(ball_speed * math.cos(angle))ball_dy = int(ball_speed * math.sin(angle))ball_x += ball_dxball_y += ball_dy# 检查并处理小球与边界的碰撞if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:ball_dx = -ball_dx # 水平方向反弹ball_color = random.choice(COLORS) # 随机改变颜色if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:ball_dy = -ball_dy # 垂直方向反弹ball_color = random.choice(COLORS) # 随机改变颜色# 绘制屏幕screen.fill(BLACK) # 用黑色填充屏幕# 如果开始按钮应该显示,则绘制开始按钮if show_start_button:pygame.draw.rect(screen, GRAY, start_button_rect)screen.blit(start_button_text, start_button_text_rect)# 绘制游戏结束按钮(始终显示,但只有在游戏开始后有效)pygame.draw.rect(screen, SEMI_TRANSPARENT_GRAY, game_over_button_rect)screen.blit(game_over_button_text, game_over_button_text_rect)# 如果游戏已经开始,则绘制小球if game_started:pygame.draw.circle(screen, ball_color, (ball_x, ball_y), ball_radius) # 绘制小球# 更新显示pygame.display.flip()# 控制帧率clock.tick(60)# 退出 Pygame
pygame.quit()
sys.exit()
以上程序基本都是文心一言自动生成,在程序中修改math.pi圆周率,基本成只有这个需要修改的