Exemplo n.º 1
0
Arquivo: game.py Projeto: Platash/UJ
 def __init__(self):
     self.cam = cv2.VideoCapture(0)
     self.cam.set(cv2.CAP_PROP_FRAME_WIDTH, 600)
     self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)
     self.count = 0
     self.soul = LostSoul()
     self.smile = False
     self.fireballs = []
     self.hp = 100
     self.mana = 10
     self.is_alive = True
     self.attack_my = Attack([200, 0, 200])
     self.attack_cyber = Attack([0, 0, 255])
     self.demon = Cacodemon()
     self.cyber = Cyber()
     self.current_point_prev = [0, 0]
     self.current_point = [0, 0]
     self.points = 0
Exemplo n.º 2
0
Arquivo: game.py Projeto: Platash/UJ
class App:
    def __init__(self):
        self.cam = cv2.VideoCapture(0)
        self.cam.set(cv2.CAP_PROP_FRAME_WIDTH, 600)
        self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 400)
        self.count = 0
        self.soul = LostSoul()
        self.smile = False
        self.fireballs = []
        self.hp = 100
        self.mana = 10
        self.is_alive = True
        self.attack_my = Attack([200, 0, 200])
        self.attack_cyber = Attack([0, 0, 255])
        self.demon = Cacodemon()
        self.cyber = Cyber()
        self.current_point_prev = [0, 0]
        self.current_point = [0, 0]
        self.points = 0

    def __del__(self):
        self.cam.release()

    def run(self):
        vis = cv2.imread("graphics/title.png")
        cv2.putText(vis, 'If you want to live just smile :)', (120, 300), cv2.FONT_HERSHEY_PLAIN, 1.6, (255, 255, 255))
        cv2.imshow('lk_track', vis)
        cv2.waitKey()
        smiles = []
        while self.is_alive:
            ret, frame = self.cam.read()
            frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            vis = frame
            if self.hp <= 0:
                self.die(vis)
            faces = face_cascade.detectMultiScale(frame_gray, 1.3, 5)
            if len(faces) == 0:
                cv2.putText(vis, 'Get back here, you cheater!', (160, 50), cv2.FONT_HERSHEY_PLAIN, 1.6, (255, 255, 255))
                self.hp -= 1
            if len(faces) > 0 and self.count % 5 == 0:
                smiles = smile_cascade.detectMultiScale(frame_gray, 1.5, 10)
                if len(faces) > 1:
                    cv2.putText(vis, 'No mutlipayer mode here!', (160, 50), cv2.FONT_HERSHEY_PLAIN, 2.0, (255, 255, 255))

            for (x_f, y_f, w, h) in faces:
                self.check_collision_soul(x_f, y_f, w, h)
                self.current_point = [x_f, y_f]
                if (abs(self.current_point[0] - self.current_point_prev[0]) > MOVE_THRESHOLD or
                    abs(self.current_point[1] - self.current_point_prev[1]) > MOVE_THRESHOLD) and self.cyber.isRunning:

                    self.cyber.isAngry = True
                self.current_point_prev = self.current_point
                #cv2.rectangle(vis, (x_f, y_f), (x_f + w, y_f + h), (255, 0, 0), 2)

                for (x_s, y_s, w_s, h_s) in smiles:
                    if x_s > x_f and x_s + h_s < x_f + w and y_s > y_f and y_s + h_s < y_f + h:
                        #draw_str(vis, (20, 20), 'smile')
                        #cv2.rectangle(vis, (x_s, y_s), (x_s + w_s, y_s + h_s), (255, 160, 0), 2)
                        if self.mana > 5:
                            self.attack_my.init([x_f + int(w / 2), y_f + int(h / 2)], self.mana)
                            self.mana = 0

                for fireball in self.fireballs:
                    if not fireball.onScreen:
                        self.fireballs.remove(fireball)
                        continue
                    if self.attack_my.isExploding:
                        self.check_collision_fireball(fireball)
                    if fireball.current_point[1] + fireball.r/2 in range(y_f, y_f + h) and \
                                            fireball.current_point[0] + fireball.r/2 in range(x_f, x_f + w):

                        if not fireball.damage_done:
                            self.hp -= 2
                            fireball.damage_done = True
                        fireball.explode()
                        #print(self.hp)
                self.check_collision_demon()


            if self.soul.isRunning:
                roi = vis[max(self.soul.current_point[1], 0):self.soul.current_point[1] + self.soul.h,
                max(self.soul.current_point[0], 0):self.soul.current_point[0] + self.soul.w]

                vis[max(self.soul.current_point[1], 0):self.soul.current_point[1] + self.soul.h,
                max(self.soul.current_point[0], 0):self.soul.current_point[0] + self.soul.w] = self.soul.run(roi)

            if self.demon.isRunning:
                if self.count % 30 == 0:
                    self.fireballs.append(Fireball([int(self.demon.current_point[0] + self.demon.w/4),
                        int(self.demon.current_point[1] + h/2)], self.demon.speed_vector, [255, 0, 0]))

                roi2 = vis[max(self.demon.current_point[1], 0):self.demon.current_point[1] + self.demon.h,
                max(self.demon.current_point[0], 0):self.demon.current_point[0] + self.demon.w]

                vis[max(self.demon.current_point[1], 0):self.demon.current_point[1] + self.demon.h,
                max(self.demon.current_point[0], 0):self.demon.current_point[0] + self.demon.w] = self.demon.run(roi2)

            if self.cyber.isRunning:
                roi3 = vis[max(self.cyber.current_point[1], 0):self.cyber.current_point[1] + self.cyber.h,
                max(self.cyber.current_point[0], 0):self.cyber.current_point[0] + self.cyber.w]

                vis[max(self.cyber.current_point[1], 0):self.cyber.current_point[1] + self.cyber.h,
                max(self.cyber.current_point[0], 0):self.cyber.current_point[0] + self.cyber.w] = self.cyber.run(roi3)
                if self.cyber.isShooting and (not self.cyber.attackGenerated):
                    self.attack_cyber.init([self.cyber.current_point[0] + int(self.cyber.w / 4),
                                            self.cyber.current_point[1] + int(self.cyber.h / 2)], 10)
                    self.cyber.attackGenerated = True

            for fireball in self.fireballs:
                fireball.run(vis)

            if self.count % 15 == 0 and self.mana < 10:
                self.mana += 1

            if self.attack_my.isExploding:
                self.attack_my.run(vis)

            if self.attack_cyber.isExploding:
                self.attack_cyber.run(vis)
                self.hp -= 1

            self.draw_params(vis)
            cv2.imshow('lk_track', vis)
            self.soul.update()
            self.demon.update()
            self.cyber.update()
            self.count += 1
            if self.count > 200:
                self.count = 0

            ch = 0xFF & cv2.waitKey(1)
            if ch == 27:
                break

    def check_collision_fireball(self, fireball):
        if pow(self.attack_my.current_point[0] - fireball.current_point[0], 2) + pow(self.attack_my.current_point[1] - fireball.current_point[1], 2) > \
                pow(self.attack_my.current_r, 2):
            fireball.explode()

    def check_collision_soul(self, x_f, y_f, w, h):
        if self.attack_my.isExploding:
            #print("attack exploding")
            if pow(self.attack_my.current_point[0] - (self.soul.current_point[0] + self.soul.w/2), 2) + \
                    pow(self.attack_my.current_point[1] - (self.soul.current_point[1] + self.soul.h/2), 2) > \
                    pow(self.attack_my.current_r, 2):
                self.points += 1
                fireball = Fireball(self.soul.current_point, self.soul.speed_vector, [255, 0, 0])
                fireball.explode()
                self.fireballs.append(fireball)
                self.soul.init()
                return

        if self.soul.direction == 0:
            #print("direction 0")
            if (self.soul.current_point[0] + self.soul.w) in range(x_f, x_f + w) and \
                    (self.soul.current_point[1] + self.soul.h) in range (y_f, y_f + h):
                if not self.soul.isTurning:
                    fireball_exp = Fireball(self.soul.current_point, self.soul.speed_vector, [255, 0, 0])
                    fireball_exp.explode()
                    self.fireballs.append(fireball_exp)

                    self.soul.speed_vector[0] = -self.soul.speed_vector[0]
                    self.hp -= 2
                    self.soul.isTurning = True

        else:
            #print("direction 1")
            if self.soul.current_point[0] in range(x_f, x_f + w) and \
                    self.soul.current_point[1] + self.soul.h in range(y_f, y_f + h):
                if not self.soul.isTurning:
                    fireball_exp = Fireball(self.soul.current_point, self.soul.speed_vector, [255, 0, 0])
                    fireball_exp.explode()
                    self.fireballs.append(fireball_exp)

                    self.soul.speed_vector[0] = -self.soul.speed_vector[0]
                    self.hp -= 2
                    self.soul.isTurning = True

    def check_collision_demon(self):
        if self.attack_my.isExploding:
            if pow(self.attack_my.current_point[0] - (self.demon.current_point[0] + self.demon.w/2), 2) + \
                           pow(self.attack_my.current_point[1] - (self.demon.current_point[1] + self.demon.h/2 ), 2) > \
                    pow(self.attack_my.current_r, 2):
                self.points += 1
                fireball = Fireball(self.demon.current_point, self.demon.speed_vector, [255, 0, 0])
                fireball.explode()
                self.fireballs.append(fireball)
                self.demon.init()
                return


    def draw_params(self, vis):
         #cv2.putText(vis, self.points, (50, 20), cv2.FONT_HERSHEY_PLAIN, 2.2, (255, 255, 255))
         draw_str(vis, (20, 20), 'POINTS: %d' % self.points)
         hp_bar = 10 + self.hp
         cv2.rectangle(vis, (hp_bar, 450), (110, 470), (0, 0, 160), -1)
         cv2.rectangle(vis, (10, 450), (hp_bar, 470), (60, 160, 60), -1)
         draw_str(vis, (114, 466), 'HP')

         mana_bar = 520 + 10 * self.mana
         cv2.rectangle(vis, (mana_bar, 450), (620, 470), (0, 0, 160), -1)
         cv2.rectangle(vis, (520, 450), (mana_bar, 470), (160, 0, 20), -1)
         draw_str(vis, (470, 466), 'MANA')

    def die(self, vis):
        color = cv2.applyColorMap(vis, cv2.COLORMAP_HOT)
        dst = cv2.addWeighted(vis, 0.4, color, 0.6, -30)
        cv2.putText(dst, 'We are pleased to inform that', (40, 100), cv2.FONT_HERSHEY_PLAIN, 1.6, (255, 255, 255))
        cv2.putText(dst, 'you have died.', (50, 130), cv2.FONT_HERSHEY_PLAIN, 2.2, (255, 255, 255))
        cv2.putText(dst, 'Our cute little creatures', (70, 200), cv2.FONT_HERSHEY_PLAIN, 1.6, (255, 255, 255))
        cv2.putText(dst, 'have found you very tasty.', (70, 230), cv2.FONT_HERSHEY_PLAIN, 1.6, (255, 255, 255))
        cv2.putText(dst, 'Press escape to escape.', (120, 340), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255))
        cv2.imshow('lk_track', dst)
        self.is_alive = False
        cv2.waitKey()
        cv2.destroyAllWindows()