gpt4 book ai didi

python - 用 Pygame 画抛物线

转载 作者:太空宇宙 更新时间:2023-11-04 07:23:13 24 4
gpt4 key购买 nike

我想在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com