Python 语法和 Pygame 的问题

Problems with Python syntax and Pygame

我正在开发一款游戏,遇到了一些(至少对我而言)无法解决的问题。

我的代码存在的问题是:

  1. 当询问您是否要保存屏幕截图时,仅输入任何可以通过 lower() 方法转换为 "yes""no" 的内容尽管 or 语句将被接受 - 该代码块中的条件之一是 if save_option.lower() == ("yes" or "y"):,它将只接受 "yes",否则条件被评估为 False。我不知道这是为什么。

  2. def introduction(): 中渲染游戏介绍时,由于某种原因,每个表面对象都被绘制了两次。这是它的样子: 但是,当我将每帧移动的像素增加到 60 时,会发生这种情况:

  3. 当“送出他所有的礼物”到达屏幕顶部时,introduction()结束,主游戏循环开始。 可以在这段代码中看到与此相关的任何逻辑错误:

    而不是 pygame.sprite.Group.has(intro_story_surface_objects): 对于 pygame.event.get() 中的事件: 如果 event.type == QUIT 或 event.type == KEYDOWN 和 event.key
    == K_ESCAPE: 销毁()

    DS.blit(BACKGROUND, (0, 0))
    pygame.draw.rect(DS, (0, 0, 0, 50), (0, DISPLAY_HEIGHT - 200, DISPLAY_WIDTH, 200), 0) # (screen, color, (x,y,width,height), thickness)
    
    count = 0
    for line in intro_story_surface_objects:
    
        line_x_y = (intro_story_surface_objects[line].rect.x, intro_story_surface_objects[line].rect.y)
        intro_story_sprite_group.draw(DS)
        intro_story_surface_objects[line].rect.y -= 60 # intro_story_surface_objects[line].speed
    
        if intro_story_surface_objects[line].rect.y + intro_story_surface_objects[line].rect.height < 0:
            intro_story_surface_objects[line].kill()
            # print("Kill")
            line_kills += 1
    
            if line_kills == len(intro_story_surface_objects):
                return
    
    
    pygame.display.update()
    clock.tick(30)
    

再次感谢您的帮助。

来自基兰

  1. 对于问题一。尝试: if save_option.lower() in ['yes', 'y']:

括号中的 or 语句是这样说的:如果第一个值的计算结果为假,则与第二个值进行比较。由于字符串 'yes' 始终是 True save_option.lower() 永远不会与 'y'.

进行比较

很遗憾,剩下的我无能为力。我没用过Pygame。不过祝你好运!祝游戏愉快。

回答我自己的问题 2.

除此之外,我还需要回答另外 2 个问题。

执行代码时,文本看起来像是被绘制了两次。问题是每次更改每行文本的 x 和 y 坐标时,整个行列表都会被绘制两次。

只是一个小意外。我的代码的结构如下:

  • while there are lines of text:
  • draw all the lines in this command - intro_story_sprite_group.draw(DS)
  • loop through each line:
  • Change its (x, y)

不要这样做:

  • while there are lines of text:
  • loop through each line:
  • draw all the lines in this command - intro_story_sprite_group.draw(DS)
  • Change its (x, y)

请不要忘记我还需要回答 2 个问题! 感谢您的帮助,

基兰