Example #1
0
File: game.py Project: Ran4/comgud
 def update(self, key):
     self.gameTime += 1
     self.dt = 1/60.
     
     if self.gameTime % 30 == 0: self.blackHoleSize += 1
     elif self.gameTime % 30 == 5: self.blackHoleSize -= 1
         
     #Quicker gameplay: continously increase all player's gravityFactor
     #if self.gameTime % 15 == 0:
     #    for player in self.players:
     #        player.po.gravityFactor += 0.01
     
     if self.gameTime > 1 and self.gameTime % con.POWERUP_SPAWN_DELAY == 0:
         p = Powerup()
         p.respawn(self)
         self.powerups.append(p)
     
     for powerup in self.powerups:
         powerup.update(self, key)
     
     for bullet in self.bullets:
         bullet.update(self, key)
     
     self.bullets = filter(lambda x: not x.markedForRemoval, self.bullets)
     self.powerups = filter(lambda x: not x.markedForRemoval, self.powerups)
         
     for player in self.players:
         player.update(self, key)
     
     self.players = filter(lambda x: x.livesLeft > 0, self.players)
Example #2
0
    def __init__(self, x, y):
        Powerup.__init__(self, x, y)

        sprite_sheet = SpriteSheet("data/item_objects.png")

        self.x_vel = 2

        self.display_frame = sprite_sheet.get_image(coords.MUSHROOM_POWERUP, c.IMG_MULTIPLIER, c.BLACK)
        self.refresh_image(self.display_frame)

        self.time = 0

        self.y_cap = self.rect.y - self.rect.height
Example #3
0
    def __init__(self, x, y):
        Powerup.__init__(self, x, y)

        sprite_sheet = SpriteSheet("data/item_objects.png")

        self.x_vel = 2

        self.display_frame = sprite_sheet.get_image(coords.MUSHROOM_POWERUP,
                                                    c.IMG_MULTIPLIER, c.BLACK)
        self.refresh_image(self.display_frame)

        self.time = 0

        self.y_cap = self.rect.y - self.rect.height
Example #4
0
 def addPowerup(self):
     x_point = 300 + random.randint(-250, 250)
     new_powerup = Powerup(self.powerup_width, self.powerup_height, x_point,
                           0, self.powerup_color)
     self.powerups.append(new_powerup)
     print("upgrade")
     return
Example #5
0
    def spawn_enemies(self):
        spawns = self.level.get_spawns()
        if spawns[Level.SHARKS]:
            # Make a new shark
            self.sharks.append(Shark())
            self.shark_sprites.add(self.sharks[-1])

        if spawns[Level.PIRATES]:
            # Make a new pirate ship
            self.pirates.append(Pirateboat())
            self.pirate_sprites.add(self.pirates[-1])

        if spawns[Level.MINES]:
            self.mines.append(Mine())
            self.mine_sprites.add(self.mines[-1])

        if spawns[Level.SEAGULLS]:
            self.seagulls.append(Seagull())
            self.seagull_sprites.add(self.seagulls[-1])

        if spawns[Level.TITANIC]:
            self.titanic = Titanic()
            self.titanic_sprite.add(self.titanic)

        if spawns[Level.POWERUPS]:
            self.powerups.append(Powerup())
            self.powerup_sprites.add(self.powerups[-1])
    def __init__(self):
        (w,h) = (800,800)
        pygame.init()
        pygame.display.set_mode((w,h), 0, 32)
        self.screen = pygame.display.get_surface()
        
        self.player = Player(self.screen.get_rect())
        self.player_list = [self.player]                

        self.enemy = Enemy(400,100)
        self.enemy_list = [self.enemy]

        self.respawn = False
        self.respawn_time = time.time()

        self.power_up = Powerup(self.screen)
Example #7
0
def create_powerup(ai_settings, screen, powerups, images):
    number_powerup_x = ai_settings.powerup_allowed
    for powerup_amount in range(number_powerup_x):
        if len(powerups) < number_powerup_x:
            if ai_settings.frame_count / 60 >= ai_settings.powerup_cooldown:
                powerup = Powerup(ai_settings, screen, images)
                powerups.add(powerup)
                print("powerup spawned")
                ai_settings.powerup_cooldown += ai_settings.powerup_increase
Example #8
0
def create_powerup():
    ids = [0, 1, 2]
    powerup_id = random.choice(ids)
    x_set = range(0, width - powerup_width)
    y_set = range(-110, -90)
    # (x, y) = (random.choice(x_set), random.choice(y_set))
    (x, y) = get_block_pos()
    powerup = Powerup(powerup_id, powerup_width, powerup_height, x, y)
    powerup_group.add(powerup)
Example #9
0
 def __init__(self,
              rows=30,
              cols=1000,
              start_x=23,
              start_y=17,
              boss_x=950,
              boss_y=5):
     self._score = 0
     self._coins = 0
     self._board = Board(rows, cols)
     self._hero = Mandalorian(start_x, start_y)
     self._boss = Enemy(boss_x, boss_y)
     self._getch = getInput._getChUnix()
     self._rem_time = 100
     self._fired_bullets = []
     self._thrown_snowballs = []
     self._coins_placed = []
     self._zappers = []
     self._speedup = False
     self._magnet = Magnet(0, 0)
     self._powerup = Powerup(0, 0)
     self._speedup = False
     self._ground = Ground()
Example #10
0
def decode_powerup_data(msg):
    decoded = []
    powdict = {}  # A single item will look like id:[x, y, type]

    for att in msg[1:]:
        att = att.split("/")
        idd = att[
            0]  # an id is used for each attribute so the order of the list doesnt matter
        powdict.setdefault(idd, [0, 0, 0])
        if att[1][0] == "x":
            powdict[idd][0] = int(att[1][1:])
        if att[1][0] == "y":
            powdict[idd][1] = int(att[1][1:])
        if att[1][0] == "t":
            powdict[idd][2] = int(att[1][1:])
    for po in powdict.values():
        decoded.append(Powerup(po[0], po[1], po[2]))

    return decoded
Example #11
0
    def __init__(self):
        pyxel.init(240, 240, caption='Bourne Again', fps=30)
        pyxel.mouse(True)

        pyxel.load('stuff.pyxres')

        self.seemsg = ''
        self.seemsg_iter = ''
        self.seemsg_out = ''

        self.player = Player(30, 120)

        self.beam_angle = None
        self.inputed_angle = None
        self.beam_start_time = 0

        #self.wll_index = 0

        self.targets = {
            'A': Target(200, 120, 'Target A feels lonely'),
            'B': Target(1, 20, 'Fortune awaits Target B'),
            'C': Target(120, 199, 'Target C is in the mood for shawarma')
        }

        self.walls = [Solid(150, 120), Solid(100, 45), Solid(24, 200)]

        self.flag = Flag(60, 24)

        self.powerup = Powerup(80, 120)

        self.upgraded = False
        self.blocked_functions = ['up', 'down', 'left']

        self.locked = False
        self.has_won = False

        self.raycast_distance = 0
        self.raycast_has_collided = False

        pyxel.playm(1, loop=True)

        pyxel.run(self.update, self.draw)
