python cocos2d画多个圆、实心圆
以下代码画圆,只显示一个圆,圆周有小缺口。如何画多个圆呢,能画完美的圆吗,能画实心圆吗,如何将显示窗口放到屏幕中间位置呢?
import cocos
from cocos.director import director
from pyglet.gl import *
from math import cos, tan, pi
class TestLayer(cocos.layer.Layer):
def __init__(self):
super().__init__()
# 画圆
circle_centers=[(100, 540), (260,540), (420,540), (260,380)]
for circle_center in circle_centers:
self.circle_center = circle_center
self.circle_radius = 50
def draw(self):
super().draw()
# 设置颜色为红色并绘制圆
glColor4ub(255, 0, 0, 255)
glLineWidth(10)
num_segments = 350
theta = 2 * pi / num_segments
tangetial_factor = tan(theta)# Calculate the tangential factor
radial_factor = cos(theta)# Calculate the radial factor
x = self.circle_radius# Start at angle = 0
y = 0
glBegin(GL_LINE_LOOP)
for i in range(num_segments):
glVertex2f(x + self.circle_center, y + self.circle_center)
tx = -y
ty = x
x += tx * tangetial_factor
y += ty * tangetial_factor
x *= radial_factor
y *= radial_factor
glEnd()
def main():
director.init(resizable=True, width=520, height=640, caption="szq")
test_layer = TestLayer()
main_scene = cocos.scene.Scene(test_layer)
director.run(main_scene)
if __name__ == '__main__':
main()
要解决您遇到的问题,可以按照以下步骤改进代码:
1. **画多个圆:** 您需要将圆心的坐标存储在一个列表中,然后在`draw`方法中遍历该列表,绘制每一个圆。
2. **画完美的圆:** 增加`num_segments`的数量可以让圆更加平滑。
3. **画实心圆:** 使用`GL_TRIANGLE_FAN`代替`GL_LINE_LOOP`绘制实心圆。
4. **将窗口放到屏幕中间:** 调整窗口创建的参数。
以下是修正后的代码示例:
import cocos
from cocos.director import director
from pyglet.gl import *
from math import cos, sin, pi
class TestLayer(cocos.layer.Layer):
def __init__(self):
super().__init__()
# 定义多个圆心及其半径
self.circles = [
((100, 150), 50),
((200, 300), 70),
((300, 450), 90)
]
def draw(self):
super().draw()
# 设置颜色为红色,默认透明度为不透明
glColor4ub(255, 0, 0, 255)
num_segments = 100# 用于更平滑的圆
theta = 2 * pi / num_segments
for circle_center, circle_radius in self.circles:
x = circle_radius
y = 0
# 绘制实心圆
glBegin(GL_TRIANGLE_FAN)
glVertex2f(circle_center, circle_center)# 圆心
for i in range(num_segments + 1):# 包含终点以闭合圆
glVertex2f(circle_center + x, circle_center + y)
t = x
x = cos(theta) * x - sin(theta) * y
y = sin(theta) * t + cos(theta) * y
glEnd()
def main():
# 初始化舞台
director.init(resizable=True, width=640, height=480, caption="szq")
# 将窗口放置于屏幕中央(仅适用于某些操作系统)
screen = director.get_window().screen
window_position = (screen.width - 640) // 2, (screen.height - 480) // 2
director.window.set_location(*window_position)
test_layer = TestLayer()
main_scene = cocos.scene.Scene(test_layer)
director.run(main_scene)
if __name__ == '__main__':
main()
### 解释:
- **多个圆:** 使用`self.circles`定义圆心和半径的列表。
- **平滑圆:** 调整`num_segments`来增加圆的平滑度。
- **实心圆:** 使用`GL_TRIANGLE_FAN`来填充圆。
- **居中窗口:** 获取当前屏幕的大小,然后设置窗口的位置。这部分代码在不同操作系统上可能表现不同。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 画多个圆、实心圆的方法是对的。调整`num_segments`来增加圆的平滑度,基本没效果。
居中窗口也不行。
screen = director.get_window().screen
AttributeError: 'Director' object has no attribute 'get_window' ydwb 发表于 2024-11-13 20:09
画多个圆、实心圆的方法是对的。调整`num_segments`来 ...
关于调节圆的平滑度以及居中窗口的需求,我们可以尝试以下几个步骤进行改进:
1. **调节圆的平滑度:** 如果增大 `num_segments` 似乎效果不明显,可以检查是否设置到了足够高的值。一般来说 `num_segments` 设置为 100 已经比较平滑了,如果还不够尝试 200 或 300。
2. **居中窗口的位置:** 使用 Pyglet 提供的方法直接设置窗口的位置,而不用通过 `Director` 的方法。
以下是修订后的代码:
import cocos
from cocos.director import director
from pyglet.gl import *
from math import cos, sin, pi
class TestLayer(cocos.layer.Layer):
def __init__(self):
super().__init__()
# 定义多个圆心及其半径
self.circles = [(150, 150, 50), (300, 300, 75), (450, 150, 100)]
def draw(self):
super().draw()
# 设置颜色为红色
glColor4ub(255, 0, 0, 255)
num_segments = 100# 用于更平滑的圆
theta = 2 * pi / num_segments
for circle_center_x, circle_center_y, circle_radius in self.circles:
x = circle_radius
y = 0
# 绘制实心圆
glBegin(GL_TRIANGLE_FAN)
glVertex2f(circle_center_x, circle_center_y)# 圆心
for i in range(num_segments + 1):# 包含终点以闭合圆
glVertex2f(circle_center_x + x, circle_center_y + y)
t = x
x = cos(theta) * x - sin(theta) * y
y = sin(theta) * t + cos(theta) * y
glEnd()
def main():
# 初始化舞台
window_width = 640
window_height = 480
director.init(resizable=True, width=window_width, height=window_height, caption="szq")
# 将窗口放置于屏幕中央
display = director.window.display
screen_width, screen_height = display.get_default_screen().width, display.get_default_screen().height
window_x = (screen_width - window_width) // 2
window_y = (screen_height - window_height) // 2
director.window.set_location(window_x, window_y)
test_layer = TestLayer()
main_scene = cocos.scene.Scene(test_layer)
director.run(main_scene)
if __name__ == '__main__':
main()
### 解释:
- **多圆和实心圆绘制:** 使用适合的 `GL_TRIANGLE_FAN` 方法进行绘制。
- **窗口居中:** 使用 Pyglet 的 `display` 和 `get_default_screen` 方法来获得屏幕尺寸,并通过 `set_location` 函数来设置窗口的位置到屏幕的中心。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 以上窗口居中方法正确。画实心圆比较圆滑,画线框圆(如线宽10)增大 `num_segments`效果不好,
即使到10000,1000000直到电脑无反应,也达不到预计效果。现考虑在实心圆内再画一个小一点的
背景色实心圆来实现。以下代码应如何修改呢?
import cocos
from cocos.director import director
from pyglet.gl import *
from math import cos, sin, pi
class TestLayer(cocos.layer.Layer):
def __init__(self):
super().__init__()
# 定义多个圆心及其半径、颜色
self.circles = [(150, 150, 50,'red'), (300, 300, 75,'red'), (450, 150, 100,'red'),
(150, 150, 40,'black'),(300, 300, 65,'black'), (450, 150, 90,'black')]
def draw(self):
super().draw()
num_segments = 100# 用于更平滑的圆
theta = 2 * pi / num_segments
for circle_center_x, circle_center_y, circle_radius,color in self.circles:
x = circle_radius
y = 0
glBegin(GL_TRIANGLE_FAN)
glVertex2f(circle_center_x, circle_center_y)# 圆心
for i in range(num_segments+1 ):# 包含终点以闭合圆
glVertex2f(circle_center_x + x, circle_center_y + y)
t = x
x = cos(theta) * x - sin(theta) * y
y = sin(theta) * t + cos(theta) * y
glEnd()
def main():
# 初始化舞台
window_width = 640
window_height = 480
director.init(resizable=True, width=window_width, height=window_height, caption="szq")
# 将窗口放置于屏幕中央
display = director.window.display
screen_width, screen_height = display.get_default_screen().width, display.get_default_screen().height
window_x = (screen_width - window_width) // 2
window_y = (screen_height - window_height) // 2
director.window.set_location(window_x, window_y)
test_layer = TestLayer()
main_scene = cocos.scene.Scene(test_layer)
director.run(main_scene)
if __name__ == '__main__':
main() ydwb 发表于 2024-11-13 21:14
以上窗口居中方法正确。画实心圆比较圆滑,画线框圆( ...
要在实心圆内再画一个小一点的背景色实心圆以实现圆环的效果,可以按照以下步骤修改代码:
1. **画大圆:** 先画出具有指定颜色的大圆。
2. **画小圆:** 在大圆内部画一个稍小的圆,颜色与背景色一致,这样就能形成一个圆环效果。
以下是修改后的代码示例:
import cocos
from cocos.director import director
from pyglet.gl import *
from math import cos, sin, pi
class TestLayer(cocos.layer.Layer):
def __init__(self):
super().__init__()
# 定义多个圆心及其半径、颜色
self.circles = [
# 示例:圆心(x, y),半径,颜色
(200, 200, 100, (255, 0, 0)),# 红色大圆
(400, 300, 70, (0, 255, 0)) # 绿色大圆
]
self.background_color = (255, 255, 255)# 背景色,白色
def draw(self):
super().draw()
num_segments = 100# 用于更平滑的圆
theta = 2 * pi / num_segments
for circle_center_x, circle_center_y, circle_radius, color in self.circles:
# 画大圆 (环的外圈,实际可见)
R, G, B = color
glColor4ub(R, G, B, 255)# 设置颜色
x, y = circle_radius, 0
glBegin(GL_TRIANGLE_FAN)
glVertex2f(circle_center_x, circle_center_y)
for _ in range(num_segments + 1):
glVertex2f(circle_center_x + x, circle_center_y + y)
t = x
x = cos(theta) * x - sin(theta) * y
y = sin(theta) * t + cos(theta) * y
glEnd()
# 画小圆 (环的内圈,用背景色覆盖)
inner_radius = circle_radius - 10# 调整这个值来设置环的厚度
if inner_radius > 0:
glColor4ub(*self.background_color, 255)# 设置背景色
x, y = inner_radius, 0
glBegin(GL_TRIANGLE_FAN)
glVertex2f(circle_center_x, circle_center_y)
for _ in range(num_segments + 1):
glVertex2f(circle_center_x + x, circle_center_y + y)
t = x
x = cos(theta) * x - sin(theta) * y
y = sin(theta) * t + cos(theta) * y
glEnd()
def main():
# 初始化舞台
window_width = 640
window_height = 480
director.init(resizable=True, width=window_width, height=window_height, caption="szq")
# 将窗口放置于屏幕中央
display = director.window.display
screen_width, screen_height = display.get_default_screen().width, display.get_default_screen().height
window_x = (screen_width - window_width) // 2
window_y = (screen_height - window_height) // 2
director.window.set_location(window_x, window_y)
test_layer = TestLayer()
main_scene = cocos.scene.Scene(test_layer)
director.run(main_scene)
if __name__ == '__main__':
main()
### 说明:
- **颜色设置:** 使用`glColor4ub`设置绘制的颜色。
- **内外圆:** 外圆用指定颜色绘制,内圆用背景色绘制,从而形成环形。
- **环厚度:** 通过调整`inner_radius`相对于`circle_radius`的大小,改变环的厚度。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]