【问题标题】:Python Pygame Newbie Code Showing Blank ScreenPython Pygame 新手代码显示空白屏幕
【发布时间】:2012-08-31 23:00:56
【问题描述】:

我执行以下代码并得到一个空白(黑色)窗口。

窗口标题显示,但我还没有加载图像(我尝试使用其他图像而不是使用的图像)。 .py 文件和图片在同一目录下。

background_image_filename='checkmark.jpg'
mouse_image_filename='digestive_bw.png'
import pygame, sys
from pygame.locals import*
from sys import exit


pygame.init()

screen=pygame.display.set_mode((800,800),0,32)
#pygame.display.set_caption("Hello, Howdy, Mate, and Hi there world!")


background=pygame.image.load(background_image_filename).convert()
mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

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

screen.blit(background,(0,0))

x,y=pygame.mouse.get_pos()
x-=mouse_cursor.get_width() /2
y=-mouse_cursor.get_height() /2
screen.blit(mouse_cursor,(x,y))

pygame.display.update()

我已经安装了 python 3.2 和 pygame 1.9.2。如果我不能让它工作,我会考虑卸载这些并安装 3.1 + 1.9.1。

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    你应该把代码放在循环中,并使用时钟来避免使用所有的cpu:

    clock = pygame.time.Clock()
    
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
    
        screen.blit(background,(0,0))
    
        x,y=pygame.mouse.get_pos()
        x-=mouse_cursor.get_width() /2
        y=-mouse_cursor.get_height() /2
        screen.blit(mouse_cursor,(x,y))
    
        pygame.display.update()
        clock.tick(30)  # keep 30 fps
    

    【讨论】:

      【解决方案2】:

      你可能想做的其他事情,你把 y=- 放在一个点上,把 x-= 放在它旁边。我不认为你打算这样做。

      【讨论】:

        【解决方案3】:

        提示:您可以在使用Rect 时简化width / 2。它们还有其他有用的动态属性:http://www.pygame.org/docs/ref/rect.html(宽度、中心、centerx、topleft 等...)

        代码:

        mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()
        mouse_rect = mouse_cursor.get_rect()
        
        mouse_rect.center = pygame.mouse.get_pos()
        screen.blit(mouse_cursor, mouse_rect)
        pygame.display.update()
        

        这是:

        mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()
        
        x,y=pygame.mouse.get_pos()
        x-=mouse_cursor.get_width() /2
        y=-mouse_cursor.get_height() /2
        screen.blit(mouse_cursor,(x,y))
        pygame.display.update()
        

        【讨论】:

          【解决方案4】:

          你有没有尝试添加

          pygame.display.flip(screen)?
          

          还需要更新

          pygame.display.update(screen) 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-18
            相关资源
            最近更新 更多