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

2024-10-02 18:25:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用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()

这不是完整的程序,只是为列和磁盘绘制精灵的部分。现在,它完全是静态的。然而,每当我尝试运行它时,我都会在用于绘制矩形的“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

我不知道该试什么。我昨天开始和派格莱特交往,所以我是个新手。如果答案显而易见,请原谅我。谢谢


Tags: inpyimageselfimginitlineusers
2条回答

我知道这个问题没有那么老,但是pyglet在这个问题发布后添加了一个名为pyglet.shapes的东西,它允许非常容易地创建Rectangle(x, y, w, h, color)。一个完整的例子是:

import pyglet
from pyglet import shapes

window = pyglet.window.Window(500, 500)
batch = pyglet.graphics.Batch()

red_sequare = shapes.Rectangle(150, 240, 200, 20, color=(255, 55, 55), batch=batch)
green_sequare = shapes.Rectangle(175, 220, 150, 20, color=(55, 255, 55), batch=batch)
blue_sequare = shapes.Rectangle(200, 200, 100, 20, color=(55, 55, 255), batch=batch)

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

pyglet.app.run()

这将产生:

enter image description here

图像的锚定必须是完整的。当您分别计算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])

相关问题 更多 >