【问题标题】:Importing a Pygame project into another pygame project将一个 Pygame 项目导入另一个 pygame 项目
【发布时间】:2021-09-06 12:37:54
【问题描述】:

目前,我正在尝试创建一个小菜单系统以协助 pygame 项目,我想要菜单做的只是按下 ENTER/RETURN,我的游戏代码将打开运行,此时只要导入pygame文件就会立即打开。

这是我目前的代码:

import pygame
import sys
import bruh

pygame.init()
display = pygame.display.set_mode((800,800))
run = False
clock = pygame.time.Clock()
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
font = pygame.font.Font('American Captain.ttf', 32)
user_text = ''
rect = pygame.Rect(200,200,140,32)
color = pygame.Color('lightskyblue3')



while True:

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RETURN]:
        bruh
    pygame.draw.rect(display,color, rect, 2)

    display.fill((0, 0, 0))
    text_surface = font.render(user_text,True,(255,255,255))
    display.blit(text_surface, (rect.x+5, rect.y+5))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_BACKSPACE:
            user_text = user_text[:-1]
        else:
            user_text +=  event.unicode
    clock.tick(60)
    title = font.render("BEATDOWN!", True, (255,255,255))
    text_pos = (180,50)
    display.blit(title, text_pos)

    clock.tick(60)
    pygame.display.flip()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    你的问题有点难以理解,但我认为你想要这样的东西:

    原文:

    import pygame
    import sys
    # import bruh  # run the menu <- you dont want this
    
    ...
    
    while True:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RETURN]:
            import bruh   # run the menu <- you want this
    
    ...
    

    或者这个:

    在菜单文件中:

    def run():
        # do whatever
    

    在原文中:

    import pygame
    import sys
    import bruh  # import <- you want this
    
    ...
    
    while True:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RETURN]:
            bruh.run()  # run the menu <- and this
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多