def run_game(): # determine the pygame window position x = 100 y = 45 os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x, y) # Initialize game and create a screen object. pygame.init() programIcon = pygame.image.load('images/covid_ic.png') pygame.display.set_icon(programIcon) ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) pygame.display.set_caption("Corona Epidemic") # Make the play button. play_button = Button(ai_settings, screen, "Play") # Create an instance to store game statistics. stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Make a shooter. shooter = Shooter(ai_settings, screen) # Make a group to store bullets in. bullets = Group() covids = Group() # Create the fleet on covids. gf.create_fleet(ai_settings, screen, shooter, covids) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, shooter, covids, bullets) if stats.game_active: shooter.update() gf.update_bullets(ai_settings, screen, stats, sb, shooter, covids, bullets) gf.update_covids(ai_settings, screen, stats, sb, shooter, covids, bullets) gf.update_screen(ai_settings, screen, stats, sb, shooter, covids, bullets, play_button)
def run_game(): pygame.init() target_settings = Settings() screen = pygame.display.set_mode( (target_settings.screen_width, target_settings.screen_height)) pygame.display.set_caption("Shoot The Box") play_button = Button(target_settings, screen, "Start") stats = GameStats(target_settings) sb = Scoreboard(target_settings, screen, stats) shooter = Shooter(target_settings, screen) target = Target(target_settings, screen) bullets = Group() while True: gf.check_events(target_settings, stats, screen, sb, shooter, target, bullets, play_button) if stats.game_active: shooter.update() gf.update_bullets(target_settings, stats, screen, sb, shooter, target, bullets) gf.update_target(target_settings, target) gf.update_screen(target_settings, stats, screen, sb, shooter, target, bullets, play_button)
def run_game(): pygame.init() sideway_shooter = Settings() screen = pygame.display.set_mode(( sideway_shooter.width, sideway_shooter.height)) pygame.display.set_caption("__Hjira__") shooter = Shooter(screen) bullets = Group() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: shooter.moving_up = True if event.key == pygame.K_DOWN: shooter.moving_down = True elif event.key == pygame.K_SPACE: new_bullet = Bullet(screen, sideway_shooter, shooter) bullets.add(new_bullet) elif event.type == pygame.KEYUP: if event.key == pygame.K_UP: shooter.moving_up = False if event.key == pygame.K_DOWN: shooter.moving_down = False shooter.update() screen.fill(sideway_shooter.bg_color) shooter.belitme() bullets.update() for bullet in bullets.sprites(): bullet.draw_bullet() for bullet in bullets.copy(): if bullet.rect.top <= 0: bullets.remove(bullet) print(len(bullets)) pygame.display.flip()
# All the non player character sprites npcGroups = [laserGroup, centipede, mushroomGroup] while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: newLaser = shooter.shoot() laserGroup.add(newLaser) allSpriteGroup.add(newLaser) pressedKeys = pygame.key.get_pressed() shooter.update(pressedKeys) # Move everything in the game (referring to the underlying positions of game objects) for gameSprite in list(itertools.chain(npcGroups)): gameSprite.update() screen.fill(black) # Draw all the game objects on the screen in their new positions for gameSprite in allSpriteGroup: screen.blit(gameSprite.image, gameSprite.rect) # Deal with centipede segments that were shot collisions = pygame.sprite.groupcollide(laserGroup, centipede, True, False) segmentsHit = [] for laser in collisions:
class Frobo(wpilib.IterativeRobot): """Control code for Frobo, the 2013 Competition Robot of FRC Team 2403 (Plasma Robotics)""" def robotInit(self): """Initialization method for the Frobo class. Initializes objects needed elsewhere throughout the code.""" # Initialize Joystick self.controller = Joystick(Values.CONTROLLER_ID) # Initialize Drive Sub-System self.drive = FroboDrive(self, Values.DRIVE_LEFT_MAIN_ID, Values.DRIVE_LEFT_SLAVE_ID, Values.DRIVE_RIGHT_MAIN_ID, Values.DRIVE_RIGHT_SLAVE_ID) # Initialize Shooter Sub-System self.compressor = wpilib.Compressor() self.shooter = Shooter(self, Values.SHOOT_FRONT_ID, Values.SHOOT_BACK_ID, Values.SHOOT_SOLENOID_FORWARD_CHANNEL_ID, Values.SHOOT_SOLENOID_REVERSE_CHANNEL_ID) def robotPeriodic(self): pass # DISABLED MODE def disabledInit(self): """Method ran when Frobo first enters the *disabled* mode.""" self.shooter.disable() def disabledPeriodic(self): """Method that runs while Frobo is in the *disabled* mode.""" pass # AUTONOMOUS MODE def autonomousInit(self): """Method ran when Frobo first enters the *autonomous* mode.""" pass def autonomousPeriodic(self): """Method that runs while Frobo is in the *autonomous* mode.""" pass # TELEOP MODE def teleopInit(self): """Method ran when Frobo first enters the *teleop* mode.""" self.shooter.disable() def teleopPeriodic(self): """Method that runs while Frobo is in the *teleop* mode.""" self.drive.fps(power_axis=self.controller.LEFTY, turn_axis=self.controller.RIGHTX) self.shooter.update(self.controller) # TEST MODE def testInit(self): """Method ran when Frobo first enters the *test* mode.""" self.shooter.disable() def testPeriodic(self): """Method that runs while Frobo is in the *test* mode.""" self.shooter.update(self.controller)
class SidewaysShooter: def __init__(self): """Initialize the game and create game resources.""" pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height)) pygame.display.set_caption("Sideways Shooter") # Create an instance to store game statistics, and create a scoreboard. self.stats = GameStats(self) self.sb = Scoreboard(self) self.shooter = Shooter(self) self.lines = pygame.sprite.Group() self.bullets = pygame.sprite.Group() self.bulletbs = pygame.sprite.Group() self.balls = pygame.sprite.Group() self.ball2s = pygame.sprite.Group() self._create_fleet() self._create_fleet2() # Make the play button. self.play_button = PlayButton(self, "Right wheel to play") # Make the Settings button. self.settings_button = SettingsButton(self, "Settings") # Make the setttings menu. self.settings_menu = SettingsMenu(self, "fast or slow press y or n") # Make the speed button. self.speed_button = SpeedButton(self, "you have selected the fast option") # Make the second speed button. self.speed_button2 = SpeedButton2(self, "You have selected the slower options") def run_game(self): """Start the main game loop.""" Running, Pause = 0, 1 state = Running while True: self._check_events() if self.stats.game_active: self._check_events() self.shooter.update() self._update_bullets() self._update_shooter() self.check_if_empty() self._update_screen() def _check_events(self): LEFT = 1 MIDDLE = 2 RIGHT = 3 """Respond to keypresses and mouse events.""" for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self._check_keydown_events(event) elif event.type == pygame.KEYUP: self._check_keyup_events(event) elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT: #mouse_pos2 = pygame.mouse.get_pos() #self._check_play_button(mouse_pos2) #None self._fire_bullet() elif event.type == pygame.MOUSEBUTTONDOWN and event.button == MIDDLE: mouse_pos = pygame.mouse.get_pos() self._check_play_button(mouse_pos) #pause #self._check_settings_button(mouse_pos) #elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos2 = pygame.mouse.get_pos() self.settings_button.flag = True #self._check_settings_button(mouse_pos2) elif event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT: #mouse_pos = pygame.mouse.get_pos() #self._check_play_button(mouse_pos) self._fire_bulletb() def _check_play_button(self, mouse_pos): """Start a new game when the player clicks Play.""" button_clicked = self.play_button.rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: # Reset the game statistics. self.stats.reset_stats() self.stats.game_active = True self.sb.prep_score() self.sb.prep_shooters() # Get rid of any remaining balss and aliens self.balls.empty() self.ball2s.empty() self.bullets.empty() self.bulletbs.empty() self.lines.empty() self.shooter3() # Create a new fleet. self._create_fleet() self._create_fleet2() # Hide the mouse cursor. pygame.mouse.set_visible(False) def _check_settings_button(self, mouse_pos2): """load the settings when the player clicks settings""" if self.settings_button.rect.collidepoint(mouse_pos2): #self.settings_button.flag = True #self.settings_menu.check_image() self.settings_menu.draw_button() def _check_keydown_events(self, event): """Respond to keypresses.""" if event.key == pygame.K_x: self.shooter.moving_right = True elif event.key == pygame.K_c: self.shooter.moving_left = True elif event.key == pygame.K_SPACE: self._fire_line() elif event.key == pygame.K_RIGHT: self.settings_button.flag = True #self.speed_button.draw_button() elif event.key == pygame.K_LEFT: self.settings_button.flag = False self.settings_menu.draw_button() pygame.mouse.set_visible(True) elif event.key == pygame.K_y: self.speed_button.flag2 = True while self.speed_button.flag2 == True: self.speed_button2.flag3 = False break self.settings.shooter_speed2 = 0.09 elif event.key == pygame.K_n: self.speed_button2.flag3 = True while self.speed_button2.flag3 == True: self.speed_button.flag2 = False break self.settings.shooter_speed2 = 0.01 elif event.key == pygame.K_p: self.stats.game_active = False pygame.mouse.set_visible(True) self.settings_button.flag = False #self.settings_button() self.balls.empty() self.ball2s.empty() self.bullets.empty() self.bulletbs.empty() self.lines.empty() self.shooter3() self._create_fleet() self._create_fleet2() elif event.key == pygame.K_q: pygame.mouse.set_visible(True) sys.exit() def shooter3(self): self.shooter = Shooter(self) def check_if_empty(self): if len(self.balls) == 0 and len(self.ball2s) == 0: pass def _check_keyup_events(self, event): """Respond to key releases.""" if event.key == pygame.K_x: self.shooter.moving_right = False elif event.key == pygame.K_c: self.shooter.moving_left = False def _fire_bullet(self): """Create a new bullet and add it to the group.""" if len(self.bullets) < self.settings.bullets_allowed: new_bullet = Bullet(self) self.bullets.add(new_bullet) def _fire_bulletb(self): """Create a new bullet and add it to the group.""" if len(self.bulletbs) < self.settings.bulletbs_allowed: new_bulletb = Bulletb(self) self.bulletbs.add(new_bulletb) def _update_bullets(self): """Update position of bullerts and get rid of old bullets.""" # Update bullet positions self.bullets.update() self.bulletbs.update() self.lines.update() # Get rid of bullets that have disappeared. for bullet in self.bullets.copy(): if bullet.rect.right >= self.settings.screen_width: self.bullets.remove(bullet) for bulletb in self.bulletbs.copy(): if bulletb.rect.right >= self.settings.screen_width: self.bulletbs.remove(bulletb) self._check_bullet_ball_collisions() def _check_bullet_ball_collisions(self): """Respond to bullet-ball collisions.""" # Remove any bullets and balls that have collided. # Check for any bullets that have hit any balls # If so, get rid of the bullets and the ball collisions = pygame.sprite.groupcollide(self.bullets, self.balls, True, True) collisions2 = pygame.sprite.groupcollide(self.bulletbs, self.ball2s, True, True) collisions3 = pygame.sprite.groupcollide(self.lines, self.balls, True, True) collisions4 = pygame.sprite.groupcollide(self.lines, self.ball2s, True, True) if collisions3 or collisions4 == True: self._shooter_hit() if collisions: for balls in collisions.values(): self.stats.score += self.settings.ball_points * len(balls) self.sb.prep_score() self.sb.check_high_score() if collisions2: for ball2s in collisions2.values(): self.stats.score += self.settings.ball_points2 * len(ball2s) self.sb.prep_score() self.sb.check_high_score() def _update_shooter(self): """Update the position of the shooter.""" self.shooter.update() # Look for ball-shooter collisions. if pygame.sprite.spritecollideany(self.shooter, self.balls): self._shooter_hit() if pygame.sprite.spritecollideany(self.shooter, self.ball2s): self._shooter_hit() def _shooter_hit(self): """Respond to the shooter being hit.""" if self.stats.shooters_left > 0: # Decrement ships_left, and update scoreboard. self.stats.shooters_left -= 1 self.sb.prep_shooters() # Get rid of any remaining balls and bullets. self.balls.empty() self.ball2s.empty() self.bullets.empty() self.bulletbs.empty() self.lines.empty() # Create a new fleet self.shooter3() self._create_fleet() self._create_fleet2() # Pause. sleep(0.5) else: self.stats.game_active = False pygame.mouse.set_visible(True) def _create_fleet(self): """Create the fleet of balls.""" # Create a ball and find the number of balls in one row. # Spacing between ball is equal to one balls width. ball = Ball(self) ball_width = ball.rect.height ball_width, ball_height = ball.rect.size available_space_x = self.settings.screen_width number_balls_x = available_space_x // (6 * ball_width) # Determine the number of rows of aliens that fit on the screen. ship_height = self.shooter.rect.height available_space_y = self.settings.screen_height number_rows = available_space_y // (2 * ball_height) # Create the first fleet of aliens. for row_number in range(number_rows): for ball_number in range(number_balls_x): # Create a ball and place it in the row. ball = Ball(self) ball_width, ball_height = ball.rect.size ball.y = 2 * ball_width + (5 * ball_width * ball_number) ball.rect.y = ball.y ball.rect.x = 19 * ball_width + (2 * ball_width * row_number) self.balls.add(ball) def _create_fleet2(self): """Create the fleet of ball2s.""" # Create a yellow ball and find the number of balls in a row. # Spacing between each ball is equal to one ball width. ball2 = Ball2(self) ball_width2 = ball2.rect.height ball_width2, ball_height2 = ball2.rect.size available_space_x2 = self.settings.screen_width number_ball2s_x = available_space_x2 // (6 * ball_width2) # Determine the number of balls that fit on the screen shooter_height = self.shooter.rect.height available_space_y2 = self.settings.screen_width number_rows2 = available_space_y2 // (2 * ball_height2) # Create the first fleet of balls. for row_number2 in range(number_rows2): for ball2_number in range(number_ball2s_x): # Create a ball and place it in the row. ball2 = Ball2(self) ball_width2, ball_height2 = ball2.rect.size ball2.y = 5 * ball_width2 * ball2_number ball2.rect.y = ball2.y ball2.rect.x = 19 * ball_width2 + (2 * ball_width2 * row_number2) self.ball2s.add(ball2) def _fire_line(self): """Create a new line and add it to the lines group.""" for i in range(1): new_line = Line(self) self.lines.add(new_line) def _update_screen(self): """Update images on the screen, and flip to the new screen.""" # Draw the play button if the game is inactive. #if not self.stats.game_active: #self.play_button.draw_button() self.screen.fill(self.settings.bg_color) self.shooter.blitme() #self._fire_line() for bullet in self.bullets.sprites(): bullet.draw_bullet() for bulletb in self.bulletbs.sprites(): bulletb.draw_bulletb() for line in self.lines.sprites(): #self._fire_line() line.draw_line() self.balls.draw(self.screen) self.ball2s.draw(self.screen) # Draw the score information. self.sb.show_score() # Draw the play button if the game is inactive. if not self.stats.game_active: self.play_button.draw_button() # Draw the settings if the game is inactive. if not self.stats.game_active: self.settings_button.draw_button() if self.settings_button.flag == True and not self.stats.game_active: #self.settings_menu.check_image() self.settings_menu.draw_button() if self.speed_button.flag2 == True and not self.stats.game_active: self.speed_button2.flag3 = False self.speed_button.draw_button3() if self.speed_button2.flag3 == True and not self.stats.game_active: self.speed_button.flag2 = False self.speed_button2.draw_button4() #self.speed_button2.flag3 = False #self.speed_button.flag2 = False pygame.display.flip()
class SpaceShooter: '''The main class that controls all assets and behaviour of the game''' def __init__(self): # Setting up the screen pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.screen_width, self.settings.screen_height)) pygame.display.set_caption('SpaceShooter') self.stats = Stats(self) self.space_bg = Space(self) self.shooter = Shooter(self) # Create a list that will store the lasers self.lasers = pygame.sprite.Group() self.spaceships = pygame.sprite.Group() self.display_shooters = pygame.sprite.Group() self._create_fleet() self._display_shooters() self.button = Button(self, 'Play') self.quitbutton = QuitButton(self, 'Quit') self.sb = Scoreboard(self) def run_game(self): while True: self._check_events() if self.stats.game_active: self.shooter.update() self._update_lasers() self._update_spaceships() self._screen_updates() def _check_events(self): # Pressing certain key types for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.shooter.moving_up = True if event.key == pygame.K_DOWN: self.shooter.moving_down = True if event.key == pygame.K_q: sys.exit() if event.key == pygame.K_SPACE: self._fire_laser() elif event.type == pygame.KEYUP: self.shooter.moving_up = False self.shooter.moving_down = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() self._check_play_button(mouse_pos) self._check_quit_button(mouse_pos) def _check_play_button(self, mouse_pos): '''Start a new game when the player clicks play''' button_clicked = self.button.rect.collidepoint(mouse_pos) if button_clicked and not self.stats.game_active: # Reset the game settings self.settings.initialise_dynamic_settings() # Reset the whole game so it starts from the beginning self.stats.reset_stats() self._display_shooters() self.stats.game_active = True self.sb.prep_score() self.spaceships.empty() self.lasers.empty() self._create_fleet() self.shooter.center_shooter() # Hide the mouse cursor pygame.mouse.set_visible(False) def _check_quit_button(self, mouse_pos): '''Quit the quit when the quit button is clicked''' quit_button_clicked = self.quitbutton.rect.collidepoint(mouse_pos) if quit_button_clicked and not self.stats.game_active and self.stats.shooter_left == 0: sys.exit() def _fire_laser(self): '''Create a new laser and add it to the group''' if len(self.lasers) < self.settings.laser_allowed: new_laser = Laser(self) self.lasers.add(new_laser) def _update_lasers(self): self.lasers.update() # Get rid of bullets that have disappeared for laser in self.lasers.copy(): if laser.rect.left >= self.settings.screen_width: self.lasers.remove(laser) self._check_lasers_spaceships_collisions() def _check_lasers_spaceships_collisions(self): '''Respond to laser spaceship collisions''' #Check for collisions between lasers and spaceship # Destroy the spaceship collisions = pygame.sprite.groupcollide(self.lasers, self.spaceships, False, True) if collisions: for spaceships in collisions.values(): self.stats.score += self.settings.spaceship_points * len( spaceships) self.sb.prep_score() # Add a new fleet of spaceships when they dissapear if not self.spaceships: self.lasers.empty() self._create_fleet() self.settings.increase_speed() def _create_fleet(self): '''Create a fleet of spaceship''' spaceship = Spaceship(self) spaceship_height, spaceship_width = spaceship.rect.size # Number of spaceships that fit on the screen vertically shooter_width = self.shooter.rect.width available_space_y = self.settings.screen_height - (2 * spaceship_height) number_spaceship_y = available_space_y // (1 * spaceship_height) # Number of spaceships that fit on the screen horizontally available_space_x = (self.settings.screen_width - (3 * spaceship_width) - (shooter_width)) number_columns = available_space_x // (3 * spaceship_width) for number_column in range(number_columns): for number_spaceship in range(number_spaceship_y): self._create_spaceship(number_spaceship, number_column) def _create_spaceship(self, number_spaceship, number_column): # Creatings spaceship and setting the position spaceship = Spaceship(self) spaceship_width, spaceship_height = spaceship.rect.size spaceship.y = spaceship_height + (2 * spaceship_height * number_spaceship) spaceship.rect.y = spaceship.y spaceship.x = (self.settings.screen_width - (2 * spaceship_width + (2 * spaceship_width * number_column))) spaceship.rect.x = spaceship.x self.spaceships.add(spaceship) def _display_shooters(self): display_shooter = ShooterDisplay(self) display_shooter_width = display_shooter.rect.width for shooter_number in range(self.stats.shooter_left): display_shooter = ShooterDisplay(self) display_shooter.rect.x = self.settings.screen_width - ( display_shooter_width * shooter_number) - display_shooter_width display_shooter.rect.y = display_shooter.rect.height / 2 self.display_shooters.add(display_shooter) def _remove_display_shooters(self): for display_shooter in self.display_shooters.copy(): if len(self.display_shooters) > self.stats.shooter_left: self.display_shooters.remove(display_shooter) def _update_spaceships(self): '''Update the position of the fleet of spaceships''' self._check_fleet_edges() self.spaceships.update() # Collision between the shooter and spaceships if pygame.sprite.spritecollideany(self.shooter, self.spaceships): self._shooter_hit() self._check_spaceships_bottom() def _check_fleet_edges(self): '''Respond appropriately when spaceship reaches the edges''' for spaceship in self.spaceships.sprites(): if spaceship.check_edges(): self._change_fleet_direction() break def _change_fleet_direction(self): '''Move the fleet left and then move it down''' for spaceship in self.spaceships.sprites(): spaceship.rect.x -= self.settings.spaceship_dropspeed self.settings.spaceship_direction *= -1 def _screen_updates(self): # Updating the screen self.space_bg.blitme() self.shooter.blitme() for laser in self.lasers.sprites(): laser.draw_laser() self.spaceships.draw(self.screen) self.display_shooters.draw(self.screen) self.sb.show_score() if not self.stats.game_active: self.button.draw_msg() if self.stats.shooter_left == 0: self.quitbutton.draw_msg() pygame.display.flip() def _shooter_hit(self): '''Respond to the shooter being hit by a spaceship''' if self.stats.shooter_left > 0: # Decrement shooter left self.stats.shooter_left -= 1 # Get rid of any remaining lasers and spaceships self._remove_display_shooters() self.spaceships.empty() self.lasers.empty() # Create a new fleet and center the shooter self._create_fleet() self.shooter.center_shooter() # Pause the game to see the collision sleep(0.5) else: self.stats.game_active = False pygame.mouse.set_visible(True) def _check_spaceships_bottom(self): '''Check to see if the spaceships have reached the left side of the screen''' screen_rect = self.screen.get_rect() for spaceship in self.spaceships.sprites(): if spaceship.rect.left <= screen_rect.left: self._shooter_hit() break