Пример #1
0
def spawn_bullet(bullets, dir, pos, pli):
    global counter
    counter += 1
    if counter >= 10:
        if dir is 'N':
            bullets.append(Bullet(pli, speed=(0, -10)))
            bullets[-1].rect.center = (pos[0], pos[1])
            bullets[-1].rotate()
        elif dir is 'E':
            bullets.append(Bullet(pli, speed=(10,0)))
            bullets[-1].rect.center = (pos[0], pos[1])
        elif dir is 'S':
            bullets.append(Bullet(pli, speed=(0, -10)))
            bullets[-1].rect.center = (pos[0], pos[1])
            bullets[-1].flip()
            bullets[-1].rotate()
        else:
            bullets.append(Bullet(pli, speed=(10, 0)))
            bullets[-1].rect.center = (pos[0], pos[1])
            bullets[-1].flip()
        counter = 0
Пример #2
0
 for monster in Groups.all_monsters:
     if monster.rect.y > constants.HEIGHT:
         level = '1'
         show_defeat()
 for cannon in Groups.all_people:
     if pygame.sprite.spritecollideany(cannon, Groups.all_monsters) or \
             pygame.sprite.spritecollideany(cannon, Groups.enemy_bullets):
         level = '1'
         cannon.kill()
         show_defeat()
 keys = pygame.key.get_pressed()
 if keys[pygame.K_ESCAPE]:
     show_intro(level)
 Groups.all_people.update()
 if schet % 3 == 0 and keys[pygame.K_SPACE]:
     bullet = Bullet(cannon.get_pos()[0] - 20, cannon.get_pos()[1])
     pygame.mixer.Channel(5).set_volume(0.03)
     pygame.mixer.Channel(5).play(pygame.mixer.Sound('data/shot.mp3'))
 if schet % (int(level) * 18) == 0:
     for sprite in Groups.all_monsters:
         if sprite.power in range(5, 10) and sprite.rect.y in range(
                 -sprite.rect.height, constants.HEIGHT):
             enemy_bullet = EnemyBullet(
                 sprite.rect.x + random.randint(-sprite.rect.width // 2,
                                                sprite.rect.width // 2),
                 sprite.rect.y + sprite.rect.height, 0)
             pygame.mixer.Channel(6).set_volume(0.3)
             pygame.mixer.Channel(6).play(
                 pygame.mixer.Sound('data/shot.mp3'))
         elif sprite.power == 75 and sprite.rect.y in range(
                 -sprite.rect.height, constants.HEIGHT):
Пример #3
0
            # Inventory management
            if event.key == pygame.K_q:
                player.setNextInventoryItem()
            if event.key == pygame.K_SPACE:
                usedPowerUp = player.useCurrentItem()
                if usedPowerUp is not None:
                    usedPowerUp.applyPlayerEffect(player)
                    powerUpsInEffect.append(usedPowerUp)

            # Bullet Movement
            if count > bulletDelay or player.bullet == "fire":
                count = 0
                if player.bullet == "fire":
                    bullet = Fire(player.x, player.y)
                else:
                    bullet = Bullet(player.x, player.y)
                bullet.x = player.x
                bullet.y = player.y
                if event.key == pygame.K_UP:
                    bullet.direction = "up"
                    bullets.append(bullet)
                    bullet_group.add(bullet)
                    f.write("Bullet shot moving up\n") #TC2 #TC6
                if event.key == pygame.K_DOWN:
                    bullet.direction = "down"
                    bullets.append(bullet)
                    bullet_group.add(bullet)
                    f.write("Bullet shot moving down\n") #TC2 #TC6
                if event.key == pygame.K_LEFT:
                    bullet.direction = "left"
                    bullets.append(bullet)
Пример #4
0
def player_move(pos, fr, cl, fire, shot_frame, st, jump, js, keys, money):
    pos_0 = 0
    pos_1 = 0
    a25 = player_size[0] / 2
    a55 = player_size[1] - player_size[0] / 2
    ind = int((fr * delay_time) / 100) % 4
    position = [pos[0] - a25, pos[1] - a55]
    rotated_image = player.pic[ind]
    if jump == 0:
        if keys[pygame.K_a] or keys[pygame.K_LEFT]:
            pos_0 -= 1
        if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
            pos_0 += 1
        if keys[pygame.K_w] or keys[pygame.K_UP]:
            pos_1 -= 1
        if keys[pygame.K_s] or keys[pygame.K_DOWN]:
            pos_1 += 1
    else:
        pos[1] += (jump * 5 * player.speed)
        win.blit(
            pygame.transform.scale(
                rotated_image,
                (int(1.2 * player_size[0]), int(1.2 * player_size[1]))),
            (position[0], position[1]))
        if abs(pos[1] - js) >= screen_height / 7:
            jump = 0

    pos_0 *= player.speed
    pos_1 *= player.speed
    if pos_0 != 0 and pos_1 != 0:
        pos_0 /= np.sqrt(2)
        pos_1 /= np.sqrt(2)

    pos_0 = int(pos_0)
    pos_1 = int(pos_1)

    if jump == 0:
        for an in animals:
            if pos[0] - a25 > an.x + an.pic[0].get_width(
            ) or pos[0] + a25 < an.x:
                x = pos[0] + pos_0
                if pos[1] - a25 < an.y + an.pic[0].get_height(
                ) and pos[1] + a25 > an.y:
                    if x - a25 <= an.x + an.pic[0].get_width(
                    ) and x + a25 >= an.x:
                        if x + a25 > an.x + an.pic[0].get_width():
                            pos[0] = an.x + an.pic[0].get_width(
                            ) - pos_0 + a25 + 1
                        else:
                            pos[0] = an.x - a25 - pos_0 - 1
                        break
            else:
                x = pos[1] + pos_1
                if x - a25 < an.y + an.pic[0].get_height() and x + a25 > an.y:
                    if x - a25 > an.y:
                        if screen_height - player_size[1] >= pos[
                                1] + screen_height / 7:
                            jump = 1
                        else:
                            jump = -1
                        js = pos[1]
                        player.hp -= an.damage
                    else:
                        pos[1] -= pos_1
                    break

    pos[0] += pos_0
    pos[1] += pos_1
    if pos[0] >= road_end - player_size[0] / 2:
        pos[0] = road_end - player_size[0] / 2
    elif pos[0] <= road_start + player_size[0] / 2:
        pos[0] = road_start + player_size[0] / 2
    if pos[1] >= screen_height - player_size[0]:
        pos[1] = screen_height - player_size[0] - 1
    elif pos[1] <= a25:
        pos[1] = a25

    if (cl or keys[pygame.K_SPACE]) and time.time() - bullet_reload[sel_bull] >= bullet[sel_bull].reload\
            and bullet[sel_bull].cost <= money:
        money -= bullet[sel_bull].cost
        d = bullet[sel_bull].damage
        r = bullet[sel_bull].reload
        rad = bullet[sel_bull].radius
        l = bullet[sel_bull].lvl
        if sel_bull == 1:
            bull = Bullet(10, 10, 10, BLACK, d,
                          (3 * screen_height) * delay_time / 1000, r, None, l,
                          True, rad)
        elif sel_bull == 2:
            bull = Bullet(1, 5, 10, RED, d,
                          (6 * screen_height) * delay_time / 1000, r, None, l)
        else:
            bull = Bullet(0, 5, 5, BROWN, d,
                          (5 * screen_height) * delay_time / 1000, r, None, l)
        bull.x = pos[0] + int(0.28 * a25)
        bull.y = pos[1] - a55
        bullets.append(bull)
        bullet_reload[sel_bull] = time.time()

    if cl or keys[pygame.K_SPACE]:
        fire = True
    else:
        fire = False

    if not fire:
        if pos_0 > 0:
            if pos_1 > 0:
                rotated_image = pygame.transform.rotate(player.pic[ind], -135)
                position = [pos[0] - a25, pos[1] - a25]
            elif pos_1 < 0:
                rotated_image = pygame.transform.rotate(player.pic[ind], -45)
                position = [pos[0] - a25, pos[1] - a55]
            else:
                rotated_image = pygame.transform.rotate(player.pic[ind], -90)
                position = [pos[0] - a25, pos[1] - a25]
        elif pos_0 < 0:
            if pos_1 > 0:
                rotated_image = pygame.transform.rotate(player.pic[ind], 135)
                position = [pos[0] - a55, pos[1] - a25]
            elif pos_1 < 0:
                rotated_image = pygame.transform.rotate(player.pic[ind], 45)
                position = [pos[0] - a55, pos[1] - a55]
            else:
                rotated_image = pygame.transform.rotate(player.pic[ind], 90)
                position = [pos[0] - a55, pos[1] - a25]
        else:
            if pos_1 > 0:
                rotated_image = pygame.transform.rotate(player.pic[ind], 180)
                position = [pos[0] - a25, pos[1] - a25]

    if not jump:
        win.blit(rotated_image, (position[0], position[1]))
    return pos, fire, shot_frame, st, jump, js, money
Пример #5
0
win.fill(GREEN1)
bull_lvl = []
txt = open("money.txt", "r")
money = int(txt.readline())
bull_lvl.append(min(int(txt.readline()), 15))
bull_lvl.append(min(int(txt.readline()), 15))
bull_lvl.append(min(int(txt.readline()), 15))
hp_lvl = int(txt.readline())
speed_lvl = int(txt.readline())
level = int(txt.readline())
txt.close()
start_but = Button(int(screen_width / 10), int(screen_width / 20),
                   (100, 255, 0), GREEN)
b_s = int(screen_height / 200)
bullet = [
    Bullet(0, b_s, b_s, BROWN, 1, (5 * screen_height) * delay_time / 1000, 0.5,
           None),
    Bullet(10, 2 * b_s, 2 * b_s, BLACK, 2,
           (3 * screen_height) * delay_time / 1000, 1.5, None, 1, True,
           int(screen_width / 12)),
    Bullet(1, b_s, 2 * b_s, RED, 0.5, (6 * screen_height) * delay_time / 1000,
           0.1, None)
]

for i in range(3):
    for _ in range(bull_lvl[i] - 1):
        bullet[i].lvlup()
sel_bull = 0
bullet_reload = [0, 0, 0]
screen_speed = (screen_height / 2) * delay_time / 1000
ms = (screen_height / 2) * delay_time / 1000
gold_speed = (2 * screen_height) * delay_time / 1000
Пример #6
0
        stamina = stamina - 2
    if keys[pygame.K_s] and keys[pygame.K_j] and stamina > 1:
        player.move(0,4)
        stamina = stamina - 2

    if not keys[pygame.K_j]: # key for stamina
        if stamina != 150:
            stamina = stamina + 1

    if event.type == MOUSEBUTTONDOWN: # register mouse down
        if event.button == 1 and click == False:
            click = True
        
    if click: # shoot if mouse click
        x, y = pygame.mouse.get_pos()
        b = Bullet(WHITE, player.rect.centerx, player.rect.centery, 10, 10, 5, x, y)
        bullet_group.add(b)
        all_sprites_list.add(b)
        click = False

    
    # -- #
    screen.fill(BLACK)
    font = pygame.font.Font(None, 25)
    txt = font.render("press 'j' to sprint    " + str(stamina), True, WHITE)
    screen.blit(txt, (400, 100))
    
    all_sprites_list.update()

    all_sprites_list.draw(screen)
 
Пример #7
0
 def fire(self):
     b = Bullet(self.display, self)
     return b