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()
刪除'補(白色)'或'screen.blit' BTW之前使用它,你不必加載相同的圖像milions倍'while'環 - 在循環之前加載它。 – furas
並使用'screen'而不是'gameDisplay' - 'gameDisplay'可能與'screen'不同。你不必使用'set_mode'兩次。 – furas
它的工作!非常感謝! –