![](/img/trans.png)
[英]How to draw a line on an image given the slope and the intercept coordinates (x, y) using Python
[英]Draw shape given x and y coordinates pygame
我正在尝试在给定坐标的情况下绘制一系列矩形,这只是 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()
此代码应绘制类似于以下的形状:
█ █ ██
相反,它正在绘制没有连贯形状的东西。
改变这一行:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.