用户:
上知天文查看:0 回复:0 评论:0 创建时间:2023-04-09T15:35:31
# coding:utf-8
import pygame
import time
# pygame初始化
pygame.init()
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption("文明")
EarthWorld = []
resources = [0,0,0]#[0]电能 [1]粮食[2]铁
window = ['100','100',EarthWorld,resources]#window[0]和window[1]为部落大小的长和宽
w = str(window)
#定义函数资源
def time():
for i in EarthWorld:
if i == 'water':
return
elif i == 'sun':
resources[0] += 1
time.sleep(1)
# 导入 / 判断存档
with open('存档.txt', 'r', encoding='utf-8') as f:
content = f.read()
if not content:
with open('存档.txt', 'w', encoding='utf-8') as f:
f.write(str(window)) # 需要用 str() 转换为字符串后再写入文件
else:
window = eval(content) # 注意使用 eval() 将字符串转换为列表
#加载图片
bg = pygame.image.load('images/bg.png')
bg_x = [0]
bg_y = [0]
# 主循环
while True:
canvas.blit(bg,(bg_x[0],bg_y[0]))
#设置背景移动
if event.type == pygame.KEYDOWN:
i = 0
if event.key == pygame.K_w:
for ba in bg_y:
if ba >= 600:
bg_y[i] = -600
else:
bg_y[i] -= 5
i += 1
elif event.key == pygame.K_s:
for ba in bg_y:
if ba <= -600:
bg_y[i] = 600
else:
bg_y[i] += 5
i += 1
elif event.key == pygame.K_a:
for ba in bg_x:
if ba >= 600:
bg_x[i] = -800
else:
bg_x[i] -= 5
i += 1
# 设置窗口关闭
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 设置窗口主循环
pygame.display.update()
报错信息:
D:\L1A\venv\Scripts\python.exe C:/Users/Administrator/Desktop/文明/主界面/主界面.py
pygame 2.1.0 (SDL 2.0.16, Python 3.6.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/文明/主界面/主界面.py", line 41, in <module>
if event.type == pygame.KEYDOWN:
NameError: name 'event' is not defined
进程已结束,退出代码1
背景