Advertisement
Guest User

aaaa

a guest
Oct 23rd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import math
  4. import random
  5. import sys
  6. from PyQt4 import QtGui, QtCore
  7.  
  8. class Fish(object):
  9. def __init__(self, file_name, living_area):
  10. self.img = QtGui.QImage(file_name)
  11. self.map = QtGui.QPixmap(self.img)
  12. self.img_rect = QtCore.QRectF(0, 0, self.img.width(), self.img.height())
  13. self.flip = 0
  14. self.size = random.uniform(200, 300)
  15. self.aspect = self.img.height() / float(self.img.width())
  16. self.living_area = living_area
  17.  
  18. self.v = random.uniform(1, 10)
  19. self.a = random.uniform(0, 2 * math.pi)
  20. self.x = random.uniform(self.half_width(), living_area.width() - self.half_width())
  21. self.y = random.uniform(self.half_height(), living_area.height() - self.half_height())
  22.  
  23. def half_width(self):
  24. return self.size * 0.5
  25.  
  26. def half_height(self):
  27. return self.size * self.aspect * 0.5
  28.  
  29. def move(self, dt = 1):
  30. ds = Fish.get_ds(self.velocity, self.azimuth, dt)
  31. self.x += ds[0]
  32. self.y += ds[1]
  33.  
  34. change = 1
  35. if self.x < self.half_width():
  36. self.x = self.half_width(); change = 0
  37. elif self.x + self.half_width() > self.living_area.width():
  38. self.x = self.living_area.width() - self.half_width(); change = 0
  39.  
  40. if self.y < self.half_height():
  41. self.y = self.half_height(); change = 0
  42. elif self.y + self.half_height() > self.living_area.height():
  43. self.y = self.living_area.height() - self.half_height(); change = 0
  44.  
  45. if change * random.uniform(0, 1) < 0.001:
  46. self.velocity = random.uniform(1, 10)
  47. self.azimuth = random.uniform(0, 2 * math.pi)
  48.  
  49. flip = 1
  50. if ds[0] < 0: flip = -1
  51. if self.flip != flip:
  52. self.flip = flip
  53. self.map = QtGui.QPixmap(self.img.transformed(QtGui.QTransform(self.flip, 0, 0, 0, 1, 0, 0, 0, 1)))
  54.  
  55. def get_velocity(self):
  56. return self.v
  57.  
  58. def set_velocity(self, value):
  59. if 0 < value:
  60. self.v = value
  61.  
  62. def get_azimuth(self):
  63. return self.a
  64.  
  65. def set_azimuth(self, value):
  66. while value >= 2*math.pi:
  67. value -= 2*math.pi
  68.  
  69. if 0 < value < 2*math.pi:
  70. self.a = value
  71.  
  72. def get_position(self):
  73. return (self.x, self.y)
  74.  
  75. def set_position(self, value):
  76. if 0 <= value[0] <= self.living_area.width() and 0 <= value[1] <= self.living_area.height():
  77. self.x = value[0]
  78. self.y = value[1]
  79.  
  80. velocity = property(get_velocity,set_velocity)
  81. azimuth = property(get_azimuth,set_azimuth)
  82. position = property(get_position,set_position)
  83.  
  84. @staticmethod
  85. def get_ds(velocity, azimuth, dt):
  86. return (velocity * math.cos(azimuth) * dt, velocity * math.sin(azimuth) * dt)
  87.  
  88. def draw(self, painter):
  89. rectangle = QtCore.QRectF(QtCore.QPointF(self.x - self.half_width(), self.y - self.half_height()), QtCore.QSizeF(self.size, self.size*self.aspect))
  90. painter.drawPixmap(rectangle, self.map, self.img_rect)
  91.  
  92. class Aquarium(QtGui.QWidget):
  93. def __init__(self):
  94. super(Aquarium, self).__init__()
  95. self.initUI()
  96.  
  97. def initUI(self):
  98. self.setWindowTitle("Aquarium")
  99.  
  100. self.playground = QtGui.QPixmap()
  101. self.playground.load("aquarium_1024.jpg")
  102. self.playground_rect = QtCore.QRectF(0, 0, self.playground.width(), self.playground.height())
  103. self.setMinimumSize(10, 10)
  104. self.setMaximumSize(self.playground.width(), self.playground.height())
  105.  
  106. self.setGeometry(20, 40, self.playground.width(), self.playground.height())
  107.  
  108. self.fishes = []
  109.  
  110. self.show()
  111.  
  112. self.timer = QtCore.QTimer(self)
  113. QtCore.QObject.connect(self.timer, QtCore.SIGNAL('timeout()'), self.moveFishes)
  114. self.timer.start(10)
  115.  
  116. def addFish(self, fish):
  117. self.fishes.append(fish)
  118.  
  119. def moveFishes(self):
  120. for fish in self.fishes:
  121. fish.move(0.15)
  122. self.update()
  123.  
  124. def closeEvent(self, event):
  125. reply = QtGui.QMessageBox.question(self, 'Aquarium', "Do you really want to devitalize all those fishes?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
  126. if reply == QtGui.QMessageBox.Yes:
  127. event.accept()
  128. else:
  129. event.ignore()
  130.  
  131. def paintEvent(self, event):
  132. painter = QtGui.QPainter()
  133. painter.begin(self)
  134. painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
  135. painter.setRenderHint(QtGui.QPainter.TextAntialiasing, True)
  136. painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)
  137. painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing, True)
  138.  
  139. rectangle = QtCore.QRectF(QtCore.QPoint(0, 0), QtCore.QSizeF(self.playground.width(), self.playground.height()))
  140. painter.drawPixmap(rectangle, self.playground, self.playground_rect)
  141.  
  142. for fish in self.fishes:
  143. fish.draw(painter)
  144.  
  145. painter.end()
  146.  
  147.  
  148. class SuperFish(Fish):
  149. def __init__(self, file_name, living_area):
  150. super(SuperFish, self).__init__(file_name, living_area)
  151.  
  152. def is_collision(self, other):
  153. if (math.fabs(other.position[0] - self.position[0]) < self.half_width() + other.half_width()) and (math.fabs(other.position[1] - self.position[1]) < self.half_height() + other.half_height()):
  154. print("Kolize ryb ...")
  155.  
  156. def __lt__(self, other):
  157. if self.size * (self.size * self.aspect) < other.size * (other.size * other.aspect):
  158. return True
  159. else:
  160. return False
  161.  
  162. def __gt__(self, other):
  163. if self.size * (self.size * self.aspect) > other.size * (other.size * other.aspect):
  164. return True
  165. else:
  166. return False
  167.  
  168.  
  169.  
  170. def main():
  171. app = QtGui.QApplication(sys.argv)
  172. aquarium = Aquarium()
  173. aquarium.addFish(Fish("little_fish.png", aquarium))
  174. aquarium.addFish(SuperFish("medium_fish.png", aquarium))
  175. aquarium.addFish(Fish("big_fish.png", aquarium))
  176. aquarium.addFish(SuperFish("clown_fish.png", aquarium))
  177. sys.exit(app.exec_())
  178.  
  179. if __name__ == '__main__':
  180. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement