def __init__(self): Application.__init__(self) self.screenRect = self.screen.get_rect() self.minDt = 200 self.enemyGroup = Group() self.enemyGroup.add(BasicEnemy(100,100,0,1,self.screenRect,80)) self.bulletGroup = Group() self.player = Player(self.screenRect) self.playerGroup = GroupSingle(self.player) self.playerWeaponType = 1 self.playerFired = False self.playerMoveX = 0 self.playerMoveY = 0 self.playerMoveFlag = False
class Game(Application): def __init__(self): Application.__init__(self) self.screenRect = self.screen.get_rect() self.minDt = 200 self.enemyGroup = Group() self.enemyGroup.add(BasicEnemy(100,100,0,1,self.screenRect,80)) self.bulletGroup = Group() self.player = Player(self.screenRect) self.playerGroup = GroupSingle(self.player) self.playerWeaponType = 1 self.playerFired = False self.playerMoveX = 0 self.playerMoveY = 0 self.playerMoveFlag = False def handle_event(self,event): if event.type == MOUSEBUTTONUP and event.button == 1: self.playerFired = True if event.type == KEYDOWN: ## need to put in KEYUP to turn off the thing... if event.key == K_DOWN: self.playerMoveY = 1 self.playerMoveFlag = True if event.key == K_UP: self.playerMoveY = -1 self.playerMoveFlag = True if event.key == K_LEFT: self.playerMoveX = -1 self.playerMoveFlag = True if event.key == K_RIGHT: self.playerMoveX = 1 self.playerMoveFlag = True if event.key == K_SPACE: self.playerFired = True if event.type == KEYUP: if event.key == K_DOWN: self.playerMoveY = 0 self.playerMoveFlag = True if event.key == K_UP: self.playerMoveY = 0 self.playerMoveFlag = True if event.key == K_LEFT: self.playerMoveX = 0 self.playerMoveFlag = True if event.key == K_RIGHT: self.playerMoveX = 0 self.playerMoveFlag = True def update(self, screen): dt = min(self.minDt, self.clock.get_time()) self.enemyGroup.update(dt) if self.playerFired: self.bulletGroup.add(self.player.shoot(self.playerWeaponType)) self.playerFired = False ## need to validate holding down the key... if self.playerMoveFlag: self.player.setDirection(self.playerMoveX, self.playerMoveY) self.playerMoveFlag = False self.bulletGroup.update(dt) self.playerGroup.update(dt) pygame.sprite.groupcollide(self.enemyGroup, self.bulletGroup, True, True) def draw(self,screen): screen.fill((0,0,0)) self.playerGroup.draw(screen) self.enemyGroup.draw(screen) self.bulletGroup.draw(screen)
def main(): run = True FPS = 60 lives = 5 level = 0 mainFont = pygame.font.SysFont('comicsans',50) lostLabel = mainFont.render(f'You Lost!!!', 1 , (255,255,255)) lost_counter = 0 enemies = [] wave_length = 5 enemy_vel = 1 laser_vel = 6 clock = pygame.time.Clock() player = Player(300,300) player_vel = 6 lost = False def redraw(): win.blit(BG,(0,0)) livesLabel = mainFont.render(f'lives: {lives}', 1 , (255,255,255)) levelLabel = mainFont.render(f'level: {level}', 1 , (255,255,255)) win.blit(levelLabel,(10,10)) win.blit(livesLabel,(win_width - livesLabel.get_width() - 10, 10 )) for enemy in enemies: enemy.draw(win) player.draw(win) if lost: win.blit(lostLabel,(win_width//2 - lostLabel.get_width()//2 , win_height//2)) pygame.display.update() while run: clock.tick(FPS) redraw() if lives <= 0 or player.health <= 0: lost = True lost_counter += 1 if lost: if lost_counter >= FPS*3: run = False else: continue while len(enemies) == 0: level += 1 wave_length += 4 for i in range(wave_length): enemyX = random.randint(50,win_width - 100) enemyY = random.randint(-1500 - (level*100),-100) enemyColor = random.choice(['red','blue','green']) enemy = Enemy(enemyX,enemyY,enemyColor) enemies.append(enemy) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_a] and (player.x - player_vel > 0): player.x -= player_vel if keys[pygame.K_d] and (player.x + player_vel + player.get_width() < win_width): player.x += player_vel if keys[pygame.K_w]and (player.y - player_vel > 0): player.y -= player_vel if keys[pygame.K_s] and (player.y + player_vel + player.get_height() + 15 < win_height) : player.y += player_vel if keys[pygame.K_SPACE]: player.shoot() for enemy in enemies[:]: enemy.move(enemy_vel) enemy.move_lasers(laser_vel,player) if random.randint(0,1.5*60) == 1: enemy.shoot() if collide(enemy, player): player.health -= 10 enemies.remove(enemy) elif enemy.y + enemy.get_height() > win_height: lives -= 1 enemies.remove(enemy) player.move_lasers(-laser_vel, enemies)