Yub*_*Lee 0 python pygame python-2.7 pygame-surface
码:
#!/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()
Run Code Online (Sandbox Code Playgroud)
因为我想使我的代码运行得更快,所以我想在它们之上创建表面和blit rect,以便我可以这样做pygame.display.update(taskbarrect)并加快我的代码的速度。但是,我不知道如何创建多个曲面。我试过taskbarrect=(xcoordinate, ycoordinate, width, length)将图像或文本或其他任何内容刷一下,但是尝试说tuple object has no attribute blit。尝试代码中的方法(由@elegent建议)给出'pygame.Rect' object has no attribute 'blit'。
我究竟做错了什么?
Pygame使用表面来表示任何形式的图像。这可能是
您的主屏幕Surface(由pygame.display.set_mode()函数返回)
myDisplay = pygame.display.set_mode((500, 300))
Run Code Online (Sandbox Code Playgroud)或创建的pygame.Surface类的对象:
#create a new Surface
myNewSurface = Surface((500, 300))
#change its background color
myNewSurface.fill((55,155,255))
#blit myNewSurface onto the main screen at the position (0, 0)
myDisplay.blit(myNewSurface, (0, 0))
#update the screen to display the changes
display.update() #or display.flip()
Run Code Online (Sandbox Code Playgroud)Pygame的display.update()方法允许通过向其传递一个对象或对象列表来仅更新屏幕的某些部分pygame.Rect。因此,我们还可以调用:
myUpdateRect= pygame.Rect((500, 300), (0, 0))
display.update(myUpdateRect)
Run Code Online (Sandbox Code Playgroud)
无论如何,我建议使用该pygame.draw模块在表面上绘制诸如矩形,圆形和多边形之类的简单形状,因为所有这些函数都返回一个矩形,代表更改后的像素的边界区域。
这使您可以将此信息传递给update()函数,因此Pygame仅更新刚绘制的形状的像素:
myDisplay = pygame.display.set_mode((500, 300))
myRect= pygame.Rect((100, 200), (50, 100))
pygame.display.update(pygame.draw.rect(myDisplay, (55, 155, 255), myRect))
Run Code Online (Sandbox Code Playgroud)
更新:
def taskbar():
basicfont = pygame.font.SysFont(None, 24)
text = basicfont.render(strftime("%Y-%m-%d", gmtime()), True, (0, 0, 0))
text2 = basicfont.render(strftime("%H:%M:%S", gmtime()), True, (0, 0, 0))
screen.fill((55, 155, 255))
screen.blit(text, (w - 100, h - 37))
screen.blit(text2, (w - 100, h - 17))
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 :)
| 归档时间: |
|
| 查看次数: |
9102 次 |
| 最近记录: |