Python Pygame 绘制矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19780411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pygame Drawing a Rectangle
提问by Jared
Im making a game that requires knowing how to draw a rectangle in python 3.2.
我正在制作一个需要知道如何在 python 3.2 中绘制矩形的游戏。
I have checked lot of sources but none show exactly how to do it.
我已经检查了很多来源,但没有一个确切地说明如何做到这一点。
Thanks!
谢谢!
回答by Phorce
Have you tried this:
你有没有试过这个:
Taken from the site:
取自网站:
pygame.draw.rect(screen, color, (x,y,width,height), thickness) draws a rectangle (x,y,width,height) is a Python tuple x,y are the coordinates of the upper left hand corner width, height are the width and height of the rectangle thickness is the thickness of the line. If it is zero, the rectangle is filled
pygame.draw.rect(screen, color, (x,y,width,height), Thickness) 绘制一个矩形 (x,y,width,height) 是一个 Python 元组 x,y 是左上角的坐标width、height 是矩形的宽度,height 是线的粗细。如果为零,则填充矩形
回答by Phorce
import pygame, sys
from pygame.locals import *
def main():
pygame.init()
DISPLAY=pygame.display.set_mode((500,400),0,32)
WHITE=(255,255,255)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
main()
This creates a simple window 500 pixels by 400 pixels that is white. Within the window will be a blue rectangle. You need to use the pygame.draw.rect
to go about this, and you add the DISPLAY
constant to add it to the screen, the variable blue to make it blue (blue is a tuple that values which equate to blue in the RGB values and it's coordinates.
这将创建一个简单的 500 像素 x 400 像素的白色窗口。窗口内将出现一个蓝色矩形。您需要使用pygame.draw.rect
来解决此问题,然后添加DISPLAY
常量以将其添加到屏幕,变量 blue 使其变为蓝色(蓝色是一个元组,其值等同于 RGB 值中的蓝色及其坐标。
Look up pygame.orgfor more info
查找pygame.org以获取更多信息
回答by l. zhang
here's how:
就是这样:
import pygame
screen=pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
red=255
blue=0
green=0
left=50
top=50
width=90
height=90
filled=0
pygame.draw.rect(screen, [red, blue, green], [left, top, width, height], filled)
pygame.display.flip()
running=True
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()