def main(): """ Main Program """ # Call this function so the Pygame library can initialize itself pygame.init() # Create a 500x500 sized screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE) pygame.display.set_caption("my-py-game") # Create the player paddle object player = Player(64, 64) # List of moving sprites moving_sprites = pygame.sprite.Group() moving_sprites.add(player) # List of all sprites all_sprites_list = pygame.sprite.Group() # List of all bullets bullet_list = pygame.sprite.Group() level_changed = True current_level_x = 0 current_level_y = 0 font = pygame.font.Font(None, 30) clock = pygame.time.Clock() done = False while not done: # --- Event Processing --- for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # if event.type == pygame.VIDEORESIZE: # old_screen_saved = screen # screen = pygame.display.set_mode((event.w, event.h), # pygame.RESIZABLE) # # On the next line, if only part of the window # # needs to be copied, there's some other options. # screen.blit(old_screen_saved, (0, 0)) # del old_screen_saved if event.type == pygame.MOUSEBUTTONDOWN: # Fire a bullet if the user clicks the mouse button # Get the mouse position pos = pygame.mouse.get_pos() mouse_x = pos[0] mouse_y = pos[1] # Create the bullet based on where we are, and where we want to go. bullet = Bullet(player.rect.centerx, player.rect.centery, mouse_x, mouse_y) # Add the bullet to the lists all_sprites_list.add(bullet) bullet_list.add(bullet) moving_sprites.add(bullet) if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: player.changespeed(-PLAYER_SPEED, 0) player.set_direction('left') if event.key == pygame.K_d: player.changespeed(PLAYER_SPEED, 0) player.set_direction('right') if event.key == pygame.K_w: player.changespeed(0, -PLAYER_SPEED) if event.key == pygame.K_s: player.changespeed(0, PLAYER_SPEED) if event.type == pygame.KEYUP: if event.key == pygame.K_a: player.changespeed(PLAYER_SPEED, 0) if event.key == pygame.K_d: player.changespeed(-PLAYER_SPEED, 0) if event.key == pygame.K_w: player.changespeed(0, PLAYER_SPEED) if event.key == pygame.K_s: player.changespeed(0, -PLAYER_SPEED) # --- Game Logic --- if level_changed: if current_level_x == 0 and current_level_y == 0: # print('Entering level 1') current_level = Level(1) else: # print('Entering level 2') current_level = Level(2) current_level.draw_bg_tiles(screen) current_level.draw_walls(screen) pygame.display.flip() level_changed = False # Dirty rects are all the rects that need to be updated at the end of this frame dirty_rects = [] # get tiles intersecting player bg_to_draw = [] for bg_tile in current_level.bg_tiles: if bg_tile.rect.colliderect(player.rect): bg_to_draw.append(bg_tile) for bullet in bullet_list: if bg_tile.rect.colliderect(bullet.rect): bg_to_draw.append(bg_tile) # add those tiles to dirty rects dirty_rects.extend([bg_tile.rect for bg_tile in bg_to_draw]) dirty_rects.append(player.rect) player.move(current_level.wall_tiles) # Changing between levels if player.rect.y < -TILE_WIDTH / 2 and current_level_y == 0: level_changed = True current_level_y += 1 player.rect.y = SCREEN_HEIGHT - TILE_WIDTH / 2 - 1 if player.rect.y > SCREEN_HEIGHT - TILE_WIDTH / 2 and current_level_y > 0: level_changed = True current_level_y -= 1 player.rect.y = -TILE_WIDTH / 2 + 1 for bullet in bullet_list: bullet.update(current_level.wall_tiles) # --- Drawing --- # draw walls and player for bg_tile in bg_to_draw: bg_tile.draw_to_screen(screen) for bullet in bullet_list: screen.blit(bullet.image, bullet.rect) screen.blit(player.image, player.rect) # print(dirty_rects) if SHOW_FPS: fps = font.render(str(int(clock.get_fps())), True, pygame.Color('white')) fps_bg_image = pygame.Surface([32, 32]) fps_bg_image.fill(BLACK) screen.blit(fps_bg_image, (0, 0)) screen.blit(fps, (0, 0)) dirty_rects.append(fps_bg_image.get_rect()) pygame.display.update(dirty_rects) clock.tick(60) pygame.quit()
def startGame(): all_sprites_list = pygame.sprite.RenderPlain() block_list = pygame.sprite.RenderPlain() monsta_list = pygame.sprite.RenderPlain() pacman_collide = pygame.sprite.RenderPlain() wall_list = setupRoomOne(all_sprites_list) gate = setupGate(all_sprites_list) pinky_turn = 0 pinky_steps = 0 blinky_turn = 0 blinky_steps = 0 inky_turn = 0 inky_steps = 0 clyde_turn = 0 clyde_steps = 0 Pacman = Player(w, pacman_height, "images/pacman.png") all_sprites_list.add(Pacman) pacman_collide.add(Pacman) Blinky = Ghost(w, blinky_height, "images/Blinky.png") monsta_list.add(Blinky) all_sprites_list.add(Blinky) Pinky = Ghost(w, monster_height, "images/Pinky.png") monsta_list.add(Pinky) all_sprites_list.add(Pinky) Inky = Ghost(inky_width, monster_height, "images/Inky.png") monsta_list.add(Inky) all_sprites_list.add(Inky) Clyde = Ghost(clyde_width, monster_height, "images/Clyde.png") monsta_list.add(Clyde) all_sprites_list.add(Clyde) # Draw the grid for row in range(19): for column in range(19): if (row == 7 or row == 8) and (column == 8 or column == 9 or column == 10): continue else: block = Block(yellow, 4, 4) # Set a random location for the block block.rect.x = (30 * column + 6) + 26 block.rect.y = (30 * row + 6) + 26 b_collide = pygame.sprite.spritecollide( block, wall_list, False) p_collide = pygame.sprite.spritecollide( block, pacman_collide, False) if b_collide: continue elif p_collide: continue else: # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block) bll = len(block_list) score = 0 done = False i = 0 while done == False: # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT or event.key == ord('a'): Pacman.changespeed(-30, 0) if event.key == pygame.K_RIGHT or event.key == ord('d'): Pacman.changespeed(30, 0) if event.key == pygame.K_UP or event.key == ord('w'): Pacman.changespeed(0, -30) if event.key == pygame.K_DOWN or event.key == ord('s'): Pacman.changespeed(0, 30) if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == ord('a'): Pacman.changespeed(30, 0) if event.key == pygame.K_RIGHT or event.key == ord('d'): Pacman.changespeed(-30, 0) if event.key == pygame.K_UP or event.key == ord('w'): Pacman.changespeed(0, 30) if event.key == pygame.K_DOWN or event.key == ord('s'): Pacman.changespeed(0, -30) # ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT Pacman.update(wall_list, gate) returned = Pinky.changespeed(Pinky_directions, False, pinky_turn, pinky_steps, pinky_movements_list) pinky_turn = returned[0] pinky_steps = returned[1] Pinky.changespeed(Pinky_directions, False, pinky_turn, pinky_steps, pinky_movements_list) Pinky.update(wall_list, False) returned = Blinky.changespeed(Blinky_directions, False, blinky_turn, blinky_steps, blinky_movements_list) blinky_turn = returned[0] blinky_steps = returned[1] Blinky.changespeed(Blinky_directions, False, blinky_turn, blinky_steps, blinky_movements_list) Blinky.update(wall_list, False) returned = Inky.changespeed(Inky_directions, False, inky_turn, inky_steps, inky_movements_list) inky_turn = returned[0] inky_steps = returned[1] Inky.changespeed(Inky_directions, False, inky_turn, inky_steps, inky_movements_list) Inky.update(wall_list, False) returned = Clyde.changespeed(Clyde_directions, "clyde", clyde_turn, clyde_steps, clyde_movements_list) clyde_turn = returned[0] clyde_steps = returned[1] Clyde.changespeed(Clyde_directions, "clyde", clyde_turn, clyde_steps, clyde_movements_list) Clyde.update(wall_list, False) # See if the Pacman block has collided with anything. blocks_hit_list = pygame.sprite.spritecollide(Pacman, block_list, True) # Check the list of collisions. if len(blocks_hit_list) > 0: score += len(blocks_hit_list) # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT screen.fill(black) wall_list.draw(screen) gate.draw(screen) all_sprites_list.draw(screen) monsta_list.draw(screen) text = font.render("Score: " + str(score) + "/" + str(bll), True, red) screen.blit(text, [10, 10]) if score == bll: doNext("Congratulations, you won!", 145, all_sprites_list, block_list, monsta_list, pacman_collide, wall_list, gate) monsta_hit_list = pygame.sprite.spritecollide(Pacman, monsta_list, False) if monsta_hit_list: doNext("Game Over", 235, all_sprites_list, block_list, monsta_list, pacman_collide, wall_list, gate) pygame.display.flip() clock.tick(10)