1
0

__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python
  2. # ----------------------------------------------------------------------------
  3. # pyglet
  4. # Copyright (c) 2006-2008 Alex Holkner
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above copyright
  14. # notice, this list of conditions and the following disclaimer in
  15. # the documentation and/or other materials provided with the
  16. # distribution.
  17. # * Neither the name of pyglet nor the names of its
  18. # contributors may be used to endorse or promote products
  19. # derived from this software without specific prior written
  20. # permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. # POSSIBILITY OF SUCH DAMAGE.
  34. # ----------------------------------------------------------------------------
  35. '''Bounces balls around a window and plays noises.
  36. This is a simple demonstration of how pyglet efficiently manages many sound
  37. channels without intervention.
  38. '''
  39. import os
  40. import random
  41. import sys
  42. from pyglet.gl import *
  43. import pyglet
  44. from pyglet.window import key
  45. pyglet.resource.path.insert(0, os.path.dirname(__file__))
  46. pyglet.resource.reindex()
  47. BALL_IMAGE = 'ball.png'
  48. BALL_SOUND = 'ping.wav'
  49. sound = pyglet.resource.media(BALL_SOUND, streaming=False)
  50. class Ball(pyglet.sprite.Sprite):
  51. ball_image = pyglet.resource.image(BALL_IMAGE)
  52. width = ball_image.width
  53. height = ball_image.height
  54. def __init__(self):
  55. x = random.random() * (window.width - self.width)
  56. y = random.random() * (window.height - self.height)
  57. super(Ball, self).__init__(self.ball_image, x, y, batch=balls_batch)
  58. self.dx = (random.random() - 0.5) * 1000
  59. self.dy = (random.random() - 0.5) * 1000
  60. def update(self, dt):
  61. if self.x <= 0 or self.x + self.width >= window.width:
  62. self.dx *= -1
  63. sound.play()
  64. if self.y <= 0 or self.y + self.height >= window.height:
  65. self.dy *= -1
  66. sound.play()
  67. self.x += self.dx * dt
  68. self.y += self.dy * dt
  69. self.x = min(max(self.x, 0), window.width - self.width)
  70. self.y = min(max(self.y, 0), window.height - self.height)
  71. window = pyglet.window.Window(640, 480, caption='Noisy')
  72. @window.event
  73. def on_key_press(symbol, modifiers):
  74. if symbol == key.SPACE:
  75. balls.append(Ball())
  76. elif symbol == key.BACKSPACE:
  77. if balls:
  78. del balls[-1]
  79. elif symbol == key.ESCAPE:
  80. window.has_exit = True
  81. @window.event
  82. def on_draw():
  83. window.clear()
  84. balls_batch.draw()
  85. label.draw()
  86. def update(dt):
  87. for ball in balls:
  88. ball.update(dt)
  89. balls_batch = pyglet.graphics.Batch()
  90. label = pyglet.text.Label('Press space to add a ball, backspace to remove',
  91. font_size=14,
  92. x=window.width // 2, y=10,
  93. anchor_x='center')
  94. balls = []
  95. def main():
  96. pyglet.clock.schedule_interval(update, 1/30.)
  97. pyglet.app.run()