Facebook
From sdfrsw, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 128
  1. import math
  2. import pyglet
  3. from pyglet.window import key
  4.  
  5. window = pyglet.window.Window(width=300, height=300)
  6. batch = pyglet.graphics.Batch()
  7. batch2 = pyglet.graphics.Batch()
  8.  
  9. sprite_image = pyglet.image.load('ufo.png')
  10. sprite_image.anchor_x = sprite_image.width // 2
  11. sprite_image.anchor_y = sprite_image.height // 2
  12. sprite = pyglet.sprite.Sprite(sprite_image,
  13.                               x=window.width // 2,
  14.                               y=window.height // 2,
  15.                               batch=batch2)
  16. batch = pyglet.graphics.Batch()
  17. rectangle = pyglet.shapes.Rectangle(x=50, y=50, width=200, height=100, color=(255, 255, 0), batch=batch)
  18.  
  19. def get_background_color(x, y):
  20.     window.switch_to()
  21.     window.clear()
  22.     batch.draw()
  23.     buffer = pyglet.image.get_buffer_manager().get_color_buffer().get_region(x, y, 1, 1)
  24.     color = buffer.get_image_data().get_data('RGB', buffer.width * 3)
  25.     return tuple(color)
  26.    
  27. def update(dt):
  28.     sprite.rotation += sprite.rotation_speed
  29.     angle_rad = -sprite.rotation * (3.14159 / 180.0)
  30.     sprite.x += sprite.velocity * dt * math.cos(angle_rad)
  31.     sprite.y += sprite.velocity * dt * math.sin(angle_rad)
  32.     background_color = get_background_color(int(sprite.x), int(sprite.y))
  33.     print(f"x: {sprite.x:.2f}, y: {sprite.y:.2f}, color: {background_color}")
  34.  
  35. sprite.velocity = 20  # velocjity
  36. sprite.rotation_speed = 0
  37.  
  38.  
  39. @window.event
  40. def on_key_press(symbol, modifiers):
  41.     if symbol == key.LEFT:
  42.         sprite.rotation -= 7
  43.     elif symbol == key.RIGHT:
  44.         sprite.rotation += 3
  45.  
  46. @window.event
  47. def on_draw():
  48.     window.clear()
  49.     batch.draw()
  50.     batch2.draw()
  51.  
  52. pyglet.clock.schedule_interval(update, 1 / 60)
  53. pyglet.app.run()
  54.