Pygame创建表面

3

代码:

#!/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()

因为我想让我的代码运行更快,所以我想创建表面并在其上绘制矩形,这样我就可以做pygame.display.update(taskbarrect)并加速我的代码。然而,我不知道如何创建多个表面。我尝试过taskbarrect=(xcoordinate, ycoordinate, width, length),然后对图像、文本或其他东西进行blitting,但尝试后会显示tuple object has no attribute blit。尝试代码中的方法(由@elegent建议)会显示'pygame.Rect' object has no attribute 'blit'
我做错了什么?

1
提供实际发生错误的一些代码,有助于我们更好地帮助您。;) - elegent
你不需要创建一个新的Surface;只需将文本blit到主屏幕上(请参见我的更新答案)。 - elegent
@优雅的话,会有很大的区别吗?即帧率会减少多少? - Yubin Lee
1个回答

11

Pygame使用surfaces来表示任何形式的图像。这可以是:

  • your main screen Surface, which is returned by the pygame.display.set_mode() function

    myDisplay = pygame.display.set_mode((500, 300))
    
  • or created object of the pygame.Surface class:

    #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()
    

Pygame的display.update()有一个方法,允许我们通过传递一个对象或pygame.Rect对象的列表来更新屏幕的部分区域。因此,我们也可以调用:

myUpdateRect= pygame.Rect((500, 300), (0, 0))
display.update(myUpdateRect)

无论如何,我建议使用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))

更新:
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()

希望这可以帮到你 :)

追踪(Traceback)(最近的调用在最上面): 文件“desktop/opsys.py”,第45行,在<module>中: taskbar() 文件“desktop/opsys.py”,第27行,在taskbar中: taskbarrect = pygame.Surface((0,int(h-40)),(int(w),int(h))) 类型错误:需要整数。 - Yubin Lee
@YuBinLee:正如我在回答中提到的,pygame.Surface类的构造函数需要一个表示表面大小(即分辨率)的元组。您想要创建一个pygame.Rect对象:taskbarrect = pygame. Rect ((0, int(h-40)), (int(w), int(h))) - elegent
追踪(Traceback)(最近的调用在最上面): 文件“C:\ Users \ chef \ Desktop \ opsys.py”,第45行,在<module>中: taskbar() 文件“C:\ Users \ chef \ Desktop \ opsys.py”,第30行,在taskbar中: taskbarrect.blit(text,(w-100,h-37)) 属性错误:'pygame.Rect'对象没有'blit'属性。 - Yubin Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接