代碼:pygame的創建曲面
#!/usr/bin/python
import pygame, time, sys, random, os
from pygame.locals import *
from time import gmtime, strftime
pygame.init()
w = 640
h = 400
screen = pygame.display.set_mode((w, h),RESIZABLE)
clock = pygame.time.Clock()
x = y = 100
def starting():
basicfont = pygame.font.SysFont(None, 48)
text = basicfont.render('Starting...', True, (255, 255, 255), (0, 0, 255))
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery
screen.fill((0, 0, 255))
screen.blit(text, textrect)
def taskbar():
basicfont = pygame.font.SysFont(None, 24)
taskbarrect = pygame.Rect((0, int(h-40)), (int(w), int(h)))
text = basicfont.render(strftime("%Y-%m-%d", gmtime()), True, (0, 0, 0))
text2 = basicfont.render(strftime("%H:%M:%S", gmtime()), True, (0, 0, 0))
taskbarrect.blit(text, (w - 100, h - 37))
taskbarrect.blit(text2, (w - 100, h - 17))
pygame.display.update()
starting()
screen.fill((255,255,255))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type==VIDEORESIZE:
w = event.dict['size'][0]
h = event.dict['size'][1]
screen=pygame.display.set_mode(event.dict['size'],RESIZABLE)
taskbar()
因爲我想讓我的代碼運行速度更快,我想在它們上面創建曲面和塊傳送rects,這樣我可以做pygame.display.update(taskbarrect)
,加快我的代碼。但是,我不知道如何創建多個曲面。我試過taskbarrect=(xcoordinate, ycoordinate, width, length)
然後blitting一個圖像或文字或任何,但嘗試它說tuple object has no attribute blit
。嘗試代碼中的方法(由@elegent建議)給出'pygame.Rect' object has no attribute 'blit'
。
我在做什麼錯?
僅供參考,it's努力幫助你,當你不提供我們一些代碼在錯誤實際發生。 ;) – elegent
我加了代碼。 –
你不需要創建一個新的Surface;只需將文本粘貼到主屏幕上(請參閱我的更新回答)。 – elegent