python-3.x - 我尝试在某个时间点在 pygame 中插入一个字符。我究竟做错了什么?
问题描述
我试图在某个时刻在我的窗口中显示一个角色。我究竟做错了什么?
这是我的第一个游戏,我是 pygame 的新手。我尝试了谷歌给我的代码。
screenwidth = 800
screenheight = 600
win = pygame.display.set_mode((screenwidth, screenheight))
bg = pygame.image.load("bg.png")
char = pygame.image.load("char.png")
x = 400
y = 300
vel = 5
isJumping = False
running = True
clock = pygame.time.Clock()
def redrawBackground():
win.blit(bg, (0, 0))
pygame.display.update()
def player(x, y):
win.blit(char, (x, y))
while running:
clock.tick(60)
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_one_x -= vel
if keys[pygame.K_RIGHT]:
player_one_x += vel
if not(isJumping):
if keys[pygame.K_SPACE]:
for i in range(10):
player_one_y -= vel
isJumping = True
player(x, y)
redrawBackground()
我希望这段代码的结果显示名为“char.png”的文件,但它没有显示出来。
解决方案
您的代码正在绘制播放器,然后用背景覆盖它:
while running:
#...
player(x, y)
redrawBackground()
这可能会更好:
def redrawBackground():
win.blit(bg, (0, 0))
# removed the call to pygame.display.update()
和
while running:
#...
redrawBackground()
player(x, y)
pygame.display.update()
推荐阅读
- three.js - 无法读取未定义的属性“isInterleavedBufferAttribute”
- java - 继承的重写 toString 方法没有使用正确的属性
- typescript - 为什么类构造函数的类型推断在while循环中不起作用
- c++ - 获取 clang++:错误:编译包含文件系统库的代码时
- c++ - 是否可以使用右值来初始化数据成员?
- mocking - 另一个类中的模拟方法
- ios - 如何更改数组中的所有元素?
- url-routing - 直接导航到子目录时未找到 Firebase 站点错误
- javascript - 单击功能按钮时,我的控制台出现 404 错误
- python - VGG16 模型的 OpenCV Python 图像预处理