Python 如何在此代码中添加背景而不出错? def game(): 运行=真 运行时: 屏幕填充((0,0,0)) mx,my=pygame.mouse.get_pos() 按钮3=pygame.Rect(3001001300700) 如果按钮_3.碰撞点((mx,my)): 如果单击: 开始 绘制文本(“游戏”,字体,(255,255,255),屏幕,20,20) 对于pygame.event.get()中的事件: 如果event.type==退出: pygame.quit() sys.exit() 如果event.type==KEYDOWN: 如果event.key==K_转义: 运行=错误 #设置pygame窗口名称 img1=pygame.image.load(r“C:\Users\ga sa\Downloads\As.png”) img2=pygame.image.load(r“C:\Users\ga sa\Downloads\AssetsXOR.png”) img3=pygame.image.load(r“C:\Users\ga sa\Downloads\AssetsNOT.png”) img4=pygame.image.load(r“C:\Users\ga sa\Downloads\AssetsAND.png”) 图像=[img1、img2、img3、img4] 当前图像=-1 img_rects=[images[i].get_rect(topleft=(20+40*i,20))表示范围内的i(len(images))] LeftButton=0 而1: 对于pygame.event.get()中的e: 如果e.type==退出: pygame.quit() 出口(0) 如果e.type==pygame.MOUSEBUTTONDOWN: mouse_rect=pygame.rect(e.pos,(1,1)) 当前图像=鼠标右键碰撞列表(img右键) 如果e.type==MOUSEMOTION: 如果e.按钮[LeftButton]: rel=e.rel 如果0

Python 如何在此代码中添加背景而不出错? def game(): 运行=真 运行时: 屏幕填充((0,0,0)) mx,my=pygame.mouse.get_pos() 按钮3=pygame.Rect(3001001300700) 如果按钮_3.碰撞点((mx,my)): 如果单击: 开始 绘制文本(“游戏”,字体,(255,255,255),屏幕,20,20) 对于pygame.event.get()中的事件: 如果event.type==退出: pygame.quit() sys.exit() 如果event.type==KEYDOWN: 如果event.key==K_转义: 运行=错误 #设置pygame窗口名称 img1=pygame.image.load(r“C:\Users\ga sa\Downloads\As.png”) img2=pygame.image.load(r“C:\Users\ga sa\Downloads\AssetsXOR.png”) img3=pygame.image.load(r“C:\Users\ga sa\Downloads\AssetsNOT.png”) img4=pygame.image.load(r“C:\Users\ga sa\Downloads\AssetsAND.png”) 图像=[img1、img2、img3、img4] 当前图像=-1 img_rects=[images[i].get_rect(topleft=(20+40*i,20))表示范围内的i(len(images))] LeftButton=0 而1: 对于pygame.event.get()中的e: 如果e.type==退出: pygame.quit() 出口(0) 如果e.type==pygame.MOUSEBUTTONDOWN: mouse_rect=pygame.rect(e.pos,(1,1)) 当前图像=鼠标右键碰撞列表(img右键) 如果e.type==MOUSEMOTION: 如果e.按钮[LeftButton]: rel=e.rel 如果0,python,pygame,pygame-surface,Python,Pygame,Pygame Surface,在绘制场景之前,您必须blit背景,并且必须在游戏循环中绘制。绘制背景时,它会覆盖以前绘制的所有内容: 画背景 绘制场景(所有对象) 更新显示 请注意,绘制背景图像时无需清除背景。如果背景图像覆盖整个窗口,则无需清除背景 屏幕填充(0) 运行时: # [...] 而1: 对于pygame.event.get()中的e: 如果e.type==退出: pygame.quit() 出口(0) # [...] #画背景 #screen.fill(0)在绘制场景之前,必须blit背景,并且必须在游戏循环中

在绘制场景之前,您必须
blit
背景,并且必须在游戏循环中绘制。绘制背景时,它会覆盖以前绘制的所有内容:

  • 画背景
  • 绘制场景(所有对象)
  • 更新显示
  • 请注意,绘制背景图像时无需清除背景。如果背景图像覆盖整个窗口,则无需清除背景

    屏幕填充(0)

    运行时:
    # [...]
    而1:
    对于pygame.event.get()中的e:
    如果e.type==退出:
    pygame.quit()
    出口(0)
    # [...]
    #画背景
    
    #screen.fill(0)在绘制场景之前,必须
    blit
    背景,并且必须在游戏循环中绘制。绘制背景时,它会覆盖以前绘制的所有内容:

  • 画背景
  • 绘制场景(所有对象)
  • 更新显示
  • 请注意,绘制背景图像时无需清除背景。如果背景图像覆盖整个窗口,则无需清除背景

    屏幕填充(0)

    运行时:
    # [...]
    而1:
    对于pygame.event.get()中的e:
    如果e.type==退出:
    pygame.quit()
    出口(0)
    # [...]
    #画背景
    
    #屏幕填充(0)我试过了,它没有工作。背景没有出现,我想把它当作背景,现在它工作了。谢谢试了,它没有工作。背景没有出现。我想把一个图像当作是它的背景现在它的工作。问题解决了吗?请阅读并考虑到你发现最有用的。问题解决了吗?请阅读并考虑到你发现最有用的。
    def game():
    running = True
    while running:
        screen.fill((0, 0, 0))
        mx, my = pygame.mouse.get_pos()
        button_3 = pygame.Rect(300, 100, 1300, 700)
        if button_3.collidepoint((mx, my)):
            if click:
                begin()
    
        draw_text('game', font, (255, 255, 255), screen, 20, 20)
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    running = False
    
    
        # set the pygame window name
    
        img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
        img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
        img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
        img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
    
        images = [img1, img2, img3, img4]
    
        current_image = -1
        img_rects = [images[i].get_rect(topleft=(20 + 40 * i, 20)) for i in range(len(images))]
    
        LeftButton = 0
        while 1:
            for e in pygame.event.get():
                if e.type == QUIT:
                    pygame.quit()
                    exit(0)
    
                if e.type == pygame.MOUSEBUTTONDOWN:
                    mouse_rect = pygame.Rect(e.pos, (1, 1))
                    current_image = mouse_rect.collidelist(img_rects)
    
                if e.type == MOUSEMOTION:
                    if e.buttons[LeftButton]:
                        rel = e.rel
                        if 0 <= current_image < len(images):
                            img_rects[current_image].x += rel[0]
                            img_rects[current_image].y += rel[1]
    
            screen.fill(0)
            for i in range(len(images)):
                screen.blit(images[i], img_rects[i])
            pygame.display.flip()
            pygame.time.delay(30)
    
        screen.blit(BACKGROUND_FASE, (0, 0))
        pygame.display.update()
        mainClock.tick(60)