【问题标题】:is there a way to hide parts of my python code on vscode? [duplicate]有没有办法在 vscode 上隐藏我的部分 python 代码? [复制]
【发布时间】:2021-02-08 05:14:17
【问题描述】:

有没有办法选择我的部分代码隐藏?我的意思是这样,我正在尝试使用 vscode 用 python 编写游戏,我想隐藏它的某些部分

import pygame, random, os
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1020, 585))
pygame.display.set_caption('2snakes!')

#files location
current_path = os.path.dirname(__file__)
data_path = os.path.join(current_path, 'data')
icon = pygame.image.load(os.path.join(data_path, 'icon.png'))
pygame.display.set_icon(icon)

#variables
direction = 'RIGHT'
direction2 = 'RIGHT'
change_to = direction
change2_to = direction2
fps = 12

#snake
size = 15
s_pos = 60
snake = [(s_pos + size * 2, s_pos),(s_pos + size, s_pos),(s_pos, s_pos)]
s_skin = pygame.Surface((size, size))
s_skin.fill((82,128,208))

#snake2
size2 = 15
s2_pos = 195
snake2 = [(s2_pos + size2 * 2, s2_pos),(s2_pos + size2, s2_pos),(s2_pos, s2_pos)]
s2_skin = pygame.Surface((size2, size2))
s2_skin.fill((208,128,82))

#apple
apple = pygame.Surface((size, size))
apple_pos = ((random.randint(0, 68) * 15, (random.randint(0, 39)) * 15))

#collission
def collision(c1,c2):
    return (c1[0] == c2[0]) and (c1[1] == c2[1])

#game over
def gameOverBlue():
    #pygame.quit()
    print("gameoverblue")

def gameOverRed():
    print("gameoverred")

#update
while True:
    
    #fps
    pygame.time.Clock().tick(fps)

    #quit game
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

        #input
        elif event.type == pygame.KEYDOWN:
            #snake
            if event.key == ord('w'):
                change_to = 'UP'
            if event.key == ord('s'):
                change_to = 'DOWN'
            if event.key == ord('a'):
                change_to = 'LEFT'
            if event.key == ord('d'):
                change_to = 'RIGHT'   
                    
            #snake2
            if event.key == pygame.K_UP:
                change2_to = 'UP'
            if event.key == pygame.K_DOWN:
                change2_to = 'DOWN'
            if event.key == pygame.K_LEFT:
                change2_to = 'LEFT'
            if event.key == pygame.K_RIGHT:
                change2_to = 'RIGHT'

            #quit game
            if event.key == pygame.K_ESCAPE:
                pygame.event.post(pygame.event.Event(pygame.QUIT))

    
    #smooth snake movement
        #snake
    if change_to == 'UP' and direction != 'DOWN':
        direction = 'UP'
    if change_to == 'DOWN' and direction != 'UP':
        direction = 'DOWN'
    if change_to == 'LEFT' and direction != 'RIGHT':
        direction = 'LEFT'
    if change_to == 'RIGHT' and direction != 'LEFT':
        direction = 'RIGHT'
        
        #snake2
    if change2_to == 'UP' and direction2 != 'DOWN':
        direction2 = 'UP'
    if change2_to == 'DOWN' and direction2 != 'UP':
        direction2 = 'DOWN'
    if change2_to == 'LEFT' and direction2 != 'RIGHT':
        direction2 = 'LEFT'
    if change2_to == 'RIGHT' and direction2 != 'LEFT':
        direction2 = 'RIGHT'

    #movement
        #snake
    new_pos = None
    if direction == 'DOWN':
        new_pos = (snake[0][0], snake[0][1] + size)
    if direction == 'UP':
        new_pos = (snake[0][0], snake[0][1] - size)
    if direction == 'LEFT':
        new_pos = (snake[0][0] - size, snake[0][1])
    if direction == 'RIGHT':
        new_pos = (snake[0][0] + size, snake[0][1])
    if new_pos:
        snake = [new_pos] + snake
        del snake[-1]
    
    new_pos2 = None
    if direction2 == 'DOWN':
        new_pos2 = (snake2[0][0], snake2[0][1] + size2)
    if direction2 == 'UP':
        new_pos2 = (snake2[0][0], snake2[0][1] - size2)
    if direction2 == 'LEFT':
        new_pos2 = (snake2[0][0] - size2, snake2[0][1])
    if direction2 == 'RIGHT':
        new_pos2 = (snake2[0][0] + size2, snake2[0][1])
    if new_pos2:
        snake2 = [new_pos2] + snake2
        del snake2[-1]

   #snake apple collision
    if collision(snake[0], apple_pos):
        snake.append((-20,-20))
        apple_pos = ((random.randint(0, 68) * 15, (random.randint(0, 39)) * 15))
    #snake2 apple collision
    if collision(snake2[0], apple_pos):
        snake2.append((-20,-20))
        apple_pos = ((random.randint(0, 68) * 15, (random.randint(0, 39)) * 15))

    #snake wall collisison
    if snake[0][0] < 0 or snake[0][1] < 0:
        gameOver()
    elif snake[0][0] > 1020 or snake[0][1] > 585:
        gameOver()

    #snake2 wall collisison
    if snake2[0][0] < 0 or snake2[0][1] < 0:
        gameOverRed()
    elif snake2[0][0] > 1020 or snake2[0][1] > 585:
        gameOverRed()

    #self collisison
        if snake[0] in snake[1:]:
            print("self collision")
            print(snake2[0], "is in", snake2[2:])
    if snake2[0] in snake2[1:]:
            print("self collision")
            print(snake2[0], "is in", snake2[2:])

    #rendering
    apple.fill((255,0,0))
    screen.fill((0,0,0))
    screen.blit(apple,apple_pos)
    for pos in snake:
        screen.blit(s_skin,pos)
    for pos2 in snake2:
        screen.blit(s2_skin,pos2)
    pygame.display.update()

这是我的代码,#smooth snake movement 中的代码是我想要隐藏的部分,是一大段代码,当我在其中寻找一些错误或东西时,它只会让我感到困惑代码

def collision(c1,c2):
    return (c1[0] == c2[0]) and (c1[1] == c2[1])

我想像在 vscode 中使用 def 一样隐藏它,有没有办法做到这一点?

【问题讨论】:

  • 您可以做一些refactoring 并将您的代码分散在不同的文件中,这是常见的做法。
  • 这个问题与 pygame 或 python 无关。问题是如何隐藏部分源代码。

标签: visual-studio-code


【解决方案1】:

是的,当您在选项卡下有代码时: 代码 更多代码 更多代码 您可以点击左侧的“代码”附近,它会自动缩小里面的所有代码(在这种情况下是“更多代码”和“更多代码”。

您也可以尝试在 vscode 左侧的“扩展”按钮上找到代码收缩器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2020-03-07
    • 1970-01-01
    相关资源
    最近更新 更多