gpt4 book ai didi

python - 如何在 Pyglet 中绘制简单的形状?

转载 作者:行者123 更新时间:2023-12-01 06:21:41 28 4
gpt4 key购买 nike

我正在尝试使用 Pyglet 制作汉诺塔程序,但在绘制矩形时遇到问题。这是我的代码:

import pyglet

width, height = 500, 500
window = pyglet.window.Window(width, height)

rectR = pyglet.image.SolidColorImagePattern(color=(255, 0, 0, 1))
rectG = pyglet.image.SolidColorImagePattern(color=(0, 255, 0, 1))
rectB = pyglet.image.SolidColorImagePattern(color=(0, 0, 255, 1))
rectW = pyglet.image.SolidColorImagePattern(color=(255, 255, 255, 1))

column_img = rectW.create_image(20, 40)
disk7_img = rectR.create_image(140, 20)
disk5_img = rectG.create_image(100, 20)
disk3_img = rectB.create_image(60, 20)

def center_images(images):
for image in images:
image.anchor_x = image.width/2
image.anchor_y = image.height/2
center_images([column_img, disk7_img, disk5_img, disk3_img])

class Sprite:
def __init__(self, img, x=0, y=0):
self.img, self.x, self.y = img, x, y

class Column(Sprite):
def __init__(self, x, y, disk_codes):
super(Column, self).__init__(column_img, x=x, y=y)
disks = []
for disk_code in disk_codes:
disks.append(Disk(disk_code))
self.disks = sorted(disks, key= lambda disk: disk.img.width)
top = 0
for i, disk in enumerate(self.disks):
self.disks[i].x = self.x
self.disks[i].y = self.y - self.img.height/2 + top + disk.img.height/2
top += disk.img.height

def draw(self):
self.img.blit(self.x, self.y)
for disk in self.disks:
disk.img.blit(disk.x, disk.y)

class Disk(Sprite):
def __init__(self, code):
if code == 'R':
super(Disk, self).__init__(disk7_img)
elif code == 'G':
super(Disk, self).__init__(disk5_img)
elif code == 'B':
super(Disk, self).__init__(disk3_img)
else:
raise ValueError('Invalid disk code')

column = Column(100, 100, ['R', 'G', 'B'])

@window.event
def on_draw():
window.clear()
column.draw()

pyglet.app.run()

这不是完整的程序,只是绘制列和圆盘 Sprite 的部分。目前,它是完全静态的。然而,每当我尝试运行它时,我都会在应该绘制矩形的“blit”函数中收到错误。这是错误:

Traceback (most recent call last):
File "hanoi.py", line 63, in <module>
pyglet.app.run()
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/app/__init__.py", line 107, in run
event_loop.run()
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/app/base.py", line 170, in run
self._run()
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/app/base.py", line 182, in _run
timeout = self.idle()
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/app/base.py", line 309, in idle
window.dispatch_event('on_draw')
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/window/__init__.py", line 1320, in dispatch_event
if EventDispatcher.dispatch_event(self, *args) != False:
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/event.py", line 408, in dispatch_event
if handler(*args):
File "hanoi.py", line 61, in on_draw
column.draw()
File "hanoi.py", line 41, in draw
self.img.blit(self.x, self.y)
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/image/__init__.py", line 901, in blit
self.get_texture().blit(x, y, z, width, height)
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/image/__init__.py", line 832, in get_texture
self._current_texture = self.create_texture(Texture, rectangle, force_rectangle)
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/image/__init__.py", line 825, in create_texture
self.anchor_x, self.anchor_y, 0, None)
File "/Users/thomasfaulhaber/miniconda3/envs/hanoi/lib/python3.6/site-packages/pyglet/image/__init__.py", line 996, in blit_to_texture
data)
ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type

我不知道该尝试什么。我昨天开始使用 Pyglet,所以我很新。如果答案是显而易见的,请原谅我。谢谢!

最佳答案

图像的 anchor 必须是完整的。当您分别计算 anchor_xanchor_y 时,您必须使用底除运算符 (//) 而不是除运算符 (/) :
(参见Binary arithmetic operations)

def center_images(images):
for image in images:
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
center_images([column_img, disk7_img, disk5_img, disk3_img])

关于python - 如何在 Pyglet 中绘制简单的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327181/

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