用户:
上知天文查看:7 回复:7 评论:7 创建时间:2023-04-04T22:10:44
#coding : utf - 8
import pygame
#pygame初始化
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption("文明征战")
window = []
w = str(window)
#导入 / 判断存档
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(w)
else:
window = list(content)
#定义图片
bg = pygame.image.load("images/背景.png")
image_list = [bg]
#设置背景移动
def bg_xy(bg_image,bg_x,bg_y):
#背景设置
x , y = 0 , 0
if event.key == pygame.K_W and pygame.KEYUP:
y -= 1
canvas.blit(bg_image,(bg_x+x, bg_y+y))
elif event.key == pygame.K_S and pygame.KEYUP:
y += 1
canvas.blit(bg_image,(bg_x+x, bg_y+y))
elif event.key == pygame.K_A and pygame.KEYUP:
x -= 1
canvas.blit(bg_image, (bg_x + x, bg_y + y))
elif event.key == pygame.K_D and pygame.KEYUP:
x += 1
canvas.blit(bg_image,(bg_x + x, bg_y + y))
else:
canvas.blit(bg_image,(bg_x + x, bg_y + y))
#主循环
while True:
#设置移动背景
bg_xy(bg,0,0)
#设置窗口关闭
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
#设置窗口主循环
pygame.display.update()pygame.init()没有被调用。
存档读取后的判断存档内容的操作没有写在游戏主循环之前。
bg_xy() 函数中 if 语句的条件不正确,应该是 if event.type == pygame.KEYDOWN,而不是 if event.key == pygame.K_W and pygame.KEYUP: 等等。并且,事件应该在主循环中获取。
canvas.blit(bg_image,(bg_x+x, bg_y+y)) 中的 (bg_x + x, bg_y + y) 应该只传入一个元组参数,而不是两个加号连接的表达式。
以下是修正后的代码,你可以参考一下:
# coding:utf-8
import pygame
# pygame初始化
pygame.init()
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption("文明征战")
window = []
w = str(window)
# 导入 / 判断存档
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(w)
else:
window = list(content)
# 定义图片
bg = pygame.image.load("images/背景.png")
image_list = [bg]
# 设置背景移动
def bg_xy(bg_image, bg_pos):
# 背景设置
x, y = bg_pos
canvas.blit(bg_image, (x, y))
# 主循环
while True:
# 设置移动背景
bg_xy(bg, (0, 0))
# 设置窗口关闭
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 检测键盘事件,控制背景移动
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
bg_pos_y = bg_pos_y - 5
elif event.key == pygame.K_s:
bg_pos_y = bg_pos_y + 5
elif event.key == pygame.K_a:
bg_pos_x = bg_pos_x - 5
elif event.key == pygame.K_d:
bg_pos_x = bg_pos_x + 5
# 设置窗口主循环
pygame.display.update()点赞0
评论
小麦做的面包#coding : utf - 8
import pygame
#pygame初始化
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption("文明征战")
window = []
w = str(window)
#导入 / 判断存档
try:
with open('存档.txt', 'r', encoding='utf-8') as f:
window = list(f.read())
except FileNotFoundError:
with open('存档.txt', 'w', encoding='utf - 8') as f:
f.write(w)
#定义图片
bg = pygame.image.load("images/背景.png")
image_list = [bg]
#设置背景移动
def bg_xy(bg_image,event):
global bgx,bgy
if event.key == pygame.K_w and pygame.KEYUP:
bgy -= 10
elif event.key == pygame.K_s and pygame.KEYUP:
bgy += 10
elif event.key == pygame.K_a and pygame.KEYUP:
bgx -= 10
elif event.key == pygame.K_d and pygame.KEYUP:
bgx += 10
canvas = pygame.display.set_mode((800, 600))
canvas.blit(bg_image, (bgx, bgy))
bgx,bgy=0,0
#主循环
while True:
#设置窗口关闭
for event in pygame.event.get():
#设置移动背景
if event.type == pygame.KEYDOWN:
bg_xy(bg,event)
if event.type == pygame.QUIT:
exit()
#设置窗口主循环
pygame.display.update()
1.楼主的报错,把event作为了bg_xy的一个参数,把bg_xy的调用放到了循环每一个event里
2.你每次调用bg_xy都把x,y设置为0,0,所以背景不会动,把bg_x,bg_y设置为了全局变量直接改背景的坐标
3.关于开头初始化,你原来用的是判断content是否为空,但是文件打开失败会直接报错而不是返回空,改为了try except
4.移动背景的时候会有重影,在每次设置背景之前都初始化一下:
canvas = pygame.display.set_mode((800, 600))
清空原来的背景
点赞0
评论
Ok啊,改完了
# coding:utf-8
import pygame
# pygame初始化
pygame.init()
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption("文明征战")
window = []
w = str(window)
# 导入 / 判断存档
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_pos_x = 0
bg_pos_y = 0
# 定义图片
bg = pygame.image.load("images/背景.png")
image_list = [bg]
# 设置背景移动
def bg_xy(bg_image, bg_pos):
# 背景设置
x, y = bg_pos
canvas.blit(bg_image, (x, y))
# 主循环
while True:
# 设置移动背景
bg_xy(bg, (bg_pos_x, bg_pos_y))
# 设置窗口关闭
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 检测键盘事件,控制背景移动
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
bg_pos_y = bg_pos_y - 5
elif event.key == pygame.K_s:
bg_pos_y = bg_pos_y + 5
elif event.key == pygame.K_a:
bg_pos_x = bg_pos_x - 5
elif event.key == pygame.K_d:
bg_pos_x = bg_pos_x + 5
# 设置窗口主循环
pygame.display.update()
点赞0
评论