예제 #1
0
    def make(self, gameDisplay, back):

        gameDisplay = display_init()
        sound = True

        try:
            pygame.mixer.init()
        except Exception as err:
            sound = False
            print(_('error with sound'), err)

        black = (0, 0, 0)
        white = (255, 255, 255)
        clock = pygame.time.Clock()
        timer = pygame.time.Clock()

        crashed = False
        disp_width = 600
        disp_height = 600

        press = 0

        info = pygame.display.Info()

        if not (gameDisplay):

            gameDisplay = pygame.display.set_mode(
                (info.current_w, info.current_h))

            pygame.display.set_caption(_("Stick Hero"))
            # gameicon=pygame.image.load('data/images/icon.png')
            # pygame.display.set_icon(gameicon)

        fruit = pygame.image.load("images/welcomescreen/fruit.png")
        fruit = pygame.transform.scale(fruit, (int(sx(40)), int(sy(40))))

        scoreplate = pygame.image.load("images/scoreplate.png").convert()
        scoreplate = pygame.transform.scale(scoreplate,
                                            (int(sx(40)), int(sy(50))))

        scoreplate.set_alpha(100)

        help = pygame.image.load("images/help.png")
        help = pygame.transform.scale(help, (int(sx(40)), int(sy(40))))

        hero = pygame.image.load("images/hero.png")
        hero = pygame.transform.scale(hero, (int(sx(38)), int(sy(38))))

        play = pygame.image.load("images/play.png")
        play = pygame.transform.scale(play, (int(sx(170)), int(sy(170))))

        beta = pygame.image.load("images/alpha.png")
        beta = pygame.transform.scale(beta, (int(sx(105)), int(sy(248))))

        # herotr=hero

        # herotr=pygame.transform.scale(hero,(30,26))

        # hero1=pygame.image.load("images/hero1.png")

        font_path = "fonts/Arimo.ttf"
        font_size = int(sx(70))
        font1 = pygame.font.Font(font_path, font_size)
        font2 = pygame.font.Font("fonts/Arimo.ttf", int(sx(15)))
        font3 = pygame.font.Font("fonts/Arimo.ttf", int(sx(40)))
        font4 = pygame.font.Font("fonts/Arimo.ttf", int(sx(20)))

        down = 1
        bounce = 0
        i = 0

        maxscore = 0
        fruitmaxscore = 0
        path = get_score_path()

        if os.path.getsize(path) == 0:

            with open(path, 'wb') as output:
                pickle.dump(maxscore, output, pickle.HIGHEST_PROTOCOL)
                pickle.dump(fruitmaxscore, output, pickle.HIGHEST_PROTOCOL)

        with open(path, 'rb') as input:  # REading
            maxscore = pickle.load(input)
            fruitmaxscore = pickle.load(input)

        # GAME LOOP BEGINS !!!

        while not crashed:
            # Gtk events
            mouse_button_up = False

            while Gtk.events_pending():
                Gtk.main_iteration()
            for event in pygame.event.get():
                # totaltime+=timer.tick()
                if event.type == pygame.QUIT:
                    crashed = True
                elif event.type == pygame.MOUSEBUTTONUP:
                    mouse_button_up = True

            mos_x, mos_y = pygame.mouse.get_pos()

            # print event

            i += 1

            if (i > 20):
                i = 0

            if (i % 3 == 0):
                if (down == 1):
                    bounce += 1
                    if (bounce > 8):
                        down = 0
                if (down == 0):
                    bounce -= 1
                    if (bounce < 0):
                        down = 1

            gameDisplay.fill(white)
            gameDisplay.blit(back, (sx(350, 1), 0))

            # scoreplate.set_alpha(20)
            # gameDisplay.blit(scoreplate,(540,40))

            gameDisplay.blit(help, (sx(380, 1), sy(20)))
            # score blitting
            gameDisplay.blit(play, (sx(510, 1), sy(200 + bounce)))

            gameDisplay.blit(beta, (sx(540, 1), sy(470)))

            gameDisplay.blit(hero, (sx(568, 1), sy(432)))

            # score check

            if fruit.get_rect(center=(sx(790 + 20, 1),
                                      sy(20 + 20))).collidepoint(mos_x, mos_y):
                if (pygame.mouse.get_pressed())[0] == 1 and press == 0:

                    gameDisplay.blit(scoreplate, (sx(780, 1), sy(40)))
                    # gameDisplay.blit(scoreplate,(780,60))

                    head1 = font2.render(_(str(fruitmaxscore)), 1, (white))
                    gameDisplay.blit(head1, (sx(785, 1), sy(60)))

                if mouse_button_up:
                    press = 0

            # GAME START

            if play.get_rect(center=(sx(510 + 85, 1),
                                     sy(200 + bounce + 85))).collidepoint(
                                         mos_x, mos_y):
                if (pygame.mouse.get_pressed())[0] == 1 and press == 0:

                    return 2
                '''
                if event.type==pygame.MOUSEBUTTONUP:
                    press=0

                '''

            # Help menu

            if help.get_rect(center=(sx(380 + 20, 1),
                                     sy(20 + 20))).collidepoint(mos_x, mos_y):
                if (pygame.mouse.get_pressed())[0] == 1 and press == 0:

                    a = rulescreen()
                    catch = a.make(gameDisplay)

                    if (catch == 0):
                        return 2
                '''

                if event.type==pygame.MOUSEBUTTONUP:
                    press=0
                '''

            gameDisplay.blit(fruit, (sx(780, 1), sy(20)))

            head1 = font1.render(_("STICK"), 1, (black))
            gameDisplay.blit(head1, (sx(500, 1), sy(20)))

            head2 = font1.render(_("HERO"), 1, (black))
            gameDisplay.blit(head2, (sx(510, 1), sy(80)))

            # fruitscores=font2.render(str(fruitscore),1,(0,0,0))
            # gameDisplay.blit(fruitscores,(770+fruitscoreshift,13))

            # left and right black background patches

            pygame.draw.rect(gameDisplay, black, (0, 0, sx(350, 1), sy(768)))

            pygame.draw.rect(gameDisplay, black,
                             (sx(840, 1), 0, sx(693, 1), sy(768)))

            pygame.display.update()
            clock.tick(60)

            if crashed == True:  # Game crash or Close check
                pygame.quit()
                sys.exit()

        # Just a window exception check condition

        event1 = pygame.event.get()
        if event1.type == pygame.QUIT:
            crashed = True

        if crashed == True:
            pygame.quit()
            sys.exit()
