给出x和y坐标pygame的绘制形状

时间:2017-06-10 14:52:54

标签: python python-3.x pygame pygame-surface

我尝试绘制一系列给定坐标的矩形,这只是一个x和y值的列表。

这是我的代码:

import pygame

pygame.init()

screen = pygame.display.set_mode((400, 400))

x = [5, 5, 4, 5]
y = [2, 3, 4, 4]


def draw(x, y):
    block = 30

    coord = zip(x, y)

    for _x, _y in coord:

        _x1 = _x * block
        _y1 = _y * block
        x2 = _x1 + block
        y2 = _y1 + block

        pygame.draw.rect(screen, 0xffffff, (_x1, _y1, x2, y2))


running = True

while running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False
    draw(x, y)
    pygame.display.flip()

pygame.quit()
quit()

此代码应绘制一个类似于此的形状:

 0
 0
00
相反,它绘制的东西没有连贯的形状。

1 个答案:

答案 0 :(得分:1)

更改此行:

pygame.draw.rect(screen, 0xffffff, (_x1, _y1, x2, y2))

到此:

pygame.draw.rect(screen, 0xffffff, (_x1, _y1, block, block))

draw.rect将矩形作为由(x,y,width,height)指定的参数

表示_x1 = 5,_y1 = 2 如果您执行(_x1, _y1, x2, y2),则表示(x=150, y=60, width=180, height=90)