【问题标题】:Pygame creating surfacesPygame 创建表面
【发布时间】:2016-02-25 17:21:15
【问题描述】:

代码:

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

因为我想让我的代码运行得更快,所以我想在它们之上创建表面和 blit rects,这样我就可以执行pygame.display.update(taskbarrect) 并加快我的代码速度。但是,我不知道如何创建多个表面。我尝试过taskbarrect=(xcoordinate, ycoordinate, width, length),然后对图像或文本或其他内容进行传输,但尝试显示tuple object has no attribute blit。尝试代码中的方法(由@elegent 建议)得到'pygame.Rect' object has no attribute 'blit'

我做错了什么?

【问题讨论】:

  • 仅供参考,当您不向我们提供一些实际发生错误的代码时,很难为您提供帮助。 ;)
  • 你不需要创建一个新的 Surface;只需将文本粘贴到主屏幕上(请参阅我更新的答案)。
  • @elegent 会有很大不同吗?即fps会降低多少?

标签: python python-2.7 pygame pygame-surface


【解决方案1】:

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

  • 你的主屏幕Surface,由pygame.display.set_mode()函数返回

    myDisplay = pygame.display.set_mode((500, 300))
    
  • 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()
    

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 行,在 taskbar() 文件“desktop/opsys.py”,第 27 行,在任务栏 taskbarrect = pygame.Surface((0, int(h-40)), (int(w), int(h))) TypeError: an integer is required
  • @YuBinLee:正如我在回答中提到的,pygame.Surface 类的构造函数需要一个表示表面大小(即分辨率)的元组。你想创建一个pygame.Rect 对象:taskbarrect = pygame. Rect ((0, int(h-40)), (int(w), int(h)))
  • Traceback(最近一次调用最后):文件“C:\Users\chef\Desktop\opsys.py”,第 45 行,在 taskbar() 文件“C:\Users\chef \Desktop\opsys.py",第 30 行,任务栏 taskbarrect.blit(text, (w - 100, h - 37)) AttributeError: 'pygame.Rect' object has no attribute 'blit'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
相关资源
最近更新 更多