Pygame 绘制矩形

问题描述 投票:0回答:6

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

我检查了很多来源,但没有一个确切地说明如何去做。

python pygame rectangles 2d-games
6个回答
27
投票
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()

这将创建一个 500 x 400 像素的白色简单窗口。窗口内将是一个蓝色矩形。您需要使用

pygame.draw.rect
来解决这个问题,然后添加
DISPLAY
常量以将其添加到屏幕,变量 blue 使其变为蓝色(蓝色是一个元组,其值等于 RGB 值中的蓝色它是坐标。

查找pygame.org了解更多信息


14
投票

方法如下:

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

6
投票

你试过这个吗:

PyGame 绘图基础

取自网站:

pygame.draw.rect(screen, color, (x,y,width,height), thickness) 画一个矩形(x,y,width,height)是一个Python元组 x,y是左上角的坐标corner width, height 是矩形的宽度和高度 thickness 是线条的粗细。如果为零,则矩形被填充


5
投票

使用模块pygame.draw可以绘制矩形、圆形、多边形、连线、椭圆形或弧形等形状。一些例子:

pygame.draw.rect
绘制填充的矩形形状或轮廓。参数是目标Surface(即显示),colorrectangle和可选的轮廓widthrectangle 参数是一个包含 4 个分量 (x, y, width, height) 的元组,其中 (x, y) 是矩形的左上角点。或者,参数可以是一个
pygame.Rect
对象:

pygame.draw.rect(window, color, (x, y, width, height))
rectangle = pygame.Rect(x, y, width, height)
pygame.draw.rect(window, color, rectangle)

pygame.draw.circle
绘制实心圆或轮廓。参数是目标Surface(即显示),colorcenterradius和可选轮廓widthcenter 参数是一个包含 2 个组件 (x, y) 的元组:

pygame.draw.circle(window, color, (x, y), radius)

pygame.draw.polygon
绘制填充的多边形或轮廓。参数是目标Surface(即显示),colorpoints列表和可选轮廓width。每个 point 是一个包含 2 个组件的元组 (x, y):

pygame.draw.polygon(window, color, [(x1, y1), (x2, y2), (x3, y3)])

最小的例子: repl.it/@Rabbid76/PyGame-Shapes

import pygame

pygame.init()

window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill((255, 255, 255))

    pygame.draw.rect(window, (0, 0, 255), (20, 20, 160, 160))
    pygame.draw.circle(window, (255, 0, 0), (100, 100), 80)
    pygame.draw.polygon(window, (255, 255, 0), 
        [(100, 20), (100 + 0.8660 * 80, 140), (100 - 0.8660 * 80, 140)])

    pygame.display.flip()

pygame.quit()
exit()

1
投票

最低工作量是:

# Importing the library
import pygame

# Initializing Pygame
pygame.init()

# Initializing surface
surface = pygame.display.set_mode((400,300))

# Initializing color
color = (255,0,0)

# Drawing Rectangle
pygame.draw.rect(surface, color, pygame.Rect(30, 30, 60, 60))
pygame.display.flip()

来源


0
投票

有更简单的使用方法

pygame.draw.rect()

pygame.draw.rect(surface/screen, color, [x,y,width,height])

然后记得更新显示。

© www.soinside.com 2019 - 2024. All rights reserved.