我想在pygame中画一条抛物线。我制作了一个pixelarray对象并循环通过它来确定一个像素是否在抛物线上。我似乎得到了一个点之间有差距的图像。如何使它成为一条连续的线?
import pygame
import sys
from pygame.locals import *
import math
WIDTH = 640
HEIGHT = 480
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
pxarray = pygame.PixelArray(screen)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
for y, py in enumerate(pxarray):
for x, px in enumerate(py):
if int(x) == (int(y)*int(y)) - 30*int(y) + 450:
pxarray[y][x] = 0xFFFFFF
pygame.display.update()
答案 0 :(得分:7)
color = 255, 0, 0
first = True
prev_x, prev_y = 0, 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
for y, py in enumerate(pxarray):
for x, px in enumerate(py):
if int(x) == (int(y)*int(y)) - 30*int(y) + 450:
pxarray[y][x] = 0xFFFFFF
if first:
first = False
prev_x, prev_y = x, y
continue
pygame.draw.line(screen, color, (prev_y, prev_x), (y, x))
prev_x, prev_y = x, y
first = True
pygame.display.flip()
答案 1 :(得分:1)
您需要将抛物线绘制为一系列线段。您使用的段数将决定它的外观是否平滑,因此如果您计算抛物线x = y ^ 2 + 30y + 450,则计算多个y值的x值并从(x0)绘制线条,y0)到(x1,y1)等等。
如果您没有线条图原语,则需要使用Bresenham's Algorithm之类的内容实现一个。