2012-06-18 104 views
0

我想在pygame中顯示一個遊戲。但是由於某種原因,這不起作用,有什麼想法?這裏是我的代碼:Python Pygame圖像不顯示

import pygame, sys 
from pygame.locals import * 
pygame.init() 
screen=pygame.display.set_mode((640,360),0,32) 
pygame.display.set_caption("My Game") 
p = 1 
green = (0,255,0) 
pacman ="imgres.jpeg" 
pacman_x = 0 
pacman_y = 0 
while True: 
    pacman_obj=pygame.image.load(pacman).convert() 
    screen.blit(pacman_obj, (pacman_x,pacman_y)) 
    blue = (0,0,255) 
    screen.fill(blue) 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type==KEYDOWN: 
      if event.key==K_LEFT: 
       p=0 
    pygame.display.update() 
+1

>「它不會出於某種原因」 你有什麼更具體一點?目前發生了什麼? 在這段代碼之前是否有這樣的一行?:'os.environ ['SDL_VIDEO_CENTERED'] ='1''這是pygame工作所需的一件事。 – Darthfett

+3

我從來沒有使用'os.environ ['SDL_VIDEO_CENTERED'] ='1'',所以我不認爲它是「你需要pygame工作」的東西。 – hammythepig

回答

6

只是一個猜測,因爲我還沒有真正運行這個:

是屏幕正顯示了藍色,你就錯過了吃豆子形象?可能發生的事情是,你正在把pacman塞到屏幕上,然後做一個screen.fill(藍色),這基本上是用藍色覆蓋你的pacman圖像。嘗試在您的代碼中顛倒這些步驟(即填充藍色屏幕,然後blitting pacman)。

+1

聽起來對我來說似乎合理。 +1。 – MarkR

1

注:

  • 您正在創建一個新的圖像的每一幀。這很慢。
  • 座標的blit可以Rect()小號

這裏的更新的代碼。

WINDOW_TITLE = "hi world - draw image " 

import pygame 
from pygame.locals import * 
from pygame.sprite import Sprite 
import random 
import os 

class Pacman(Sprite): 
    """basic pacman, deriving pygame.sprite.Sprite""" 
    def __init__(self, file=None): 
     """create surface""" 
     Sprite.__init__(self) 
     # get main screen, save for later 
     self.screen = pygame.display.get_surface()     

     if file is None: file = os.path.join('data','pacman.jpg') 
     self.load(file) 

    def draw(self): 
     """draw to screen""" 
     self.screen.blit(self.image, self.rect) 

    def load(self, filename): 
     """load file""" 
     self.image = pygame.image.load(filename).convert_alpha() 
     self.rect = self.image.get_rect()  

class Game(object): 
    """game Main entry point. handles intialization of game and graphics, as well as game loop"""  
    done = False 
    color_bg = Color('seagreen') # or also: Color(50,50,50) , or: Color('#fefefe') 

    def __init__(self, width=800, height=600): 
     """Initialize PyGame window. 

     variables: 
      width, height = screen width, height 
      screen = main video surface, to draw on 

      fps_max  = framerate limit to the max fps 
      limit_fps = boolean toggles capping FPS, to share cpu, or let it run free. 
      color_bg = backround color, accepts many formats. see: pygame.Color() for details 
     """ 
     pygame.init() 

     # save w, h, and screen 
     self.width, self.height = width, height 
     self.screen = pygame.display.set_mode((self.width, self.height)) 
     pygame.display.set_caption(WINDOW_TITLE)   

     # fps clock, limits max fps 
     self.clock = pygame.time.Clock() 
     self.limit_fps = True 
     self.fps_max = 40   

     self.pacman = Pacman() 

    def main_loop(self): 
     """Game() main loop. 
     Normally goes like this: 

      1. player input 
      2. move stuff 
      3. draw stuff 
     """ 
     while not self.done: 
      # get input    
      self.handle_events() 

      # move stuff    
      self.update() 

      # draw stuff 
      self.draw() 

      # cap FPS if: limit_fps == True 
      if self.limit_fps: self.clock.tick(self.fps_max) 
      else: self.clock.tick() 

    def draw(self): 
     """draw screen""" 
     # clear screen." 
     self.screen.fill(self.color_bg) 

     # draw code 
     self.pacman.draw() 

     # update/flip screen. 
     pygame.display.flip() 

    def update(self): 
     """move guys.""" 
     self.pacman.rect.left += 10 

    def handle_events(self): 
     """handle events: keyboard, mouse, etc.""" 
     events = pygame.event.get() 

     for event in events: 
      if event.type == pygame.QUIT: self.done = True 
      # event: keydown 
      elif event.type == KEYDOWN: 
       if event.key == K_ESCAPE: self.done = True 

if __name__ == "__main__": 
    game = Game() 
    game.main_loop()  
1

如果屏幕顯示了藍色,那是因爲你需要「位圖傳送」的形象後,你做screen.fill

此外,藍色還必須在頂部定義的,它應該是在不同的循環。這裏是如何,我會做到這一點:

import pygame, sys 
    from pygame.locals import * 
    pygame.init() 
    screen=pygame.display.set_mode((640,360),0,32) 
    pygame.display.set_caption("My Game") 
    p = 1 
    green = (0,255,0) 
    blue = (0,0,255) 
    pacman ="imgres.jpeg" 
    pacman_x = 0 
    pacman_y = 0 
    pacman_obj=pygame.image.load(pacman).convert() 
    done = False 
    clock=pygame.time.Clock() 
    while done==False: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       done=True 
      if event.type==KEYDOWN: 
       if event.key==K_LEFT: 
        p=0 
     screen.fill(blue) 
     screen.blit(pacman_obj, (pacman_x,pacman_y)) 

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

    pygame.quit() 
-1

首先,你的blit的圖像,然後用藍色填充它,所以它可能只顯示藍屏,

解決方案:首先填滿屏幕的藍色,然後的blit它。

import pygame, sys 
from pygame.locals import * 
pygame.init() 
screen=pygame.display.set_mode((640,360),0,32) 
pygame.display.set_caption("My Game") 
p = 1 
green = (0,255,0) 
pacman ="imgres.jpeg" 
pacman_x = 0 
pacman_y = 0 
while True: 
    pacman_obj=pygame.image.load(pacman).convert() 

    blue = (0,0,255) 
    screen.fill(blue) # first fill screen with blue 
    screen.blit(pacman_obj, (pacman_x,pacman_y)) # Blit the iamge 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type==KEYDOWN: 
      if event.key==K_LEFT: 
       p=0 
    pygame.display.update()