python - 用 Pygame 画抛物线

标签 python math graphics pygame

我想在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()

最佳答案

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

enter image description here

关于python - 用 Pygame 画抛物线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11708161/

相关文章:

javascript - 在 JS 中四舍五入到最接近的倍数

c - 好的、免费的、易于使用的 C 图形库?

javascript - 为什么我的三 Angular 条纹要和之前的三 Angular 条纹连接起来

python - Pandas,如何找到互补的时间范围?

python - 如何在 Altair 的 HConcatChart 中配置图表位置

python - 使用两个堆栈的解决方案

opencv - 给定点的世界坐标(在二维图像平面位置和深度值中),我该如何导出?

基于 Sublime Text 3 的 Python 2.7 不打印 '\uFFFD' 字符

python - 覆盖python中的递归方法

math - Bairstow 方法初始二次近似