如何在python pygame中创建rect变量

时间:2014-11-09 17:31:29

标签: python python-2.7 variables pygame rect

我使用pygame库在Python中编程。我想知道如何制作一个矩形变量,如:

import ...
rect1 = #RECT VARIABLE
rect2 = #RECT VARIABLE

3 个答案:

答案 0 :(得分:2)

只需

pygame.Rect(left, top, width, height)

这将在pygame中返回一个Rect对象

答案 1 :(得分:1)

也许这样的事情会起作用吗?

import pygame
# your whole code until you need a rectangle
rect1 = pygame.draw.rect( (x_coordinate, y_coordinate), (#a rgb color), (#size of rect in pixels) )

如果您想要一个100x150位置0x50的红色矩形,它将是:

rect1 = pygame.draw.rect( (0, 50), (255, 0, 0), (100, 150) )

要将其插入您使用的屏幕:

pygame.screen_you_have_created.blit(rect1, 0, 50)

答案 2 :(得分:-1)

对于矩形绘制图案是:pygame.Rect(left,top,width,height)

您可以根据需要修改此代码。

import pygame, sys
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Title')
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
basicFont = pygame.font.SysFont(None, 48)
message =  'Hello '

text = basicFont.render(message, False, WHITE, BLUE)
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery
windowSurface.fill(WHITE)
pixArray = pygame.PixelArray(windowSurface)
pixArray[480][380] = BLACK
del pixArray
windowSurface.blit(text, textRect)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == K_ESCAPE:
            pygame.quit()
            sys.exit()