Python游戏制作
时间: 2023-11-29 09:45:55 浏览: 37
根据提供的引用内容,可以使用Pygame库来制作Python游戏。下面是一个简单的示例代码,可以创建一个窗口并在窗口中显示一个小球:
```python
import pygame
# 初始化Pygame
pygame.init()
# 设置窗口标题和大小
pygame.display.set_caption("My Game")
screen = pygame.display.set_mode((800, 600))
# 设置小球的初始位置和速度
x = 50
y = 50
vx = 5
vy = 5
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 移动小球
x += vx
y += vy
# 碰到边界反弹
if x < 0 or x > 800:
vx = -vx
if y < 0 or y > 600:
vy = -vy
# 填充背景色
screen.fill((255, 255, 255))
# 在窗口中绘制小球
pygame.draw.circle(screen, (255, 0, 0), (x, y), 20)
# 更新窗口
pygame.display.flip()
# 退出Pygame
pygame.quit()
```