2016-11-14 67 views
0

我想用pygame製作python中的鋼琴拼圖。所以,我已經開始製作一個介紹窗口,但是我無法將我的背景圖片1上傳到窗口中。我實際上想要在玩家開始遊戲時顯示名稱「Piano tiles」和背景圖片。這裏是我的代碼:鋼琴拼圖:背景圖像

import pygame,os,random,time 
from pygame.locals import * 
wix=800 
wiy=800 
pygame.init() 
white=(255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,155,0) 
clock = pygame.time.Clock() 
smallfont = pygame.font.SysFont("comicsansms", 25) 
medfont = pygame.font.SysFont("comicsansms", 50) 
largefont = pygame.font.SysFont("comicsansms", 100) 
gameDisplay=pygame.display.set_mode((wix,wiy)) 
bg = pygame.image.load("background.jpg") 
pygame.display.set_caption("Piano Tiles") 
def game_intro(): 
    screen=pygame.display.set_mode((wix,wiy)) 
    intro =True 
    while intro: 
     bg = pygame.image.load("background.jpg") 
     screen.blit(bg,(0,0)) 
     gameDisplay.fill(white) 
     message_to_screen("PIANO TILES",black,-100,"large") 
     pygame.display.update() 
     clock.tick(8) 

def text_objects(text,color,size): 
    if size == "small": 
     textSurface = smallfont.render(text, True, color) 
    elif size == "medium": 
     textSurface = medfont.render(text, True, color) 
    elif size == "large": 
     textSurface = largefont.render(text, True, color) 


    return textSurface, textSurface.get_rect() 
def message_to_screen(msg,color, y_displace=0, size = "small"): 
    textSurf, textRect = text_objects(msg,color, size) 
    textRect.center = (wix/ 2), (wiy/2)+y_displace 
    gameDisplay.blit(textSurf, textRect) 

game_intro() 
pygame.time.wait(4000) 
pygame.quit() 
quit() 
+0

刪除'補(白色)'或'screen.blit' BTW之前使用它,你不必加載相同的圖像milions倍'while'環 - 在循環之前加載它。 – furas

+0

並使用'screen'而不是'gameDisplay' - 'gameDisplay'可能與'screen'不同。你不必使用'set_mode'兩次。 – furas

+0

它的工作!非常感謝! –

回答

0

你有你之前使用fill(white)blit任何東西,因爲它清晰的屏幕。

它可能看起來像這樣

import pygame 

# --- constants --- (UPPER_CASE names) 

WIX = 800 
WIY = 800 

WHITE = (255,255,255) 
BLACK = ( 0, 0, 0) 
RED = (255, 0, 0) 
GREEN = ( 0,155, 0) 

FPS = 8 

# --- functions --- (lower_case names) 

def text_objects(text, color, size): 
    if size == "small": 
     text_font = small_font 
    elif size == "medium": 
     font = med_font 
    elif size == "large": 
     font = large_font 

    #fonts = {"small": small_font, "medium": med_font, "large": large_font} 
    #font = fonts[size] 

    text_surface = font.render(text, True, color) 
    text_rect = text_surface.get_rect() 

    return text_surface, text_rect 

def message_to_screen(screen, msg, color, y_displace=0, size="small"): 
    text_surface, text_rect = text_objects(msg, color, size) 
    text_rect.center = screen.get_rect().center 
    text_rect.y += y_displace 
    screen.blit(text_surface, text_rect) 

def game_intro(screen, text): 
    #bg = pygame.image.load("background.jpg") 

    screen.fill(WHITE) # use if background is smaller then screen 

    screen.blit(bg, (0,0)) 

    message_to_screen(screen, text, BLACK, -100, "large") 
    message_to_screen(screen, text, BLACK, 0, "large") 
    message_to_screen(screen, text, BLACK, 100, "large") 

    pygame.display.update() 

    clock = pygame.time.Clock() 

    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       return False # exit program 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        return True # go to next stage 
      elif event.type == pygame.MOUSEBUTTONDOWN: 
       if event.button == 1: 
        return True # go to next stage 

     clock.tick(FPS) 

# --- main --- (lower_case names) 

# - init - 

pygame.init() 

screen = pygame.display.set_mode((WIX, WIY)) 
#screen_rect = screen.get_rect() 

pygame.display.set_caption("Piano Tiles") 

# - resources - 

bg = pygame.image.load("Obrazy/test.png")#"background.jpg") 

small_font = pygame.font.SysFont("comicsansms", 25) 
med_font = pygame.font.SysFont("comicsansms", 50) 
large_font = pygame.font.SysFont("comicsansms", 100) 

#fonts = {"small": small_font, "medium": med_font, "large": large_font} 

# - stages - 

go_next = game_intro(screen, "HELLO WORLD") 
if go_next: 
    go_next = game_intro(screen, "PIANO TILES") 
if go_next: 
    go_next = game_intro(screen, "BYE, BYE") 

# - exit - 

pygame.quit()