Example #12
0
 def __init__(self, **kw):
     super(RootWidget, self).__init__(**kw)
     self.level = 1
     self.seconds = 0
     self.combo = 1
     self.score = 0
     self.hits = 0
     self.cons = 0
     self.rainboxes = [Rainbox(pos=(-500, 0)) for i in range(10)]
     self.powerup = Powerup()
     self.miniball = Rainball(size=(60, 60), name='mini')
     self.miniball.speed = 20
     self.maxiball = Rainball(size=(120, 120), name='maxi', element='stone')
     self.background = Background(size=Window.size)
     self.target_l = Image(source='img/target_l.zip', size=(60, 60))
     self.lbl_fps = Label(pos=(Window.width - 100, 0))
     self.lbl_level = PopLabel(pos=(100, Window.height - 100),
                               font_name='font/Mouser.ttf')
     self.lbl_hits = PopLabel(pos=(100, Window.height - 140),
                              font_name='font/Mouser.ttf')
     self.lbl_combo = PopLabel(pos=(100, Window.height - 180),
                               font_name='font/Mouser.ttf')
     self.lbl_score = PopLabel(pos=(100, Window.height - 220),
                               font_name='font/Mouser.ttf')
     self.add_widget(self.background, canvas='before')
     self.add_widget(self.target_l)
     self.add_widget(self.miniball)
     self.add_widget(self.maxiball)
     self.add_widget(self.lbl_fps)
     self.add_widget(self.lbl_level)
     self.add_widget(self.lbl_hits)
     self.add_widget(self.lbl_combo)
     self.add_widget(self.lbl_score)
     self.add_widget(self.powerup)
     for i in range(10):
         self.add_widget(self.rainboxes[i])
     self._keyboard = Window.request_keyboard(self._keyboard_closed, self,
                                              'text')
     self._keyboard.bind(on_key_down=self._on_key_down)
     sounds['music'].loop = True
     sounds['music'].play()
     Clock.schedule_interval(self.update, 1 / 60.)
