Pygame实现小游戏

好的,下面是一个使用 Pygame 制作简单游戏的例子:

import pygame# 初始化 Pygame
pygame.init()

# 设置窗口尺寸
screen_width, screen_height = 640, 480
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置窗口标题
pygame.display.set_caption('My Game')

# 设置背景颜色
bg_color = pygame.Color('grey12')

# 加载图像
image = pygame.image.load('player.png')

# 获取图像的矩形对象
rect = image.get_rect()

# 设置图像的位置
rect.center = (screen_width // 2, screen_height // 2)

# 定义一个布尔变量表示游戏是否结束
done = False

# 游戏主循环
while not done:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

    # 填充背景
    screen.fill(bg_color)

    # 绘制图像
    screen.blit(image, rect)

    # 更新屏幕
    pygame.display.flip()

# 退出 Pygame
pygame.quit()

这个例子中,我们使用 Pygame 创建了一个窗口,并在窗口中加载了一张图片,然后在每一帧中都将图片绘制到屏幕上。每一帧都会处理游戏事件,如果检测到退出事件,就会将 done 设置为 True,然后退出游戏循环。

希望这个例子对你有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值