Python Pyglet制作的简单模拟时钟程序

30 篇文章 7 订阅
23 篇文章 1 订阅
import pyglet
from datetime import datetime

WIDTH = 960
HEIGHT = 720
window = pyglet.window.Window(WIDTH,HEIGHT)

background_pattern= pyglet.image.SolidColorImagePattern(color=(255,255,255,255))
background_image = background_pattern.create_image(WIDTH,HEIGHT)

pattern = pyglet.image.SolidColorImagePattern(color=(200,100,230,255))

image = pattern.create_image(150,2)
second = pyglet.sprite.Sprite(image, x=WIDTH//2, y=HEIGHT//2)

image = pattern.create_image(120,4)
minute = pyglet.sprite.Sprite(image, x=WIDTH//2, y=HEIGHT//2)

image = pattern.create_image(100,6)
hour = pyglet.sprite.Sprite(image, x=WIDTH//2, y=HEIGHT//2)

three = pyglet.text.Label("3", x=WIDTH//2 + 150, y=HEIGHT//2,font_name="Courier", font_size=30,color=(0,255,255,255))
six = pyglet.text.Label("6", x=WIDTH//2, y=HEIGHT//2-150,font_name="Courier", font_size=30,color=(0,255,255,255))
nine = pyglet.text.Label("9", x=WIDTH//2 - 150, y=HEIGHT//2,font_name="Courier", font_size=30,color=(0,255,255,255))
twelve = pyglet.text.Label("12", x=WIDTH//2, y=HEIGHT//2+150,font_name="Courier", font_size=30,color=(0,255,255,255))

@window.event
def on_draw():
    window.clear()
    background_image.blit(0,0)
    second.draw()
    minute.draw()
    hour.draw()
    three.draw()
    six.draw()
    nine.draw()
    twelve.draw()

@window.event
def on_mouse_release(x,y,button,modifier):
    print(x,y,button,modifier)

def callback(dt):
    now = datetime.now()
    second.rotation = 360 * now.second / 60 - 90
    minute.rotation = 360 * now.minute / 60 - 90 
    hour.rotation = 360 * ( now.hour %12 + now.minute /60) /24 
    print(now.second, now.minute, now.hour)
    print(second.rotation, minute.rotation, hour.rotation)

pyglet.clock.schedule_interval(callback,1/60)

pyglet.app.run()

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我了解您的问题。要在Pyglet中将纹理渲染到立方体上,您需要使用OpenGL。您需要创建一个OpenGL上下文,并将其与Pyglet窗口绑定。然后,您可以使用Pyglet的Batch和Group来创建立方体,并将纹理与其关联。具体实现可以参考以下代码: ```python import pyglet from pyglet.gl import * # 创建OpenGL上下文并绑定到Pyglet窗口 window = pyglet.window.Window() context = window.context context.set_current() glEnable(GL_DEPTH_TEST) # 创建Batch和Group batch = pyglet.graphics.Batch() group = pyglet.graphics.TextureGroup(pyglet.image.load('texture.png').texture) # 创建立方体的顶点和纹理坐标 vertices = [ # front face (-1, -1, 1), ( 1, -1, 1), ( 1, 1, 1), (-1, 1, 1), # back face (-1, -1, -1), (-1, 1, -1), ( 1, 1, -1), ( 1, -1, -1), # top face (-1, 1, -1), (-1, 1, 1), ( 1, 1, 1), ( 1, 1, -1), # bottom face (-1, -1, -1), ( 1, -1, -1), ( 1, -1, 1), (-1, -1, 1), # left face (-1, -1, -1), (-1, -1, 1), (-1, 1, 1), (-1, 1, -1), # right face ( 1, -1, 1), ( 1, -1, -1), ( 1, 1, -1), ( 1, 1, 1), ] tex_coords = [ # front face (0, 0), (1, 0), (1, 1), (0, 1), # back face (0, 0), (1, 0), (1, 1), (0, 1), # top face (0, 0), (1, 0), (1, 1), (0, 1), # bottom face (0, 0), (1, 0), (1, 1), (0, 1), # left face (0, 0), (1, 0), (1, 1), (0, 1), # right face (0, 0), (1, 0), (1, 1), (0, 1), ] # 创建立方体 faces = [ (0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15), (16, 17, 18, 19), (20, 21, 22, 23), ] for i, face in enumerate(faces): vertices = [] tex_coords = [] for j in face: vertices.extend(vertices_list[j]) tex_coords.extend(tex_coords_list[j]) batch.add_indexed(4, GL_TRIANGLE_FAN, group, range(4), ('v3f', vertices), ('t2f', tex_coords)) # 绘制立方体 @window.event def on_draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslatef(0, 0, -5) glRotatef(30, 1, 0, 0) glRotatef(30, 0, 1, 0) batch.draw() pyglet.app.run() ``` 这里假设您已经有一个名为“texture.png”的纹理图像。我希望这可以帮助您解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值