Example #13
0
class Engine(Board, Mandalorian, Enemy, Bullet, Magnet, Coins, Ground, Zapper,
             Snowball):
    def __init__(self,
                 rows=30,
                 cols=1000,
                 start_x=23,
                 start_y=17,
                 boss_x=950,
                 boss_y=5):
        self._score = 0
        self._coins = 0
        self._board = Board(rows, cols)
        self._hero = Mandalorian(start_x, start_y)
        self._boss = Enemy(boss_x, boss_y)
        self._getch = getInput._getChUnix()
        self._rem_time = 100
        self._fired_bullets = []
        self._thrown_snowballs = []
        self._coins_placed = []
        self._zappers = []
        self._speedup = False
        self._magnet = Magnet(0, 0)
        self._powerup = Powerup(0, 0)
        self._speedup = False
        self._ground = Ground()

    def create_powerup(self):
        flag = False
        while flag == False:
            x = random.randint(100, 700)
            y = random.randint(2, 17)
            self._powerup.set_coords(x, y)
            flag = self._powerup.create_obj(self._board)

    def create_magnet(self):
        flag = False
        while flag == False:
            x = random.randint(100, 700)
            y = random.randint(2, 17)
            self._magnet.set_coords(x, y)
            flag = self._magnet.create_obj(self._board)

    def create_all_zappers(self):
        for i in range(1, 20):  # 25 zappers
            mod = random.randint(5, 34)
            start_x = mod + i * 40
            start_y = random.randint(5, 17)
            Z = Zapper(start_x, start_y)
            if Z.create_zapper(self._board) == True:
                self._zappers.append(Z)

    def create_all_coins(self):
        for i in range(9):
            mod = random.randint(0, 9)
            start_x = mod + i * 85
            start_y = random.randint(5, 17)
            C = Coins(start_x, start_y)
            if C.create_obj(self._board) == True:
                self._coins_placed.append(C)

    def run(self):
        pos_x = 0
        gravity = 1
        fall_at_time = 0
        mag_exists = True
        iterator = 0
        start = False
        dims = self._board.get_dims()
        MAT = self._board.get_matrix()
        COPY = self._board.get_copy()
        hero_size = self._hero.get_dim()
        not_damage = self._hero.get_not_damage()

        def alarmhandler(signum, frame):
            raise TypeError

        def getinp(timeout=0.15):
            signal.signal(signal.SIGALRM, alarmhandler)
            signal.setitimer(signal.ITIMER_REAL, timeout)
            try:
                ch = self._getch()
                signal.alarm(0)
                return ch
            except TypeError:
                pass
            signal.signal(signal.SIGALRM, signal.SIG_IGN)
            return ''

        def terminate(msg):
            # os.system("killall -9 aplay")
            ##################UPDATE SCORE!! #########################
            os.system('reset')
            print(msg)
            exit()

        # self._board.Place(0,dims[1],0,dims[0],'*')
        self.create_all_coins()
        self.create_all_zappers()
        self.create_magnet()
        self.create_powerup()
        self._ground.create_ground(self._board)
        T = self._magnet.get_coords()
        x_mag = T[0]
        y_mag = T[1]
        print('\033[0;0H', end='')
        self._board.CopyBoard()
        # print(type(self._coins))
        X = self._hero.position(MAT, COPY)
        self._coins += X[0]
        self._speedup = self._speedup or X[1]
        Z = Zapper(0, 0)
        MAT = self._board.get_matrix()
        COPY = self._board.get_copy()
        self._boss.position(MAT, COPY)
        MAT = self._board.get_matrix()
        COPY = self._board.get_copy()
        self._hero.check_collision(MAT)
        T1 = time.time()
        T2 = T1
        T3 = T1
        last_activated = None
        start = True
        sped = False
        while True:
            # os.system('clear')
            self._score = self._coins * 50 + (10 -
                                              self._boss.get_lives()) * 1000
            # print(self._speedup)
            if (self._speedup == True and sped == False):
                self._board.set_speed(self._board.get_speed() + 1)
                sped = True
            if start == False and self._hero.get_shield() == True and round(
                    time.time()) - round(last_activated) >= 10:
                self._hero.set_shield(False)
            elif start == True and round(time.time()) - round(T1) > 2:
                self._hero.set_shield(False)
                start = False
            self._hero.shielded()
            MAT = self._board.get_matrix()
            COPY = self._board.get_copy()
            iterator += 1
            self._rem_time = 100 - (round(time.time()) - round(T1))
            if self._rem_time < 0:
                terminate("You run out of time.\n You lose. See you soon..\n")
            print('\033[0;0H')
            # time.sleep(0.6)
            # for i in range(30):
            # for j in range(204):
            # print(bg.CYAN +" ",end ='')
            # print()
            # print('\033[0;0H')
            # time.sleep(0.001)
            # os.system('clear')

            TIME = self._rem_time
            PrintTime = [TIME // 100, (TIME % 100) // 10, (TIME % 10)]
            # print("TIME REMAINING:",PrintTime[0],PrintTime[1],PrintTime[2],end ='\n')
            print("TIME REMAINING : ", PrintTime[0], end='')
            print(PrintTime[1], end='')
            print(PrintTime[2])

            LIVES = self._hero.get_lives()
            PrintLives = [LIVES // 10, (LIVES % 10)]
            print("LIVES:", PrintLives[0], end='')
            print(PrintLives[1])

            print("SCORE:", self._score, end='\n')
            print("COINS:", self._coins, end='\n')

            BOSS = self._boss.get_lives()
            PrintBoss = [BOSS // 10, BOSS % 10]
            print("BOSS LIVES:", PrintBoss[0], end='')
            print(PrintBoss[1])
            if self._hero.die(MAT) == True:
                terminate(
                    "You lost all your lives.\n You lose. See you soon..\n")
            if self._boss.die(MAT) == True:
                terminate("You win!\nYou saved Baby Yoda! See You soon..\n")
            # print(start,":::",round(time.time()) - round(T1))
            pos_hero = self._hero.get_coords()
            hero_x = pos_hero[0]
            hero_y = pos_hero[1]
            ALLOW = self._hero.get_allowed()
            UP = ALLOW[0]
            DOWN = ALLOW[1]
            RIGHT = ALLOW[2]
            LEFT = ALLOW[3]
            # print(ALLOW)

            if abs(
                    hero_x - x_mag
            ) < 100 and x_mag < pos_x + 204 and x_mag > pos_x and iterator % 2 == 1 and mag_exists == True:
                self._hero.disappear(MAT, COPY)
                MAT = self._board.get_matrix()
                COPY = self._board.get_copy()
                self._hero.attract(x_mag, y_mag)
                X = self._hero.position(MAT, COPY)
                self._coins += X[0]
                self._speedup = self._speedup or X[1]
                self._board.set_matrix(MAT)
                self._board.set_copy(COPY)

            elif abs(
                    hero_x - x_mag
            ) < 100 and x_mag < pos_x + 204 and x_mag > pos_x and mag_exists == True:
                self._hero.set_attracted(True)
            else:
                self._hero.set_attracted(False)  # enables gravity

            gravity = fall_at_time // 2 + 1
            inp = getinp()
            if inp == 'q':
                # QUIT
                terminate("See You Soon...")

            elif inp == '7':
                ### HACKS !!!!!
                pos_x = dims[1] - 190

            elif inp == ' ':
                #### SHIELD
                # print("SHIELD!")
                if last_activated == None or round(
                        time.time()) - round(last_activated) >= 60 + 10:
                    self._hero.set_shield(True)
                    last_activated = time.time()
                else:
                    print("RECHARGING SHIELD!!")

            elif inp == 'w':
                # Jump
                if UP == True:
                    MAT = self._board.get_matrix()
                    COPY = self._board.get_copy()
                    self._hero.disappear(MAT, COPY)
                    self._hero.set_moving(True)
                    pos_hero = self._hero.get_coords()
                    hero_x = pos_hero[0]
                    hero_y = pos_hero[1] - 1
                    if hero_y < 0:
                        hero_y = 0
                    self._hero.set_coords(hero_x, hero_y)
                    X = self._hero.position(MAT, COPY)
                    self._coins += X[0]
                    self._speedup = self._speedup or X[1]
                else:
                    MAT = self._board.get_matrix()
                    COPY = self._board.get_copy()
                    pos_hero = self._hero.get_coords()
                    hero_x = pos_hero[0]
                    hero_y = pos_hero[1]
                    for i in range(hero_size[0]):
                        if MAT[hero_y - 1][hero_x + i] not in not_damage:
                            if self._hero.get_shield() == False:
                                self._hero.set_lives(self._hero.get_lives() -
                                                     1)
                            self._hero.disappear(MAT, COPY)
                            if MAT[hero_y - 1][hero_x + i] == '*':  ## ZAPPER
                                Z.erase_zapper(MAT, COPY, hero_x + i,
                                               hero_y - 1)
                            elif MAT[hero_y - 1][hero_x + i] == 'M':  ## MAGNET
                                MAT[hero_y - 1][hero_x + i] = ' '
                                COPY[hero_y - 1][hero_x + i] = ' '
                                mag_exists = False
                            # elif MAT[hero_y-1][hero_x+i] == 'P':
                            #     MAT[hero_y-1][hero_x+i] = ' '
                            #     COPY[hero_y-1][hero_x+i] = ' '
                            #     self._speedup = True
                            #     self._board.set_speed(self._board.get_speed()+1)
                            time.sleep(0.01)
                            X = self._hero.position(MAT, COPY)
                            self._coins += X[0]
                            self._speedup = self._speedup or X[1]
                            ### invincibility
                            self._board.set_matrix(MAT)
                            self._board.set_copy(COPY)
                            # MAT = self._board.get_matrix()
                            # COPY = self._board.get_copy()
                            # pos_hero = self._hero.get_coords()
                            # hero_x = pos_hero[0]
                            # hero_y = pos_hero[1]
                            break

            elif inp == 'a':
                # Go Left
                if LEFT == True:
                    MAT = self._board.get_matrix()
                    COPY = self._board.get_copy()
                    self._hero.disappear(MAT, COPY)
                    self._hero.set_moving(True)
                    self._hero.set_direction(-1)
                    self._hero.set_coords(self._hero.get_coords()[0] - 1,
                                          self._hero.get_coords()[1])
                    X = self._hero.position(MAT, COPY)
                    self._coins += X[0]
                    self._speedup = self._speedup or X[1]

                else:
                    MAT = self._board.get_matrix()
                    COPY = self._board.get_copy()
                    pos_hero = self._hero.get_coords()
                    hero_x = pos_hero[0]
                    hero_y = pos_hero[1]
                    for i in range(hero_size[1]):
                        if MAT[hero_y + i][hero_x - 1] not in not_damage:
                            if self._hero.get_shield() == False:
                                self._hero.set_lives(self._hero.get_lives() -
                                                     1)
                            self._hero.disappear(MAT, COPY)
                            if MAT[hero_y + i][hero_x - 1] == '*':  ## ZAPPER
                                Z.erase_zapper(MAT, COPY, hero_x - 1,
                                               hero_y + i)
                            elif MAT[hero_y + i][hero_x - 1] == 'M':
                                MAT[hero_y + i][hero_x - 1] = ' '
                                COPY[hero_y + i][hero_x - 1] = ' '
                                mag_exists = False
                            # elif MAT[hero_y-1][hero_x+i] == 'P':
                            #     MAT[hero_y-1][hero_x+i] = ' '
                            #     COPY[hero_y-1][hero_x+i] = ' '
                            #     self._speedup = True
                            #     self._board.set_speed(self._board.get_speed()+1)

                            time.sleep(0.01)
                            X = self._hero.position(MAT, COPY)
                            self._coins += X[0]
                            self._speedup = self._speedup or X[1]
                            self._board.set_matrix(MAT)
                            self._board.set_copy(COPY)
                            ### invincibility
                            break

            elif inp == 'd':
                # Go right
                if RIGHT == True:
                    MAT = self._board.get_matrix()
                    COPY = self._board.get_copy()
                    pos_hero = self._hero.get_coords()
                    hero_x = pos_hero[0]
                    hero_y = pos_hero[1]
                    self._hero.disappear(MAT, COPY)
                    self._hero.set_moving(True)
                    self._hero.set_direction(1)
                    self._hero.set_coords(hero_x + 1, hero_y)
                    pos_hero = self._hero.get_coords()
                    hero_x = pos_hero[0]
                    hero_y = pos_hero[1]
                    self._hero.check_collision(MAT)
                    ALLOW = self._hero.get_allowed()
                    UP = ALLOW[0]
                    DOWN = ALLOW[1]
                    RIGHT = ALLOW[2]
                    LEFT = ALLOW[3]
                    if RIGHT == True:
                        self._hero.set_coords(hero_x + 1, hero_y)
                        X = self._hero.position(MAT, COPY)
                        self._coins += X[0]
                        self._speedup = self._speedup or X[1]
                        self._board.set_matrix(MAT)
                        self._board.set_copy(COPY)
                        pos_hero = self._hero.get_coords()
                        hero_x = pos_hero[0]
                        hero_y = pos_hero[1]
                        ALLOW = self._hero.get_allowed()
                        UP = ALLOW[0]
                        DOWN = ALLOW[1]
                        RIGHT = ALLOW[2]
                        LEFT = ALLOW[3]
                        X = self._hero.position(MAT, COPY)
                        self._coins += X[0]
                        self._speedup = self._speedup or X[1]
                        if self._speedup == True:
                            if RIGHT == True:
                                MAT = self._board.get_matrix()
                                COPY = self._board.get_copy()
                                self._hero.disappear(MAT, COPY)
                                self._hero.set_moving(True)
                                self._hero.set_direction(1)
                                self._hero.set_coords(hero_x + 1, hero_y)
                                pos_hero = self._hero.get_coords()
                                hero_x = pos_hero[0]
                                hero_y = pos_hero[1]
                                self._hero.check_collision(MAT)
                                ALLOW = self._hero.get_allowed()
                                X = self._hero.position(MAT, COPY)
                                self._coins += X[0]
                                self._speedup = self._speedup or X[1]
                                UP = ALLOW[0]
                                DOWN = ALLOW[1]
                                RIGHT = ALLOW[2]
                                LEFT = ALLOW[3]

                            else:
                                for i in range(hero_size[1]):
                                    if MAT[hero_y +
                                           i][hero_x +
                                              hero_size[0]] not in not_damage:
                                        if self._hero.get_shield() == False:
                                            self._hero.set_lives(
                                                self._hero.get_lives() - 1)
                                        pos_hero = self._hero.get_coords()
                                        hero_x = pos_hero[0]
                                        hero_y = pos_hero[1]
                                        if MAT[hero_y + i][
                                                hero_x +
                                                hero_size[0]] == '*':  ## ZAPPER
                                            Z.erase_zapper(
                                                MAT, COPY,
                                                hero_x + hero_size[0],
                                                hero_y + i)
                                        elif MAT[hero_y +
                                                 i][hero_x +
                                                    hero_size[0]] == 'M':
                                            MAT[hero_y + i][hero_x +
                                                            hero_size[0]] = ' '
                                            COPY[hero_y +
                                                 i][hero_x +
                                                    hero_size[0]] = ' '
                                            mag_exists = False
                                        # elif MAT[hero_y-1][hero_x+i] == 'P':
                                        #     # print("YE")
                                        #     MAT[hero_y-1][hero_x+i] = ' '
                                        #     COPY[hero_y-1][hero_x+i] = ' '
                                        #     self._speedup = True
                                        #     self._board.set_speed(self._board.get_speed()+1)

                                        time.sleep(0.01)
                                        X = self._hero.position(MAT, COPY)
                                        self._coins += X[0]
                                        self._speedup = self._speedup or X[1]
                                        break
                    else:
                        for i in range(hero_size[1]):
                            if MAT[hero_y + i][hero_x +
                                               hero_size[0]] not in not_damage:
                                if self._hero.get_shield() == False:
                                    self._hero.set_lives(
                                        self._hero.get_lives() - 1)
                                pos_hero = self._hero.get_coords()
                                hero_x = pos_hero[0]
                                hero_y = pos_hero[1]
                                if MAT[hero_y +
                                       i][hero_x +
                                          hero_size[0]] == '*':  ## ZAPPER
                                    Z.erase_zapper(MAT, COPY,
                                                   hero_x + hero_size[0],
                                                   hero_y + i)
                                elif MAT[hero_y + i][hero_x +
                                                     hero_size[0]] == 'M':
                                    MAT[hero_y + i][hero_x +
                                                    hero_size[0]] = ' '
                                    COPY[hero_y + i][hero_x +
                                                     hero_size[0]] = ' '
                                    mag_exists = False
                                # elif MAT[hero_y-1][hero_x+i] == 'P':
                                #     MAT[hero_y-1][hero_x+i] = ' '
                                #     COPY[hero_y-1][hero_x+i] = ' '
                                #     self._speedup = True
                                #     self._board.set_speed(self._board.get_speed()+1)

                                time.sleep(0.01)
                                X = self._hero.position(MAT, COPY)
                                self._coins += X[0]
                                self._speedup = self._speedup or X[1]
                                break
                                #invincibility
                    self._board.set_matrix(MAT)
                    self._board.set_copy(COPY)
                else:
                    # print("!")
                    # time.sleep(1)
                    MAT = self._board.get_matrix()
                    COPY = self._board.get_copy()
                    pos_hero = self._hero.get_coords()
                    hero_x = pos_hero[0]
                    hero_y = pos_hero[1]
                    for i in range(hero_size[1]):
                        if MAT[hero_y + i][hero_x +
                                           hero_size[0]] not in not_damage:
                            if self._hero.get_shield() == False:
                                self._hero.set_lives(self._hero.get_lives() -
                                                     1)
                            self._hero.disappear(MAT, COPY)
                            if MAT[hero_y + i][hero_x +
                                               hero_size[0]] == '*':  ## ZAPPER
                                Z.erase_zapper(MAT, COPY,
                                               hero_x + hero_size[0],
                                               hero_y + i)
                            elif MAT[hero_y + i][hero_x + hero_size[0]] == 'M':
                                MAT[hero_y + i][hero_x + hero_size[0]] = ' '
                                COPY[hero_y + i][hero_x + hero_size[0]] = ' '
                                mag_exists = False
                            # elif MAT[hero_y-1][hero_x+i] == 'P':
                            #     MAT[hero_y-1][hero_x+i] = ' '
                            #     COPY[hero_y-1][hero_x+i] = ' '
                            #     self._speedup = True
                            #     self._board.set_speed(self._board.get_speed()+1)
                            time.sleep(0.01)
                            X = self._hero.position(MAT, COPY)
                            self._coins += X[0]
                            self._speedup = self._speedup or X[1]
                            ### invincibility
                            self._board.set_matrix(MAT)
                            self._board.set_copy(COPY)
                            break

            elif inp == 'k':
                ## FIRE BULLET
                pos_hero = self._hero.get_coords()
                hero_x = pos_hero[0]
                hero_y = pos_hero[1]

                self._fired_bullets += [
                    Bullet(hero_x + self._board.get_speed(),
                           hero_y + self._board.get_speed(),
                           self._hero.get_direction())
                ]

            else:
                self._hero.set_moving(False)

                #### UPDATE NECESSARY
            MAT = self._board.get_matrix()
            COPY = self._board.get_copy()
            pos_hero = self._hero.get_coords()
            hero_x = pos_hero[0]
            hero_y = pos_hero[1]
            ALLOW = self._hero.get_allowed()
            UP = ALLOW[0]
            DOWN = ALLOW[1]
            RIGHT = ALLOW[2]
            LEFT = ALLOW[3]

            if RIGHT == False and hero_x == pos_x:  #### CHECK
                for i in range(hero_size[1]):
                    if MAT[hero_y + i][hero_x +
                                       hero_size[0]] not in not_damage:
                        if self._hero.get_shield() == False:
                            self._hero.set_lives(self._hero.get_lives() - 1)
                        self._hero.disappear(MAT, COPY)
                        if MAT[hero_y + i][hero_x +
                                           hero_size[0]] == '*':  ## ZAPPER
                            Z.erase_zapper(MAT, COPY, hero_x + hero_size[0],
                                           hero_y + i)
                            # time.sleep(3)
                        elif MAT[hero_y + i][hero_x + hero_size[0]] == 'M':
                            # MAT[hero_y+i][[hero_x+hero_size[0]] = ' '
                            self._board.Place(hero_x + hero_size[0],
                                              hero_x + hero_size[0],
                                              hero_y + i, hero_y + i, ' ')
                            self._board.PlaceCopy(hero_x + hero_size[0],
                                                  hero_x + hero_size[0],
                                                  hero_y + i, hero_y + i, ' ')
                            mag_exists = False
                        # elif MAT[hero_y-1][hero_x+i] == 'P':
                        #     MAT[hero_y-1][hero_x+i] = ' '
                        #     COPY[hero_y-1][hero_x+i] = ' '
                        #     self._board.set_speed(self._board.get_speed()+1)
                        #     self._speedup = True

                        time.sleep(0.01)
                        X = self._hero.position(MAT, COPY)
                        self._coins += X[0]
                        self._speedup = self._speedup or X[1]
                        self._board.set_matrix(MAT)
                        self._board.set_copy(COPY)
                        ### invincibility
                        break

            MAT = self._board.get_matrix()
            COPY = self._board.get_copy()

            for ii in self._fired_bullets:
                x = ii.Update_bullet(MAT, COPY, dims[1], pos_x)
                if x == True:
                    self._fired_bullets.remove(ii)
                elif x == None:
                    pass
                else:
                    # print(x)
                    # time.sleep(1)
                    y = Z.erase_zapper(MAT, COPY, x[0] + ii.get_direction(),
                                       x[1])
                    if y == False:  # zapper not found, i.e. boss
                        if MAT[x[1]][x[0] + ii.get_direction()] == 'M':
                            # print("MAG DESTR")
                            # sleep(1)
                            MAT[x[1]][x[0] + ii.get_direction()] = ' '
                            # COPY[x[1]][x[0]+ii.direction] = ' '     ### CHECK
                            self._fired_bullets.remove(ii)
                            del ii
                            mag_exists = False

                        elif MAT[x[1]][x[0] + ii.get_direction(
                        )] == 'o' or MAT[x[1]][x[0] + ii.get_direction(
                        ) + 1] == 'o':  # or MAT[x[1]][x[0]+ii.get_direction()]==fg.RED+'o'
                            # print("SNOWBALLL DESTR")
                            MAT[x[1]][x[0] + ii.get_direction()] = ' '
                            self._fired_bullets.remove(ii)
                            del ii

                        elif dims[1] - x[0] < 190 and x[
                                0] >= self._boss.get_coords()[0] and MAT[x[1]][
                                    x[0] + ii.get_direction()] != 'o':
                            self._boss.set_lives(self._boss.get_lives() - 1)

            self._board.set_matrix(MAT)
            self._board.set_copy(COPY)

            ALLOW = self._hero.get_allowed()
            UP = ALLOW[0]
            DOWN = ALLOW[1]
            RIGHT = ALLOW[2]
            LEFT = ALLOW[3]

            if inp != 'w' and DOWN == True and self._hero.get_attracted(
            ) == False:  # as long as you hold no up  gravity ; If allowed_down is False, then don t allow to come down;mag overcomes gravity also
                cntr = 0
                self._hero.disappear(MAT, COPY)
                # if self.hero.falling == True:         # checks if falling (i.e. gravity is acting)
                while cntr < gravity and DOWN == True:
                    self._hero.fall(1)
                    self._hero.check_collision(MAT)
                    ALLOW = self._hero.get_allowed()
                    DOWN = ALLOW[1]
                    cntr += 1
                    X = self._hero.position(MAT, COPY)
                    self._coins += X[0]
                    self._speedup = self._speedup or X[1]
                    self._hero.disappear(MAT, COPY)
                fall_at_time += 1  # increments time of falling
                X = self._hero.position(MAT, COPY)
                self._coins += X[0]
                self._speedup = self._speedup or X[1]
                # else:
            else:
                fall_at_time = 0

            ALLOW = self._hero.get_allowed()
            UP = ALLOW[0]
            DOWN = ALLOW[1]
            RIGHT = ALLOW[2]
            LEFT = ALLOW[3]
            MAT = self._board.get_matrix()
            COPY = self._board.get_copy()
            pos_hero = self._hero.get_coords()
            hero_x = pos_hero[0]
            hero_y = pos_hero[1]

            if DOWN == False and inp != 'w':  # downward collision check
                for i in range(hero_size[0]):
                    if MAT[hero_y + hero_size[0]][hero_x +
                                                  i] not in not_damage:
                        if self._hero.get_shield() == False:
                            self._hero.set_lives(self._hero.get_lives() - 1)
                        self._hero.disappear(MAT, COPY)
                        if MAT[hero_y + hero_size[1]][hero_x +
                                                      i] == '*':  ## ZAPPER
                            Z.erase_zapper(MAT, COPY, hero_x + i,
                                           hero_y + hero_size[1])
                        elif MAT[hero_y + hero_size[1]][hero_x + i] == 'M':
                            # self.board.matrix[self.hero.y_coord+self.hero.size_y][self.hero.x_coord+i] = ' '
                            self._board.Place(hero_x + i, hero_x + i,
                                              hero_y + hero_size[1],
                                              hero_y + hero_size[1], ' ')
                            self._board.PlaceCopy(hero_x + i, hero_x + i,
                                                  hero_y + hero_size[1],
                                                  hero_y + hero_size[1], ' ')
                            mag_exists = False
                        time.sleep(0.01)
                        X = self._hero.position(MAT, COPY)
                        self._coins += X[0]
                        self._speedup = self._speedup or X[1]
                        self._board.set_matrix(MAT)
                        self._board.set_copy(COPY)

                        ### invincibility
                        break

            ALLOW = self._hero.get_allowed()
            UP = ALLOW[0]
            DOWN = ALLOW[1]
            RIGHT = ALLOW[2]
            LEFT = ALLOW[3]
            MAT = self._board.get_matrix()
            COPY = self._board.get_copy()
            pos_hero = self._hero.get_coords()
            hero_x = pos_hero[0]
            hero_y = pos_hero[1]
            self._hero.disappear(MAT, COPY)

            if RIGHT == True or hero_x != pos_x:
                pos_x += self._board.get_speed()

            if hero_y < 0:
                hero_y = 0

            if hero_x < pos_x:
                hero_x = pos_x

            elif hero_x + 3 > pos_x + 204:  # at a time 204 columns.
                hero_x = pos_x + 204 - 3

            self._hero.set_coords(hero_x, hero_y)

            X = self._hero.position(MAT, COPY)
            self._coins += X[0]
            self._speedup = self._speedup or X[1]
            self._board.set_matrix(MAT)
            self._board.set_copy(COPY)
            MAT = self._board.get_matrix()
            COPY = self._board.get_copy()
            pos_hero = self._hero.get_coords()
            hero_x = pos_hero[0]
            hero_y = pos_hero[1]
            cols_over = self._board.PrintBoard(pos_x)
            if (
                    RIGHT == True or hero_x != pos_x
            ) and cols_over == True:  ## CHECK THIS FURTHER. What exactly happens at the end of the board. SHUD THIS BE POSITIONED NEAR POS_X+=SPEED ? HOw why?
                pos_x -= self._board.get_speed()
            self._hero.check_collision(MAT)

            ALLOW = self._hero.get_allowed()
            UP = ALLOW[0]
            DOWN = ALLOW[1]
            RIGHT = ALLOW[2]
            LEFT = ALLOW[3]
            pos_hero = self._hero.get_coords()
            hero_x = pos_hero[0]
            hero_y = pos_hero[1]

            if abs(self._boss.get_coords()[0] - hero_x) < 150:
                self._boss.fight(self._hero, MAT, COPY)
                x = random.randint(0, 15)
                if iterator % 5 == 0 and self._boss.get_lives() > 0:
                    self._thrown_snowballs += [
                        Snowball(self._boss.get_coords()[0],
                                 self._boss.get_coords()[1] + x)
                    ]
                for ii in self._thrown_snowballs:
                    x = ii.Update_snowball(MAT, COPY, pos_x)
                    if x == True:
                        self._thrown_snowballs.remove(ii)
                    elif x == None:
                        pass
                    else:
                        if MAT[x[1]][x[0] - 1] in ['^', '\\', '/', '|']:
                            if self._hero.get_shield() == False:
                                self._hero.set_lives(self._hero.get_lives() -
                                                     1)
                            self._hero.disappear(MAT, COPY)
                            time.sleep(0.01)
                            X = self._hero.position(MAT, COPY)
                            self._coins += X[0]
                            self._speedup = self._speedup or X[1]
                            self._board.set_matrix(MAT)
                            self._board.set_copy(COPY)
Example #14
0
    def run_game(self):
        game_over = True
        running = True

        while running:
            if game_over:
                self.show_go_screen()
                game_over = False
                self.all_sprites = pygame.sprite.Group()
                self.mobs = pygame.sprite.Group()
                self.enemies = pygame.sprite.Group()
                self.bullets = pygame.sprite.Group()
                self.powerups = pygame.sprite.Group()

                self.player = Player(self.settings, self.graphics_provider,
                                     self.sound_provider, self.bullets,
                                     self.all_sprites)

                self.all_sprites.add(self.player)
                for i in range(8):
                    self.new_mob()

                for i in range(3):
                    self.new_enemy()

                score = 0

            # keep loop running at the right speed
            self.clock.tick(self.settings.FPS)
            # Process input (events)
            for event in pygame.event.get():
                # check for closing window
                if event.type == pygame.QUIT:
                    running = False

            # Update
            self.all_sprites.update()

            # check to see if a bullet hit a mob
            hits = pygame.sprite.groupcollide(self.mobs, self.bullets, True,
                                              True)
            for hit in hits:
                score += 50 - hit.radius
                random.choice(self.sound_provider.expl_sounds).play()
                expl = Explosion(hit.rect.center, 'large',
                                 self.graphics_provider)
                self.all_sprites.add(expl)
                if random.random() > 0.9:
                    powerup = Powerup(hit.rect.center, self.settings,
                                      self.graphics_provider)
                    self.all_sprites.add(powerup)
                    self.powerups.add(powerup)
                self.new_mob()

            # check to see if a bullet hit an enemy
            hits = pygame.sprite.groupcollide(self.enemies, self.bullets, True,
                                              True)
            for hit in hits:
                score += 50 - hit.radius
                random.choice(self.sound_provider.expl_sounds).play()
                expl = Explosion(hit.rect.center, 'large',
                                 self.graphics_provider)
                self.all_sprites.add(expl)
                if random.random() > 0.9:
                    powerup = Powerup(hit.rect.center, self.settings,
                                      self.graphics_provider)
                    self.all_sprites.add(powerup)
                    self.powerups.add(powerup)
                self.new_enemy()

            # check to see if a mob hit the player
            hits = pygame.sprite.spritecollide(self.player, self.mobs, True,
                                               pygame.sprite.collide_circle)
            for hit in hits:
                self.player.shield -= hit.radius * 2
                expl = Explosion(hit.rect.center, 'small',
                                 self.graphics_provider)
                self.all_sprites.add(expl)
                self.new_mob()
                if self.player.shield <= 0:
                    self.sound_provider.player_die_sound.play()
                    death_explosion = Explosion(self.player.rect.center,
                                                'player',
                                                self.graphics_provider)
                    self.all_sprites.add(death_explosion)
                    self.player.hide()
                    self.player.lives -= 1
                    self.player.shield = 100

            # check to see if player hit a powerup
            hits = pygame.sprite.spritecollide(self.player, self.powerups,
                                               True)
            for hit in hits:
                if hit.type == 'shield':
                    self.player.shield += random.randrange(10, 30)
                    self.sound_provider.shield_sound.play()
                    if self.player.shield >= 100:
                        self.player.shield = 100
                if hit.type == 'gun':
                    self.player.powerup()
                    self.sound_provider.power_sound.play()

            # if the player died and the explosion has finished playing
            if self.player.lives == 0 and not death_explosion.alive():
                game_over = True

            # Draw / render
            self.screen.fill(self.settings.colors.black)
            self.screen.blit(self.graphics_provider.background,
                             self.graphics_provider.background_rect)
            self.all_sprites.draw(self.screen)
            self.draw_text(self.screen, str(score), 18,
                           self.settings.WIDTH / 2, 10)
            self.draw_shield_bar(self.screen, 5, 5, self.player.shield)
            self.draw_lives(self.screen, self.settings.WIDTH - 100, 5,
                            self.player.lives,
                            self.graphics_provider.player_mini_img)
            # *after* drawing everything, flip the display
            pygame.display.flip()
Example #15
0
    def change_owner(self, owner):
        self.owner = owner
        self.color = owner.inverted_color

        if random.random() < globals.pw_chance:
            globals.powerups.append(Powerup(self.i, self.j))
Example #16
0
 def generatePowerup(self, pos, vel):
     if self.level < 3:
         self.powerups.append(
             Powerup(pos, conf.POWERUP_LIST[random.randrange(0, 8)], vel))
Example #17
0
 def __init__(self, row, col, position_row, position_col, startTime):
     Powerup.__init__(self, row, col, position_row, position_col, startTime)
     self.symbol = 'I'
Example #18
0
def run_game():
    pygame.init()
    game_settings = Settings()
    restored_settings = Settings()

    screen = pygame.display.set_mode(
        (game_settings.screen_length, game_settings.screen_height))
    pygame.display.set_caption("Catch It!")
    screen.fill((0, 160, 220))

    # Create basket object
    basket = Basket(screen, game_settings)

    # Create ball object, generate position, draw starting position
    ball = Ball(game_settings, screen, basket)
    ball.generate_random_pos()
    ball.draw_ball()
    bullets = Group()

    # Create spoiled fruit object, generate position, draw starting position
    spoiled_fruits = Group()
    spoiled_fruit = SpoiledFruit(game_settings, screen, basket)
    spoiled_fruits.add(spoiled_fruit)
    spoiled_fruit.generate_random_pos()
    spoiled_fruit.draw_ball()

    powerup = Powerup(game_settings, screen)

    # Create clock object to measure fps
    clock = pygame.time.Clock(
    )  # This creates a Clock object that is used to track time, with the other methods that come along with it.
    pygame.font.init()  # Initializes font module
    fps_font = pygame.font.Font(
        None, 30
    )  # This creates a new font object that we will use to display the fps. 'None' just tells it to use the default pygame font, 30 is font size.
    fps_constant = 150

    lives_font = pygame.font.Font(None, 30)
    play_button = PlayButton(screen, game_settings, 'Play!', 50,
                             game_settings.screen_height / 2)
    play_again_button = PlayButton(screen, game_settings, 'Play Again', 50,
                                   game_settings.screen_height - 200)
    play_button.draw_button()
    pygame.display.flip()

    # Score for how many good fruits are caught.
    score = pygame.font.Font(None, 50)

    # Pause text when game is paused
    pause = pygame.font.Font(None, 100)

    while True:
        gf.check_events(basket, bullets, screen, game_settings,
                        restored_settings, play_button, play_again_button,
                        spoiled_fruits, ball, pause)
        if game_settings.game_active == False:
            clock.tick()

        if game_settings.game_active:

            gf.check_spoiledfruit_collisions(ball, basket, game_settings,
                                             screen, spoiled_fruit, bullets,
                                             spoiled_fruits)

            gf.check_other_collisions(game_settings, screen, basket, ball,
                                      spoiled_fruits, powerup)

            gf.check_powerup_collisions(game_settings, screen, powerup, basket,
                                        bullets)

            gf.check_game_assist(game_settings, ball, spoiled_fruits)

            gf.update_screen(screen, game_settings, basket, ball, clock,
                             fps_font, lives_font, bullets, spoiled_fruit,
                             spoiled_fruits, play_button, play_again_button,
                             powerup, score, fps_constant)
        else:
            pygame.display.flip()
class Game:
    def __init__(self):
        (w,h) = (800,800)
        pygame.init()
        pygame.display.set_mode((w,h), 0, 32)
        self.screen = pygame.display.get_surface()
        
        self.player = Player(self.screen.get_rect())
        self.player_list = [self.player]                

        self.enemy = Enemy(400,100)
        self.enemy_list = [self.enemy]

        self.respawn = False
        self.respawn_time = time.time()

        self.power_up = Powerup(self.screen)

    def run(self):
        pygame.display.update()
        pygame.time.wait(30)
        self.screen.fill((0,20,0,0))
        pressed_key = pygame.key.get_pressed()
        
        #player
        for p in self.player_list:
            p.key_handler(pressed_key)
            p.update(self.screen)
            p.hit_target(self.enemy_list)
            if not p.is_alive:
                print 'player destroyed'
                self.player_list.remove(p)
        
        #enemy
        for e in self.enemy_list:
            e.update(self.screen, self.player_list)
            e.hit_target(self.player_list)
            if not e.is_alive:
                print 'enemy destroyed'
                self.enemy_list.remove(e)

        #respawn enemy
        if not self.respawn and (not self.enemy_list or not self.player_list):
            self.respawn_time = time.time()
            self.respawn = True

        if time.time() - self.respawn_time > 3 and self.respawn:
            self.respawn_time = time.time()
            self.respawn = False
            if not self.enemy_list:
                self.enemy_list.append(Enemy(400,100))
            if not self.player_list:
                self.player_list.append(Player(self.screen.get_rect()))

        #powerup
        self.power_up.update(self.player_list)

        #event
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
Example #20
0
 def __init__(self,x,y,time):
     Powerup.__init__(self,x,y)
     self._color = POWERUP_DOUBLE_COLOR
     self._timer = POWERUP_DOUBLE_EXPIRE_TIMER
     self._start = time
     self._type = 'DOUBLE'
Example #21
0
def game(game):
    pygame.init()
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)

    one = pygame.image.load("one.jpg").convert_alpha()
    two = pygame.image.load("two.jpg").convert_alpha()
    three = pygame.image.load("three.png").convert_alpha()
    one = pygame.transform.scale(one, (30, 60))
    two = pygame.transform.scale(two, (45, 60))
    three = pygame.transform.scale(three, (40, 60))

    walls = pygame.sprite.Group()
    walls.add(Wall(0, 0, 5, 600))
    walls.add(Wall(0, 0, 1000, 5))
    walls.add(Wall(1000, 0, 5, 600))
    walls.add(Wall(0, 600, 1000, 5))
    walls.add(Wall(100, 100, 400, 5))
    walls.add(Wall(100, 200, 5, 300))
    walls.add(Wall(200, 100, 5, 200))
    walls.add(Wall(200, 400, 5, 100))
    walls.add(Wall(300, 200, 100, 5))
    walls.add(Wall(200, 400, 400, 5))
    walls.add(Wall(600, 200, 5, 200))
    walls.add(Wall(300, 500, 400, 5))
    walls.add(Wall(600, 0, 5, 100))
    walls.add(Wall(500, 200, 5, 100))
    walls.add(Wall(300, 300, 200, 5))
    walls.add(Wall(800, 200, 5, 300))
    walls.add(Wall(700, 100, 5, 100))
    walls.add(Wall(700, 300, 5, 100))
    walls.add(Wall(700, 100, 200, 5))
    walls.add(Wall(800, 500, 100, 5))
    walls.add(Wall(900, 200, 5, 200))
    wlist = walls.sprites()

    grid = []

    for i in range(13):
        curli = []
        if i % 2 == 0:
            for j in range(10):
                curli.append(0)
                for item in wlist:
                    if item.contains(j * 100 + 30, i / 2 * 100):
                        curli[j] = 1
        else:
            for j in range(11):
                curli.append(0)
                for item in wlist:
                    if item.contains(j * 100, (i - 1) * 50 + 30):
                        curli[j] = 1
        grid.append(curli)
    print(grid)

    SCREENWIDTH = 1005
    SCREENHEIGHT = 605
    size = (SCREENWIDTH, SCREENHEIGHT)
    screen = pygame.display.set_mode(size)

    cd = 0
    tic = 0
    s1 = 0
    s2 = 0
    pygame.display.set_caption("Player 1 - " + str(s1) + "  Player 2 - " + str(s2))

    d = random.randint(0,3)
    d2 = random.randint(0,3)
    xpos1 = 100*random.randint(0, 9) + 20
    ypos1 = 100*random.randint(0, 5) + 35

    var = True
    while var:
        xpos2 = 100 * random.randint(0, 9) + 20
        ypos2 = 100 * random.randint(0, 5) + 35
        xn = xpos2 // 100
        yn = ypos2 // 100
        count = 0
        if grid[yn * 2][xn] == 1:
            count += 1
        if grid[yn * 2 + 2][xn] == 1:
            count += 1
        if grid[yn * 2 + 1][xn] == 1:
            count += 1
        if grid[yn * 2+1][xn + 1] == 1:
            count += 1
        if count < 2:
            var = False


    if game == 2:
        player = Tank(xpos1, ypos1, "1", 0)
        player2 = Tank(xpos2, ypos2, "2", 0)
    if game == 1:
        player = Tank(xpos1, ypos1, "1", 1)
        player2 = Tank(xpos2, ypos2, "2", 1)

    player.setangle(d*30)
    player.setangle(0)
    player2.setangle(d2*30)
    player2.setangle(0)


    all_sprites_list = pygame.sprite.Group()
    players = pygame.sprite.Group()
    p1 = pygame.sprite.Group()
    p1.add(player)
    p2 = pygame.sprite.Group()
    p2.add(player2)
    all_sprites_list.add(player)
    all_sprites_list.add(player2)
    players.add(player)
    players.add(player2)
    shrinkTime = random.randint(0,1000)
    wallbreakTime = random.randint(0,1000)





    all_sprites_list.add(walls)
    player.walls = walls
    player2.walls = walls


    carryOn = True
    MOVE_RIGHT = 1
    MOVE_LEFT = -1
    direction = 0
    direction2 = 0
    SPIN_UP = 1
    SPIN_DOWN = -1
    angle = 0
    angle2 = 0
    score= 0
    ais = True
    p2angle = 0
    AIdir = -1
    m = 0
    last = 0
    resetAngle = False
    CLOCK = 0
    p1shots = 0
    p2shots = 0

    while carryOn:
        CLOCK+=1
        screen.fill(WHITE)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                carryOn = False
            if event.type == KEYDOWN:
                if event.key == K_DOWN:
                    direction = MOVE_LEFT
                elif event.key == K_UP:
                    direction = MOVE_RIGHT
                elif event.key == K_LEFT:
                    angle = SPIN_UP
                elif event.key == K_RIGHT:
                    angle = SPIN_DOWN
                elif event.key == K_SPACE:
                    if player.alive() and p1shots <=5:
                        bullet = player.shoot()
                        all_sprites_list.add(bullet)
                elif event.key == K_s:
                    direction2 = MOVE_LEFT
                elif event.key == K_w:
                    direction2 = MOVE_RIGHT
                elif event.key == K_a:
                    angle2 = SPIN_UP
                elif event.key == K_d:
                    angle2 = SPIN_DOWN
                elif event.key == K_q:
                    if player2.alive() and p2shots <= 5:
                        bullet = player2.shoot()
                        all_sprites_list.add(bullet)


            elif event.type == KEYUP:
                if event.key == K_UP:
                    direction = 0
                elif event.key == K_DOWN:
                    direction = 0
                elif event.key == K_RIGHT:
                    angle = 0
                elif event.key == K_LEFT:
                    angle = 0
                elif event.key == K_w:
                    direction2 = 0
                elif event.key == K_s:
                    direction2 = 0
                elif event.key == K_d:
                    angle2 = 0
                elif event.key == K_a:
                    angle2 = 0

        player.setangle(angle)
        player.move(direction)

        p1shots = 0
        p2shots = 0

        for item in all_sprites_list.sprites():
            if item.getType() == "bullet":
                if item.tank == "2":
                    p2shots += 1
                if item.tank == "1":
                    p1shots += 1

        if game == 1:
            if ais:
                olda = p2angle
                p2angle = player2.RAIdir(grid,player)
                if olda != p2angle:
                    tic = 0
                if p2angle != -1:
                    if (p2angle - player2.dir) % 360 < 2 or (player2.dir - p2angle) % 360 < 2:
                        if tic % 50 == 15 and p2shots <= 5:
                            if player2.alive():
                                bullet = player2.shoot()
                                all_sprites_list.add(bullet)
                    elif (p2angle - player2.dir) % 360 < 180:
                        player2.setangle(1)
                    else:
                        player2.setangle(-1)
                else:
                    ais = False

            else:
                if AIdir == -1:
                    AIdir = player2.AIMove(grid, player, last)
                if (AIdir - player2.dir) % 360 < 2 or (player2.dir - AIdir) % 360 < 2:
                    if m < 20:
                        player2.move(1)
                        m += 1
                    else:
                        player2.move(0)
                        last = AIdir
                        AIdir = -1
                        m = 0
                        ais = True
                elif (AIdir - player2.dir) % 360 < 180:
                    player2.setangle(1)
                else:
                    player2.setangle(-1)
        tic += 1

        if game == 2:
            if CLOCK%1000 == shrinkTime:
                all_sprites_list.add(Powerup("shrink", 100 * random.randint(0, 9) + 20, 100 * random.randint(0, 5) + 35))

            if CLOCK%1000 == wallbreakTime:
                all_sprites_list.add(Powerup("wallbreak", 100 * random.randint(0, 9) + 20, 100 * random.randint(0, 5) + 35))

        if game == 2:
            player2.setangle(angle2)
            player2.move(direction2)
        num = 0
        for item in all_sprites_list:
            if item.getType() == "bullet":
                num = item.hit(players, p1, p2)
            elif item.getType() == "powerup":
                num = item.hit(player, player2)
                if num == "1":
                    all_sprites_list.remove(item)
                    player.setPowerup(item)
                elif num == "2":
                    all_sprites_list.remove(item)
                    player2.setPowerup(item)

        if not(player in all_sprites_list.sprites()):
            if not score == 1:
                score = -1
        elif not(player2 in all_sprites_list.sprites()):
            if not score == -1:
                score = 1
        else:
            score = 0

        all_sprites_list.update()

        if (not player2.alive()) or (not player.alive()):
            cd += 1
            if cd < 25:
                screen.blit(three, (930, 20))
            elif 75 > cd > 50:
                screen.blit(two, (930, 20))
            elif 100 < cd < 125:
                screen.blit(one, (930, 20))
            if cd == 150:
                cd = 0
                if not player.alive() and not player2.alive():
                    s1 += 1
                    s2 += 1
                elif not player.alive():
                    s2 += 1
                elif not player2.alive():
                    s1 += 1

                d = random.randint(0, 3)
                d2 = random.randint(0, 3)
                xpos1 = 100 * random.randint(0, 9) + 20
                ypos1 = 100 * random.randint(0, 5) + 35
                var = True
                while var:
                    xpos2 = 100 * random.randint(0, 9) + 20
                    ypos2 = 100 * random.randint(0, 5) + 35
                    xn = xpos2 // 100
                    yn = ypos2 // 100
                    count = 0
                    if grid[yn * 2][xn] == 1:
                        count += 1
                    if grid[yn * 2 + 2][xn] == 1:
                        count += 1
                    if grid[yn * 2 + 1][xn] == 1:
                        count += 1
                    if grid[yn * 2 + 1][xn + 1] == 1:
                        count += 1
                    if count < 2:
                        var = False
                if game == 2:
                    player = Tank(xpos1, ypos1, "1", 0)
                    player2 = Tank(xpos2, ypos2, "2", 0)
                if game == 1:
                    player = Tank(xpos1, ypos1, "1", 1)
                    player2 = Tank(xpos2, ypos2, "2", 1)

                player.setangle(d * 30)
                player.setangle(0)
                player2.setangle(d2 * 30)
                player2.setangle(0)

                all_sprites_list = pygame.sprite.Group()
                players = pygame.sprite.Group()
                p1 = pygame.sprite.Group()
                p1.add(player)
                p2 = pygame.sprite.Group()
                p2.add(player2)
                all_sprites_list.add(player)
                all_sprites_list.add(player2)
                players.add(player)
                players.add(player2)
                all_sprites_list.add(walls)
                player.walls = walls
                player2.walls = walls
                CLOCK = 0
                shrinkTime = random.randint(0, 1000)
                wallbreakTime = random.randint(0, 1000)




        pygame.display.set_caption("Player 1 - " + str(s1) + "  Player 2 - " + str(s2))
        all_sprites_list.draw(screen)

        pygame.display.flip()

    pygame.quit()
Example #22
0
                running = False

    # Updated
    all_sprites.update()
    # mobs and bullets collision.
    hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
    for hit in hits:
        stats.score += 50 - hit.radius
        score_board.prep_score()
        explode = Explosion(ui_settings, mob_explose_sheet, 64, 64,
                            hit.rect.center)
        explode.effects.play()
        all_sprites.add(explode)
        # Randomize the chances of getting a powerupsself.
        if random.random() > 0.9:
            power = Powerup(ui_settings, hit.rect.center)
            all_sprites.add(power)
            powers.add(power)
        m = Mob(ui_settings)
        all_sprites.add(m)
        mobs.add(m)

    # Check if the ship hit the powersself.
    powerhits = pygame.sprite.spritecollide(player, powers, True)
    if powerhits:
        for hit in powerhits:
            hit.effects.play()
            if hit.power_type == 'shield':
                player.shield += random.randrange(10, 20)
                if player.shield >= 100:
                    player.shield = 100
Example #23
0
    args = parser.parse_args()

    strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA,
                              LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
    strip.begin()

    board = Matrix_board(16, 16)
    board.createBoard()

    snake = Snake(True, 3, 4, 8, 0)
    snake.spawnSnake(strip, Color, board)

    apple = Apple()
    apple.spawnApple(strip, Color, randint(0, 255))

    powerup = Powerup()

    print("*** AVAILABLE DEVICES ***")
    for device in devices:
        print(device)
    print("*************************")

    print('Press Ctrl-C to quit.')
    if not args.clear:
        print('Use "-c" argument to clear LEDs on exit')

    try:
        t1 = threading.Thread(target=gamepadEventListener)
        t1.daemon = True
        t1.start()