예제 #2
0
    def make(self):

        gameDisplay = display_init()
        sound = True

        try:
            pygame.mixer.init()
        except Exception as err:
            sound = False
            print('error with sound', err)

        black = (0, 0, 0)
        white = (255, 255, 255)
        clock = pygame.time.Clock()
        timer = pygame.time.Clock()

        crashed = False
        disp_width = 600
        disp_height = 600

        press = 0

        info = pygame.display.Info()

        if not (gameDisplay):

            gameDisplay = pygame.display.set_mode(
                (info.current_w, info.current_h))

            pygame.display.set_caption(_("Stick Hero"))
            gameicon = pygame.image.load('images/icon.png')
            pygame.display.set_icon(gameicon)

        hero = scale_img("images/hero.png")
        herotr = hero

        hero1 = scale_img("images/hero1.png")
        hero2 = scale_img("images/hero2.png")
        hero3 = scale_img("images/hero3.png")

        herodown = pygame.transform.flip(hero, False, True)
        hero1down = pygame.transform.flip(hero1, False, True)
        hero2down = pygame.transform.flip(hero2, False, True)
        hero3down = pygame.transform.flip(hero3, False, True)

        scoreplate = scale_img("images/scoreplate.png").convert()
        scoreplate.set_alpha(50)

        stick = scale_img("images/stick.png").convert()
        # background=pygame.image.load("images/background.png").convert()
        alpha = scale_img("images/alpha.png").convert()
        beta = scale_img("images/beta.png").convert()
        gamma = scale_img("images/gamma.png").convert()
        delta = scale_img("images/delta.png").convert()
        back1 = pygame.image.load("background/back1.png").convert()
        back1 = pygame.transform.scale(back1, (int(sx(1280)), int(sy(720))))
        back2 = pygame.image.load("background/back2.png").convert()
        back2 = pygame.transform.scale(back2, (int(sx(1280)), int(sy(720))))

        back3 = pygame.image.load("background/back3.jpg").convert()
        back3 = pygame.transform.scale(back3, (int(sx(1280)), int(sy(720))))

        back4 = pygame.image.load("background/back4.png").convert()
        back4 = pygame.transform.scale(back4, (int(sx(1280)), int(sy(720))))

        back5 = pygame.image.load("background/back5.jpg").convert()
        back5 = pygame.transform.scale(back5, (int(sx(1280)), int(sy(720))))

        back6 = pygame.image.load("background/back6.jpg").convert()
        back6 = pygame.transform.scale(back6, (int(sx(1280)), int(sy(720))))

        back7 = pygame.image.load("background/back7.png").convert()
        back7 = pygame.transform.scale(back7, (int(sx(1280)), int(sy(720))))

        fruit = scale_img("images/fruit.png").convert()
        # BIRD FRAMES

        frame1 = pygame.image.load("birds/1.png")
        # frame1=pygame.transform.flip(frame1,True,False)

        frame2 = pygame.image.load("birds/2.png")
        # frame2=pygame.transform.flip(frame2,True,False)

        frame3 = pygame.image.load("birds/3.png")
        # frame3=pygame.transform.flip(frame3,True,False)

        frame4 = pygame.image.load("birds/4.png")
        # frame4=pygame.transform.flip(frame4,True,False)

        frame5 = pygame.image.load("birds/5.png")
        # frame5=pygame.transform.flip(frame5,True,False)

        frame6 = pygame.image.load("birds/6.png")
        # frame6=pygame.transform.flip(frame6,True,False)

        frame7 = pygame.image.load("birds/7.png")
        # frame7=pygame.transform.flip(frame7,True,False)

        frame8 = pygame.image.load("birds/8.png")
        # frame8=pygame.transform.flip(frame8,True,False)

        birds = [
            frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8
        ]

        backgroundlist = [back1, back2, back3, back4, back5, back6, back7]

        back = backgroundlist[randint(0, 6)]
        # back=back5

        # stickx1=455
        # sticky1=50

        herokicklist = [hero, herotr]

        herolist = [hero, hero1, hero2, hero3]

        herodownlist = [herodown, hero1down, hero2down, hero3down]

        pillarlist = [alpha, beta, gamma, delta]

        # Sound loads

        pop1 = pygame.mixer.Sound("sound/pop_1.ogg")
        pop2 = pygame.mixer.Sound("sound/pop_2.ogg")

        stickgrow = pygame.mixer.Sound("sound/stick_grow_loop.ogg")

        kick = pygame.mixer.Sound("sound/kick.ogg")

        landing = pygame.mixer.Sound("sound/fall.ogg")

        scoresound = pygame.mixer.Sound("sound/score.ogg")

        dead = pygame.mixer.Sound("sound/dead.ogg")
        kick = pygame.mixer.Sound("sound/kick.ogg")
        rollupdown = pygame.mixer.Sound("sound/roll_up_down.ogg")
        eating_fruit = pygame.mixer.Sound("sound/eating_fruit.ogg")
        perfectsound = pygame.mixer.Sound("sound/perfect.ogg")

        flappy = pygame.mixer.Sound("sound/bird/bonus_loop_bird.ogg")
        chichi = pygame.mixer.Sound("sound/bird/bonus_trigger_bird.ogg")

        font_path = "fonts/Arimo.ttf"
        font_size = int(sx(40))
        font1 = pygame.font.Font(font_path, font_size)
        font2 = pygame.font.Font("fonts/Arimo.ttf", int(sx(25)))
        font3 = pygame.font.Font("fonts/Arimo.ttf", int(sx(40)))
        font4 = pygame.font.Font("fonts/Arimo.ttf", int(sx(20)))

        # VARIABLE INITIALIZATION

        stickx1 = stickx = sx(455, 1)
        sticky1 = sticky = sy(472)

        anglenum = 90
        angle = (pi / 180) * anglenum

        sticklength = 0

        time = 0
        flag = 0  # stick fall flag
        keypressflag = 0

        moveit = 0  # hero move flag

        herox = sx(429, 1)
        heroy = sy(442)

        heropointer = 0

        i = 0
        j = 0
        k = 0

        pillar1x = sx(355, 1)
        msgx = pillar2x = sx(650, 1)

        pillar3x = randint(int(sx(845, 1)), int(sx(900, 1)))

        pillar1 = alpha
        pillar2 = beta
        pillar3 = pillarlist[randint(0, 2)]

        herofall = 0
        herofallflag = 0

        pillarmoveflag = 0

        stickmove = 0

        backx = 0

        pillarfound = 0

        score = 0

        keyinit = 0

        speed = sx(8)

        acc1 = acc2 = acc3 = 0

        pillarfast = 0

        pillardist = randint(int(sx(60, 1)), int(sx(260, 1)))
        lastpillardist = pillardist

        stickgrowsound = 0

        ext = 0

        backx1 = sx(350, 1)
        backx2 = sx(1630, 1)

        upsidedown = False

        keypress = 0

        if (pillar1x > (sx(429, 1)) and pillar1x < (sx(840, 1))):
            # acc1=2
            pillar2nd = pillar1x

        if (pillar2x > (sx(429, 1)) and pillar2x < (sx(840, 1))):
            # acc2=2
            pillar2nd = pillar2x

        if (pillar3x > (sx(429, 1)) and pillar3x < (sx(840, 1))):
            # acc3=2
            pillar2nd = pillar3x

        bouncedown = True
        bounce = 0

        fruitx = 0

        fruitgot = False
        fruitflag = 0

        herod = sy(33)

        fruitscore = 0
        score = 0

        path = get_score_path()
        if not os.path.exists(path):
            open(path, 'w+')

        if os.path.getsize(path) == 0:

            with open(path, 'wb') as output:
                pickle.dump(0, output, pickle.HIGHEST_PROTOCOL)
                pickle.dump(0, output, pickle.HIGHEST_PROTOCOL)

        with open(path, 'rb') as input:  # REading
            fruitscore = pickle.load(input)
            fruitscore = pickle.load(input)

        scoreshift = 0
        fruitscoreshift = 0
        shift1 = 1
        shift2 = 1

        perfectflag = 0
        vanish = 0
        perfect = 0
        b1 = 0
        b2 = 2
        b3 = 4
        b4 = 6

        birdx = sx(900, 1)
        birdxslow = sx(950, 1)
        birdxfast = sx(860, 1)

        birdgroupshow = 0
        birdsingleshow = 0
        birdmainshow = 0
        birdpickup = 0
        birdsound = 0

        flagchk1 = flagchk2 = flagchk3 = 0

        flagchk = 0

        catch = 0

        # lastpillardist=pillar2x-457

        # GAME LOOP BEGINS !!!

        while not crashed:
            # Gtk events
            keydown_pressed = False
            keyup_pressed = False
            while Gtk.events_pending():
                Gtk.main_iteration()
            for event in pygame.event.get():
                # totaltime+=timer.tick()
                if event.type == pygame.QUIT:
                    crashed = True
                elif event.type == pygame.KEYDOWN and event.key == 273:
                    keydown_pressed = True
                elif event.type == pygame.KEYUP and event.key == 273:
                    keyup_pressed = True

            mos_x, mos_y = pygame.mouse.get_pos()

            # print "hello"

            if (catch == 0):
                b = welcomescreen()
                catch = b.make(gameDisplay, back)

            gameDisplay.fill(white)
            gameDisplay.blit(back, (backx1, 0))
            gameDisplay.blit(back, (backx2, 0))
            gameDisplay.blit(fruit, (sx(800, 1), sy(20)))

            # scoreplate.set_alpha(20)
            gameDisplay.blit(scoreplate, (sx(540, 1), sy(40)))

            # score blitting

            # Bird frames processing

            if (i % 7 == 0):
                b1 += 1
                if (b1 == 8):
                    b1 = 0

                b2 += 1
                if (b2 == 8):
                    b2 = 0

                b3 += 1
                if (b3 == 8):
                    b3 = 0

                b4 += 1
                if (b4 == 8):
                    b4 = 0

            if (birdgroupshow == 1):

                gameDisplay.blit(
                    pygame.transform.scale(birds[b1],
                                           (int(sx(57)), int(sy(39)))),
                    (birdx + sx(10), sy(100)))
                gameDisplay.blit(
                    pygame.transform.scale(
                        birds[b2], (int(sx(57 - 10)), int(sy(39 - 10)))),
                    (birdx - sx(50), sy(115)))

                gameDisplay.blit(
                    pygame.transform.scale(
                        birds[b3], (int(sx(57 - 20)), int(sy(39 - 20)))),
                    (birdx, sy(110 + 30)))

            if (birdsingleshow == 1):

                gameDisplay.blit(
                    pygame.transform.scale(
                        birds[b3], (int(sx(57 + 10)), int(sy(39 + 10)))),
                    (birdxslow, sy(110)))

            # Birds movement

            if (birdx >= (sx(300, 1)) and birdgroupshow == 1):
                birdx -= sx(3)

            if (birdxslow >= (sx(300, 1)) and birdsingleshow == 1):
                birdxslow -= sx(2)

            if (birdmainshow == 1):

                birdxfast -= birdspeed

            # Birds coordinates updates

            if (birdx < (sx(300, 1))):
                birdx = sx(900, 1)
                birdgroupshow = 0

            if (birdxslow < (sx(300, 1))):
                birdxslow = sx(950, 1)
                birdsingleshow = 0

            if ((birdxfast + sx(30)) <= (sx(300, 1))):
                birdxfast = sx(860, 1)
                birdmainshow = 0

            if (birdsingleshow == 0 and i % 18 == 0):
                r = randint(0, 99)
                if (r % 18 == 0):
                    birdgroupshow = 1
                    birdsingleshow = 1

            # BIRD PICK'S YOU UP

            if (pygame.transform.scale(
                    birds[b4],
                (int(sx(57 + 30)), int(sy(39 + 20)))).get_rect(
                    center=(birdxfast + sx(40), sy(400 + 20))).colliderect(
                        herolist[j].get_rect(center=(herox + sx(18), heroy +
                                                     herod + sy(15))))):

                herox = birdxfast + sx(30)
                birdpickup = 1

                if (birdsound == 0):
                    birdsound = 1

                moveit = 0

                if (herox < (sx(320, 1))):

                    ext = 1

            #pygame.draw.circle(gameDisplay,white, (birdxfast,400) ,3, 2)

            #pygame.draw.circle(gameDisplay,white, (herox+18,heroy+herod) ,3, 2)

            if (birdsound == 1):
                birdsound = 2
                flappy.play(0)

            scores = font1.render(str(score), 1, (255, 255, 255))
            gameDisplay.blit(scores, (sx(580 + scoreshift, 1), sy(40)))
            fruitscores = font2.render(str(fruitscore), 1, (0, 0, 0))
            gameDisplay.blit(fruitscores,
                             (sx(770 + fruitscoreshift, 1), sy(13)))

            if (perfect == 1):

                vanish -= 1

                msg1 = font3.render(_("Perfect!!!"), 1, (0, 0, 0))
                gameDisplay.blit(msg1, (sx(510, 1), sy(120)))

                msg2 = font4.render(_("+1"), 1, (0, 0, 0))
                gameDisplay.blit(msg2, (msgx, sy(460 + vanish)))

                if (vanish < -50):
                    perfect = 0
                    vanish = 0

            # fruits bounce up-down

            if ((pillar2nd - sx(429)) > (sx(80, 1)) and fruitx >= (sx(429, 1))
                    and fruitx != 0 and not (fruitgot)):
                gameDisplay.blit(fruit, (fruitx, sy(480 + bounce)))

            if (bouncedown == True):
                if (i % 6 == 0):
                    bounce += 1
                    if (bounce > 5):
                        bouncedown = not (bouncedown)
            else:
                if (i % 6 == 0):
                    bounce -= 1
                    if (bounce < 0):
                        bouncedown = not (bouncedown)

            # Fruit vanish condition

            if (fruitflag == 0 and herodownlist[j].get_rect(center=(
                    herox + sx(18), heroy + herod + sy(10))).colliderect(
                        fruit.get_rect(center=(fruitx + sx(14),
                                               sy(480 + bounce + 10))))):
                fruitgot = not fruitgot
                fruitflag = 1
                if (herofallflag != 1):
                    fruitscore += 1
                    # rollupdown.stop()
                    # eating_fruit.stop()
                    eating_fruit.play()

            # if(upsidedown==1 and herox+15>=fruitx):
            #    fruitgot=not fruitgot

            #pygame.draw.circle(gameDisplay,black, (herox+15,heroy+60) ,3, 2)

            #pygame.draw.circle(gameDisplay,black, (herox+18,heroy+10+33) ,3, 2)

            #pygame.draw.circle(gameDisplay,black, (fruitx+13,480+bounce+10) ,3, 2)

            # backgound frames roll-over

            if (backx1 < -(sx(1280))):
                # if not(back==back2):
                backx1 = sx(1270)
                # else:
                #    backx1=1260
            if (backx2 < -(sx(1280))):
                # if not(back==back2):
                backx2 = sx(1270)
                # else:
                #    backx2=1260

            if (i > 20):
                i = 0

            i += 1
            if (i % 4 == 0):
                j += 1

            if (j == 4):
                j = 0

            if (moveit == 1):

                if (upsidedown == False):
                    herod = 0
                    gameDisplay.blit(herolist[j],
                                     (herox, heroy +
                                      (herod - sy(birdpickup * 5))))
                else:
                    herod = 33
                    gameDisplay.blit(herodownlist[j],
                                     (herox, heroy + sy(herod)))

            # Main bird display

            if (birdmainshow == 1):

                gameDisplay.blit(
                    pygame.transform.scale(
                        birds[b4], (int(sx(57 + 30)), int(sy(39 + 20)))),
                    (birdxfast, sy(400)))

            # Inverted hero collsion with pillar test

            if (upsidedown == True and (herox + sx(30)) >= pillar2nd):

                herofall = 1
                moveit = 0
                flag = 1

            # print upsidedown

            if (moveit == 0):

                if (k <= 6):
                    gameDisplay.blit(herokicklist[0], (herox, heroy))
                if (k <= 12):
                    gameDisplay.blit(herokicklist[1],
                                     (herox - sx(1), heroy + sy(2)))

                if (keypressflag == 1):
                    k += 1
                if (k == 12):
                    k = 0

            if (moveit == 1):  # hero moving right
                herox += sx(4)
                heropointer += sx(4)
                backx1 -= sx(1)
                backx2 -= sx(1)

            if (herox >= (sx(845, 1))):
                herofallflag = 1
                herofall = 1
                moveit = 0
                flag = 1

            gameDisplay.blit(pillar1, (pillar1x, sy(470)))

            gameDisplay.blit(pillar2, (pillar2x, sy(470)))

            gameDisplay.blit(pillar3, (pillar3x, sy(470)))

            #pygame.draw.circle(gameDisplay,white, (birdxfast+40,400+40) ,3, 2)

            #pygame.draw.circle(gameDisplay,white, (herox+18,heroy+herod+15) ,3, 2)

            # sticklength calculation

            if (flag == 0):

                if (stickx == stickx1):
                    sticklength = abs(sticky - sticky1)
                if (sticky == sticky1):
                    sticklength = abs(stickx1 - stickx)

            # stick fall from Vertical to Horizontal

            if (anglenum > 0 and flag == 1):

                anglenum -= 0.03 * (time) * (time)
                if (anglenum <= 0):
                    kick.stop()
                    landing.play(0)
                    anglenum = 0
                    sticky1 = sy(472)
                    stickx1 = stickx + sticklength
                    # sticklength=stickx1-stickx
                    flag = 0
                    moveit = 1
                    time = 0

                    # 2nd PILLAR DETECTION
                    '''
                    
                    if(pillar1x>429 and pillar1x<840):
                        #acc1=2
                        pillar2nd=pillar1x
                 
                    if(pillar2x>429 and pillar2x<840):
                        #acc2=2
                        pillar2nd=pillar2x
                       
                    if(pillar3x>429 and pillar3x<840):
                        #acc3=2
                        pillar2nd=pillar3x
                    
                    '''

                    # Birds Speed calculation

                    if (sticklength > sx(190, 1) and randint(0, 2) == 0):
                        birdspeed = int(sx(1680) / sticklength)
                        birdspeed += sx(3)
                        birdmainshow = 1
                        chichi.play(0)

                    colortest = gameDisplay.get_at(
                        (int(sx(457, 1) + sticklength + sx(2)),
                         int(heroy + sy(40))))

                    if not ((colortest[0] == 0 and colortest[1] == 0
                             and colortest[2] == 0) or
                            (colortest[0] == 1 and colortest[1] == 1
                             and colortest[2] == 1)):
                        herofallflag = 1
                    colortest = gameDisplay.get_at(
                        (int(sx(457, 1) + sticklength), int(heroy + sy(30))))

                    if (colortest[0] == 255):
                        perfectflag = 1
                        perfectsound.play()
                        perfect = 1
                        msgx = pillar2nd

                time += 1

            # stick fall from horizontal to bottom

            if (herofall == 1):

                if (anglenum > -90):
                    anglenum -= 0.03 * (time) * (time)

                    # print "hey"

                    if (anglenum <= -90):

                        # print "hey"
                        anglenum = -90
                        sticky1 = sticky + sticklength
                        stickx1 = sx(455, 1)
                        # sticklength=stickx1-stickx
                        # flag=0
                        moveit = 1
                        time = 0

                    time += 1

            # angle calculation

            angle = (pi / 180) * anglenum

            # keypress check

            if (keyinit == 0):

                if keydown_pressed:
                    # jump.play(0)

                    keypressflag = 1
                    keyinit = 1
                    stickgrowsound = 1

            if (keypressflag == 1):

                if keyup_pressed:
                    flag = 1

                    stickgrow.stop()
                    kick.stop()
                    kick.play()

                    kick.play(0)
                    keypressflag = 0

            if (stickgrowsound == 1):

                stickgrow.play(-1)
            '''            
            if(pillar1x>429 and pillar1x<840):
                #acc1=2
                pillar2nd=pillar1x
                    
            if(pillar2x>429 and pillar2x<840):
                #acc2=2
                pillar2nd=pillar2x
                        
            if(pillar3x>429 and pillar3x<840):
                #acc3=2
                pillar2nd=pillar3x
            
            '''

            if (moveit == 1 and heropointer <= (pillar2nd - sx(457, 1))):

                if keydown_pressed and keypress == 0:
                    # jump.play(0)

                    rollupdown.play()
                    upsidedown = not upsidedown

                    keypress = 1

            if keyup_pressed and keypress == 1:

                keypress = 0

            if keypressflag == 1:

                stickgrowsound = 0
                if (sticky1 >= 0):
                    sticky1 -= sy(5)

            # print pillar2nd

            # coordinates calculation while stick free fall

            if (flag == 1):
                sticky1 = sy(472) - sticklength * sin(angle)
                stickx1 = sx(455, 1) + sticklength * cos(angle)

            # zeroing the length of the stick as it surpassed left boundary

            if (stickx <= (sx(349, 1))):

                stickmove = 0
                stickx1 = stickx = sx(455, 1)
                sticky1 = sticky = sy(472)

            if ((stickx1 - stickx) != 0 or sticky1 - sticky != 0):
                pygame.draw.line(gameDisplay, black, (stickx1, sticky1),
                                 (stickx, sticky), int(sx(6)))

            # test circles

            #pygame.draw.circle(gameDisplay,white, (herox+30,heroy+30) ,2, 2)
            #pygame.draw.circle(gameDisplay,white, (457+sticklength+2,heroy+30) ,2, 2)

            # if hero has to fall

            if ((herox + sx(30)) >= sx(457, 1) + sticklength
                    and herofallflag == 1):
                herofall = 1
                moveit = 0
                flag = 1

            # if hero has to stop
            if ((herox + sx(30)) >= sx(457, 1) + sticklength
                    and herofallflag == 0 and moveit == 1 and heroy < sy(768)):

                color = gameDisplay.get_at(
                    (int(herox + sx(30 + 4)), int(heroy + sy(40))))

                if not ((color[0] == 0 and color[1] == 0 and color[2] == 0) or
                        (color[0] == 1 and color[1] == 1 and color[2] == 1)):
                    moveit = 0
                    pillarmoveflag = 1
                    stickmove = 1

                    if (pillar1x > sx(840, 1)):
                        acc1 = 1
                        acc2 = 0
                        acc3 = 0
                        pillarfast = pillar1x

                    if (pillar2x > sx(840, 1)):
                        acc1 = 0
                        acc2 = 1
                        acc3 = 0
                        pillarfast = pillar2x

                    if (pillar3x > sx(840, 1)):
                        acc1 = 0
                        acc2 = 0
                        acc3 = 1
                        pillarfast = pillar3x
                    '''
                    
                        
                    if(pillar1x>429 and pillar1x<840):
                        #acc1=2
                        pillar2nd=pillar1x
                    
                    if(pillar2x>429 and pillar2x<840):
                        #acc2=2
                        pillar2nd=pillar2x
                        
                    if(pillar3x>429 and pillar3x<840):
                        #acc3=2
                        pillar2nd=pillar3x 
                        
                    
                    '''

                    time = abs((heropointer) / speed)
                    # print heropointer

                    acc = abs(
                        ((pillarfast) - (sx(429, 1) + pillardist)) / time)

                    # print
                    # str(((pillarfast)-(429+pillardist)))+str(heropointer)

                    # print pillardist
                    # print heropointer

            if (moveit == 0 and pillarmoveflag == 1):

                if (stickmove == 1):
                    stickx1 -= speed
                    stickx -= speed

                if (heropointer > 0):

                    if (acc1 == 0):
                        pillar1x -= (speed)
                    if (acc2 == 0):
                        pillar2x -= (speed)
                    if (acc3 == 0):
                        pillar3x -= (speed)

                    herox -= speed
                    heropointer -= speed
                    fruitx -= speed
                    # print "help"

                    # if(abs(pillarfast-450)>=pillardist):
                    if (acc1 == 1):
                        pillar1x -= (acc)
                        pillarfast = pillar1x

                    if (acc2 == 1):
                        pillar2x -= (acc)
                        pillarfast = pillar2x

                    if (acc3 == 1):
                        pillar3x -= (acc)
                        pillarfast = pillar3x

                else:
                    # if ((heropointer<=0) and
                    # (abs(pillarfast-450)<=pillardist)):

                    landing.stop()
                    scoresound.stop()
                    scoresound.play(0)

                    # print "hello"

                    vanish = 0

                    pillarmoveflag = 0

                    if (lastpillardist < sx(160, 1)):
                        pillardist = randint(int(sx(160, 1)), int(sx(260, 1)))
                        lastpillardist = pillardist
                    else:
                        pillardist = randint(int(sx(100, 1)), int(sx(160, 1)))
                        lastpillardist = pillardist

                    if (score < (10**shift1) - 1):
                        if not (perfectflag == 1):
                            score += 1
                        else:
                            score += 2
                    else:
                        if not (perfectflag == 1):
                            score += 1
                        else:
                            score += 2
                        shift1 += 1
                        scoreshift -= 10

                    if not (fruitscore < (10**shift2) - 1):

                        # fruitscore+=1
                        shift2 += 1
                        fruitscoreshift -= 4

                    perfectflag = 0

                    pillar1x += speed
                    pillar2x += speed
                    pillar3x += (speed)

                    # re-initialization of the variables

                    stickx1 = stickx = sx(455, 1)
                    sticky1 = sticky = sy(472)

                    anglenum = 90
                    angle = (pi / 180) * anglenum

                    sticklength = 0

                    time = 0
                    flag = 0  # stick fall flag
                    keypressflag = 0

                    moveit = 0  # hero move flag

                    herox = sx(429, 1)
                    heroy = sy(442)
                    heropointer = 0

                    i = 0
                    j = 0
                    k = 0

                    fruitgot = False
                    fruitflag = 0

                    herofall = 0
                    herofallflag = 0

                    pillarmoveflag = 0

                    stickmove = 0

                    keyinit = 0
                    flagchk = 0

                    # print fruitx

            if (pillarmoveflag == 0):
                pillarmoveflag = 2

                # print "help"

                if (pillar1 == delta and pillar1x < (sx(415, 1))):
                    pillarfast = pillar1x = randint(int(sx(845, 1)),
                                                    int(sx(900, 1)))
                    pillar1 = pillarlist[randint(0, 2)]
                    flagchk1 = 1

                if (pillar2 == delta and pillar2x < (sx(415, 1))):
                    pillarfast = pillar2x = randint(int(sx(845, 1)),
                                                    int(sx(900, 1)))
                    pillar2 = pillarlist[randint(0, 2)]
                    flagchk2 = 1

                if (pillar3 == delta and pillar3x < (sx(415, 1))):
                    pillarfast = pillar3x = randint(int(sx(845, 1)),
                                                    int(sx(900, 1)))
                    pillar3 = pillarlist[randint(0, 2)]
                    flagchk3 = 1

                if (pillar1x <= (sx(348, 1)) and flagchk1 != 1):
                    pillarfast = pillar1x = randint(int(sx(845, 1)),
                                                    int(sx(900, 1)))
                    pillar1 = pillarlist[randint(0, 2)]

                if (pillar2x <= (sx(348, 1)) and flagchk2 != 1):
                    pillarfast = pillar2x = randint(int(sx(845, 1)),
                                                    int(sx(900, 1)))
                    pillar2 = pillarlist[randint(0, 2)]

                if (pillar3x <= (sx(348, 1)) and flagchk3 != 1):
                    pillarfast = pillar3x = randint(int(sx(845, 1)),
                                                    int(sx(900, 1)))
                    pillar3 = pillarlist[randint(0, 2)]

                # 2nd PILLAR DETECTION

                if (pillar1x > (sx(457, 1)) and pillar1x < (sx(840, 1))):
                    # acc1=2
                    pillar2nd = pillar1x

                if (pillar2x > (sx(457, 1)) and pillar2x < (sx(840, 1))):
                    # acc2=2
                    pillar2nd = pillar2x

                if (pillar3x > (sx(457, 1)) and pillar3x < (sx(840, 1))):
                    # acc3=2
                    pillar2nd = pillar3x

                flagchk1 = flagchk2 = flagchk3 = 0

                # fruit placement

                if ((pillar2nd - sx(459, 1)) > (sx(80, 1))):
                    fruitx = randint(int(sx(470, 1)),
                                     int(pillar2nd - sx(20 - 8)))

            # print pillar1.rect.topleft

            # left and right black background patches

            pygame.draw.rect(gameDisplay, black, (0, 0, sx(350, 1), sy(768)))

            pygame.draw.rect(gameDisplay, black,
                             (sx(840, 1), 0, sx(693, 1), sy(768)))

            if (herofall == 1 or ext == 1):

                if (ext != 1):
                    heroy += sy(15)

                if (heroy > (sy(770)) or ext == 1):

                    landing.stop()
                    dead.stop()
                    dead.play()
                    #rect = pygame.Rect(350, 0, 490, 768)
                    #sub = gameDisplay.subsurface(rect)
                    #pygame.image.save(sub, "screenshot/screenshot.png")

                    a = scorescreen()
                    catch = a.make(gameDisplay, back, score, fruitscore)

                    if (catch == 1 or catch == 0):

                        # VARIABLE INITIALIZATION

                        stickx1 = stickx = sx(455, 1)
                        sticky1 = sticky = sy(472)

                        anglenum = 90
                        angle = (pi / 180) * anglenum

                        sticklength = 0

                        time = 0
                        flag = 0  # stick fall flag
                        keypressflag = 0

                        moveit = 0  # hero move flag

                        herox = sx(429, 1)
                        heroy = sy(442)

                        heropointer = 0

                        i = 0
                        j = 0
                        k = 0

                        pillar1x = sx(355, 1)
                        msgx = pillar2x = sx(650, 1)
                        pillar3x = randint(int(sx(845, 1)), int(sx(900, 1)))

                        pillar1 = alpha
                        pillar2 = beta
                        pillar3 = pillarlist[randint(0, 2)]

                        herofall = 0
                        herofallflag = 0

                        pillarmoveflag = 0

                        stickmove = 0

                        ackx = 0

                        pillarfound = 0

                        score = 0

                        keyinit = 0

                        ext = 0

                        speed = sx(8)

                        acc1 = acc2 = acc3 = 0

                        pillarfast = 0

                        pillardist = randint(int(sx(60, 1)), int(sx(260, 1)))
                        lastpillardist = pillardist

                        stickgrowsound = 0

                        back = backgroundlist[randint(0, 6)]

                        keypress = 0

                        backx1 = sx(350, 1)
                        backx2 = sx(1630, 1)

                        upsidedown = False
                        '''
        
                        if(pillar1x>429 and pillar1x<840):
                            #acc1=2
                            pillar2nd=pillar1x
                    
                        if(pillar2x>429 and pillar2x<840):
                            #acc2=2
                            pillar2nd=pillar2x
                        
                        if(pillar3x>429 and pillar3x<840):
                            #acc3=2
                            pillar2nd=pillar3x 
                            
                        '''

                        bouncedown = True
                        bounce = 0

                        fruitx = 0

                        fruitgot = False
                        fruitflag = 0

                        herod = sy(33)

                        fruitscore = 0
                        score = 0

                        scoreshift = 0
                        fruitscoreshift = 0
                        shift1 = 1
                        shift2 = 1

                        perfectflag = 0
                        vanish = 0
                        perfect = 0
                        b1 = 0
                        b2 = 2
                        b3 = 4
                        b4 = 6

                        birdx = sx(900, 1)
                        birdxslow = sx(950, 1)
                        birdxfast = sx(860, 1)

                        birdgroupshow = 0
                        birdsingleshow = 0
                        birdmainshow = 0
                        birdpickup = 0
                        birdsound = 0

                        flagchk1 = flagchk2 = flagchk3 = 0

                        flagchk = 0

            #pygame.draw.circle(gameDisplay,white,(pillar2nd,700) ,3, 2)

            pygame.display.update()
            clock.tick(60)

            if crashed == True:  # Game crash or Close check
                pygame.quit()
                sys.exit()

        # Just a window exception check condition

        event1 = pygame.event.get()
        if event1.type == pygame.QUIT:
            crashed = True

        if crashed == True:
            pygame.quit()
            sys.exit()
