pygame创建曲面 [英] Pygame creating surfaces

查看:144
本文介绍了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()

因为我想使我的代码运行更快,所以我想在它们之上创建曲面和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使用 display.update() 具有一种方法通过传递一个对象或> c7> 反对.因此,我们也可以调用:

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

无论如何,我建议使用 pygame.draw 模块进行绘制简单的形状,例如 rectangles circles polygons 到一个表面上,因为所有这些函数都返回一个矩形,代表更改后的像素的边界区域./p>

这使您可以将此信息传递给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()

希望这会有所帮助:)

Code:

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

Because I want to make my code run faster, I want to create surfaces and blit rects on top of them, so that I can do pygame.display.update(taskbarrect) and speed up my code. However, I don't know how to create multiple surfaces. I have tried taskbarrect=(xcoordinate, ycoordinate, width, length) then blitting an image or text or whatever, but trying it says tuple object has no attribute blit. Trying the method in the code (suggested by @elegent) gives 'pygame.Rect' object has no attribute 'blit'.

What am I doing wrong?

解决方案

Pygame uses surfaces to represent any form of image. This could be either

  • 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's display.update() has a method that allows one to update only some portions of the screen by passing one object or a list of pygame.Rect objects to it. Therefore, we could also call:

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

Anyway, I recommend to using the pygame.draw module to draw simple shapes like rectangles, circles and polygons onto a surface, because all of these functions return a rectangle representing the bounding area of changed pixels.

This enables you to pass this information to the update() function, so Pygame only updates the pixels of the just drawn shapes:

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

Update:

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

Hope this helps :)

这篇关于pygame创建曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