用户:
加钢特尔查看:0 回复:1 评论:0 创建时间:2024-08-09T18:44:57
#游戏窗口 import pygame pygame.init() PlantsVsZombies = pygame.display.set_mode((1000,800))#设置窗口大小和名称 pygame.display.set_caption('PlantsVsZombies')
bg = pygame.image.load('./piz/map1.jpg')
while 1: for event in pygame.event.get(): if event.Type == pygame.QUIT or(event.Type == pygame.KEYDOWN and.key == pygame.K_ESCAPE): exit() pygame.display.update() 就是这个
import pygame
# 初始化 pygame
pygame.init()
# 创建窗口
PlantsVsZombies = pygame.display.set_mode((1000, 800)) # 设置窗口大小
pygame.display.set_caption('PlantsVsZombies') # 设置窗口标题
# 加载背景图像
bg = pygame.image.load('./piz/map1.jpg')
# 主循环
running = True # 使用一个布尔值来控制循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
running = False # 设置为 False 来退出循环
# 绘制背景
PlantsVsZombies.blit(bg, (0, 0)) # 将背景图像绘制到窗口
pygame.display.update() # 更新窗口
# 退出 pygame
pygame.quit()
修改和改进的地方:
event.type中,type应为小写,而不是Type。running来控制主循环的退出,而不是直接调用exit(),这样可以更好地管理程序的结束。PlantsVsZombies.blit(bg, (0, 0))来在窗口中绘制背景图像。pygame.quit()来正确关闭pygame。点赞0
评论