예제 #3
0
    def make(self, gameDisplay, back, score, fruitscore):

        gameDisplay = display_init()
        sound = True

        try:
            pygame.mixer.init()
        except Exception as err:
            sound = False
            print('error with sound', err)

        black = (0, 0, 0)
        white = (255, 255, 255)
        clock = pygame.time.Clock()
        timer = pygame.time.Clock()

        crashed = False
        disp_width = 600
        disp_height = 600

        press = 0

        info = pygame.display.Info()


        if not(gameDisplay):

            gameDisplay = pygame.display.set_mode(
                (info.current_w, info.current_h))

        replay = pygame.image.load("images/scorescreen/replay.png")
        replay = pygame.transform.scale(replay, (int(sx(104)), int(sy(102))))

        scoreplate = pygame.image.load("images/scorescreen/scoreplate.png")
        scoreplate = pygame.transform.scale(scoreplate, (int(sx(230 + 130)), int(sy(140 + 80))))

        plate = pygame.image.load("images/scoreplate.png").convert()
        plate = pygame.transform.scale(plate, (int(sx(340)), int(sy(90))))
        plate.set_alpha(220)

        home = pygame.image.load("images/scorescreen/home.png")
        home = pygame.transform.scale(
                    home, (int(sx(108)), int(sy(106))))
        # back=pygame.image.load("screenshot/screenshot.png")

        back.convert()
        back.set_alpha(225)

        font_path = "fonts/Arimo.ttf"
        font_size = int(sx(50))
        font1 = pygame.font.Font(font_path, font_size)
        font2 = pygame.font.Font("fonts/Arimo.ttf", int(sx(30)))
        font3 = pygame.font.Font("fonts/Arimo.ttf", int(sx(40)))
        font4 = pygame.font.Font("fonts/Arimo.ttf", int(sx(20)))

        down = 1
        bounce = 0
        i = 0

        keypressflag = 0

        maxscore = 0
        fruitmaxscore = 0
        path = get_score_path()

        with open(path, 'rb') as input:  # REading
            maxscore = pickle.load(input)
            fruitmaxscore = pickle.load(input)

        if(fruitscore > fruitmaxscore):
            fruitmaxscore = fruitscore
            with open(path, 'wb') as output:
                pickle.dump(maxscore, output, pickle.HIGHEST_PROTOCOL)
                pickle.dump(fruitmaxscore, output, pickle.HIGHEST_PROTOCOL)

        if(score > maxscore):
            maxscore = score
            with open(path, 'wb') as output:
                pickle.dump(maxscore, output, pickle.HIGHEST_PROTOCOL)
                pickle.dump(fruitmaxscore, output, pickle.HIGHEST_PROTOCOL)

        # GAME LOOP BEGINS !!!

        while not crashed:
            # Gtk events
            mouse_button_up = False
            while Gtk.events_pending():
                Gtk.main_iteration()
            for event in pygame.event.get():
                # totaltime+=timer.tick()

                if event.type == pygame.QUIT:
                    crashed = True

                elif event.type == pygame.MOUSEBUTTONUP:
                    mouse_button_up = True

                elif event.type == pygame.KEYDOWN:
                    # jump.play(0)

                    return 1
                # event=pygame.event.poll()

            mos_x, mos_y = pygame.mouse.get_pos()

            # print event

            i += 1

            if(i > 20):
                i = 0

            if(i % 3 == 0):
                if(down == 1):
                    bounce += 1
                    if(bounce > 8):
                        down = 0
                if(down == 0):
                    bounce -= 1
                    if(bounce < 0):
                        down = 1

            gameDisplay.fill(white)
            gameDisplay.blit(back, (sx(350, 1), 0))

            gameDisplay.blit(plate, (sx(430, 1), sy(40)))

            head1 = font1.render(_("GAME OVER!"), 1, (white))
            gameDisplay.blit(head1, (sx(440, 1), sy(50)))

            gameDisplay.blit(scoreplate, (sx(420, 1), sy(200)))

            gameDisplay.blit(home, (sx(380 + 60 + 25, 1), sy(400 + 50)))

            gameDisplay.blit(replay, (sx(600 + 60 - 25, 1), sy(400 + 50)))

            # score check

            scores = font2.render(str(score), 1, black)
            gameDisplay.blit(scores, (sx(575, 1), sy(250)))

            maxscores = font2.render(str(maxscore), 1, black)
            gameDisplay.blit(maxscores, (sx(575, 1), sy(350)))

            # GAME START

            if home.get_rect(center=(sx(380 + 60 + 52 + 25, 1), sy(400 + 50 + 51))).collidepoint(mos_x, mos_y):
                gameDisplay.blit(home, (sx(380 + 60 + 25 - 2, 1), sy(400 + 50 - 2)))

                if(pygame.mouse.get_pressed())[0] == 1 and press == 0:

                    return 0

                if mouse_button_up:
                    press = 0

            # Help menu

            if replay.get_rect(center=(sx(600 + 60 + 52 - 25, 1), sy(400 + 50 + 51))).collidepoint(mos_x, mos_y):
                gameDisplay.blit(replay, (sx(600 + 60 - 25 - 2, 1), sy(400 + 50 - 2)))

                if(pygame.mouse.get_pressed())[0] == 1 and press == 0:

                    return 1

            pygame.draw.rect(gameDisplay, black, (0, 0, sx(350, 1), sy(768)))

            pygame.draw.rect(gameDisplay, black, (sx(840, 1), 0, sx(693, 1), sy(768)))

            pygame.display.update()
            clock.tick(60)

            if crashed == True:                                   # Game crash or Close check
                pygame.quit()
                sys.exit()

        # Just a window exception check condition

        event1 = pygame.event.get()
        if event1.type == pygame.QUIT:
            crashed = True

        if crashed == True:
            pygame.quit()
            sys.exit()