def get_sector_1(): objects = [] enemy_set = [[150, 22, 0], [210, 18, 1], [210, 28, 2], [240, 10, 3], [240, 15, 3], [240, 30, 4], [240, 35, 4], [240, 40, 4]] enemies = [enemy.StrongEnemy(Vector2D(345, 22), 5)] for i in range(0, 7): enemies.append( enemy.TinyEnemy(Vector2D(enemy_set[i][0], enemy_set[i][1]), enemy_set[i][2])) objects.append(enemies) obstacles = [ Obstacle(Vector2D(150, 3), 5), Obstacle(Vector2D(150, 48), 6), Obstacle(Vector2D(230, 3), 2), Obstacle(Vector2D(230, 39), 1) ] objects.append(obstacles) powerups = [ PowerUp(Vector2D(50, 22), 2), PowerUp(Vector2D(180, 22), 3), PowerUp(Vector2D(250, 22), 1) ] objects.append(powerups) return objects
def fillBricks(self, num): array = [] array2 = [] if num == 1: a = 0 for i in range(0, 18, 3): for j in range(0, 144, 8): stren = random.randint(1, 5) hl = int(j / 8) whbrick = np.random.choice(2, 1, p=[0.9, 0.1]) if whbrick: stren = 3 if (hl == a or hl == a + 1 or hl == 17 - a or hl == 17 - a - 1) and int(i / 3) != 5: stren = 6 whbrick = 0 array.append( Brick(9 + i, 30 + j, 3, 8, 0, 0, stren, whbrick)) if (stren == 3 or stren == 4): array2.append( PowerUp(9 + i, 30 + j, 1, 1, 0, 0, random.choice(POWERUPS))) a += 2 if num == 2: a = 10 for i in range(0, 18, 3): for j in range(0, 144, 8): stren = random.randint(1, 5) hl = int(j / 8) whbrick = np.random.choice(2, 1, p=[0.9, 0.1]) if whbrick: stren = 3 if (hl == a or hl == a + 1 or hl == 17 - a or hl == 17 - a - 1) and int(i / 3) != 0: stren = 6 whbrick = 0 if not ((int(i / 3) == 0 and int(j / 8) == 0) or (int(i / 3) == 0 and int(j / 8) == 17)): array.append( Brick(9 + i, 30 + j, 3, 8, 0, 0, stren, whbrick)) if (stren == 3 or stren == 4): array2.append( PowerUp(9 + i, 30 + j, 1, 1, 0, 0, random.choice(POWERUPS))) a -= 2 if num == 3: array.append(Boss(0, int(FRAMEWIDTH / 2), 7, 33, 0, 1, 20, 0)) for i in range(9, 18, 3): for j in range(0, 144, 8): stren = random.randint(1, 5) if stren == 5: array.append(Brick(9 + i, 30 + j, 3, 8, 0, 0, stren, 0)) return array, array2
def setup_game(self, filename: str) -> None: """ Set up a game with walls, player, food, monsters, and powerUp food on the screen """ # open data file to initialize the game screen with open(os.path.join("images", filename)) as f: data = [line.split() for line in f] w = len(data[0]) h = len(data) + 1 self._actors = [] self.stage_width, self.stage_height = w, h - 1 self.size = (w * ICON_SIZE, h * ICON_SIZE) # loop though each line to the file and set up the game for i in range(len(data)): for j in range(len(data[i])): # initialize all objects in the file and add them to the _actors attribute legend = data[i][j] if legend == 'P': # P is player object player = Player("player.png", j, i) self.player = player self._actors.append(player) elif legend == 'X': # X is a wall object self._actors.append(Wall("wall.png", j, i)) elif legend == 'M': # M is a monster object self._actors.append(Monster("monster.png", j, i)) elif legend == 'O': # O is a food object self._actors.append(Food("food.png", j, i)) elif legend == 'U': # U is a powerup object self._actors.append(PowerUp("powerUp.png", j, i))
def get_sector_end(): objects = [] enemies = [enemy.Boss(Vector2D(720, 20), 6)] objects.append(enemies) obstacles = [ Obstacle(Vector2D(525, 3), 5), Obstacle(Vector2D(525, 48), 6), Obstacle(Vector2D(590, 3), 8), Obstacle(Vector2D(610, 3), 8), Obstacle(Vector2D(630, 3), 8), Obstacle(Vector2D(705, 3), 2), Obstacle(Vector2D(705, 39), 1) ] objects.append(obstacles) powerups = [ PowerUp(Vector2D(670, 4), 2), PowerUp(Vector2D(685, 4), 3), PowerUp(Vector2D(670, 7), 2), PowerUp(Vector2D(685, 7), 3), PowerUp(Vector2D(670, 45), 1), PowerUp(Vector2D(685, 45), 1) ] objects.append(powerups) return objects
def fillBricks(self): array=[] array2=[] a=0 for i in range(0,18,3): for j in range(0,144,8): stren=random.randint(1,5) hl=int(j/8) if(hl==a or hl== a+1 or hl==17-a or hl==17-a-1) and int(i/3)!=5: stren=6 array.append(Brick(9+i,30+j,3,8,0,0,stren)) if (stren==3 or stren==4): array2.append(PowerUp(9+i,30+j,1,1,0,0,random.choice(POWERUPS))) a+=2 return array,array2
def _update(self, ballobj, paddleobj, ch, brickobj, powerupobj, bulletobj, cannonobj): self.__gameobjects = [] self.__gameobjects.append(powerupobj) self.__gameobjects.append(brickobj) self.__gameobjects.append(ballobj) self.__gameobjects.append(paddleobj) self.__gameobjects.append(bulletobj) self.__gameobjects.append(cannonobj) self._resetArray() pblength = [] for i in ballobj: pblength.append(i._y - paddleobj[0]._y) paddleobj[0].move(ch) for iti in range(len(ballobj)): # print(ballobj[iti].isCollidedWithPaddle) if (ballobj[iti].isCollidedWithPaddle): ballobj[iti].attach(paddleobj[0], pblength[iti]) if self.bossStrength == 12: if self.defenseWalls == 2: for i in range(0, 200, 8): brickobj.append( Brick(11, i, 3, 8, 0, 0, random.randint(1, 4), 0)) self.defenseWalls -= 1 if self.bossStrength == 8: if self.defenseWalls == 1: for i in range(0, 200, 8): brickobj.append( Brick(15, i, 3, 8, 0, 0, random.randint(1, 4), 0)) self.defenseWalls -= 0 if (1000 * (time.time() - self.lasttime) >= 100): for iti in range(len(ballobj)): if not (ballobj[iti].isCollidedWithPaddle): ballobj[iti].move(paddleobj[0], brickobj) if len(paddleobj[0].powerups["B"]) != 0: if MUSIC: os.system('aplay -q ./sounds/bullet.wav&') bulletobj.append( Bullet(paddleobj[0]._x - 1, paddleobj[0]._y, 1, 1, -1, 0)) bulletobj.append( Bullet(paddleobj[0]._x - 1, paddleobj[0]._y + paddleobj[0]._ylength - 1, 1, 1, -1, 0)) for iti in range(len(bulletobj)): if not (bulletobj[iti].isCollided): bulletobj[iti].move(paddleobj[0], brickobj) for iti in powerupobj: if iti.isVel: iti.move(paddleobj[0], ballobj, cannonobj) if len(ballobj) == 0: self.lives -= 1 ballobj.append( Ball(FRAMEHEIGHT - 3, paddleobj[0]._y + int(paddleobj[0]._ylength / 2), 1, 1, -2, 2)) if (3 - self.levels) == 3: for i in brickobj: if i._type == "BOSS": i.move(paddleobj[0]) self.bossStrength = i.strength break paddleobj[0].removePowerUp(ballobj) self.lasttime = time.time() self.falltime += 1 self.gravity += 1 self.bomb += 1 for item in cannonobj: item.attach(paddleobj[0]) if self.falltime > 50: flag = 0 for i in ballobj: if i._x == paddleobj[0]._x - 1 and paddleobj[ 0]._y <= i._y < paddleobj[0]._y + paddleobj[0]._ylength: flag = 1 break if flag: self.falltime = 0 for i in brickobj: if not (3 - self.levels == 3 and i._type == "BOSS"): i._x += 1 if i._x + 3 >= FRAMEHEIGHT - 3: self.lives = 0 for i in powerupobj: if i._xvel == 0 and i._yvel == 0: i._x += 1 if self.gravity > 5: for i in powerupobj: if i.isVel: self.gravity = 0 i._xvel += 1 if self.bomb > 30: for i in brickobj: if i._type == "BOSS": powerupobj.append( PowerUp(i._x + i._xlength, i._y + int(i._ylength / 2), 1, 1, 0, 1, '@')) self.bomb = 0 break
def main(): """ Main Program """ pygame.init() multiplayer = False # Set the height and width of the screen size = [constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT] screen = pygame.display.set_mode(size, pygame.FULLSCREEN) pygame.display.set_caption("Artnoc") #add in a scoreboard font = pygame.font.Font(None, 28) #player1 = Player() #player2 = Player() #enemy_list = [] #level_list = [] #level_list.append(levels.Level_01(player1, player2)) #current_level = level_list[0] #active_sprite_list = pygame.sprite.Group() #enemy_sprite_list = pygame.sprite.Group() #done = False #clock = pygame.time.Clock() play = False #----------MAIN MENU----------------- while True: multiplayer = mainMenu(screen, play, multiplayer) # Create the player1 player1 = Player() player2 = Player() enemy_list = [] powerup_list = [] # Create all the levels level_list = [] level_list.append(levels.Level_01(player1, player2)) # Set the current level current_level = level_list[0] for i in range(0, 3): enemy_list.append(Enemy(random.randint(0, 1))) enemy_list[i].level = current_level for i in range(0, 4): powerup_type = random.randint(2, 4) powerup_list.append(PowerUp(powerup_type)) powerup_list[i].level = current_level active_sprite_list = pygame.sprite.Group() enemy_sprite_list = pygame.sprite.Group() powerup_sprite_list = pygame.sprite.Group() player1.level = current_level if multiplayer == True: player2.level = current_level for i in range(0, 3): x = random.randint(constants.SCREEN_WIDTH, constants.SCREEN_WIDTH + 1) #y = random.randint(0,constants.SCREEN_HEIGHT-5) enemy_list[i].spawn(x, 0) enemy_sprite_list.add(enemy_list[i]) for i in range(0, 1): x = random.randint(0, 10000) powerup_list[i].spawn(x, 0) powerup_sprite_list.add(powerup_list[i]) player1.rect.x = 340 player1.rect.y = constants.SCREEN_HEIGHT - player1.rect.height active_sprite_list.add(player1) if multiplayer == True: player2.rect.x = 340 player2.rect.y = constants.SCREEN_HEIGHT - player2.rect.height active_sprite_list.add(player2) # Used to manage how fast the screen updates clock = pygame.time.Clock() play = True newpoweruplength = constants.SCREEN_WIDTH powerupflag = False #----------END MAIN MENU, START INITIALISATION------------- #init(multiplayer) # -------- Main Game Loop ----------- while (player1.lives > 0 and player2.lives > 0) and (play == True): for event in pygame.event.get(): # User did something if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: play = False if event.key == pygame.K_DOWN: player1.prone() if event.key == pygame.K_LEFT: player1.go_left() if event.key == pygame.K_RIGHT: player1.go_right() if event.key == pygame.K_UP: player1.aimup() if event.key == pygame.K_j: player1.jump() if event.key == pygame.K_k: player1.shoot() if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT and player1.change_x > 0: player1.stop() if event.key == pygame.K_UP or event.key == pygame.K_DOWN: player1.resetaim() if event.key == pygame.K_LEFT and player1.change_x < 0: player1.stop() if event.key == pygame.K_k: player1.stopshooting() # if multiplayer == True: if event.type == pygame.KEYDOWN: if event.key == pygame.K_s: player2.prone() if event.key == pygame.K_a: player2.go_left() if event.key == pygame.K_d: player2.go_right() if event.key == pygame.K_w: player2.aimup() if event.key == pygame.K_1: player2.jump() if event.key == pygame.K_2: player2.shoot() if event.type == pygame.KEYUP: if event.key == pygame.K_d and player2.change_x > 0: player2.stop() if event.key == pygame.K_w or event.key == pygame.K_s: player2.resetaim() if event.key == pygame.K_a and player2.change_x < 0: player2.stop() if event.key == pygame.K_2: player2.stopshooting() # Update the player1. active_sprite_list.update(enemy_list) enemy_sprite_list.update(player1) powerup_sprite_list.update(player1) if multiplayer == True: enemy_sprite_list.update(player2) powerup_sprite_list.update(player2) #update the enemies #for enemy in levels.Level.enemy_list: # enemy.update() # Update items in the level current_level.update() # If the player1 gets near the right side, shift the world left (-x) if player1.rect.x >= 500: diff = player1.rect.x - 500 player1.rect.x = 500 current_level.shift_world(-diff) current_level.draw(screen) # If the player1 gets near the left side, shift the world right (+x) if player1.rect.x <= 120: diff = 120 - player1.rect.x player1.rect.x = 120 current_level.shift_world(diff) current_level.draw(screen) if multiplayer == True: if player2.rect.x >= 500: diff = player2.rect.x - 500 player2.rect.x = 500 current_level.shift_world(-diff) current_level.draw(screen) if player2.rect.x <= 120: diff = 120 - player2.rect.x player2.rect.x = 120 current_level.shift_world(diff) current_level.draw(screen) # If the player1 gets to the end of the level, go to the next level current_position = player1.rect.x + current_level.world_shift if current_position < current_level.level_limit: player1.rect.x = 120 if current_level_no < len(level_list) - 1: current_level_no += 1 current_level = level_list[current_level_no] player1.level = current_level if multiplayer == True: current_position = player2.rect.x + current_level.world_shift if current_position < current_level.level_limit: player2.rect.x = 120 if current_level_no < len(level_list) - 1: current_level_no += 1 current_level = level_list[current_level_no] player2.level = current_level # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT current_level.draw(screen) active_sprite_list.draw(screen) enemy_sprite_list.draw(screen) powerup_sprite_list.draw(screen) for x in enemy_sprite_list: if x.bullet_list != None: x.bullet_list.draw(screen) if multiplayer == True: if player1.dead == False and player2.dead == False: if player1.bullet_list != None: player1.bullet_list.draw(screen) if player2.bullet_list != None: player2.bullet_list.draw(screen) else: if player1.dead == False: if player1.bullet_list != None: player1.bullet_list.draw(screen) text = font.render("P1 Score = " + str(player1.score), 1, (constants.WHITE)) screen.blit(text, (0, 0)) if multiplayer == True: text = font.render("P2 Score = " + str(player2.score), 1, (constants.WHITE)) screen.blit(text, (0, 40)) text = font.render("Quit to Main Menu (Esc)", 1, constants.WHITE) screen.blit(text, (constants.SCREEN_WIDTH / 2 - 120, 0)) text = font.render("P1 Lives = " + str(player1.lives), 1, (constants.WHITE)) screen.blit(text, (constants.SCREEN_WIDTH - 115, 0)) if multiplayer == True: text = font.render("P2 Lives = " + str(player2.lives), 1, (constants.WHITE)) screen.blit(text, (constants.SCREEN_WIDTH - 115, 40)) # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT # Limit to 60 frames per second clock.tick(60) # Go ahead and update the screen with what we've drawn. pygame.display.flip() # Be IDLE friendly. If you forget this line, the program will 'hang' # on exit. pygame.quit()
bg_obj.display_ceil(board_obj.grid) bg_obj.display_left_wall(board_obj.grid) bg_obj.display_right_wall(board_obj.grid) # The paddle paddle_obj = Paddle(0, 0) paddle_obj.show(board_obj.grid, int(WIDTH / 2), int(WIDTH / 2) + PADDLE_LEN) # The enemy enemy_obj = Enemy(0, 30) # Moving balls ball_obj_array = [Ball(0, 0)] # Powerups powerup_obj_array = [PowerUp(0, 0) for i in range(40)] SHOOT_TIME = [0] BOMB_TIME = [0] START_BOMB_FLAG = [0] def bricks_fallen(): os.system('clear') game_over() print() print(Fore.MAGENTA + Style.BRIGHT + "Better Luck next time!".center(SCREEN) + Style.RESET_ALL) print(Fore.MAGENTA + Style.BRIGHT + str("SCORE: " + str(SCORE[0])).center(SCREEN) + Style.RESET_ALL)