def shoot(self, shooter): if self.bulletInMagazine == 0: self.reload(True) return None if self.delaying <= 0 and self.reloading <= 0: self.shootSound.play() self.delaying = self.shootDelay self.bulletInMagazine -= 1 direction = 1 if shooter.facingRight else -1 bullets = [] bullets.append( Bullet( shooter.posX + self.offsetX, shooter.posY + self.offsetY, shooter.posZ, self.bulletSpeed * direction * math.cos(math.radians(0)), 0, self.bulletSpeed * math.sin(math.radians(0)), self.bulletDamage, 16)) bullets.append( Bullet( shooter.posX + self.offsetX, shooter.posY + self.offsetY, shooter.posZ, self.bulletSpeed * direction * math.cos(math.radians(20)), 0, self.bulletSpeed * math.sin(math.radians(20)), self.bulletDamage, 16)) bullets.append( Bullet( shooter.posX + self.offsetX, shooter.posY + self.offsetY, shooter.posZ, self.bulletSpeed * direction * math.cos(math.radians(-20)), 0, self.bulletSpeed * math.sin(math.radians(-20)), self.bulletDamage, 16)) return bullets
def __init__(self, window, first=True): self.window = window self.window_width, self.window_height = window.get_size() self.running = True self.willTabQuit = True self.score = 0 self.bg = pygame.image.load(str(Path('./assets/bg.jpg'))) self.font = pygame.font.SysFont('Hack', 14, True) self.midFont = pygame.font.SysFont('Hack', 24, True) self.bigFont = pygame.font.SysFont('Hack', 80, True) self.player = Player(self.window) self.bullet = Bullet(self.window, self.player) self.enemy = Enemy(self.window) self.display = True self.count = 0 self.state = 'menu' if first else 'start'
def shoot(self, shooter): if self.bulletInMagazine == 0: self.reload(True) return None if self.delaying <= 0 and self.reloading <= 0: self.shootSound.play() self.delaying = self.shootDelay self.bulletInMagazine -= 1 direction = 1 if shooter.facingRight else -1 return [ Bullet(shooter.posX + self.offsetX, shooter.posY + self.offsetY, shooter.posZ, self.bulletSpeed * direction, 0, 0, self.bulletDamage, 16) ]
def _fire_alien_bullet(self, alien): self.bullet_elapsed = 0.0 # create new animation for this bullet bullet_anim = config.atlas.load_animation("alien_bullet") # calculate bullet center position # it should align with the BOTTOM of the alien r = pygame.Rect(0, 0, bullet_anim.width, bullet_anim.height) r.bottom = alien.rect.bottom r.centerx = alien.rect.centerx bullet = Bullet(self.session_stats.alien_bullet, r.center, bullet_anim) self.alien_bullets.add(bullet) sounds.play("laser2")
def fire(self): if self.wait_time_to_shoot > 0.: return min_time_between_shots = 1. / self.stats.bullets_per_second self.wait_time_to_shoot = min_time_between_shots bullet_anim = config.atlas.load_static("player_bullet") r = pygame.Rect(0, 0, bullet_anim.width, bullet_anim.height) r.top = self.rect.top r.centerx = self.rect.centerx bullet = Bullet(self.stats.player_bullet, r.center, bullet_anim) self.bullet_manager.add(bullet) sounds.play("laser1")
def fire_bullet(ai_settings, screen, ship, bullets): # 创建一颗子弹,并将其加入到编组bullets中 if len(bullets) < ai_settings.bullets_allowed: new_bullet = Bullet(ai_settings, screen, ship) bullets.add(new_bullet)
def shoot(self): bullet = Bullet(self.posX, self.posY) bullet.play_sound() return bullet
class Game: def __init__(self, window, first=True): self.window = window self.window_width, self.window_height = window.get_size() self.running = True self.willTabQuit = True self.score = 0 self.bg = pygame.image.load(str(Path('./assets/bg.jpg'))) self.font = pygame.font.SysFont('Hack', 14, True) self.midFont = pygame.font.SysFont('Hack', 24, True) self.bigFont = pygame.font.SysFont('Hack', 80, True) self.player = Player(self.window) self.bullet = Bullet(self.window, self.player) self.enemy = Enemy(self.window) self.display = True self.count = 0 self.state = 'menu' if first else 'start' # Main Game State def run(self): if self.state == 'menu': self.menu() elif self.state == 'start': self.start() elif self.state == 'over': self.game_over() # Starts game def start(self): for event in pygame.event.get(): if event.type is pygame.QUIT: self.running = False if event.type is pygame.KEYDOWN: if event.key is pygame.K_TAB and self.willTabQuit: self.running = False if event.key is pygame.K_a: self.player.toggleMoving(True) self.player.moveLeft() if event.key is pygame.K_d: self.player.toggleMoving(True) self.player.moveRight() if event.key is pygame.K_w: self.player.toggleMoving(True) self.player.moveUp() if event.key is pygame.K_s: self.player.toggleMoving(True) self.player.moveDown() if event.key is pygame.K_SPACE: if self.bullet.bullet_state == 'ready': self.bullet.setShootingPos(self.player.playerX) self.bullet.bullet_state = 'shoot' if event.type is pygame.KEYUP: self.player.toggleMoving(False) # Draw Entities self.draw_window() # Draws All Entities On Window def draw_window(self): # Moves Player When Key Pressed if self.player.moving: self.player.move() # Draws Bullet On State self.bullet.draw() # Draws Enemy self.enemy.move() self.enemy.draw() # Checks Bullet-Enemy Collide if self.bullet.checkCollide(self.enemy): self.enemy.randomizePosition() self.score += 1 # Game Over State if self.enemy.isGameOver(): self.state = 'over' # Draws Player self.player.draw() # Update Player Position self.window.blit(self.update_pos(), (self.window_width - 140, 0)) # Draw Score self.window.blit(self.update_score(), (5, 20)) # Start Menu def menu(self): for event in pygame.event.get(): if event.type is pygame.QUIT: self.running = False if event.type is pygame.KEYDOWN: if event.key is pygame.K_TAB and self.willTabQuit: self.running = False if event.key is pygame.K_SPACE: self.__init__(self.window, False) self.state = 'start' # Game Title text title = 'Space Shooter' title_text = self.bigFont.render(title, 1, pygame.Color('white')) title_width, title_height = title_text.get_size() title_x, title_y = self.window_width / 2 - title_width / \ 2, self.window_height/2 - title_height/2 # Start Prompt Text start = 'Press Space To Start' start_text = self.midFont.render(start, 1, pygame.Color('white')) start_width, start_height = start_text.get_size() start_x, start_y = self.window_width / 2 - start_width / \ 2, self.window_height/2 - start_height/2 + (title_height/2) + 10 self.window.blit(title_text, (title_x, title_y)) # Blinking text self.count += 1 if self.count > 30: self.count = 0 self.display = not self.display if self.display: self.window.blit(start_text, (start_x, start_y)) # Game Over Menu def game_over(self): for event in pygame.event.get(): if event.type is pygame.QUIT: self.running = False if event.type is pygame.KEYDOWN: if event.key is pygame.K_TAB and self.willTabQuit: self.running = False if event.key is pygame.K_SPACE: self.__init__(self.window, False) self.state = 'start' # Game Over Text over = 'GAME OVER' game_over_text = self.bigFont.render(over, 1, pygame.Color('white')) over_width, over_height = game_over_text.get_size() over_x, over_y = self.window_width / 2 - over_width / \ 2, self.window_height/2 - over_height/2 # Continue Prompt Text con = 'Press Space To Continue' continue_text = self.midFont.render(con, 1, pygame.Color('white')) con_width, con_height = continue_text.get_size() con_x, con_y = self.window_width / 2 - con_width / \ 2, self.window_height/2 - con_height/2 + (over_height/2) + 10 # Score Text score = f'Score: {self.score}' score_text = self.midFont.render(score, 1, pygame.Color('white')) score_width, score_height = score_text.get_size() score_x, score_y = self.window_width / 2 - score_width / \ 2, self.window_height/2 - score_height/2 - (over_height/2) - 10 self.window.blit(game_over_text, (over_x, over_y)) self.window.blit(score_text, (score_x, score_y)) # Blinking Text self.count += 1 if self.count > 30: self.count = 0 self.display = not self.display if self.display: self.window.blit(continue_text, (con_x, con_y)) def update_pos(self): pos = f'X: {self.player.playerX} Y: {self.player.playerY}' pos_text = self.font.render(pos, 1, pygame.Color("white")) return pos_text def update_score(self): score = f'Score: {self.score}' score_text = self.font.render(score, 1, pygame.Color("white")) return score_text
def fire_bullet(ai_settings, screen, ship, bullets): if len(bullets) < ai_settings.bullets_allowed: new_bullet = Bullet(ai_settings, screen, ship) bullets.add(new_bullet)