def start(): # initiating pygame pygame.init() # making instance for game settings gs = Settings() # passing argument defined as sequence of tuple screen = pygame.display.set_mode(gs.size) # passing caption for top of GUI pygame.display.set_caption(gs.caption) # making instance of ship ship = Ship(screen,gs) # Making instance of bullet bullets = Group() # Initiating main loop for continuous update while True: # for cehecking key/mouse inputs # calling function from game_function module gf.chk_events(gs,screen,ship,bullets) # updating the bullets bullets.update() # Updating the screen gf.update(gs,screen,ship,bullets)
def run_game(): # Initialize pygame, settings and create a screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make the Play button. play_button = Button(ai_settings, screen, "Play") # Create an instance to store game statistics and create scoreboard. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Make a ship, a group of bullets, and a group of aliens. ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() # Create the fleet of aliens. gf.create_fleet(ai_settings, screen, ship, aliens) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: gf.update(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.render_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def main(): pygame.init() ss_settings = Settings() screen = pygame.display.set_mode(ss_settings.screen_size()) ss_rocket = Rocket(screen) bullets = Group() pygame.display.set_caption(ss_settings.caption) while True: gf.check_event(ss_settings, screen, ss_rocket, bullets) gf.update(ss_settings, screen, ss_rocket, bullets)
def run_game(): #初始化 pygame.init() ai_settings = settings() #创建屏幕对象 screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion') #创建一艘飞船 ship_new = ship(screen) #开始主循环 while True: gf.check_events(ship_new) gf.update(ai_settings, screen, ship_new) ship_new.update()
def main(): pg.init() settings = Settings() game_stats = GameStats() screen = pg.display.set_mode(settings.screen_size) title = 'Snake | Max score: ' + str(game_stats.max_score) + ' | Score: ' +\ str(game_stats.score) pg.display.set_caption(title) fruit = Fruit(settings, screen) snake = Snake(settings, screen) text = Text(screen, settings, 'GAME OVER') button = RestartButton(screen, settings) menu = Menu(screen) fps_controller = pg.time.Clock() while 1: if not settings.menu_passed: gf.start_game(screen, menu) gf.is_menu_passed(settings, menu) else: if not settings.game_over: gf.check_events(snake) gf.update(fruit, snake, game_stats, fps_controller, screen, settings) if not settings.fruit_on_screen: gf.update_caption(game_stats) gf.check_game_over(snake, settings) else: game_stats.update_max_score() text.draw() button.blit() pg.display.flip() if gf.is_button_pressed(button, settings): gf.restart_game(game_stats, snake, fruit, settings, screen) snake = Snake(settings, screen) fruit = Fruit(settings, screen)
def run_game(): #initializing game pygame.init() #creating an object so as to access variables values in class settings sn_settings = Settings() #drawing screen screen = pygame.display.set_mode( (sn_settings.screen_width, sn_settings.screen_height)) #creating an object snake so as to access variables values in class Snake snake = Snake(screen, sn_settings) #a list to store values of snake body to track it's movement snake_list = [] ##creating an object food so as to access variables values in class Food food = Food(screen, sn_settings) #adding screen caption pygame.display.set_caption("kings snake game") #to keep track of time while playing the game clock = pygame.time.Clock() # Make the Play button. play_button = Button(sn_settings, screen, "Play") #game statistics game_stats = GameStats(sn_settings) #game scores sb = Scoreboard(sn_settings, screen, game_stats) while True: #start main loop for the game if game_stats.game_active == True: #calling update in Snake class to automate snake movement snake.update(sn_settings) #watch for keyboard / mouse input Gf.check_event(screen, sn_settings, snake, game_stats, snake_list, play_button, sb) #displaying screen objects Gf.update(screen, sn_settings, snake_list, snake, food, play_button, game_stats, sb) #rate at which the screen is update clock.tick(sn_settings.snake_speed)
def runGame(): """initialize game, loop through gameplay functins until quit Modifies -------- screen create screen for sprites to be drawn on, add caption units create list of formations of units """ # init game, screen, settings cprof = cProfile.Profile() cprof.enable() screen = SCREEN pygame.display.set_caption("Musketcraft") flags = [] infantry = pd.read_csv('levels/BorodinoInfantry.csv') cannon = pd.read_csv('levels/BorodinoCannon.csv') cavalry = pd.read_csv('levels/BorodinoCavalry.csv') units = [] for row in cannon.itertuples(False): units.append(Battery(screen, row.angle, row.x, row.y, row.size, row.team, flags, row.strength, row.play, row.defend)) for row in infantry.itertuples(False): units.append(Company(screen, row.angle, row.x, row.y, row.sizex, row.sizey, row.team, flags, row.strength, row.play, row.defend)) for row in cavalry.itertuples(False): units.append(Squadron(screen, row.angle, row.x, row.y, row.sizex, row.sizey, row.team, flags, row.strength, row.play, row.defend)) # spawn = [Company(screen, 0, 100, 100, 3, 5, "green", flags, False, False)] # events = [SpawnEvent(600, 400, "blue", 1, 100, spawn)] events = [] [company.unitInit(units) for company in units] color = "blue" # main loop while True: color, units = check_events(color, events, units, screen, flags, cprof) test = update(screen, units, flags) if(test == "restart"): break
def run_game(): game_surface = gf.game_init() buttons = [] balls = [] sliders = [] lines = [] fixed_objects = [] free_objects = [] # 定义Button对象 button_size = (80, 60) button_pos = (gs.SIZE[0] - button_size[0] / 2, gs.SIZE[1] / 2 - button_size[1]) button_color = (255, 140, 0) text = "添加" font_size = 30 text_color = (255, 255, 255) button_add = Button(game_surface, button_size, button_color, button_pos, text, font_size, text_color) buttons.append(button_add) text = "启动" button_pos = (gs.SIZE[0] - button_size[0] / 2, gs.SIZE[1] / 2 + button_size[1]) button_control = Button(game_surface, button_size, button_color, button_pos, text, font_size, text_color) buttons.append(button_control) text = "删除" button_color = pg.Color('red') button_pos = (gs.SIZE[0] - button_size[0] / 2, gs.SIZE[1] / 2) button_undo = Button(game_surface, button_size, button_color, button_pos, text, font_size, text_color) buttons.append(button_undo) # 定义Slider对象 text = "力的大小" max_value = 100 value = max_value / 2 slider_color = Color('red') font_size = 18 rect_1 = Rect(0, 0, 20, 100) rect_1.center = button_pos[0], (gs.SIZE[1] / 6) slider_1 = slider.Slider(game_surface, rect_1, slider_color, text, text_color, font_size, max_value, value, slider.VERTICAL) sliders.append(slider_1) text = "力的方向" max_value = 360 value = max_value / 2 rect_2 = Rect(0, 0, 20, 100) rect_2.center = button_pos[0], (gs.SIZE[1] * 5 / 6) slider_2 = slider.Slider(game_surface, rect_2, slider_color, text, text_color, font_size, max_value, value, slider.VERTICAL) sliders.append(slider_2) # 定义Line对象 width = 5 pos_x = gs.SIZE[0] - buttons[0].button_size[0] start_pos = (pos_x, 0) end_pos = (pos_x, gs.SIZE[1]) line_1 = line.Line(game_surface, gs.LINE_COLOR, start_pos, end_pos, width) lines.append(line_1) width = 5 pos_x = gs.SIZE[0] - gs.MENU_SIZE[0] - width / 2 start_pos = (pos_x, 0) end_pos = (pos_x, gs.SIZE[1]) line_2 = line.Line(game_surface, gs.LINE_COLOR, start_pos, end_pos, width) lines.append(line_2) width = 5 pos_y = gs.SIZE[1] / 2 start_pos = (line_1.start_pos[0], pos_y) end_pos = (line_2.start_pos[0], pos_y) line_3 = line.Line(game_surface, gs.LINE_COLOR, start_pos, end_pos, width) lines.append(line_3) # 定义固定物体 width = 10 height = gs.SIZE[1] rect0 = Rect(0, 0, width, height) color = Color('white') points = [ rect0.bottomleft, rect0.bottomright, rect0.topright, rect0.topleft ] fixed_object0 = polygon.Polygon(game_surface, points, color, 0.5, 10, False) fixed_objects.append(fixed_object0) rect_left = gs.SIZE[0] - gs.MENU_SIZE[0] - 5 rect0 = Rect(rect_left, 0, width, height) points = [ rect0.bottomleft, rect0.bottomright, rect0.topright, rect0.topleft ] fixed_object0 = polygon.Polygon(game_surface, points, color, 0.5, 10, False) fixed_objects.append(fixed_object0) height = width width = rect_left rect0 = Rect(0, 0, width, height) points = [ rect0.bottomleft, rect0.bottomright, rect0.topright, rect0.topleft ] fixed_object0 = polygon.Polygon(game_surface, points, color, 0.5, 10, False) fixed_objects.append(fixed_object0) height = gs.SIZE[1] / 8 rect = Rect(0, gs.SIZE[1] - height, gs.SIZE[0] - gs.MENU_SIZE[0], height) points = [rect.bottomleft, rect.bottomright, rect.topright, rect.topleft] color = Color('white') cof = 0.5 m = 50 fixed_object_1 = polygon.Polygon(game_surface, points, color, cof, m, False) fixed_objects.append(fixed_object_1) height = height / 2 rect = Rect(0, gs.SIZE[1] / 2 - height, (gs.SIZE[0] - gs.MENU_SIZE[0]) / 2, height) points = [rect.bottomleft, rect.bottomright, rect.topright, rect.topleft] color = Color('white') cof = 0.5 m = 50 fixed_object_1 = polygon.Polygon(game_surface, points, color, cof, m, False) fixed_objects.append(fixed_object_1) # 定义自由物体 width = gs.SIZE[0] / 5 height = gs.SIZE[1] / 8 left = width top = rect.top - height rect = Rect(left, top, width, height) color = Color('black') cof = 0.5 m = 100 # free_object_1 = rectangle.Rectangle( # game_surface, rect, color, cof, G, True) points = [rect.bottomleft, rect.bottomright, rect.topright, rect.topleft] free_object_1 = polygon.Polygon(game_surface, points, color, cof, m, True) free_objects.append(free_object_1) pos = ((gs.SIZE[0] / 2) + 80, (gs.SIZE[1] / 2) + 40) v = (0, 0) m = 5 color = pg.Color('black') free_object_2 = ball.Ball(game_surface, pos, m, v, color, True) free_objects.append(free_object_2) color = pg.Color('white') rect = Rect(0, gs.SIZE[1] - height, gs.SIZE[0] - gs.MENU_SIZE[0], height) pos = gs.SIZE[0] - gs.MENU_SIZE[0], gs.SIZE[1] - height points = [(pos[0] - 250, pos[1]), (pos[0], pos[1]), (pos[0], pos[1] - height * 3)] fixed_object_2 = polygon.Polygon(game_surface, points, color, cof, True) fixed_objects.append(fixed_object_2) game_active = False while True: gf.update(game_surface, game_active, balls, buttons, sliders, lines, fixed_objects, free_objects) game_active = gf.check_event(game_surface, game_active, balls, buttons, sliders, free_objects)
def run(): # Initialization pygame.init() settings = Settings() state = Game_State() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height)) pygame.display.set_caption("Super Mario Bros") bg_color = settings.bg_color stats = Game_Stats(screen, settings) clock = pygame.time.Clock() mario = Mario(screen, settings, stats) pygame.mixer.music.load("Sounds/overworld.mp3") pygame.mixer.music.play(-1) # Groups map_group = Group() block_group = Group() floor_group = Group() pipe_group = Group() enemy_group = Group() powerup_group = Group() fireball_group = Group() dead_group = Group() map.generate_map(screen, settings, map_group, floor_group, pipe_group, block_group, enemy_group) f = Flag(screen, settings, 198 * settings.block_width, 13 * settings.block_height) f.add(map_group) pipesprites = pipe_group.sprites() timer = 0 death_counter = 0 # Game Loop while state.running: settings.reset_holders() clock.tick(settings.fps) # handle mario death if mario.is_dead and death_counter <= 240: # draw black screen if death_counter == 60: screen.fill(settings.bg_color) stats.lives -= 1 if stats.lives < 0: stats.init_base_values() #display game over game_over_label = Text(None, settings.TEXT_SIZE, "Game Over", settings.WHITE, 0, 0) game_over_label.rect.center = (settings.screen_width / 2, settings.screen_height / 2) game_over_label.draw(screen) else: # display level lvl_str = "World " + str(stats.world) + "-" + str( stats.level_num) level_label = Text(None, settings.TEXT_SIZE, lvl_str, settings.WHITE, 0, 0) level_label.rect.center = (settings.screen_width / 2, settings.screen_height / 2) level_label.draw(screen) pygame.display.flip() death_counter += 1 print(death_counter) elif mario.is_dead and death_counter > 240: # reset death_counter = 0 mario.kill() mario = Mario(screen, settings, stats) map_group = Group() block_group = Group() floor_group = Group() pipe_group = Group() enemy_group = Group() powerup_group = Group() fireball_group = Group() dead_group = Group() pygame.mixer.music.play(-1) map.generate_map(screen, settings, map_group, floor_group, pipe_group, block_group, enemy_group) f = Flag(screen, settings, 198 * settings.block_width, 13 * settings.block_height) f.add(map_group) stats.new_level() # victory -> change level -> use death_counter to reset if mario.has_won and death_counter <= 300: # draw black screen if death_counter == 100: stats.level_num += 1 screen.fill(settings.bg_color) # display level lvl_str = "World " + str(stats.world) + "-" + str( stats.level_num) level_label = Text(None, settings.TEXT_SIZE, lvl_str, settings.WHITE, 0, 0) level_label.rect.center = (settings.screen_width / 2, settings.screen_height / 2) level_label.draw(screen) coming_soon = Text(None, settings.TEXT_SIZE, "Coming Soon", settings.WHITE, 0, 0) coming_soon.rect.center = (settings.screen_width / 2, (settings.screen_height / 2) + settings.SPACER) coming_soon.draw(screen) pygame.display.flip() death_counter += 1 print(death_counter) elif mario.has_won and death_counter > 300: # reset game death_counter = 0 mario.kill() mario = Mario(screen, settings, stats) map_group = Group() block_group = Group() floor_group = Group() pipe_group = Group() enemy_group = Group() powerup_group = Group() fireball_group = Group() dead_group = Group() pygame.mixer.music.load("Sounds/overworld.mp3") pygame.mixer.music.play(-1) map.generate_map(screen, settings, map_group, floor_group, pipe_group, block_group, enemy_group) f = Flag(screen, settings, 198 * settings.block_width, 13 * settings.block_height) f.add(map_group) stats.new_level() # Game Play gf.check_events(state, mario, screen, settings, fireball_group, map_group) # Update here if not mario.is_dead and not mario.has_won: if timer < settings.fps: timer += 1 else: timer = 0 stats.decrement_time() gf.update(screen, settings, mario, map_group, floor_group, pipe_group, block_group, enemy_group, powerup_group, fireball_group, dead_group, f) if stats.did_time_runout(): mario.dead() if stats.time == 100: pygame.mixer.Sound('Sounds/time_warning.wav').play() # update game values stats.add_score(settings.score_holder) stats.add_coin(settings.coin_holder) if settings.one_up: stats.lives += 1 settings.one_up = False # Display here stats.update() gf.update_screen(screen, settings, stats, mario, map_group, floor_group, pipe_group, block_group, enemy_group, powerup_group, fireball_group, dead_group, f) # stats.draw() pygame.quit() sys.exit()
def run_game(): pygame.init() settings = Settings() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height)) paddles = [] paddles.append( Paddle(settings, screen, 0, settings.screen_height / 2 - settings.paddle_long / 2, settings.paddle_short, settings.paddle_long, True, True, False)) paddles.append( Paddle(settings, screen, settings.screen_width - settings.paddle_short, settings.screen_height / 2 - settings.paddle_long / 2, settings.paddle_short, settings.paddle_long, False, True, True)) paddles.append( Paddle(settings, screen, settings.screen_width / 4 - settings.paddle_long / 2, 0, settings.paddle_long, settings.paddle_short, True, False, False)) paddles.append( Paddle(settings, screen, (settings.screen_width / 4) * 3 - settings.paddle_long / 2, 0, settings.paddle_long, settings.paddle_short, False, False, True)) paddles.append( Paddle(settings, screen, settings.screen_width / 4 - settings.paddle_long / 2, settings.screen_height - settings.paddle_short, settings.paddle_long, settings.paddle_short, True, False, False)) paddles.append( Paddle(settings, screen, (settings.screen_width / 4) * 3 - settings.paddle_long / 2, settings.screen_height - settings.paddle_short, settings.paddle_long, settings.paddle_short, False, False, True)) random_position = random.randint(int(settings.screen_height / 3), int((2 * settings.screen_height) / 3)) init_x_vel = random.choice([-2, -1, 1, 2]) init_y_vel = random.choice([-2, -1, 1, 2]) balls = [] balls.append( Ball(settings, screen, settings.screen_width / 2 - settings.ball_size / 2, random_position, settings.ball_size, init_x_vel, init_y_vel)) line = [] line.append(pygame.Rect((settings.screen_width / 2 - 4, 25), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 125), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 225), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 325), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 425), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 525), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 625), (8, 50))) line.append(pygame.Rect((settings.screen_width / 2 - 4, 725), (8, 50))) clock = pygame.time.Clock() while True: if settings.ball_touch_edge: for ball in balls: balls.remove(ball) random_position = random.randint( int(settings.screen_height / 3), int((2 * settings.screen_height) / 3)) init_x_vel = random.choice([-2, -1, 1, 2]) init_y_vel = random.choice([-2, -1, 1, 2]) balls.append( Ball(settings, screen, settings.screen_width / 2 - settings.ball_size / 2, random_position, settings.ball_size, init_x_vel, init_y_vel)) settings.ball_touch_edge = False gf.check_events(paddles) gf.update(settings, screen, paddles, balls) gf.update_screen(settings, screen, paddles, balls, line) clock.tick(75)