2017-10-24 3 views
1

ではありません、私はこのプログラムを実行しようとした、しかし、このエラーがアップします:私のプログラムではなく、黒の背景を使用しての私は、背景ERRORとして画像を使用しようとすると、実行されません。pygame.surface」オブジェクトが呼び出し可能

Traceback (most recent call last): 
    File "C:/Python/ComputingCW/ComputingCW.py", line 12, in <module> 
    Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32) 
TypeError: 'pygame.Surface' object is not callable 

以前は: サーフェス= pygame.display.set_mode((W、H)、0、32)を使用し、 "Surface.fill(BLACK)"を使用して黒い背景を作成しました。

なぜ私は自分の背景として画像を使用できないのか理解できません。誰かが私を助けて、なぜこれが起こっているのか説明できますか?

import pygame, sys, time 
from pygame.locals import * 

pygame.init() 
mainClock = pygame.time.Clock() 
# all_fonts = pygame.font.get_fonts() 


W = 1280 
H = 900 

Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32) 
pygame.display.set_caption('Physics in Motion') 

BLACK = (0, 0, 0) 
GREEN = (0, 255, 0) 
WHITE = (255, 255, 255) 
BLUE = (0, 0, 255) 
RED = (255, 0, 0) 
YELLOW = (255, 255, 0) 

game = True 
mouse_pos = (0, 0) 
mouse_click = (0, 0) 
text1B = False 
text2B = False 
text3B = False 
output = '' 

while game == True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == KEYUP: 
      if event.key == K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
     if event.type == MOUSEMOTION: 
      mouse_pos = event.pos 
     if event.type == MOUSEBUTTONUP: 
      mouse_click = event.pos 


    color = WHITE 
    Font = pygame.font.SysFont('arial', 72) 
    if text1B: 
     color = RED 
    text = Font.render('Start Simulation', True, color) 
    text_rect = text.get_rect() 
    text_rect.center = (W/2, H/5) 
    if text_rect.collidepoint(mouse_click): 
     output = 'Simulation()' 
    if text_rect.collidepoint(mouse_pos): 
     text1B = True 
    else: 
     text1B = False 
    Surface.blit(text, text_rect) 

    color = WHITE 
    if text2B: 
     color = RED 

    Font = pygame.font.SysFont('arial', 72) 
    text = Font.render('Graph Generator', True, color) 
    text_rect = text.get_rect() 
    text_rect.center = (W/2, H * 2/5) 
    if text_rect.collidepoint(mouse_click): 
     output = 'GraphG()' 
    if text_rect.collidepoint(mouse_pos): 
     text2B = True 
    else: 
     text2B = False 
    Surface.blit(text, text_rect) 

    color = WHITE 
    if text3B: 
     color = RED 

    Font = pygame.font.SysFont('arial', 72) 
    text = Font.render('Exit', True, color) 
    text_rect = text.get_rect() 
    text_rect.center = (W/2, H * 3/5) 
    if text_rect.collidepoint(mouse_click): 
     pygame.quit() 
     sys.exit() 
    if text_rect.collidepoint(mouse_pos): 
     text3B = True 
    else: 
     text3B = False 
    Surface.blit(text, text_rect) 

    Font = pygame.font.SysFont('arial', 72) 
    text = Font.render(output, True, BLUE) 
    text_rect = text.get_rect() 
    text_rect.center = (W/2, H * 4/5) 
    Surface.blit(text, text_rect) 

    pygame.display.flip() 
    mainClock.tick(100000) 

答えて

0

から始まるディレクトリをトレース

'Pygame_Background.jpg'

のファイルの絶対パスを使用してみてください。今度は、このサーフェスを持っていて、それを括弧の後ろにある関数 ((W, H), 0, 32)のように呼び出すと、 TypeErrorが発生します。サーフェスオブジェクトは関数ではない(呼び出し可能ではありません)ためです。あなたが実際に何をしたいのか

は次のとおりです。

  1. が表示ウィンドウを作成します(私はここscreenそれを呼び出します)。

    screen = pygame.display.set_mode((W, H), 0, 32) 
    
  2. 背景画像を(Surfacepygame.Surfaceクラスの名前であるため、より良いbackgroundそれを呼び出す)ロードします。

    background = pygame.image.load('Pygame_Background.jpg').convert() 
    

    また、パフォーマンスを向上させるためにpygame.Surface.convertまたはconvert_alphaメソッドを呼び出します。

  3. backgroundと他のすべてをメインwhileループの画面に表示します。

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

これは名前の競合のようです。 pygame.Surfaceは、モジュール(またはサブモジュール)ですが、このラインは、このサーフェスオブジェクトを上書きするようだ:

Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32) 

Iは、例えば、surf_bgのために、左側に別の変数を使用しようとするだろう。

0

は、この行pygame.image.load('Pygame_Background.jpg')は、あなたのハードディスクからイメージをロードし、pygame.Surfaceオブジェクトに変換しますあなたのドライブの手紙

関連する問題