Pygame creating surfaces

前端 未结 1 1356
感情败类
感情败类 2021-01-24 14:05

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

scre         


        
相关标签:
1条回答
  • 2021-01-24 14:45

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

    0 讨论(0)
提交回复
热议问题