def new(self): self.score = 0 self.player = Player(0, (screenHeight / 2 - 60), 60, 60) self.font = pg.font.SysFont('helvetica', 30, True) self.bullets = [] # container for our bullet self.packages = [] self.shark = Enemy(screenWidth - 100, (screenHeight / 2 - 60), 60, 40, 800) self.shark2 = Enemy(screenWidth - 200, (screenHeight / 3 - 60), 60, 40, 800) self.shark3 = Enemy(screenWidth - 300, (screenHeight / 5 - 60), 60, 40, 800) self.shark4 = Enemy(screenWidth - 200, (400 - 60), 60, 40, 800) self.shark5 = Enemy(screenWidth - 300, (500 - 60), 60, 40, 800) self.inkLoop = 0 self.music = pg.mixer.music.load('music.mp3') pg.mixer.music.play(-1) self.ink = pg.mixer.Sound('inkshoot.wav') self.hurt = pg.mixer.Sound('hurt.wav') self.pickup = pg.mixer.Sound('pickup.wav') self.whirlpool = pg.mixer.Sound('whirlpool.wav') ## randomize squid coord: self.squidSize = 60 self.sX = random.randint(0, screenWidth - self.squidSize) self.sY = random.randint(0, screenHeight - self.squidSize) self.squid = Neighbor( self.sX, self.sY, 40, 60, 600) # randomize the squid velocity & position, up & down motion # run has to be last on the list self.run()
def __init__(self, screenHelper): self.screen = screenHelper center = self.screen.getCenter("posx") startPositionHiveBuilder = center + (center / 2) posx = startPositionHiveBuilder self.lastMovement = pygame.time.get_ticks() for x in range(7): if x < 3: self.hive.append(Enemy(self.screen, posx, 0, x)) if x > 3: self.hive.append(Enemy(self.screen, posx, 80, x)) if x == 3: # Reset posx posx = startPositionHiveBuilder # Shift left if x != 3: posx -= 80
def __init__(self, scene): self.scene = scene self.enemies = [] # add some initial enemies self.add_enemy( Enemy(position=Vector(200, 200), rotation_degrees=random.randint(0, 360))) self.add_enemy( Enemy(position=Vector(1500, 600), rotation_degrees=random.randint(0, 360))) self.add_enemy( Enemy(position=Vector(1000, 400), rotation_degrees=random.randint(0, 360))) self.add_enemy( Enemy(position=Vector(1075, 420), rotation_degrees=random.randint(0, 360))) self.add_enemy( Enemy(position=Vector(1150, 440), rotation_degrees=random.randint(0, 360)))
def generate_enemies(self, level): dummy_enemy = Enemy(0, 100, 1, 1, self.background, self.current_level) if TEST: for _ in range(1): enemy_speed = random.randint(1, 100) x = random.randint(10, self.screen_width - int(dummy_enemy.image_width)) y = self.screen_height newEnemy = Enemy(enemy_speed, 100, x, y, self.background, self.current_level) if HITBOX_ENABLED: self.hitbox_handler.generate_hitbox_for_enemy(newEnemy) self.enemies.append(newEnemy) else: for _ in range(level['enemy_amount']): max_speed = level['enemy_speed'] enemy_speed = random.randint(10, max_speed) # TODO: Change this later to match idea: x = random.randint(1, self.screen_width - int(dummy_enemy.image_width)) y = self.screen_height newEnemy = Enemy(enemy_speed, 100, x, y, self.background, self.current_level) if HITBOX_ENABLED: self.hitbox_handler.generate_hitbox_for_enemy(newEnemy) self.enemies.append(newEnemy)
def trySpawn(enemy, room, minimap): if not (isinstance(enemy,Boss) or isinstance(enemy,Spawner)): return None else: spawn = None if enemy.name == "peep": if random.randint(0, math.ceil(1000 / minimap.level)) == 0: spawn = Enemy("bunny") if random.randint(0, math.ceil(100 / minimap.level)) == 0: # i.e. 1 in 100,000 chance per frame spawn = Boss("peep") elif isinstance(enemy, Spawner): if enemy.spawnCD == 0: spawn = Spawner(enemy.name, math.ceil(enemy.spawnSpd * (minimap.level + 1) / minimap.level)) if spawn is not None: spawn.setup() spawn.x, spawn.y = randomPos((constants.gameW-spawn.w)/2, (constants.gameH-spawn.h)/2) room.enemies.append(spawn)
def update(self, dt): self.player.velocity=Vector(0,0) # reset velocity to zero, if no keys are pressed the player will stop if self.scene.input.keyboard.is_pressed(Keycode.w): self.player.velocity += Vector(0, -settings.PLAYER_SPEED) if self.scene.input.keyboard.is_pressed(Keycode.s): self.player.velocity += Vector(0, settings.PLAYER_SPEED) if self.scene.input.keyboard.is_pressed(Keycode.a): self.player.velocity += Vector(-settings.PLAYER_SPEED, 0) if self.scene.input.keyboard.is_pressed(Keycode.d): self.player.velocity += Vector(settings.PLAYER_SPEED, 0) for event in self.scene.input.events(): if event.keyboard_key: # check if the event is a keyboard key related event if event.keyboard_key.is_key_down: # check if the event is "key down event" # check which key was pressed down: if event.keyboard_key.key == Keycode.tab: self.player.cycle_weapons() elif event.keyboard_key.key == Keycode.num_1: self.player.change_weapon(WeaponType.MachineGun) elif event.keyboard_key.key == Keycode.num_2: self.player.change_weapon(WeaponType.GrenadeLauncher) elif event.keyboard_key.key == Keycode.num_3: self.player.change_weapon(WeaponType.ForceGun) elif event.keyboard_key.key == Keycode.space: self.scene.enemies_controller.add_enemy(Enemy(position=self.scene.camera.unproject_position( self.scene.input.mouse.get_position()), rotation_degrees=random.randint(0, 360))) mouse_pos = self.scene.camera.unproject_position(self.scene.input.mouse.get_position()) player_rotation_vector = mouse_pos - self.player.position self.player.rotation_degrees = player_rotation_vector.to_angle_degrees() # Handle weapon logic if self.player.current_weapon is not None: # decrease weapons cooldown time by dt self.player.current_weapon.cooldown_time_remaining -= dt # if left mouse button pressed and weapon is ready to shoot, then, well, shoot a bullet! if self.scene.input.mouse.is_pressed(MouseButton.left) and self.player.current_weapon.cooldown_time_remaining < 0: self.player.current_weapon.shoot_bullet()
distance = 0 fuel = 100 #Loading Images playerImage = pygame.image.load('./sprites/player.png').convert_alpha() enemyImage = pygame.image.load('./sprites/enemy.png').convert_alpha() roadImage = pygame.image.load('./sprites/road.png').convert() screenStart = pygame.image.load('./screens/start.png').convert() screenEnd = pygame.image.load('./screens/end.png').convert() #Define Objects player1 = Player(playerImage, 400, 600, 5) players = pygame.sprite.Group() players.add(player1) enemy1 = Enemy(enemyImage) enemy2 = Enemy(enemyImage) enemy3 = Enemy(enemyImage) enemy4 = Enemy(enemyImage) enemys = pygame.sprite.Group() enemys.add(enemy1) enemys.add(enemy2) enemys.add(enemy3) enemys.add(enemy4) road = Road(roadImage, screen, displayHeight) #Catch Events def events(): for event in pygame.event.get():
] class Room(object): def __init__(self, roomType, enemies, items, obstructions=stdObstructions): self.type = roomType self.enemies = enemies self.items = items self.obstructions = obstructions self.doors = [] # START start = Room("standard",[],[],[]) # STANDARD standard = [ Room("standard", [Enemy("apple")], []), Room("standard", [Enemy("apple"), Enemy("cherry")],[Item(1)]), Room("standard", [Enemy("cherry"), Enemy("cherry")], [Item(1,25),Item(0,-25)]), Room("standard", [Spawner("banana")], []) ] # SHOP shop = [ Room("shop", [], [Item(3)]), Room("shop", [], [Item(4)]) ] # DISH dish = [ Room("dish", [], [Item(0,-5,-10)]) ]
class Game: ## ========= SET UP & INIT GAME ========= def __init__(self): pg.init() pg.display.set_caption(title) self.screen = pg.display.set_mode((screenWidth, screenHeight)) self.clock = pg.time.Clock() self.running = True # the game is running on load self.font_name = pg.font.match_font(FONT_NAME) ## ========= NEW CLASS ASSETS ========= def new(self): self.score = 0 self.player = Player(0, (screenHeight / 2 - 60), 60, 60) self.font = pg.font.SysFont('helvetica', 30, True) self.bullets = [] # container for our bullet self.packages = [] self.shark = Enemy(screenWidth - 100, (screenHeight / 2 - 60), 60, 40, 800) self.shark2 = Enemy(screenWidth - 200, (screenHeight / 3 - 60), 60, 40, 800) self.shark3 = Enemy(screenWidth - 300, (screenHeight / 5 - 60), 60, 40, 800) self.shark4 = Enemy(screenWidth - 200, (400 - 60), 60, 40, 800) self.shark5 = Enemy(screenWidth - 300, (500 - 60), 60, 40, 800) self.inkLoop = 0 self.music = pg.mixer.music.load('music.mp3') pg.mixer.music.play(-1) self.ink = pg.mixer.Sound('inkshoot.wav') self.hurt = pg.mixer.Sound('hurt.wav') self.pickup = pg.mixer.Sound('pickup.wav') self.whirlpool = pg.mixer.Sound('whirlpool.wav') ## randomize squid coord: self.squidSize = 60 self.sX = random.randint(0, screenWidth - self.squidSize) self.sY = random.randint(0, screenHeight - self.squidSize) self.squid = Neighbor( self.sX, self.sY, 40, 60, 600) # randomize the squid velocity & position, up & down motion # run has to be last on the list self.run() ## ========= RUN GAME ========= def run(self): self.playing = True while self.playing: self.clock.tick(FPS) self.events() self.update() self.draw() ## ========= GAME LOOP - UPDATE ========= def update(self): #if player reaches ~3/4 of screen create whirlpool suction down if self.player.y >= screenHeight - 100: self.player.y += abs(self.player.vel) self.player.hit(self.screen) self.whirlpool.play() # player dies if self.player.health == 0: self.playing = False ## ========= GAME LOOP - EVENTS ========= def events(self): for event in pg.event.get(): keys = pg.key.get_pressed() ## ============== INTERNAL GAME CONTROLS ============== # check for closing window if event.type == pg.QUIT: if self.playing: self.playing = False self.running == False # quit game via ESC key if keys[pg.K_ESCAPE]: pg.quit() sys.exit() ## octopus movement arrow keys if keys[pg.K_LEFT] and self.player.x > self.player.vel: self.player.x -= self.player.vel if keys[pg. K_RIGHT] and self.player.x < screenWidth - self.player.width: self.player.x += self.player.vel if keys[pg.K_UP] and self.player.y > self.player.vel: self.player.y -= self.player.vel if keys[pg. K_DOWN] and self.player.y < screenWidth - self.player.width: self.player.y += self.player.vel ## SPACE down add more bullets to octopus if keys[pg.K_SPACE] and self.inkLoop == 0: self.ink.play() if len(self.bullets) < 30: self.bullets.append( Projectile( round(self.player.x + self.player.width // 2), round(self.player.y + self.player.height // 2), 6, (0, 0, 0))) self.inkLoop = 1 ## ============== OCTOPUS & SHARK COLLISION ============== self.playerCollision(self.player, self.shark, self.score) self.playerCollision(self.player, self.shark2, self.score) self.playerCollision(self.player, self.shark3, self.score) self.playerCollision(self.player, self.shark4, self.score) self.playerCollision(self.player, self.shark5, self.score) ## ============== BULLET COLLISION LOGIC ============== # shoots bullets one at a time by delaying if self.inkLoop > 0: self.inkLoop += 1 if self.inkLoop > 3: self.inkLoop = 0 for self.bullet in self.bullets: ## BULLET & SHARK COLLISION self.enemyCollision(self.shark, self.score) self.enemyCollision(self.shark2, self.score) self.enemyCollision(self.shark3, self.score) self.enemyCollision(self.shark4, self.score) self.enemyCollision(self.shark5, self.score) if self.bullet.x < 800 and self.bullet.x > 0: self.bullet.x += self.bullet.vel # bullet is going to move vel direction else: self.bullets.pop(self.bullets.index( self.bullet)) # pop off the bullet or delete them ## ============== PACKAGE COLLISION LOGIC ============== for self.package in self.packages: ## OCTOPUS & PACKAGE COLLISION self.packageCollision(self.player) if self.package.x < 800 and self.package.x > 0: self.package.x += self.package.vel # package is going to move vel direction else: self.packages.pop(self.packages.index(self.package)) ## add packages on load, will only reload if package disappears packageSize = 40 pX = random.randint(0, screenWidth - packageSize) pY = random.randint(0, screenHeight - packageSize) if len(self.packages) <= 2: self.packages.append( Package(pX, pY, packageSize, packageSize, brown)) ## ============== NEIGHBOR COLLISION LOGIC ============== self.neighborCollision(self.player, self.squid, self.score) ## ========= GAME LOOP - DRAW ========= def draw(self): ## text self.screen.fill(blue) self.draw_text('Score: ' + str(self.score), 22, black, 40, 10) self.draw_text('Packages: ' + str(self.player.collectCount), 22, black, 700, 10) self.player.draw(self.screen) ## draw bullets for self.bullet in self.bullets: self.bullet.draw(self.screen) ## draw enemies self.shark.draw(self.screen) self.shark2.draw(self.screen) self.shark3.draw(self.screen) self.shark4.draw(self.screen) self.shark5.draw(self.screen) ## draw packages for self.package in self.packages: self.package.draw(self.screen) ## after getting packages, deliver to correct neighbor if self.player.collectCount >= 1: # draw the squid self.squid.draw(self.screen) self.draw_text( str(self.player.deliveryCount) + ' successful deliveries', 22, black, screenWidth / 2, 10) if self.player.collectCount >= 8 and self.player.delivered: self.player.delivered = False self.player.deliveryCount += 1 self.player.collectCount -= 8 pg.display.update() ## ========= SHOW START SCREEN ========= def show_start_screen(self): self.screen.fill(blue) self.draw_text(title, 48, black, screenWidth / 2, screenHeight / 4) self.draw_text('Use arrow keys to move and space to shoot.', 22, black, screenWidth / 2, screenHeight / 2) self.draw_text( 'Collect 8 packages and then deliver to your shy neighbor.', 22, black, screenWidth / 2, (screenHeight / 2 - 100)) self.draw_text("press a key to play", 22, black, screenWidth / 2, screenHeight * 3 / 4) pg.display.flip() self.wait_for_key() ## ========= SHOW GAME OVER / CONTINUE SCREEN ========= def show_go_screen(self): if not self.running: #if running is false, end the fn so that when the user clicks 'x' it just exits the game return self.screen.fill(black) self.draw_text('GAME OVER', 48, blue, screenWidth / 2, screenHeight / 4) self.draw_text('Score: ' + str(self.score), 22, blue, screenWidth / 2, screenHeight / 2) self.draw_text( "Press a key to play. Shoot sharks and collect 8 packages to deliver to your shy neighbor.", 22, blue, screenWidth / 2, screenHeight * 3 / 4) pg.display.flip() self.wait_for_key() ## ========= INIT GAME ON KEY PRESS ========= def wait_for_key(self): waiting = True while waiting: self.clock.tick(FPS) for event in pg.event.get(): if event.type == pg.QUIT: #user clicks x, waiting for keys waiting = False self.running = False if event.type == pg.KEYUP: waiting = False ## ========= REUSABLE TEXT ========= def draw_text(self, text, size, color, x, y): font = pg.font.Font(self.font_name, size) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() text_rect.midtop = (x, y) self.screen.blit(text_surface, text_rect) ## ========= PROJECTILE & ENEMY COLLISION ========= def enemyCollision(self, enemyObj, score): if self.bullet.y - self.bullet.radius < enemyObj.hitbox[ 1] + enemyObj.hitbox[ 3] and self.bullet.y + self.bullet.radius > enemyObj.hitbox[ 1]: # phrase 1 checks to see if the bullet is in the bottom of our shark, phrase 2 checks the top if self.bullet.x + self.bullet.radius > enemyObj.hitbox[ 0] and self.bullet.x - self.bullet.radius < enemyObj.hitbox[ 0] + enemyObj.hitbox[ 2]: # check if bullet is within left & right x coord of shark hitbox enemyObj.hit() if enemyObj.visible == True: self.score += 10 self.bullets.pop(self.bullets.index(self.bullet)) ## ========= ENEMY & PLAYER COLLISION ========= def playerCollision(self, player, enemy, score): if enemy.visible == True: # octopus no longer sustains damage if enemy is not visible if player.hitbox[1] < enemy.hitbox[1] + enemy.hitbox[ 3] and player.hitbox[1] + player.hitbox[3] > enemy.hitbox[ 1]: if player.hitbox[0] + player.hitbox[2] > enemy.hitbox[ 0] and player.hitbox[ 0] < enemy.hitbox[0] + enemy.hitbox[2]: player.hit(self.screen) self.hurt.play() self.score -= 5 ## ========= PLAYER & PACKAGE COLLISION ========= def packageCollision(self, player): if self.player.hitbox[1] < self.package.hitbox[ 1] + self.package.hitbox[3] and self.player.hitbox[ 1] + self.player.hitbox[3] > self.package.hitbox[1]: if self.player.hitbox[0] + self.player.hitbox[ 2] > self.package.hitbox[0] and self.player.hitbox[ 0] < self.package.hitbox[0] + self.package.hitbox[2]: self.pickup.play() if self.player.visible == True: self.score += 100 self.player.collectCount += 1 self.packages.pop(self.packages.index(self.package)) ## ========= NEIGHBOR (SQUID) & PLAYER COLLISION ========= def neighborCollision(self, player, neighbor, score): if player.hitbox[1] < neighbor.hitbox[1] + neighbor.hitbox[ 3] and player.hitbox[1] + player.hitbox[3] > neighbor.hitbox[1]: if player.hitbox[0] + player.hitbox[2] > neighbor.hitbox[ 0] and player.hitbox[ 0] < neighbor.hitbox[0] + neighbor.hitbox[ 2] and self.player.collectCount >= 8: self.player.delivered = True print('touched the squid!', self.player.delivered)