def shoot(self): self.shooting = True self.facing = self.vel / 3 if len(self.bullets) < 3: if self.facing > 0: self.bullets.append(fire(self.x + 25, self.y + 25, self.facing)) if self.facing < 0: self.bullets.append(fire(self.x - 5, self.y + 25, self.facing))
health -= int(sum(health_list)) health_list.clear() for event in pg.event.get(): if event.type == pg.QUIT: pg.quit() exit() if event.type == pg.KEYDOWN: if event.key == pg.K_SPACE: bullet1.play() if lm or ls: facing = -1 elif rm or rs: facing = 1 if len(bullet) < 8: if rs or rm: bullet.append(fire(xin + 35, yin + 16, facing)) elif ls or lm: bullet.append(fire(xin - 5, yin + 16, facing)) if event.key == pg.K_LEFT: lm = True ls = False rs = False if not jump: ls = True if event.key == pg.K_RIGHT: rm = True ls = False rs = False if not jump: rs = True if event.key == pg.K_UP:
def Game(display, enemy_list, block_list, key_x, key_y, score, bg): # Initial variables #----------------------------------------------------------------------- music = pg.mixer.music.load('audio/dvs.mp3') key = False key_appear = True pg.mixer.music.play(-1) xin = 50 #---------Initial X-Position yin = 585 #---------Initial Y-Position change = 8 #----------Motion lm = False #----------Left Motion rm = False #----------Right Motion ls = False #----------Left Stand rs = True #----------Right Stand jump = False jump_h = 10 #--------Jump Height neg = 1 bullet = [] fall = 1 fall_state = True health = 10 health_list = [] block_rect = [] for block in block_list: block_rect.append( pg.Rect(int(block[0]), int(block[2]), int(block[1] - block[0]), 25)) gameExit = False # Main Game Loop #------------------------------------------------------------------------------ while not gameExit: clock.tick(24) # Player Bullets #------------------------------------------------------------------------------ for bullets in bullet: if bullets.x > xin + 200 or bullets.x < xin - 200: # player bullets max range if bullets in bullet: bullet.pop(bullet.index(bullets)) else: bullets.x += bullets.vel for enemy in enemy_list: if bullets.x + 10 >= enemy.x and bullets.x + 10 <= enemy.x + 30: if bullets.y + 10 >= enemy.y and bullets.y <= enemy.y + 50: if bullets in bullet: enemy.hit() damage2.play() bullet.pop(bullet.index(bullets)) # Enemy Damage #----------------------------------------------------------------------------- for enemy in enemy_list: if enemy.bullet_hit >= 3: # enemy life enemy.dead(display) enemy_list.pop(enemy_list.index(enemy)) score += 50 # Enemy Shooting #----------------------------------------------------------------------------- for enemy in enemy_list: if enemy.y + 25 > yin and enemy.y < yin + 50: if xin > enemy.x and enemy.vel > 0: enemy.shoot() bullet2.play() elif xin < enemy.x and enemy.vel < 0: enemy.shoot() bullet2.play() else: enemy.shooting = False # Player health #------------------------------------------------------------------------------ for enemy in enemy_list: health_list.append(enemy.h) if len(health_list) > len(enemy_list): health_list.pop(0) if sum(health_list) > 0: health -= int(sum(health_list)) damage1.play() health_list.clear() # Event Handeling #----------------------------------------------------------------------------- for event in pg.event.get(): if event.type == pg.QUIT: pg.quit() exit() # Key Pressed: if event.type == pg.KEYDOWN: if event.key == pg.K_p: pause_screen() if event.key == pg.K_SPACE: bullet1.play() if lm or ls: facing = -1 elif rm or rs: facing = 1 if len(bullet ) < 4: # max number of bullets of player at a time if rs or rm: bullet.append(fire(xin + 35, yin + 16, facing)) elif ls or lm: bullet.append(fire(xin - 5, yin + 16, facing)) if event.key == pg.K_LEFT: lm = True ls = False rs = False if not jump: ls = True if event.key == pg.K_RIGHT: rm = True ls = False rs = False if not jump: rs = True if event.key == pg.K_UP: jump = True # Key Released if event.type == pg.KEYUP: if event.key == pg.K_c: if rm: rs = True rm = False ls = False elif lm: ls = True lm = False rs = False if event.key == pg.K_RIGHT: rs = True rm = False ls = False elif event.key == pg.K_LEFT: ls = True lm = False rs = False # Animations Background and stuff #------------------------------------------------------------------------------------------------------------------ stage.bg(display, bg) #background stage.bottom(display, 900, 600, 250, 400) stage.border(display, 900, 600) for block in block_list: stage.tile1(display, block[0], block[1], block[2]) for enemy in enemy_list: enemy.draw(display, xin, yin) xin, yin, jump_h, neg, jump = player.player(display, xin, yin, change, lm, rm, rs, ls, jump, jump_h, neg, bullet, health) player_block = pg.Rect(xin + 15, yin + 4, 20, 46) # Player motion restrictions #----------------------------------------------------------------------------------------------------------------------- for block in block_rect: if player_block.colliderect(block): if player_block.right >= block.left and player_block.left < block.left: player_block.right = block.left if player_block.left <= block.right: player_block.left = block.right if player_block.bottom >= block.top and player_block.top < block.top: player_block.bottom = block.top jump = False jump_h = 10 fall_state = False if player_block.top <= block.bottom: player_block.top = block.bottom jump = False jump_h = 10 player_block.top = block.bottom ## if fall_state: ## jump = False if fall_state and not jump: yin += (fall**2) * 0.30 fall += 1 if yin + 50 >= 580: fall = 1 jump = False jump_h = 10 yin = 535 fall_state = True if xin <= 15: xin = 15 elif xin >= 835: xin = 835 if yin <= 15: yin = 15 # Key and score #---------------------------------------------------------------------------------------------------------------------------- if not (key_x <= xin + 25 <= key_x + 35 and yin <= key_y + 12 <= yin + 50): if key_appear: stage.key(display, key_x, key_y) else: if key_appear: k.play() key_appear = False key = True message_to_screen1("Score: " + str(score), white, -250, 'small', 350) pg.display.update() # Stage Ending #---------------------------------------------------------------------------------------------------------------------------- if len(enemy_list) == 0 and key: time.sleep(0.5) gameExit = True state = 'next' if health < 1: time.sleep(0.5) gameExit = True state = 'retry' return score, state
def game(score_state): # No sound. :( #pygame.mixer.pre_init(44100,8,4,1024) pygame.init() pygame.font.init() #pygame.mixer.music.set_volume(2.0) # if pygame.mixer.music.get_busy(): # pass # else: # musicfile = data.filepath('midi','bumblbee.mid') # pygame.mixer.music.load(musicfile) # pygame.mixer.music.play(-1) screen = pygame.display.set_mode(SCREENRECT.size) clock = pygame.time.Clock() background = pygame.image.load(data.filepath('images/new_background.png')).convert() level = Level() tornadoes = pygame.sprite.Group() houses = pygame.sprite.Group() player = pygame.sprite.Group() rays = pygame.sprite.Group() lightnings = pygame.sprite.Group() all = pygame.sprite.OrderedUpdates() titles = pygame.sprite.Group() statuses = pygame.sprite.Group() scores = pygame.sprite.Group() sparks = pygame.sprite.Group() start_new_game = False Score.containers = all Status.containers = all Title.containers = all Tornado.containers = tornadoes,all House.containers = houses,all Ray.containers = rays,all Lightning.containers = lightnings,all Score.containers = all Spark.containers = all Status.containers = all Player.containers = all screen.blit(background,(0,0)) pygame.display.flip() # Title Screen score = Score(score_state) status_pause = 0 first_time = True while start_new_game == False: status_pause = status_pause + 1 if (first_time): title_text = "Twisted Twister!" Title(title_text,(0,0,0)) Title(title_text,(255,0,0)) Title(title_text,(0,255,255)) Title(title_text,(0,255,0)) Title(title_text,(0,0,255)) first_time = False for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == MOUSEBUTTONDOWN: start_new_game = True if event.type == KEYDOWN: if event.key == K_ESCAPE: sys.exit() else: start_new_game = True if status_pause > 100: Status ("Press any key to start...") status_pause = 0 all.clear(screen,background) all.update() dirty = all.draw(screen) pygame.display.update(dirty) clock.tick(30) kill_objects(all) house_container = [] # Stuff to do to start the game #for i in range(100,800,150): for i in range(0,5): house_container.append(House(((i*150)+100,500))) # Reset the level level.start() # Start the scoreboard score = Score(score_state) # Populate the screen with the actors fujita = start_game(level) player = Player((400,550)) # Lights! Camera! Silence on the set! playing = True bonus_house = 0 last_bonus = 0 bonus_at = 10000 # Cameras rolling... # 3...2...1... Action! while playing: active = True lightning_counter = 0 Status("Level %d, %s spotted!" % (level.get_level(), 'F' + str(fujita) + 's')) if (bonus_house > 0): for t in range(0,5): if (house_container[t].is_alive() == False): if (bonus_house >= 1): house_container[t].set_alive() bonus_house = bonus_house - 1 while active: lightning_counter = lightning_counter + 1 if lightning_counter > min(300,10000 - (level.get_level() * 200)): Lightning(player.get_position()) lightning_counter = 0 for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == KEYDOWN: # Put in an escape key / Q key handler if event.key == K_ESCAPE: active = False playing = False if event.key == K_LEFT: player.left() if event.key == K_RIGHT: player.right() if event.key == K_LCTRL or event.key == K_RCTRL: player.fire() if event.type == KEYUP: if event.key == K_LEFT: player.stop() if event.key == K_RIGHT: player.stop() pass tornado_house_collide = pygame.sprite.groupcollide(houses,tornadoes,False,False) if (tornado_house_collide): for house in (tornado_house_collide): for tornado in (tornado_house_collide[house]): if (house.is_alive()): tornado.rise() house.rise(tornado.get_fujita()) tornado_ray_collide = pygame.sprite.groupcollide(tornadoes,rays,False,False) if (tornado_ray_collide): for tornado in (tornado_ray_collide): tornado.hit(score) if (not tornadoes.sprites()): active = False lightning_player_collide = pygame.sprite.spritecollide(player,lightnings,False) if (lightning_player_collide): player.hit() Spark(player.get_midtop_position()) if (score.get_score() >= (last_bonus + bonus_at)): bonus_house = bonus_house + 1 # Should return the last score at 10,000 increments last_bonus = (score.get_score() / 1000) * 1000 Status("Bonus!") all.clear(screen,background) all.update() dirty = all.draw(screen) pygame.display.update(dirty) clock.tick(30) # Reset for the next level if playing: level.next_level() player.kill() fujita = start_game(level) player = Player((400,550)) lightning_counter = 0 houses_alive = False for i in range(0,5): house_container[i].restoration() houses_alive = houses_alive or house_container[i].is_alive() if (houses_alive or bonus_house > 0): playing = True active = True else: active = False playing = False score_state = score.get_score_state() kill_objects(all) return (score_state)