Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import math
- import random
- import sys
- from PyQt4 import QtGui, QtCore
- class Fish(object):
- def __init__(self, file_name, living_area):
- self.img = QtGui.QImage(file_name)
- self.map = QtGui.QPixmap(self.img)
- self.img_rect = QtCore.QRectF(0, 0, self.img.width(), self.img.height())
- self.flip = 0
- self.size = random.uniform(200, 300)
- self.aspect = self.img.height() / float(self.img.width())
- self.living_area = living_area
- self.v = random.uniform(1, 10)
- self.a = random.uniform(0, 2 * math.pi)
- self.x = random.uniform(self.half_width(), living_area.width() - self.half_width())
- self.y = random.uniform(self.half_height(), living_area.height() - self.half_height())
- def half_width(self):
- return self.size * 0.5
- def half_height(self):
- return self.size * self.aspect * 0.5
- def move(self, dt = 1):
- ds = Fish.get_ds(self.velocity, self.azimuth, dt)
- self.x += ds[0]
- self.y += ds[1]
- change = 1
- if self.x < self.half_width():
- self.x = self.half_width(); change = 0
- elif self.x + self.half_width() > self.living_area.width():
- self.x = self.living_area.width() - self.half_width(); change = 0
- if self.y < self.half_height():
- self.y = self.half_height(); change = 0
- elif self.y + self.half_height() > self.living_area.height():
- self.y = self.living_area.height() - self.half_height(); change = 0
- if change * random.uniform(0, 1) < 0.001:
- self.velocity = random.uniform(1, 10)
- self.azimuth = random.uniform(0, 2 * math.pi)
- flip = 1
- if ds[0] < 0: flip = -1
- if self.flip != flip:
- self.flip = flip
- self.map = QtGui.QPixmap(self.img.transformed(QtGui.QTransform(self.flip, 0, 0, 0, 1, 0, 0, 0, 1)))
- def get_velocity(self):
- return self.v
- def set_velocity(self, value):
- if 0 < value:
- self.v = value
- def get_azimuth(self):
- return self.a
- def set_azimuth(self, value):
- while value >= 2*math.pi:
- value -= 2*math.pi
- if 0 < value < 2*math.pi:
- self.a = value
- def get_position(self):
- return (self.x, self.y)
- def set_position(self, value):
- if 0 <= value[0] <= self.living_area.width() and 0 <= value[1] <= self.living_area.height():
- self.x = value[0]
- self.y = value[1]
- velocity = property(get_velocity,set_velocity)
- azimuth = property(get_azimuth,set_azimuth)
- position = property(get_position,set_position)
- @staticmethod
- def get_ds(velocity, azimuth, dt):
- return (velocity * math.cos(azimuth) * dt, velocity * math.sin(azimuth) * dt)
- def draw(self, painter):
- rectangle = QtCore.QRectF(QtCore.QPointF(self.x - self.half_width(), self.y - self.half_height()), QtCore.QSizeF(self.size, self.size*self.aspect))
- painter.drawPixmap(rectangle, self.map, self.img_rect)
- class Aquarium(QtGui.QWidget):
- def __init__(self):
- super(Aquarium, self).__init__()
- self.initUI()
- def initUI(self):
- self.setWindowTitle("Aquarium")
- self.playground = QtGui.QPixmap()
- self.playground.load("aquarium_1024.jpg")
- self.playground_rect = QtCore.QRectF(0, 0, self.playground.width(), self.playground.height())
- self.setMinimumSize(10, 10)
- self.setMaximumSize(self.playground.width(), self.playground.height())
- self.setGeometry(20, 40, self.playground.width(), self.playground.height())
- self.fishes = []
- self.show()
- self.timer = QtCore.QTimer(self)
- QtCore.QObject.connect(self.timer, QtCore.SIGNAL('timeout()'), self.moveFishes)
- self.timer.start(10)
- def addFish(self, fish):
- self.fishes.append(fish)
- def moveFishes(self):
- for fish in self.fishes:
- fish.move(0.15)
- self.update()
- def closeEvent(self, event):
- reply = QtGui.QMessageBox.question(self, 'Aquarium', "Do you really want to devitalize all those fishes?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
- if reply == QtGui.QMessageBox.Yes:
- event.accept()
- else:
- event.ignore()
- def paintEvent(self, event):
- painter = QtGui.QPainter()
- painter.begin(self)
- painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
- painter.setRenderHint(QtGui.QPainter.TextAntialiasing, True)
- painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)
- painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing, True)
- rectangle = QtCore.QRectF(QtCore.QPoint(0, 0), QtCore.QSizeF(self.playground.width(), self.playground.height()))
- painter.drawPixmap(rectangle, self.playground, self.playground_rect)
- for fish in self.fishes:
- fish.draw(painter)
- painter.end()
- class SuperFish(Fish):
- def __init__(self, file_name, living_area):
- super(SuperFish, self).__init__(file_name, living_area)
- def is_collision(self, other):
- 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()):
- print("Kolize ryb ...")
- def __lt__(self, other):
- if self.size * (self.size * self.aspect) < other.size * (other.size * other.aspect):
- return True
- else:
- return False
- def __gt__(self, other):
- if self.size * (self.size * self.aspect) > other.size * (other.size * other.aspect):
- return True
- else:
- return False
- def main():
- app = QtGui.QApplication(sys.argv)
- aquarium = Aquarium()
- aquarium.addFish(Fish("little_fish.png", aquarium))
- aquarium.addFish(SuperFish("medium_fish.png", aquarium))
- aquarium.addFish(Fish("big_fish.png", aquarium))
- aquarium.addFish(SuperFish("clown_fish.png", aquarium))
- sys.exit(app.exec_())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement