用pygame编写一个炫酷的动效小程序
以下是一个用pygame编写的炫酷动效小程序的示例代码:
import pygame
import random
# 初始化pygame
pygame.init()
# 定义窗口尺寸
WIDTH = 800
HEIGHT = 600
# 创建窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cool Animation")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义圆的初始位置和速度
circle_x = WIDTH // 2
circle_y = HEIGHT // 2
circle_speed_x = random.randint(-5, 5)
circle_speed_y = random.randint(-5, 5)
# 游戏循环
running = True
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 更新圆的位置
circle_x += circle_speed_x
circle_y += circle_speed_y
# 边界检测
if circle_x <= 0 or circle_x >= WIDTH:
circle_speed_x *= -1
if circle_y <= 0 or circle_y >= HEIGHT:
circle_speed_y *= -1
# 填充背景色
screen.fill(BLACK)
# 绘制圆
pygame.draw.circle(screen, WHITE, (circle_x, circle_y), 50)
# 刷新屏幕
pygame.display.flip()
# 退出游戏
pygame.quit()
这个示例程序创建了一个窗口,并在窗口中绘制一个移动的白色圆。圆的位置在窗口范围内随机生成,并且具有随机的速度。当圆碰到窗口边界时,速度会反向改变,从而实现了炫酷的动效。你可以根据自己的需要进行修改和扩展,加入更多的图形和动画效果
原文地址: https://www.cveoy.top/t/topic/irT3 著作权归作者所有。请勿转载和采集!