2012-08-31 24 views
2

我執行下面的代碼,只是得到一個空白(黑色)窗口。Python pygame新手代碼顯示空白屏幕

窗口標題顯示,但我還沒有得到圖像加載(我試圖使用其他圖像比使用的圖片)。 .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() 

我已經用pygame 1.9.2安裝了python 3.2。如果我無法得到這個工作,我會考慮卸載並安裝3.1 + 1.9.1。

回答

3

你應該把代碼內環路,並使用時鐘,以避免使用所有的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 
0

別的東西,你可能想要做的,你把Y = - 在一個點和X- =就在它旁邊。我不認爲你打算這麼做。

0

提示:使用Rect時,可以簡化width/2。他們有其他有用的動態性能也:http://www.pygame.org/docs/ref/rect.html(寬度,中心的centerX,左上,等...)

代碼:

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() 
0

你有沒有嘗試添加

pygame.display.flip(screen)? 

還需要更新

pygame.display.update(screen)