gpt4 book ai didi

python - 我在使用 pygame 显示表面变化时遇到问题

转载 作者:行者123 更新时间:2023-12-02 02:25:27 24 4
gpt4 key购买 nike

我当前的目标是使较小的圆围绕较大圆的中心旋转,并使矩形遵循较小圆的路径。

我创建了一个 TargetCircle 类,因为当此可视化完成时,总共会有 18 个圆圈。我的问题是,在更改每个圆的 theta 值并尝试显示更改后,没有任何反应。

目前,显示的只是顶部的 9 个圆圈以及水平线和垂直线,但是,一切都保持静止。任何帮助将非常感激。谢谢。

import pygame as pg
import pygame.gfxdraw
import math

pg.init()
windowWidth = 800
windowHeight = 800
surface = pg.display.set_mode((windowWidth, windowHeight))
pg.display.set_caption("Circle")
clock = pg.time.Clock()
black = (0, 0, 0)
white = (255, 255, 255)
gray = (50, 50, 50)
red = (255, 0, 0)


class TargetCircle(object):
def __init__(self, posX, posY):
self.circlePositionX = posX
self.circlePositionY = posY
self.radius = 38
self.theta = 0
self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))

def draw(self, win):
pg.draw.rect(win, gray, (0, self.y, 800, 1))
pg.draw.rect(win, gray, (self.x, 0, 1, 800))
pygame.gfxdraw.aacircle(win, self.circlePositionX, self.circlePositionY, self.radius, white)
pygame.gfxdraw.filled_circle(win, self.x, self.y, 2, white)


circleList = []
x = 120
for i in range(1, 10):
circle = TargetCircle(x, 40)
circle.draw(surface)
circleList.append(circle)
x += 80
pg.display.update()
loop = 0
run = True
while run:

clock.tick(160)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False

if loop == len(circleList):
loop = 0

surface.fill(0)
for i in range(len(circleList)):
circleList[i].theta += .10
circleList[i].draw(surface)
pg.display.flip()
loop += 1

pg.quit()

最佳答案

问题是您没有更新 TargetCircle 的 x 和 y 值。你设置

        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))

一旦进入__init__(在开始时调用一次),然后就不再设置。要解决此问题,请将这两行移动到 TargetCircle.draw (每帧调用):

class TargetCircle(object):
def __init__(self, posX, posY):
self.circlePositionX = posX
self.circlePositionY = posY
self.radius = 38
self.theta = 0

def draw(self, win):
self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))
pg.draw.rect(win, gray, (0, self.y, 800, 1))
pg.draw.rect(win, gray, (self.x, 0, 1, 800))
pygame.gfxdraw.aacircle(win, self.circlePositionX, self.circlePositionY, self.radius, white)
pygame.gfxdraw.filled_circle(win, self.x, self.y, 2, white)

关于python - 我在使用 pygame 显示表面变化时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716380/

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