首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在游戏窗口和函数之间进行转换

如何在游戏窗口和函数之间进行转换
EN

Stack Overflow用户
提问于 2020-07-07 09:54:10
回答 2查看 301关注 0票数 2

更新代码:

代码语言:javascript
复制
import pygame

pygame.init()

width = 700
height = 700
running = True
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Dijkstra's Path-Finding Algorithm Solver")
icon = pygame.image.load('icon.jpg')
pygame.display.set_icon(icon)

def title_main():
    title_font = pygame.font.Font('TrajanPro-Regular.otf', 40)
    text_font = pygame.font.SysFont("comicsans", 30, bold=True)
    Dijkstra_img = pygame.image.load('Dijkstra.jpg')
    text1_display = title_font.render("Dijkstra's Path-Finding", True, (255, 255, 255))
    text2_display = title_font.render('Algorithm', True, (255, 255, 255))
    text3_display = text_font.render('Press any key to continue...', True, (255, 255, 255))
    screen.blit(Dijkstra_img, (225, 40))
    screen.blit(text1_display, (75, 400))
    screen.blit(text2_display, (225, 460))
    screen.blit(text3_display, (190, 550))

def title_game():
    title_font = pygame.font.Font('TrajanPro-Regular.otf', 35)
    title_display = title_font.render('Dijkstra Path-Finding Algorithm', True, (255, 255, 255))
    screen.blit(title_display, (12, 15))

def title_underline():
    rect = pygame.Rect(0, 60, 800, 3)
    rect_display = pygame.draw.rect(screen, [255, 255, 255], rect)
    title_font = pygame.font.Font('TrajanPro-Regular.otf', 100)
    title_display = title_font.render('', True, (255, 255, 255))
    screen.blit(title_display, (270, 198))

def grid():
    grid_x = 0
    grid_y = 63
    blockSize = 20
    for x in range(width):
        for y in range(height):
            rect = pygame.Rect(x*blockSize + grid_x, y*blockSize + grid_y, blockSize, blockSize)
            pygame.draw.rect(screen, (200,200,200), rect, 1)

def game_loop():
     game_is_active = True
     while game_is_active:
         for game_event in pygame.event.get():
            if game_event.type == pygame.QUIT:
                game_is_active = False
            if game_event.type == pygame.MOUSEBUTTONDOWN:
                start = print(pygame.mouse.get_pos())
                print(start)
         screen.fill((0, 0, 0))
         title_game()
         title_underline()
         grid()
         pygame.display.update()

while running:
    for event in pygame.event.get():
        screen.fill((0, 0, 0))
        title_main()
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            running = game_loop()
        pygame.display.update()

我已经在启动GUI时运行的函数下为我的项目创建了一个标题页,但我想做的是,当用户按下任何键时,我不想再显示title_main()函数,而只是显示我的主应用程序。我使用了KEYDOWN来尝试这样做,但是由于某种原因,它的速度非常慢,没有响应性,而且还显示了title_main()函数。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-07 23:08:46

@Cribber解释了为什么您现有的循环不能工作。

我想给出一个只使用单个事件循环的答案,因为我认为拥有一个简单的主循环是事件驱动程序(即:所有PyGame程序)的最佳架构。

您的程序分为两种状态,显示“标题页”和“主应用程序”。我会将这个状态保持在一个变量中,并根据它在主循环中确定要走的路径。在绘制→输入→更新周期的每一步,检查状态(必要时)并根据值采取行动。

代码语言:javascript
复制
# Function definitions as per question

# States the app can be in
TITLE_PAGE = 10   # TODO: maybe use an Enumerated Type
MAIN_APP   = 20

app_state = TITLE_PAGE  # start with the titles

while running:
    # Handle user input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if ( app_state == TITLE_PAGE ):
                # user dismissed title page, switch to app-mode
                app_state = MAIN_APP

    # Update the screen
    screen.fill((0, 0, 0))
    if ( app_state == TITLE_PAGE ):   # In Title-page mode?
        title_main()
    elif ( app_state == MAIN_APP ):   # In Main-Application mode?
        title_game()
        title_underline()
        grid()

    pygame.display.update()

当然,当它非常简单的时候,它的实现方式并不是什么大不了的。但是,随着程序变得越来越复杂,它有助于在简单的步骤中规划出逻辑流。没有重复的事件循环代码块或屏幕绘制,因此(例如)如果有用户输入错误,它只能出现在一个地方。

编辑:处理第一次和第二次鼠标单击

这是相当容易处理两个鼠标点击。只需要记住点击计数,以及“上一次”点击的位置。

这可以通过存储一个初始化变量first_click来实现,但从None值开始。“无”很重要,因为它表示还没有单击。因此它跟踪第一次/第二次点击,以及第一次点击的位置。

所以..。

  • ,如果first_click == None→用户尚未单击
  • ,如果first_click != None→首先单击OK,等待第二。

在您的代码中,first_click被称为start。它只需要初始化为零。

代码语言:javascript
复制
start = None   # no clicks yet

def game_loop():
     game_is_active = True
     while game_is_active:
         for game_event in pygame.event.get():
            if game_event.type == pygame.QUIT:
                game_is_active = False
            if game_event.type == pygame.MOUSEBUTTONDOWN:
                # Mouse has been clicked
                if ( start == None ):             # Is this a first click?
                    # Save the First click 
                    start = pygame.mouse.get_pos()      
                else:
                    # This is a second click, process both
                    handleTwoClicks( start, pygame.mouse.get_pos() )
                    # reset back to no-clicks-made
                    start = None
票数 2
EN

Stack Overflow用户

发布于 2020-07-07 11:14:12

只有当按下按钮时,if event.type == pygame.KEYDOWN:下面的块中的行才会执行--而且每个KEYDOWN只执行一次!

由于在初始pygame.KEYDOWN之后没有单独的循环,所以在函数title_game()title_underline()grid()执行一次之后,while running:会继续。然后循环重新启动,函数title_main()也被再次执行(这就是为什么它仍然被显示)。

我认为您可能希望在注册KEYDOWN之后实现另一个循环。

您还必须单独捕获此循环中的输入,因为您不会“返回”到for event in pygame.event.get():循环中的while running:

当我理解了您的问题时,我包含了一个更干净的代码版本,并在展示了您的title_main()之后实现了一个游戏循环。

代码语言:javascript
复制
while running:  # your loop - display the title and wait for a button to be pressed
    for event in pygame.event.get():  
        screen.fill((0, 0, 0))
        title_main()
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN: 
              # start the actual game upon pressing a button for the first time
              running = game_loop() # stop the loop if the game returns with False
        pygame.display.update()
    

def game_loop():
     game_is_active = True
     while game_is_active :
         for game_event in pygame.event.get():
            if game_event.type == pygame.QUIT:
                  return False  # return to your outer loop
         # game logic - display the title, the underline and the grid: 
         screen.fill((0, 0, 0))
         title_game()
         title_underline()
         grid()
         pygame.display.update() # update screen
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62772610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档