Python Pygame绘制矩形

Python Pygame绘制矩形,python,pygame,rectangles,2d-games,Python,Pygame,Rectangles,2d Games,我正在制作一个需要知道如何在python 3.2中绘制矩形的游戏 我查阅了很多资料,但没有一个确切说明如何做到这一点 谢谢 您是否尝试过以下方法: 摘自现场: rect(screen,color,(x,y,width,height),thickness)绘制一个矩形(x,y,width,height)是Python元组x,y是左上角的坐标宽度,height是矩形的宽度,height是矩形的高度,thickness是线条的厚度。如果为零,则填充矩形 这将创建一个500像素×400像素的白色简单窗

我正在制作一个需要知道如何在python 3.2中绘制矩形的游戏

我查阅了很多资料,但没有一个确切说明如何做到这一点

谢谢

您是否尝试过以下方法:

摘自现场:

rect(screen,color,(x,y,width,height),thickness)绘制一个矩形(x,y,width,height)是Python元组x,y是左上角的坐标宽度,height是矩形的宽度,height是矩形的高度,thickness是线条的厚度。如果为零,则填充矩形

这将创建一个500像素×400像素的白色简单窗口。窗口内将显示一个蓝色矩形。您需要使用
pygame.draw.rect
来执行此操作,并添加
DISPLAY
常量以将其添加到屏幕,变量blue使其变为蓝色(blue是一个元组,其值在RGB值及其坐标中等同于蓝色)

查找更多信息,方法如下:

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()
使用模块,可以绘制矩形、圆、多边形、边、椭圆或圆弧等形状。一些示例:

绘制填充的矩形形状或轮廓。参数是目标曲面(即显示)、颜色、矩形和可选的轮廓宽度。矩形参数是包含4个组件(x、y、宽度、高度)的元组,其中(x、y)是矩形的左上点。或者,参数可以是对象:

pygame.draw.rect(窗口,颜色,(x,y,宽度,高度))
rectangle=pygame.Rect(x,y,宽度,高度)
pygame.draw.rect(窗口、颜色、矩形)
绘制填充圆或轮廓。参数是目标曲面(即显示)、颜色、中心、半径和可选轮廓宽度。中心参数是包含两个组件(x,y)的元组:

pygame.draw.circle(窗口、颜色、(x、y)、半径)
绘制填充多边形或轮廓。参数包括目标曲面(即显示)、颜色、点列表和可选轮廓宽度。每个点是一个包含两个组件(x,y)的元组:

pygame.draw.polygon(窗口,颜色,[(x1,y1),(x2,y2),(x3,y3)])

最简单的例子:

导入pygame
pygame.init()
window=pygame.display.set_模式((200200))
clock=pygame.time.clock()
运行=真
运行时:
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
窗口填充((255,255,255))
pygame.draw.rect(窗口,(0,0255),(20,20,160160))
pygame.draw.circle(窗口,(255,0,0),(100100),80)
pygame.draw.polygon(窗口,(255,255,0),
[(100, 20), (100 + 0.8660 * 80, 140), (100 - 0.8660 * 80, 140)])
pygame.display.flip()
pygame.quit()
退出()

您使用的图形是什么。手册:免费PDF:
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()