【问题标题】:Pygame get stuckPygame卡住了
【发布时间】:2023-02-09 04:34:06
【问题描述】:

我有我在 visual studio code 中运行的代码,扩展名为 pygame,但我有很多滞后!我不知道该怎么办,这是代码:

import pygame, sys

pygame.init()
clock = pygame.time.Clock()

screen_width = 900
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Hungry Plant')

bird = pygame.Rect(screen_width/2 - 15,screen_height/2 - 15,30,30)
player = pygame.Rect(screen_width/2 - 25, screen_height/1.20,50,300)

bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
light_green = (144, 238, 144)

bird_speed_x = 7

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        bird.x += bird_speed_x

        if bird.left <= 0 or bird.right >= screen_width:
            bird_speed_x *= -1

        screen.fill(bg_color)
        pygame.draw.rect(screen,light_green, player)
        pygame.draw.ellipse(screen, light_grey, bird)

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

我试图在网上找到解决方案,但没有成功!

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    这是Indentation的事情。您必须在应用程序循环中绘制场景,而不是在事件循环中:

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
    
        # indentation
        #<--|
    
        bird.x += bird_speed_x
    
        if bird.left <= 0 or bird.right >= screen_width:
            bird_speed_x *= -1
    
        screen.fill(bg_color)
        pygame.draw.rect(screen,light_green, player)
        pygame.draw.ellipse(screen, light_grey, bird)
    
        pygame.display.flip()
        clock.tick(30)
    

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 1970-01-01
      • 2012-06-23
      • 2021-10-08
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多