【问题标题】:fuction not working properly,python,pygame [duplicate]功能无法正常工作,python,pygame [重复]
【发布时间】:2021-01-06 00:53:37
【问题描述】:

它没有显示任何错误,只是在显示底部显示空白而不是 printng 开始

import pygame
import time

pygame.init()

black = (0,0,0)
white = (255,255,255)

Display = pygame.display.set_mode((800,600))
pygame.display.set_caption('fitness')

font = pygame.font.SysFont(None,200)


def message_to_screen(msg,color):
    screen_text = font.render(msg,True,color)
    Display.blit(screen_text, [800/2, 600/2])

exercises =["pelvic shift","hanging","single leg hopping","puppy pose","side strech","low lunge                                                                               arcs",  "cobra strech","jogging,skipping","vertical bends","standing strechs","side bends","swimming","toe lifts","land swimming" ,"legs up alternate", "leg kicks" , "wake up stretchinggutes and hip bridges","forward spine stretch","cat camel back stetch","mermaid stretch","cycling","jump squats","cobra stretch","downward facing dog"  ,   "bird dog", "inversion table exercise", "surya namaskars" , "side planks"]             


def exerciseLoop():  
    for x in exercises:
        for number_of_exercise in range(1,29):   
            z = exercises.index(x)
            if number_of_exercise == (z+1):
                final={}
                final.update({number_of_exercise:x})
                message_to_screen("start",black)
                clock = pygame.time.Clock()
                FPS = 30
                clock.tick(FPS)
                Display.fill(white)
                pygame.display.update()
    

exerciseLoop()

【问题讨论】:

  • 可能number_of_exercise 不等于z + 1
  • 你已经问了两次同一个问题:stuck in an infinite loop,python,pygame。你不应该那样做。改进原来的问题,而不是两次问同一个问题。

标签: python function loops for-loop pygame


【解决方案1】:

白色画在文字上方。你需要先把它涂成白色,然后再写文字:

import pygame
import time

pygame.init()

black = (0,0,0)
white = (255,255,255)

Display = pygame.display.set_mode((800,600))
pygame.display.set_caption('fitness')

font = pygame.font.SysFont(None,200)


def message_to_screen(msg,color):
    screen_text = font.render(msg,True,color)
    Display.blit(screen_text, [800/2, 600/2])

exercises =["pelvic shift","hanging","single leg hopping","puppy pose","side strech","low lunge                                                                               arcs",  "cobra strech","jogging,skipping","vertical bends","standing strechs","side bends","swimming","toe lifts","land swimming" ,"legs up alternate", "leg kicks" , "wake up stretchinggutes and hip bridges","forward spine stretch","cat camel back stetch","mermaid stretch","cycling","jump squats","cobra stretch","downward facing dog"  ,   "bird dog", "inversion table exercise", "surya namaskars" , "side planks"]             


def exerciseLoop():  
    for x in exercises:
        for number_of_exercise in range(1,29):   
            z = exercises.index(x)
            if number_of_exercise == (z+1):
                final={}
                final.update({number_of_exercise:x})
                Display.fill(white)
                message_to_screen("start",black)
                clock = pygame.time.Clock()
                FPS = 30
                clock.tick(FPS)
                pygame.display.update()
    

exerciseLoop()

【讨论】:

    【解决方案2】:

    您必须在应用程序循环中处理事件。分别见pygame.event.get()pygame.event.pump()

    对于游戏的每一帧,您都需要对事件队列进行某种调用。这可确保您的程序可以在内部与操作系统的其余部分进行交互。

    def exerciseLoop():  
         
        clock = pygame.time.Clock()
        FPS = 30
        run = True
        while run:
            clock.tick(FPS)
            for event in pygame.event.get(): 
                if event.type == pygame.QUIT:
                    run = False
    
            for x in exercises:
                for number_of_exercise in range(1,29):  
                    
                    z = exercises.index(x)
                    if number_of_exercise == (z+1):
                        final={}
                        final.update({number_of_exercise:x})  
                    
            Display.fill(white)
            message_to_screen("start",black)
                    
            pygame.display.update()
        
    exerciseLoop()
    

    典型的 PyGame 应用程序循环必须:

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      相关资源
      最近更新 更多