def run_game(): pygame.init() settings = game_settings.Settings() screen = pygame.display.set_mode(settings.resolution) snake = Snake(screen, settings) pygame.display.set_caption("Snake") my_tail = [] x, y = gf.generate_randoms() food = GroupSingle(Food(snake, screen, x, y)) tails = OrderedUpdates() gf.initialise_snake(snake, screen, my_tail, tails, settings) button = Play_button(screen, settings, "Play") end_game_screen = EndGameScreen(screen, settings, "Game Over") score = Score(screen, settings) clock = pygame.time.Clock() gametime = GTime(clock) while True: screen.fill(settings.bg_color) score.draw_me() gf.check_events(snake, food, screen, my_tail, tails, settings, button, gf, end_game_screen, score, gametime) if settings.game_active == False: if gf.lose_condition_met(snake, settings, tails, gametime) == False: button.draw_me() if settings.game_active == True: snake.update() tails.update() snake.draw_me() food.update() clock.tick(10) gametime.update() print(gametime.time) pygame.display.flip()
class KoalaKross(Microgame): def __init__(self): Microgame.__init__(self) self.rect= Rect(0,0, locals.WIDTH, locals.HEIGHT) ## surface=pygame.display.set_mode((1024,768)) ## surface.fill(Color(0,0,250)) self.koala=Koala(self) self.panda1=Panda(self,200,100,randint(2,10),randint(5,15)) self.panda2=Panda(self,500,100,randint(5,15),randint(10,25)) self.panda3=Panda(self,800,100,randint(6,17),randint(20,30)) self.bread=Bread(self,randint(400,1000),randint(100,600)) self.background=Background() self.sprites=OrderedUpdates([self.background,self.koala,self.bread,self.panda1,self.panda2,self.panda3,self.background]) self.clock=pygame.time.Clock() def start(self): ## music.load(join('games','Koala_Kross','remix.ogg') music.load('games/Koala_Kross/remix.ogg') music.play() def stop(self): music.stop() def update(self): ## time=pygame.time.get_ticks() self.sprites.update() for event in pygame.event.get(): if event.type==KEYDOWN and event.key==K_DOWN: self.koala.velocity=(0,30) if event.type==KEYUP and event.key==K_DOWN: self.koala.velocity=(0,0) if event.type==KEYDOWN and event.key==K_UP: self.koala.velocity=(0,-30) if event.type==KEYUP and event.key == K_UP: self.koala.velocity=(0,0) if event.type==KEYDOWN and event.key == K_LEFT: self.koala.velocity=(-30,0) if event.type==KEYUP and event.key==K_LEFT: self.koala.velocity=(0,0) if event.type==KEYDOWN and event.key==K_RIGHT: self.koala.velocity=(30,0) if event.type==KEYUP and event.key==K_RIGHT: self.koala.velocity=(0,0) if event.type==KEYDOWN and event.key == K_q: self.lose() def render (self, surface): ## Surface=display.set_mode((1024,768)) surface.fill(Color(0,0,0)) self.sprites.draw(surface)
class Gameboard(object): ''' classdocs ''' def __init__(self, surface, width, height, song_filename): ''' Constructor ''' #progressively increase; must end with 1 self.PROB_HEALTH = 0.4 self.PROB_KI = 0.7 self.PROB_SHIELD = 0.9 self.PROB_SWORD = 1.0 self.windowSurface = surface self.width = width self.height = height self.song_filename = song_filename board_size = (width, height) self.gameSurface = Surface(board_size) # This will be drawn every frame to the window song_file = open(song_filename) self.song_name = song_file.readline().strip() self.song_length = float(song_file.readline()) self.pixels_per_second = float(song_file.readline()) self.level_filename = song_file.readline().strip() self.music_filename = song_file.readline().strip() self.background_filename = song_file.readline().strip() self.pixel_offset = 0 self.last_frame_time = -1 self.frac_scroll = 0 # print "{0}[{1}] : {2}".format(self.song_name, self.song_length, self.pixels_per_second) # self.background_width = (self.pixels_per_second * self.song_length) + width self.background_width = width + 96 # Jank scroll edition! background_size = (self.background_width, height) self.backgroundSurface = Surface(background_size) self.__render_background() self.backgroundSurface.set_colorkey((0,0,0),pygame.RLEACCEL) #self.backgroundSurface.set_colorkey((217,62,245),pygame.RLEACCEL) #self.backgroundSurface.set_alpha(100,pygame.RLEACCEL) self.mainScreenBackground,self.mainScreenBackgroundRect = load_image_from_folder('backgrounds', self.background_filename) #self.backgroundSurface.blit(mainScreenBackground, (0,0)) #self.__render_background() possible_samurai_positions = [] for i in range(0, 6): possible_samurai_positions.append(PATH_HEIGHT * i + 15) self.samurai = Samurai(possible_samurai_positions) self.samurai_sprite_group = Group(self.samurai) self.bridge_group = OrderedUpdates() self.mine_group = OrderedUpdates() self.enemy_group = OrderedUpdates() self.attack_group = OrderedUpdates() self.healthpack_group = OrderedUpdates() self.ki_potion_group = OrderedUpdates() self.shield_group = OrderedUpdates() self.sword_group = OrderedUpdates() self.explosion_group = OrderedUpdates() # tempSprite = self.samurai_sprite_group.sprites() # tempRect = tempSprite[0].get_rect() # self.testSword = VerticalSlash(tempRect.centerx,tempRect.centery, self.remove_attack) # self.attack_group.add(self.testSword) if sys.platform == "win32": # On Windows, the best timer is time.clock() self.default_timer = time.clock else: # On most other platforms, the best timer is time.time() self.default_timer = time.time def draw(self): self.gameSurface.fill((0,0,0)) #self.gameSurface.fill((217,62,245)) origin = (0, 0) # this_scroll = 0 self.scroll_amount = 0 if self.last_frame_time > 0: cur_time = self.default_timer() self.gap_time = cur_time - self.last_frame_time # print "Pixels per second: {0}\nGap Time: {1}\nScrollAmount: {2}".format(self.pixels_per_second, self.gap_time, this_scroll) self.last_frame_time = cur_time else: self.gap_time = 0 self.last_frame_time = self.default_timer() this_scroll = self.pixels_per_second * self.gap_time self.frac_scroll += this_scroll if self.frac_scroll >= 1: self.scroll_amount = math.floor(self.frac_scroll) self.pixel_offset += self.scroll_amount # print "Now scrolling {0} pixel(s)".format(whole_part) self.frac_scroll -= self.scroll_amount if self.pixel_offset > 96: self.pixel_offset = self.pixel_offset - 96 if self.pixel_offset < 0: self.pixel_offset = 0 self.gameSurface.blit(self.mainScreenBackground, origin) window_rect = Rect(self.pixel_offset, 0, self.gameSurface.get_width(), self.gameSurface.get_height()) # print window_rect self.gameSurface.blit(self.backgroundSurface, origin, window_rect) #All other drawing self.bridge_group.update(self.scroll_amount) self.bridge_group.draw(self.gameSurface) self.mine_group.update(self.scroll_amount) self.mine_group.draw(self.gameSurface) self.enemy_group.update(self.scroll_amount) self.enemy_group.draw(self.gameSurface) self.samurai_sprite_group.update() self.samurai_sprite_group.draw(self.gameSurface) self.healthpack_group.update(self.scroll_amount) self.healthpack_group.draw(self.gameSurface) self.ki_potion_group.update(self.scroll_amount) self.ki_potion_group.draw(self.gameSurface) self.shield_group.update(self.scroll_amount) self.shield_group.draw(self.gameSurface) self.sword_group.update(self.scroll_amount) self.sword_group.draw(self.gameSurface) #self.testSword = VerticalSlash(400,400) #self.attack_group.add(self.testSword) self.attack_group.update() self.attack_group.draw(self.gameSurface) self.explosion_group.update() self.explosion_group.draw(self.gameSurface) # self.testSword.draw(self.gameSurface) for bridge in self.bridge_group.sprites(): if bridge.rect.left < 0: self.bridge_group.remove(bridge) #Annnnd blast it back to the screen window_origin = (0, 60) self.windowSurface.blit(self.gameSurface, window_origin) def add_bridge(self, bridge_num): # print "FAKE BRIDGE" new_bridge = Bridge(1101, bridge_num * PATH_HEIGHT + 39) self.bridge_group.add(new_bridge) def add_mine(self, string_num): # print "CREATE ZE LANDMINE" new_mine = Mine(1101, PATH_HEIGHT * string_num + 42) self.mine_group.add(new_mine) def add_powerup(self, string_num): r = random.random() if r < self.PROB_HEALTH: self.add_healthpack(string_num) elif r < self.PROB_KI: self.add_kiboost(string_num) elif r < self.PROB_SHIELD: self.add_shield(string_num) elif r < self.PROB_SWORD: self.add_sword(string_num) def add_healthpack(self, string_num): # print "such a healthy young man!" new_healthpack = Healthpack(1101, PATH_HEIGHT * string_num + 42) self.healthpack_group.add(new_healthpack) def add_shield(self, string_num): new_shield = Shield(1101, PATH_HEIGHT * string_num + 42) self.shield_group.add(new_shield) def add_sword(self, string_num): new_sword = MegaSword(1101, PATH_HEIGHT * string_num + 42) self.sword_group.add(new_sword) def add_kiboost(self, string_num): new_ki_potion = KiPotion(1101, PATH_HEIGHT * string_num + 42) self.ki_potion_group.add(new_ki_potion) def add_enemy(self, string_num): new_enemy = Enemy(1111, PATH_HEIGHT * string_num + 42) self.enemy_group.add(new_enemy) def add_male_groupie(self, string_num): new_enemy = MaleGroupie(1111, PATH_HEIGHT * string_num + 15) self.enemy_group.add(new_enemy) def add_lawyer(self, string_num): new_lawyer = Lawyer(1111, PATH_HEIGHT * string_num + 15) self.enemy_group.add(new_lawyer) def add_bodyguard(self, string_num): new_bodyguard = Bodyguard(1111, PATH_HEIGHT * string_num + 15) self.enemy_group.add(new_bodyguard) def remove_attack(self, attack): self.attack_group.remove(attack) def add_attack(self, attack): self.attack_group.add(attack) def add_explosion(self, y_val): new_explosion = Explosion(20, y_val, self.explosion_group) def __render_background(self): # Jank implementation that just uses that one sprite over and over again! # Jay's jank dirtBlockThingy is 96x48 pixels num_blocks = int(math.ceil(self.background_width / 96.0)) cur_width = 0 for bI in range(0, num_blocks): for hI in range(0, 6): my_location = (cur_width, (PATH_HEIGHT * hI + 35)) dp = DirtPath(my_location) # print "DirtPath at {0}".format(my_location) self.backgroundSurface.blit(dp.image, my_location) cur_width += 96
class Menu(Prefab): """ Controls the menu system. """ def __init__(self, game): """ Constructor. Args: game (Game): The game instance. """ super().__init__("menu", 0, 0) self.game = game self.leaderboard = Leaderboard() self.components = OrderedUpdates() self.clear() self.show_main_screen() self.visible = True self.leaderboard_name = None def show(self): """ Shows the menu. """ self.visible = True self.show_main_screen() def hide(self): """ Hides the menu and enables in game icons. """ self.visible = False self.clear() self.defence_buttons = [MenuButton(self, "menu_defence_button", self.game.defence_prototypes[i].display_name, (i + 1) * 64, 0, lambda: self.game.select_defence((pygame.mouse.get_pos()[0] - 64) // 64)) for i in range(len(self.game.defence_prototypes))] self.components.add(self.defence_buttons) self.wave_label = MenuLabel(self, "menu_pause_button", "Wave", 448, 0) self.lives_label = MenuLabel(self, "menu_pause_button", "Lives", 576, 0) self.money_label = MenuLabel(self, "menu_pause_button", "Money", 704, 0) self.score_label = MenuLabel(self, "menu_pause_button", "Score", 832, 0) self.components.add(self.wave_label) self.components.add(self.lives_label) self.components.add(self.money_label) self.components.add(self.score_label) self.components.add(MenuButton(self, "menu_pause_button", "Menu", 1088, 0, self.show)) self.update() def clear(self): """ Removes all components from the menu. """ self.components.remove(self.components) self.component_next = self.top def update(self): """ Called each frame. """ if not self.visible: self.wave_label.set_text("Wave: " + str(self.game.wave.number)) self.lives_label.set_text("Lives: " + str(self.game.level.lives)) self.lives_label.highlighted = (self.game.level.lives < 5) self.money_label.set_text("Money: " + str(self.game.level.money)) self.score_label.set_text("Score: " + str(self.game.level.get_score())) for i in range(len(self.defence_buttons)): self.defence_buttons[i].disabled = (self.game.defence_prototypes[i].cost > self.game.level.money) self.defence_buttons[i].selected = (self.game.defence_type == i) self.components.update() def clicked(self): """ Called when a mouse button is pressed. """ for component in self.components: if isinstance(component, MenuButton): component.clicked() def key_pressed(self, key): """ Called when a key has been pressed. Args: key: The key that was pressed. """ if self.leaderboard_name is None: return keys = { pygame.K_a: "a", pygame.K_b: "b", pygame.K_c: "c", pygame.K_d: "d", pygame.K_e: "e", pygame.K_f: "f", pygame.K_g: "g", pygame.K_h: "h", pygame.K_i: "i", pygame.K_j: "j", pygame.K_k: "k", pygame.K_l: "l", pygame.K_m: "m", pygame.K_n: "n", pygame.K_o: "o", pygame.K_p: "p", pygame.K_q: "q", pygame.K_r: "r", pygame.K_s: "s", pygame.K_t: "t", pygame.K_u: "u", pygame.K_v: "v", pygame.K_w: "w", pygame.K_x: "x", pygame.K_y: "y", pygame.K_z: "z", pygame.K_0: "0", pygame.K_1: "1", pygame.K_2: "2", pygame.K_3: "3", pygame.K_4: "4", pygame.K_5: "5", pygame.K_6: "6", pygame.K_7: "7", pygame.K_8: "8", pygame.K_9: "9" } if key in keys.keys(): self.leaderboard_name.set_text(self.leaderboard_name.text + (keys[key].upper() if pygame.key.get_pressed()[pygame.K_LSHIFT] or pygame.key.get_pressed()[pygame.K_RSHIFT] else keys[key])) elif key is pygame.K_BACKSPACE and self.leaderboard_name.text != "": self.leaderboard_name.set_text(self.leaderboard_name.text[:-1]) def draw(self, screen): """ Draws the menu and its components. Args: screen (Surface): The surface that is blitted to. """ if self.visible: screen.blit(self.image, (0, 0)) self.components.draw(screen) def add_button(self, text, callback): """ Adds a standard button to the menu screen. Args: text (str): The text to display on the button. callback (callable): The callback when the button is clicked. Returns: (MenuButton): The button. """ button = MenuButton(self, "menu_button", text, 0, self.component_next, callback) button.rect.x = (self.rect.width - button.rect.width) / 2 self.components.add(button) self.component_next += button.rect.height self.component_next += button.padding return button def add_level_button(self, level): """ Adds a change level button to the menu screen. Args: level (str): The name of the level to display on the button. """ button = MenuButton(self, "menu_level_" + level, level, 0, self.component_next, lambda: self.game.load_level(level)) button.rect.x = (self.rect.width - button.rect.width) / 2 self.components.add(button) self.component_next += button.rect.height self.component_next += button.padding def show_main_screen(self): """ Shows the main menu screen. """ self.clear() if self.game.level.time > 0: self.add_button("Continue", self.hide) self.add_button("Restart Game", lambda: self.game.load_level(self.game.level.name)) else: self.add_button("Start Game", self.hide) self.add_button("How To Play", self.show_how_to_play_screen) self.add_button("Change Level", self.show_change_level_screen) self.add_button("Leaderboard", self.show_leaderboard_screen) self.add_button("Quit Game", self.game.quit) def show_how_to_play_screen(self): """ Shows the how to play menu screen. """ self.clear() self.add_button("Back", self.show_main_screen) instructions = Prefab("menu_how_to_play", 0, self.component_next) self.components.add(instructions) instructions.rect.x = (self.rect.width - instructions.rect.width) / 2 def show_change_level_screen(self): """ Shows the change level screen. """ self.clear() self.add_button("Back", self.show_main_screen) if self.game.level.name != "basic": self.add_level_button("basic") if self.game.level.name != "path": self.add_level_button("path") if self.game.level.name != "maze": self.add_level_button("maze") def show_leaderboard_screen(self): """ Shows the leaderboard screen. """ self.leaderboard.retrieve() self.clear() self.add_button("Back", self.show_main_screen) if self.leaderboard.entries is None: self.add_button("Error Loading Leaderboard", None) else: for i in range(0, 4 if len(self.leaderboard.entries) > 6 else len(self.leaderboard.entries)): entry = self.leaderboard.entries[i] self.add_button(entry.name + " Lvl=" + entry.level + " Scr=" + str(entry.score) + " Wve=" + str(entry.wave), None) def show_lose_screen(self): """ Shows the game over screen. """ self.show() self.clear() self.add_button("Game Over", None) self.add_button("You Reached Wave " + str(self.game.wave.number), None) self.add_button(str(self.game.level.get_score()) + " Points", None) self.add_button("Restart Game", lambda: self.game.load_level(self.game.level.name)) self.add_button("Add To Leaderboard", self.show_add_to_leaderboard_screen) def show_add_to_leaderboard_screen(self): """ Shows the add to leaderboard screen. """ self.clear() self.add_button("Type Your Name", None) self.leaderboard_name = self.add_button("", None) self.add_button("Submit", self.submit_leaderboard) def submit_leaderboard(self): """ Attempts to submit a score to the leaderboard. """ if self.leaderboard_name.text != "": self.leaderboard.add(self.game.level.name, self.leaderboard_name.text, self.game.level.get_score(), self.game.wave.number) self.game.load_level(self.game.level.name) self.show_leaderboard_screen()
class Super_Saast_Bros(Microgame): def __init__(self): Microgame.__init__(self) self.character_select = True self.player1_selector = Player1_Selector() self.player2_selector = Player2_Selector() self.falco = Falco_Selector() self.fox = Fox_Selector() self.samus = Samus_Selector() self.snake = Snake_Selector() self.pit = Pit_Selector() self.mewtwo = Mewtwo_Selector() self.zelda = Zelda_Selector() self.character_possibilities = Group(self.falco, self.fox, self.samus, self.snake, self.pit, self.mewtwo, self.zelda) self.sprites = OrderedUpdates(self.falco, self.fox, self.samus, self.snake, self.pit, self.mewtwo, self.zelda, self.player1_selector, self.player2_selector) self.player1 = Fox() self.player2 = Falco() self.p1victory = pygame.image.load(join('games', 'ssb', 'p1_win.png')) self.p2victory = pygame.image.load(join('games', 'ssb', 'p2_win.png')) self.platform = Final_Destination() self.p1win = False self.p2win = False self.wincondition = False self.a_track = False self.d_track = False self.lshift_track = False self.quote_track = False self.l_track = False self.rshift_track = False self.playergroup = Group(self.player1, self.player2) self.player1_projectiles = Group() self.player2_projectiles = Group() def start(self): pygame.mixer.music.load(join('games', 'ssb', 'battlefield_melee.ogg')) pygame.mixer.music.play() def stop(self): pygame.mixer.music.stop() def update(self, events): if self.wincondition == False: if self.character_select == True: player_1_character = self.detect_character(self.player1_selector, self.character_possibilities) player_2_character = self.detect_character(self.player2_selector, self.character_possibilities) if player_1_character == self.falco: self.player1 = Falco() elif player_1_character == self.fox: self.player1 = Fox() elif player_1_character == self.samus: self.player1 = Samus() elif player_1_character == self.snake: self.player1 = Snake() elif player_1_character == self.pit: self.player1 = Pit() elif player_1_character == self.mewtwo: self.player1 = Mewtwo() elif player_1_character == self.zelda: self.player1 = Zelda() if player_2_character == self.falco: self.player2 = Falco() elif player_2_character == self.fox: self.player2 = Fox() elif player_2_character == self.samus: self.player2 = Samus() elif player_2_character == self.snake: self.player2 = Snake() elif player_2_character == self.pit: self.player2 = Pit() elif player_2_character == self.mewtwo: self.player2 = Mewtwo() elif player_2_character == self.zelda: self.player2 = Zelda() for event in events: self.player1_selector_logic(event, self.player1_selector) self.player2_selector_logic(event, self.player2_selector) self.character_select_logic(event) self.sprites.update() if self.character_select == False: self.detect_hit(self.player2, self.player1_projectiles) self.detect_hit(self.player1, self.player2_projectiles) self.remove_projectiles(self.player1_projectiles, self.sprites) self.remove_projectiles(self.player2_projectiles, self.sprites) self.sprites.update() self.detect_floor() for event in events: self.char_logic1(event, self.player1) self.char_logic2(event, self.player2) self.player1.projectile_logic() self.player2.projectile_logic() y1 = self.player1.rect.top y2 = self.player2.rect.top if (y1 > 1200) or (y2 > 1200): if y1 > 1200: self.p2win = True if y2 > 1200: self.p1win = True self.sprites.remove(self.platform, self.player1, self.player2) self.wincondition = True self.win_time = pygame.time.get_ticks() elif self.wincondition == True: if pygame.time.get_ticks() - self.win_time >= 3000: self.win() def player1_selector_logic(self, event, player_selector): if event.type == KEYDOWN: if event.key == K_w: player_selector.uvelocity -= 15 elif event.key == K_d: player_selector.rvelocity += 15 elif event.key == K_s: player_selector.dvelocity += 15 elif event.key == K_a: player_selector.lvelocity -= 15 if event.type == KEYUP: if event.key == K_w: player_selector.uvelocity += 15 elif event.key == K_d: player_selector.rvelocity -= 15 elif event.key == K_s: player_selector.dvelocity -= 15 elif event.key == K_a: player_selector.lvelocity += 15 def player2_selector_logic(self, event, player_selector): if event.type == KEYDOWN: if event.key == K_p: player_selector.uvelocity -= 15 elif event.key == K_QUOTE: player_selector.rvelocity += 15 elif event.key == K_SEMICOLON: player_selector.dvelocity += 15 elif event.key == K_l: player_selector.lvelocity -= 15 if event.type == KEYUP: if event.key == K_p: player_selector.uvelocity += 15 elif event.key == K_QUOTE: player_selector.rvelocity -= 15 elif event.key == K_SEMICOLON: player_selector.dvelocity -= 15 elif event.key == K_l: player_selector.lvelocity += 15 def detect_character(self, player_selector, character_possibilities): for character in pygame.sprite.spritecollide(player_selector, character_possibilities, False): return character def character_select_logic(self, event): if event.type == KEYDOWN: if event.key == K_t: self.character_select = False self.player1.direct = 'right' self.player2.direct = 'left' self.player1.image, self.player1.rect = _load_image(self.player1.idle[0], 250, 350) self.player2.image, self.player2.rect = _load_image(self.player2.idle[1], 650, 350) self.sprites.remove(self.falco, self.fox, self.samus, self.snake, self.pit, self.mewtwo, self.zelda, self.player1_selector, self.player2_selector) self.sprites.add(self.platform, self.player1, self.player2) self.playergroup.add(self.player1, self.player2) def detect_floor(self): for char in pygame.sprite.spritecollide(self.platform, self.playergroup, False): char.rect.y = (self.platform.rect.topleft[1] - char.rect.height) char.uvelocity = 0 char.jumps = 2 def detect_hit(self, receiver, hitboxes): for hitbox in pygame.sprite.spritecollide(receiver, hitboxes, True): receiver.percent += hitbox.damage receiver.knockback_calc(hitbox) def remove_projectiles(self, projectilegroup, spritegroup): for projectile in projectilegroup: x_left, _ = projectile.rect.topleft x_right, _ = projectile.rect.bottomright if (x_left <= 0) or (x_right >= locals.WIDTH): spritegroup.remove(projectile) def render(self, surface): surface.fill(Color(0, 0, 0)) self.sprites.draw(surface) if self.p1win == True: surface.blit(self.p1victory, (0, 0)) elif self.p2win == True: surface.blit(self.p2victory, (0, 0)) def get_timelimit(self): return 60 def char_logic1(self, event, charac): x, y = charac.rect.topleft if event.type == KEYDOWN: if event.key == K_w: self.jump_logic(charac) elif event.key == K_a: self.a_track = True charac.image, charac.rect = _load_image(charac.run[1], x, y) charac.lvelocity -= 10 charac.direct = 'left' elif event.key == K_d: self.d_track = True charac.rvelocity += 10 charac.image, charac.rect = _load_image(charac.run[0], x, y) charac.direct = 'right' elif event.key == K_LSHIFT: if charac.projectilecount > 0: charac.projectilesound.play() self.lshift_track = True if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y) x, y = charac.rect.topright charac.make_projectile(x, y, charac) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y) x, y = charac.rect.topleft charac.make_projectile(x, y, charac) self.sprites.add(charac.projectile) self.player1_projectiles.add(charac.projectile) charac.projectilecount -= 1 if charac.projectilecount == 0: charac.no_projectile_time = pygame.time.get_ticks() elif event.type == KEYUP: if event.key == K_a: charac.lvelocity += 10 self.a_track = False if self.d_track == False: if self.lshift_track == False: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.idle[0], x, y - 10) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.idle[1], x, y - 10) elif self.lshift_track == True: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y - 5) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y - 5) elif self.d_track == True: if self.lshift_track == False: charac.image, charac.rect = _load_image(charac.run[0], x, y) charac.direct = 'right' elif self.lshift_track == True: charac.image, charac.rect = _load_image(charac.gun[0], x, y - 5) elif event.key == K_d: charac.rvelocity -= 10 self.d_track = False if self.a_track == False: if self.lshift_track == False: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.idle[0], x, y - 10) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.idle[1], x, y - 10) elif self.lshift_track == True: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y - 5) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y - 5) elif self.a_track == True: if self.lshift_track == False: charac.image, charac.rect = _load_image(charac.run[1], x, y) charac.direct = 'left' elif self.lshift_track == True: charac.image, charac.rect = _load_image(charac.gun[1], x, y - 5) elif event.key == K_LSHIFT: self.lshift_track = False if (self.a_track == False) and (self.d_track == False): if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.idle[0], x, y - 10) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.idle[1], x, y - 10) elif (self.a_track == False) and (self.d_track == True): charac.image, charac.rect = _load_image(charac.run[0], x, y) charac.direct = 'right' elif (self.a_track == True) and (self.d_track == False): charac.image, charac.rect = _load_image(charac.run[1], x, y) charac.direct = 'left' elif (self.a_track == True) and (self.d_track == False): if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y) def char_logic2(self, event, charac): x, y = charac.rect.topleft if event.type == KEYDOWN: if event.key == K_p: self.jump_logic(charac) elif event.key == K_l: self.l_track = True charac.image, charac.rect = _load_image(charac.run[1], x, y) charac.lvelocity -= 10 charac.direct = 'left' elif event.key == K_QUOTE: self.quote_track = True charac.rvelocity += 10 charac.image, charac.rect = _load_image(charac.run[0], x, y) charac.direct = 'right' elif event.key == K_RSHIFT: if charac.projectilecount > 0: charac.projectilesound.play() self.rshift_track = True if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y) x, y = charac.rect.topright charac.make_projectile(x, y, charac) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y) x, y = charac.rect.topleft charac.make_projectile(x, y, charac) self.sprites.add(charac.projectile) self.player2_projectiles.add(charac.projectile) charac.projectilecount -= 1 if charac.projectilecount == 0: charac.no_projectile_time = pygame.time.get_ticks() elif event.type == KEYUP: if event.key == K_l: charac.lvelocity += 10 self.l_track = False if self.quote_track == False: if self.rshift_track == False: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.idle[0], x, y - 10) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.idle[1], x, y - 10) elif self.rshift_track == True: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y - 5) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y - 5) elif self.quote_track == True: if self.rshift_track == False: charac.image, charac.rect = _load_image(charac.run[0], x, y) charac.direct = 'right' elif self.rshift_track == True: charac.image, charac.rect = _load_image(charac.gun[0], x, y - 5) elif event.key == K_QUOTE: charac.rvelocity -= 10 self.quote_track = False if self.l_track == False: if self.rshift_track == False: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.idle[0], x, y - 10) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.idle[1], x, y - 10) elif self.rshift_track == True: if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y - 5) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y - 5) elif self.l_track == True: if self.rshift_track == False: charac.image, charac.rect = _load_image(charac.run[1], x, y) charac.direct = 'left' elif self.rshift_track == True: charac.image, charac.rect = _load_image(charac.gun[1], x, y - 5) elif event.key == K_RSHIFT: self.rshift_track = False if (self.l_track == False) and (self.quote_track == False): if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.idle[0], x, y - 10) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.idle[1], x, y - 10) elif (self.l_track == False) and (self.quote_track == True): charac.image, charac.rect = _load_image(charac.run[0], x, y) charac.direct = 'right' elif (self.l_track == True) and (self.quote_track == False): charac.image, charac.rect = _load_image(charac.run[1], x, y) charac.direct = 'left' elif (self.l_track == True) and (self.quote_track == False): if charac.direct == 'right': charac.image, charac.rect = _load_image(charac.gun[0], x, y) elif charac.direct == 'left': charac.image, charac.rect = _load_image(charac.gun[1], x, y) def jump_logic(self, ch): if ch.jumps == 2: ch.uvelocity = JUMP_BOOST ch.jumpsounds[randint(0, len(ch.jumpsounds) - 1)].play() elif ch.jumps == 1: ch.uvelocity = AIR_JUMP_BOOST ch.jumpsounds[randint(0, len(ch.jumpsounds) - 1)].play() ch.jumps -= 1
class Room: """ base class for all rooms """ # these variables are set in the subclass width = None height = None def __init__(self, screen): """ Initialization of our lists """ self.wall_list = Group() self.coin_list = Group() self.plat_move = Group() self.door_list = OrderedUpdates() # the screen and its rect self.screen = screen self.screct = self.screen.get_rect() # the room's size and image self.rect = Rect(0, 0, self.width, self.height) self.bckimg = gf.get_checkboard(20, 20, self.width, self.height, [WHITE, GREY]) #self.bckimg.fill(WHITE) def draw(self): """ Draw the room on the screen""" self.screen.blit(self.bckimg, self.rect) self.wall_list.draw(self.screen) self.door_list.draw(self.screen) self.coin_list.draw(self.screen) def move_room(self, move_x, move_y): """ Move all walls in the room """ self.move_obj(move_x, move_y) self.rect.x += move_x self.rect.y += move_y def move_obj(self, move_x, move_y): """ Move all objects in the room, but not the room itself """ self.wall_list.update(move_x, move_y) self.door_list.update(move_x, move_y) self.coin_list.update(move_x, move_y) def reset(self, player=None): """ reset the room rect's topleft to the topleft of the screen and move everything accordingly. """ displace_x = self.screct.x - self.rect.x displace_y = self.screct.y - self.rect.y self.move_room(displace_x, displace_y) if player: player.rect.x += displace_x player.rect.y += displace_y def old_set_around(self, player): """ Move the room so that the room and the player are in the right place""" plax = player.rect.x play = player.rect.y roox = self.rect.x rooy = self.rect.y # check horizontal position if player.rect.centerx < self.rect.left + self.screct.centerx: self.rect.left = self.screct.left dis_x = self.rect.x - roox self.move_obj(dis_x, 0) player.rect.x += dis_x elif player.rect.centerx > self.rect.right - self.screct.centerx: self.rect.right = self.screct.right dis_x = self.rect.x - roox self.move_obj(dis_x, 0) player.rect.x += dis_x else: player.rect.centerx = self.screct.centerx dis_x = player.rect.x - plax self.move_room(dis_x, 0) # check vertical position if player.rect.centery < self.rect.top + self.screct.centery: self.rect.top = self.screct.top dis_y = self.rect.y - rooy self.move_obj(0, dis_y) player.rect.y += dis_y elif player.rect.centery > self.rect.bottom - self.screct.centery: self.rect.bottom = self.screct.bottom dis_y = self.rect.y - rooy self.move_obj(0, dis_y) player.rect.y += dis_y else: player.rect.centery = self.screct.centery dis_y = player.rect.y - play self.move_room(0, dis_y) def reset_coins(self): """ reset all coins """ # improve this so it doesn't have to keep removing sprites already in the group self.coin_list.empty() self.get_coins() def get_coins(self): """ get the coins for the room, overloaded in subclasses """ pass