def remove_arrow(arrow_list, enm, HP_ENM, arrow_dir, sprite_layers, point): # remove arrows if they collide with enemies or go out of field of view # decrease the HP of enemy if arrow collides with enemy i = 0 l = len(arrow_list) while (i < l and i >= 0): j = 0 check = 0 for j in range(len(enm)): if pygame.sprite.collide_rect(enm[j], arrow_list[i]): if point == 10.0: specialarrow_sound = sound.create_soundfx( './sounds/specialarrow_touch.ogg') sound.volume(specialarrow_sound, 0.4) else: arrow_sound = sound.create_soundfx( './sounds/arrow_touch.ogg') sound.volume(arrow_sound, 0.4) HP_ENM[j] -= point check = 1 break if check==1 or movements1.check_collision(arrow_list[i].rect.centerx, arrow_list[i].rect.centery, 8*arrow_dir[2*i],8*arrow_dir[(2*i)+1],\ arrow_list[i].rect.width, 5, sprite_layers[3]) == (0,0): sprite_layers[1].remove_sprite(arrow_list[i]) arrow_list.pop(i) arrow_dir.pop(2 * i) arrow_dir.pop(2 * i) else: arrow_list[i].rect.centerx += 25 * arrow_dir[2 * i] arrow_list[i].rect.centery += 25 * arrow_dir[(2 * i) + 1] i += 1 l = len(arrow_list)
def remove_arrow(arrow_list,enm,HP_ENM,arrow_dir,sprite_layers,point): # remove arrows if they collide with enemies or go out of field of view # decrease the HP of enemy if arrow collides with enemy i = 0 l = len(arrow_list) while (i<l and i>=0): j=0 check = 0 for j in range(len(enm)): if pygame.sprite.collide_rect(enm[j],arrow_list[i]): if point==10.0: specialarrow_sound = sound.create_soundfx('./sounds/specialarrow_touch.ogg') sound.volume(specialarrow_sound,0.4) else: arrow_sound = sound.create_soundfx('./sounds/arrow_touch.ogg') sound.volume(arrow_sound,0.4) HP_ENM[j] -= point check = 1 break if check==1 or movements1.check_collision(arrow_list[i].rect.centerx, arrow_list[i].rect.centery, 8*arrow_dir[2*i],8*arrow_dir[(2*i)+1],\ arrow_list[i].rect.width, 5, sprite_layers[3]) == (0,0): sprite_layers[1].remove_sprite(arrow_list[i]) arrow_list.pop(i) arrow_dir.pop(2*i) arrow_dir.pop(2*i) else : arrow_list[i].rect.centerx += 25*arrow_dir[2*i] arrow_list[i].rect.centery += 25*arrow_dir[(2*i)+1] i += 1 l = len(arrow_list)
def remove_weapon(hero,enm,weap_list,weap_dir,point,sprite_layers): savegame=pk.load(open("./save.p","rb")) # remove weapons if they collide with hero # decrease the HP of player if it collides with any of enemies # if player has armor then both armor and hero shares the half of the loss j = 0 for j in range(len(enm)): if pygame.sprite.collide_rect(hero,enm[j]): weap_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(weap_sound,0.4) if savegame['sheild_hp']>0: savegame['sheild_hp'] -= (point/2) savegame['hp'] -= (point/2) if savegame['sheild_hp']<0: savegame['sheild_hp'] = 0 else : savegame['hp'] -= point pk.dump(savegame,open("./save.p","wb")) i = 0 l = len(weap_list) while (i<l and i>=0): if pygame.sprite.collide_rect(hero,weap_list[i]): weap_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(weap_sound,0.7) if savegame['sheild_hp']>0: savegame['sheild_hp'] -= (point/2) savegame['hp'] -= (point/2) if savegame['sheild_hp']<0: savegame['sheild_hp'] = 0 else: savegame['hp'] -= point sprite_layers[2].remove_sprite(weap_list[i]) weap_list.pop(i) weap_dir.pop(2*i) weap_dir.pop(2*i) elif movements1.check_collision(weap_list[i].rect.centerx, weap_list[i].rect.centery, 5*weap_dir[2*i],5*weap_dir[(2*i)+1],weap_list[i].rect.width,\ 5, sprite_layers[3]) == (0,0): sprite_layers[2].remove_sprite(weap_list[i]) weap_list.pop(i) weap_dir.pop(2*i) weap_dir.pop(2*i) else : weap_list[i].rect.centerx += 5*weap_dir[2*i] weap_list[i].rect.centery += 5*weap_dir[(2*i)+1] i += 1 l = len(weap_list) pk.dump(savegame,open("./save.p","wb"))
def remove_weapon(hero, enm, weap_list, weap_dir, point, sprite_layers): savegame = pk.load(open("./save.p", "rb")) # remove weapons if they collide with hero # decrease the HP of player if it collides with any of enemies # if player has armor then both armor and hero shares the half of the loss j = 0 for j in range(len(enm)): if pygame.sprite.collide_rect(hero, enm[j]): weap_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(weap_sound, 0.4) if savegame['sheild_hp'] > 0: savegame['sheild_hp'] -= (point / 2) savegame['hp'] -= (point / 2) if savegame['sheild_hp'] < 0: savegame['sheild_hp'] = 0 else: savegame['hp'] -= point pk.dump(savegame, open("./save.p", "wb")) i = 0 l = len(weap_list) while (i < l and i >= 0): if pygame.sprite.collide_rect(hero, weap_list[i]): weap_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(weap_sound, 0.7) if savegame['sheild_hp'] > 0: savegame['sheild_hp'] -= (point / 2) savegame['hp'] -= (point / 2) if savegame['sheild_hp'] < 0: savegame['sheild_hp'] = 0 else: savegame['hp'] -= point sprite_layers[2].remove_sprite(weap_list[i]) weap_list.pop(i) weap_dir.pop(2 * i) weap_dir.pop(2 * i) elif movements1.check_collision(weap_list[i].rect.centerx, weap_list[i].rect.centery, 5*weap_dir[2*i],5*weap_dir[(2*i)+1],weap_list[i].rect.width,\ 5, sprite_layers[3]) == (0,0): sprite_layers[2].remove_sprite(weap_list[i]) weap_list.pop(i) weap_dir.pop(2 * i) weap_dir.pop(2 * i) else: weap_list[i].rect.centerx += 5 * weap_dir[2 * i] weap_list[i].rect.centery += 5 * weap_dir[(2 * i) + 1] i += 1 l = len(weap_list) pk.dump(savegame, open("./save.p", "wb"))
def attack(drctn, hero, mon, HP_ENM): savegame = pk.load(open("./save.p", "rb")) result = -1 if (drctn == 'right'): sword_rect = pygame.Rect(hero.rect.right, hero.rect.centery, 40, 30) elif (drctn == 'left'): sword_rect = pygame.Rect(hero.rect.left - 30, hero.rect.centery, 40, 30) elif (drctn == 'up'): sword_rect = pygame.Rect(hero.rect.centerx, hero.rect.top, 30, 40) elif (drctn == 'down'): sword_rect = pygame.Rect(hero.rect.centerx, hero.rect.bottom, 30, 40) i = 0 for i in range(0, len(mon)): if pygame.Rect.colliderect(mon[i].rect, sword_rect): sword_sound = sound.create_soundfx('./sounds/sword.ogg') sound.volume(sword_sound, 0.4) result = i break else: del sword_rect result = len(mon) + 1 if (result != (len(mon) + 1)): swrd_have = savegame['eqp_weapon'] if (swrd_have == None): sword_dmg = 0 elif (swrd_have == 'dagger'): sword_dmg = 10.0 elif (swrd_have == 'sw1'): sword_dmg = 15.0 elif (swrd_have == 'sw2'): sword_dmg = 20.0 elif (swrd_have == 'sw3'): sword_dmg = 25.0 HP_ENM[result] -= sword_dmg
def attack(drctn,hero,mon,HP_ENM): savegame=pk.load(open("./save.p","rb")) result = -1 if(drctn=='right'): sword_rect=pygame.Rect(hero.rect.right,hero.rect.centery,40,30) elif(drctn=='left'): sword_rect=pygame.Rect(hero.rect.left-30,hero.rect.centery,40,30) elif(drctn=='up'): sword_rect=pygame.Rect(hero.rect.centerx,hero.rect.top,30,40) elif(drctn=='down'): sword_rect=pygame.Rect(hero.rect.centerx,hero.rect.bottom,30,40) i=0 for i in range(0,len(mon)): if pygame.Rect.colliderect(mon[i].rect,sword_rect): sword_sound = sound.create_soundfx('./sounds/sword.ogg') sound.volume(sword_sound,0.4) result = i break else: del sword_rect result = len(mon)+1 if(result != (len(mon)+1)): swrd_have=savegame['eqp_weapon'] if(swrd_have==None): sword_dmg=0 elif(swrd_have=='dagger'): sword_dmg=10.0 elif(swrd_have=='sw1'): sword_dmg=15.0 elif(swrd_have=='sw2'): sword_dmg=20.0 elif(swrd_have=='sw3'): sword_dmg=25.0 HP_ENM[result] -= sword_dmg
def demo_pygame(file_name,frm): # parser the map (it is done here to initialize the # window the same size as the map if it is small enough) world_map = tiledtmxloader.tmxreader.TileMapParser().parse_decode(file_name) # init pygame and set up a screen pygame.display.set_caption("tiledtmxloader - " + file_name + \ " - keys: arrows, 0-9") screen_width = min(1024, world_map.pixel_width) screen_height = min(768, world_map.pixel_height) screen = pygame.display.set_mode((screen_width, screen_height)) # load the images using pygame resources = tiledtmxloader.helperspygame.ResourceLoaderPygame() resources.load(world_map) # prepare map rendering assert world_map.orientation == "orthogonal" # renderer renderer = tiledtmxloader.helperspygame.RendererPygame() #background music tunnel2_4_sound = sound.create_music(file_name) sound.volume(tunnel2_4_sound,0.2) # create hero sprite if frm==0: hero_pos_x = 5*32 hero_pos_y = 95*32 else: hero_pos_x = 81*32 hero_pos_y = 62*32 hero = person.create_person(hero_pos_x, hero_pos_y ,'./images/hero_u2.png') # create monster sprites mon = [] mon_dir = [] mon_image = [] mon.append(person.create_person(16*32, 82*32,'./images/slime14.png')) mon.append(person.create_person(39*32, 82*32,'./images/slime14.png')) mon.append(person.create_person(50*32, 82*32,'./images/slime14.png')) mon.append(person.create_person(60*32, 82*32,'./images/slime14.png')) mon.append(person.create_person(67*32, 82*32,'./images/slime14.png')) mon.append(person.create_person(5*32, 74*32,'./images/slime14.png')) mon.append(person.create_person(5*32, 47*32,'./images/slime14.png')) mon.append(person.create_person(14*32, 47*32,'./images/slime14.png')) mon.append(person.create_person(23*32, 33*32,'./images/slime14.png')) mon.append(person.create_person(23*32, 20*32,'./images/slime14.png')) mon.append(person.create_person(20*32, 8*32,'./images/slime14.png')) mon.append(person.create_person(43*32, 8*32,'./images/slime14.png')) mon.append(person.create_person(43*32, 24*32,'./images/slime14.png')) mon.append(person.create_person(43*32, 41*32,'./images/slime14.png')) mon.append(person.create_person(49*32, 47*32,'./images/slime14.png')) mon.append(person.create_person(66*32, 47*32,'./images/slime14.png')) mon.append(person.create_person(72*32, 32*32,'./images/slime14.png')) mon.append(person.create_person(72*32, 23*32,'./images/slime14.png')) mon.append(person.create_person(72*32, 8*32,'./images/slime14.png')) mon.append(person.create_person(81*32, 8*32,'./images/slime14.png')) mon.append(person.create_person(81*32, 19*32,'./images/slime14.png')) mon.append(person.create_person(81*32, 36*32,'./images/slime14.png')) mon.append(person.create_person(81*32, 49*32,'./images/slime14.png')) mon.append(person.create_person(67*32, 64*32,'./images/slime14.png')) mon.append(person.create_person(82*32, 64*32,'./images/slime14.png')) #list to save the direcion constraints for enemy's movements mon_dir = [-1,0,-1,0,1,0,1,0,1,0,0,-1,1,0,-1,0,0,-1,0,1,1,0,-1,0,0,1,0,-1,1,0,-1,0,0,-1,0,1,1,0,-1,0,0,1,0,-1,0,1,1,0,-1,0] # cam_offset is for scrolling cam_world_pos_x = screen_width/2 cam_world_pos_y = screen_height/2 # set initial cam position and size renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \ screen_width, screen_height) # retrieve the layers sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources) # filter layers sprite_layers = [layer for layer in sprite_layers if not layer.is_object_group] # add the hero and monsters to the the right layer sprite_layers[1].add_sprite(hero) i = 0 for i in range(len(mon)): mon_image.append(1) sprite_layers[1].add_sprite(mon[i]) # portal for entry and exit of hero portal1=pygame.Rect(4*32,96*32,3*32,2*32) portal2=pygame.Rect(85*32,61*32,1*32,3*32) # variables for the main loop clock = pygame.time.Clock() running = True # to run while loop speed = 10 # hero speed mr=ml=md=mu=0 # image variables(change images to show movements of hero) h_drctn = 'up' # direction where hero facing right now life = 1 # to keep track wheather hero is alive or not arrow_list = [] # contains a list of arrows (currently on screen) arrow_dir = [] # directions of arrows HP_MON = [10.0]*(len(mon)) # health of monster point_arrow = 10.0 # decrease in health of enemy when arrow of hero collides with enemy point_enm = 1.0 # decrease in health of hero after collision with enemy enm_speed = 8.0 # speed of enemy savegame=pk.load(open("./save.p","rb")) old_xp = savegame['xp'] old_level = savegame['h_level'] old_armor = savegame['sheild_hp'] old_arrow = savegame['arrow_count'] # interface code c_pos=[cam_world_pos_x, cam_world_pos_y] interface=menu.create_interface(renderer,sprite_layers,screen,c_pos) hp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/hp_bar.png') # hp bar interface l_g=menu.create_l_g(renderer,sprite_layers,screen,c_pos) # level and gold interface [hp_sprite,hp]=menu.create_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos) xp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/exp_bar.png') # x interface xp_sprite=menu.create_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos) f_i=menu.create_f_i(renderer,sprite_layers,screen,c_pos) # arrow,armor,sword interface interf_fight=menu.create_interface_fight(renderer,sprite_layers,screen,c_pos) # set up timer for fps printing pygame.time.set_timer(pygame.USEREVENT, 1000) # mainloop while running: dt = clock.tick(50) savegame=pk.load(open("./save.p","rb")) # event handling for event in pygame.event.get(): if (event.type == pygame.QUIT): running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_w and savegame['arrow_count'] > 0: enemy_attack.create_arrow(arrow_list,arrow_dir,h_drctn,hero,sprite_layers) # create arrow on pressing 'w' remove_mon(HP_MON,mon,mon_dir,mon_image,sprite_layers) # remove monster sprite if health bacomes zero savegame['arrow_count'] -= 1 # decrease the arrow count and update in the dictionary pk.dump(savegame,open("./save.p","wb")) # save the changes of dictionary elif event.key == pygame.K_a: # attack with sword on pressing 'a' enemy_attack.attack(h_drctn,hero,mon,HP_MON) remove_mon(HP_MON,mon,mon_dir,mon_image,sprite_layers) # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) menu.update_f_i(f_i,c_pos) # calling hero_move() function for hero's movements mov = movements1.hero_move(mr,ml,md,mu,hero_pos_x,hero_pos_y,hero,speed,sprite_layers[3]) mr = mov[0] ml = mov[1] md = mov[2] mu = mov[3] hero_pos_x = mov[4] hero_pos_y = mov[5] # to detect the direction of hero if(mu>1): h_drctn='up' elif(mr>1): h_drctn='right' elif(md>1): h_drctn='down' elif(ml>1): h_drctn='left' # decresing the HP of player if collides any of the monsters # if HP becomes zero then start the game again for i in range(len(mon)): if pygame.sprite.collide_rect(hero,mon[i]): ouch_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(ouch_sound,0.4) if savegame['sheild_hp']>0: savegame['sheild_hp'] -= (point_enm/2) savegame['hp'] -= (point_enm/2) if savegame['sheild_hp']<0: savegame['sheild_hp'] = 0 else : savegame['hp'] -= point_enm if savegame['hp'] <= 0: sound.stop_soundfx(tunnel2_4_sound) savegame['xp'] = old_xp savegame['h_level'] = old_level savegame['sheild_hp'] = old_armor savegame['arrow_count'] = old_arrow savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) life = 0 running = False pk.dump(savegame,open("./save.p","wb")) (mon_dir,mon,mon_image) = move_mon(mon_dir,mon,mon_image,sprite_layers,enm_speed) # monsters's movement enemy_attack.remove_arrow(arrow_list,mon,HP_MON,arrow_dir,sprite_layers,point_arrow) # remove arrow if it goes out of the screen remove_mon(HP_MON,mon,mon_dir,mon_image,sprite_layers) # remove monsters menu.update_lg(l_g,c_pos) #interface update # adjust camera according to the hero's position, follow him # (don't make the hero follow the cam, maybe later you want different # objects to be followed by the cam) cam_pos_x = hero.rect.centerx cam_pos_y = hero.rect.centery if hero.rect.centerx <= 520 : cam_pos_x = 520 elif hero.rect.centerx >=2336: cam_pos_x = 2336 if hero.rect.centery >= 2875: cam_pos_y = 2875 elif hero.rect.centery <=408: cam_pos_y = 408 renderer.set_camera_position(cam_pos_x,cam_pos_y) # interface update c_pos=(cam_pos_x,cam_pos_y) interface.rect.topleft=(c_pos[0]-512,c_pos[1]-384) hp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-382) xp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-345) l_g.rect.topleft=(c_pos[0]-508,c_pos[1]-381) f_i.rect.bottomright=(c_pos[0]+500,c_pos[1]+350) interf_fight.rect.bottomright=(c_pos[0]+512,c_pos[1]+384) # next map if pygame.Rect.colliderect(hero.rect,portal1) or pygame.Rect.colliderect(hero.rect,portal2) : portal=True sound.stop_soundfx(tunnel2_4_sound) running=False # clear screen, might be left out if every pixel is redrawn anyway screen.fill((0, 0, 0)) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) # Game restarts if life == 0: demo_pygame('./maps/tunnel2_4.tmx',0) elif portal==True: shifty1.demo_pygame('./maps/village1.tmx',1)
def demo_pygame(file_name,frm): #portal portal=False # parser the map (it is done here to initialize the # window the same size as the map if it is small enough) world_map = tiledtmxloader.tmxreader.TileMapParser().parse_decode(file_name) # init pygame and set up a screen pygame.display.set_caption("tiledtmxloader - " + file_name + \ " - keys: arrows, 0-9") screen_width = min(1024, world_map.pixel_width) screen_height = min(768, world_map.pixel_height) screen = pygame.display.set_mode((screen_width, screen_height)) # load the images using pygame resources = tiledtmxloader.helperspygame.ResourceLoaderPygame() resources.load(world_map) # prepare map rendering assert world_map.orientation == "orthogonal" # renderer renderer = tiledtmxloader.helperspygame.RendererPygame() #background music mountain_top_sound = sound.create_music(file_name) sound.volume(mountain_top_sound,0.1) # create hero hero_pos_x = 3*32 hero_pos_y = 17*32 hero = person.create_person(hero_pos_x, hero_pos_y,'./images/hero_u2.png') # create enemy sprites enm = [] enm.append(person.create_person(19*32, 8*32,'./images/s1.png')) enm.append(person.create_person(19*32, 22*32,'./images/s2.png')) enm.append(person.create_person(24*32, 11*32,'./images/s1.png')) enm.append(person.create_person(26*32, 19*32,'./images/s2.png')) lizard = person.create_person(12*32, 22*32,'./images/lizard.png') # create the rectangle for portal portal1=pygame.Rect(16*32,3*32,3*32,1*32) # cam_offset is for scrolling cam_world_pos_x = 516 cam_world_pos_y = 404 # set initial cam position and size renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \ screen_width, screen_height) # retrieve the layers sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources) # filter layers sprite_layers = [layer for layer in sprite_layers if not layer.is_object_group] # add the hero and enemies the the right layer sprite_layers[1].add_sprite(hero) # variables for the main loop clock = pygame.time.Clock() running = True # to run while loop speed = 5 # hero speed mr=ml=md=mu=0 # image variables(change images to show movements of hero) flag_lizard = 0 # a variable to check when to add crab's sprite tic_flag = 0 drctn='down' # direction where hero facing right now life = 1 # to keep track wheather hero is alive or not dx_enm = [-1,1,1,-1] # list of direction in x of enemies dy_enm = [1,-1,1,-1] # list of direction in y of enemies left_enm = [15*32,16*32,21*32,23*32] # left collision coordinate for enemies(after which it will return back) right_enm = [21*32,23*32,26*32,29*32] # right collision coordinate for enemies(after which it will return back) weap_dir = [] # directions of weapons weap_list = [] # contains a list of weapons (currently on screen) arrow_list = [] # contains a list of arrows (currently on screen) arrow_dir = [] # directions of arrows gold = [] # contains a list of gold (currently on screen) heart = [] # contains a list of heart (currently on screen) flag_enm = 0 point_weapon = 1.0 # decrease in health of hero when weapon of enemy collides with hero point_arrow = 15.0 # decrease in health of enemy when arrow of hero collides with enemy lizard_attack = 10.0 # attack of lizard point_heal = 10.0 # increase in health after healing HP_LIZARD = 30.0 # health of lizard gold_increase = 5 gold_increase = 5 # increase in health of hero after getting gold heart_increase = 5.0 # increase in health of hero after getting heart speed_enm = 6 # speed of enemy HP_ENM = [100.0,100.0,100.0,100.0] # health of enemies hero_attack = 10 # hero attack defense_hero = 5 # hero defense #old variables to restore if hero dies in this map savegame=pk.load(open("save.p","rb")) old_xp = savegame['xp'] old_level = savegame['h_level'] old_gold = savegame['gold'] old_armor = savegame['sheild_hp'] old_arrow = savegame['arrow_count'] # interface code c_pos=[cam_world_pos_x, cam_world_pos_y] interface=menu.create_interface(renderer,sprite_layers,screen,c_pos) hp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/hp_bar.png') # hp bar interface l_g=menu.create_l_g(renderer,sprite_layers,screen,c_pos) # level and gold interface [hp_sprite,hp]=menu.create_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos) xp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/exp_bar.png') # xp interface xp_sprite=menu.create_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos) # arrow,armor,sword interface f_i=menu.create_f_i(renderer,sprite_layers,screen,c_pos) # arrow,armor,sword interface interf_fight=menu.create_interface_fight(renderer,sprite_layers,screen,c_pos) # set up timer for fps printing pygame.time.set_timer(pygame.USEREVENT, 1000) # mainloop while running: dt = clock.tick() savegame=pk.load(open("./save.p","rb")) #set coordinates on which turn based fighting appears if(hero_pos_x > 5*32 and flag_lizard == 0): flag_lizard = 1 elif (hero_pos_x > 16*32 and flag_enm == 0): flag_enm = 1 # event handling for event in pygame.event.get(): if (event.type == pygame.QUIT or portal == True): running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_w and ['arrow_count'] > 0 and flag_enm == 2: enemy_attack.create_arrow(arrow_list,arrow_dir,drctn,hero,sprite_layers) # create arrow on pressing 'w' enemy_attack.remove_enm(HP_ENM,enm,dx_enm,right_enm,left_enm,dy_enm,sprite_layers,gold,heart) # remove enemy sprite if health bacomes zero savegame['arrow_count'] -=1 # decrease the arrow count and update in the dictionary pk.dump(savegame,open("./save.p","wb")) # save the changes of dictionary elif event.key == pygame.K_a: # attack with sword on pressing 'a' enemy_attack.attack(drctn,hero,enm,HP_ENM) enemy_attack.remove_enm(HP_ENM,enm,dx_enm,right_enm,left_enm,dy_enm,sprite_layers,gold,heart) # remove enemy sprite if health bacomes zero # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) menu.update_f_i(f_i,c_pos) # add lizard sprite for turn based fighting if(flag_lizard == 1): sprite_layers[1].add_sprite(lizard) flag_lizard = 2 (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) # loop for turn based fighting until lizard doesn't die while(HP_LIZARD > 0 and flag_lizard==2 ): for event in pygame.event.get(): # heal on pressing 'h' if event.type == pygame.KEYDOWN and event.key == pygame.K_h and tic_flag==0: heal_sound = sound.create_soundfx('./sounds/heal.ogg') sound.volume(heal_sound,0.4) heal = person.create_person(hero.rect.centerx,hero.rect.top+11,'./images/heal_1.png') # create the heal sprite sprite_layers[2].add_sprite(heal) # add the sprite to the right layer savegame['hp'] += point_heal # increase the health and upadte in the dictionary if savegame['hp'] > savegame['max_hp']: savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) # save the changes in dictionary # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) # image update while tic_flag<=13: heal.image=pygame.image.load('./images/heal_1.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) heal.image=pygame.image.load('./images/heal_2.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) heal.image=pygame.image.load('./images/heal_3.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) tic_flag += 1 tic_flag = 25 # remove the heal sprite sprite_layers[2].remove_sprite(heal) # rendering (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) elif event.type == pygame.KEYDOWN and event.key == pygame.K_a and tic_flag==0: # hero attacks on pressing 'a' sword_sound = sound.create_soundfx('./sounds/sword.ogg') sound.volume(sword_sound,0.4) var_attack_hero = random.randint(hero_attack-2.0, hero_attack+2.0) HP_LIZARD -= var_attack_hero # lizard's health decreases enemy_attack.create_score(var_attack_hero) # lizard's image changes after hero attacks lizard.image=pygame.image.load('./images/lizard_1.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) lizard.image=pygame.image.load('./images/lizard_2.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) lizard.image=pygame.image.load('./images/lizard_3.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) # create the score sprite and add it score = person.create_person(16*32,17*32,'./images/sample-out.png') sprite_layers[1].add_sprite(score) while(tic_flag<25): score.rect.centery -= 2 tic_flag += 1 if(tic_flag == 25): sprite_layers[1].remove_sprite(score) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) lizard.image=pygame.image.load('./images/lizard.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) if tic_flag == 25: pygame.time.delay(1000) # lizard attacks var_attack_lizard = random.randint(lizard_attack-2.0,lizard_attack+2.0) var_defense_hero = random.randint(defense_hero-3.0,defense_hero) sh = (var_attack_lizard - var_defense_hero) if sh<0 : sh = 0 enemy_attack.create_score(sh) savegame['hp'] -= (sh) pk.dump(savegame,open("./save.p","wb")) # lizard's image changes when it attacks shine = 0 while shine < 10 : lizard.image=pygame.image.load('./images/lizard(1).png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) lizard.image=pygame.image.load('./images/lizard.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) shine += 1 turnbase_sound = sound.create_soundfx('./sounds/turnbase_touch.ogg') sound.volume(turnbase_sound,0.4) # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) # create the score sprite and add it score = person.create_person(hero_pos_x-32,hero_pos_y-32,'./images/sample-out.png') sprite_layers[1].add_sprite(score) while(tic_flag>0): score.rect.centery += 2 tic_flag -= 1 if(tic_flag == 0): sprite_layers[1].remove_sprite(score) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) # if lizard's HP is zero then remove its sprite if(HP_LIZARD <= 0) : sprite_layers[1].remove_sprite(lizard) HP_LIZARD = 10000.0 flag_lizard = 3 # increase the xp of player after killing the lizard if(flag_lizard == 3): savegame=pk.load(open("./save.p","rb")) savegame['xp'] +=20 pk.dump(savegame,open("./save.p","wb")) flag_lizard = 4 # add enemies after a particular coordinate if flag_enm == 1: i = 0 for i in range(len(enm)): sprite_layers[1].add_sprite(enm[i]) flag_enm = 2 # calling hero_move() function for hero's movements mov = movements1.hero_move(mr,ml,md,mu,hero_pos_x,hero_pos_y,hero,speed,sprite_layers[3]) mr = mov[0] ml = mov[1] md = mov[2] mu = mov[3] hero_pos_x = mov[4] hero_pos_y = mov[5] # to detect the direction of hero if(mu>1): drctn='up' elif(mr>1): drctn='right' elif(md>1): drctn='down' elif(ml>1): drctn='left' if flag_enm == 2: enemy_attack.create_weapons(weap_list,sprite_layers,enm,weap_dir,dy_enm,0) # create the weapons enemy_attack.move_enemy(enm,right_enm,left_enm,dx_enm,speed_enm) # random movement of enemy enemy_attack.remove_arrow(arrow_list,enm,HP_ENM,arrow_dir,sprite_layers,point_arrow) # remove arrow if it goes out of the screen enemy_attack.remove_weapon(hero,enm,weap_list,weap_dir,point_weapon,sprite_layers) # remove weapon enemy_attack.remove_enm(HP_ENM,enm,dx_enm,right_enm,left_enm,dy_enm,sprite_layers,gold,heart) # remove enemy if health of enemy becomes zero i = 0 l = len(gold) # if hero collides with gold sprite then increase the gold count and remove the sprite while(i>=0 and i<l): if pygame.sprite.collide_rect(hero,gold[i]): savegame=pk.load(open("./save.p","rb")) savegame['gold'] += gold_increase pk.dump(savegame,open("./save.p","wb")) gold_sound = sound.create_soundfx('./sounds/gold_heart.ogg') sound.volume(gold_sound,0.3) sprite_layers[1].remove_sprite(gold[i]) gold.pop(i) else: i+= 1 l = len(gold) i = 0 l = len(heart) # if hero collides with heart sprite then increase the hp and remove the sprite while(i>=0 and i<l): if pygame.sprite.collide_rect(hero,heart[i]): savegame=pk.load(open("./save.p","rb")) savegame['hp'] += heart_increase if savegame['hp'] > savegame['max_hp']: savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) heart_sound = sound.create_soundfx('./sounds/gold_heart.ogg') sound.volume(heart_sound,0.3) sprite_layers[1].remove_sprite(heart[i]) heart.pop(i) else: i+= 1 l = len(heart) if savegame['hp'] <= 0: # game starts again if player dies sound.stop_soundfx(mountain_top_sound) savegame['xp'] = old_xp savegame['h_level'] = old_level savegame['gold'] = old_gold savegame['sheild_hp'] = old_armor savegame['arrow_count'] = old_arrow savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) life = 0 running = False elif len(enm)==0 and len(heart)==0 and len(gold)==0 and HP_LIZARD==10000.0: # another map starts l = len(weap_list) while(i>=0 and i<l): sprite_layers[2].remove_sprite(weap_list[i]) weap_list.pop(i) l = len(enm) i = 0 l = len(arrow_list) while(i>=0 and i<l): sprite_layers[1].remove_sprite(arrow_list[i]) arrow_list.pop(i) l = len(arrow_list) sound.stop_soundfx(mountain_top_sound) render_update(renderer,sprite_layers,screen) portal=True # adjust camera according to the hero's position, follow him cam_pos_x = hero.rect.centerx cam_pos_y = hero.rect.centery if hero.rect.centerx <= 516 : cam_pos_x = 516 elif hero.rect.centerx >=766: cam_pos_x = 766 if hero.rect.centery <=404: cam_pos_y = 404 elif hero.rect.centery >=570: cam_pos_y = 570 renderer.set_camera_position(cam_pos_x,cam_pos_y) # adjust camera according to the hero's position, follow him c_pos=(cam_pos_x,cam_pos_y) interface.rect.topleft=(c_pos[0]-512,c_pos[1]-384) hp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-382) xp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-345) l_g.rect.topleft=(c_pos[0]-508,c_pos[1]-381) f_i.rect.bottomright=(c_pos[0]+500,c_pos[1]+350) interf_fight.rect.bottomright=(c_pos[0]+512,c_pos[1]+380) # clear screen, might be left out if every pixel is redrawn anyway screen.fill((0, 0, 0)) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) if life == 0: demo_pygame('./maps/mountain_top.tmx',0) elif portal==True: pal_lava.main()
def demo_pygame(file_name,frm): # parser the map (it is done here to initialize the # window the same size as the map if it is small enough) world_map = tiledtmxloader.tmxreader.TileMapParser().parse_decode(file_name) # init pygame and set up a screen pygame.display.set_caption("tiledtmxloader - " + file_name + \ " - keys: arrows, 0-9") screen_width = min(1024, world_map.pixel_width) screen_height = min(768, world_map.pixel_height) screen = pygame.display.set_mode((screen_width, screen_height)) # load the images using pygame resources = tiledtmxloader.helperspygame.ResourceLoaderPygame() resources.load(world_map) # prepare map rendering assert world_map.orientation == "orthogonal" # renderer renderer = tiledtmxloader.helperspygame.RendererPygame() #background music tunnel3_sound = sound.create_music(file_name) sound.volume(tunnel3_sound,0.1) # create hero,enemy sprites hero_pos_x = 12*32 hero_pos_y = 3*32 hero = person.create_person(hero_pos_x, hero_pos_y,'./images/hero_u2.png') # create enemy,crabs and ring sprites enm1 = person.create_person(6*32, 22*32,'./images/slime_d1.png') enm2 = person.create_person(16*32, 22*32,'./images/slime_d1.png') crab = person.create_person(30*32, 38*32,'./images/crab.png') crab1 = person.create_person(49*32, 38*32,'./images/crab.png') ring = person.create_person(49*32, 33*32,'./images/ring.png') # cam_offset is for scrolling cam_world_pos_x = screen_width cam_world_pos_y = screen_height # set initial cam position and size renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \ screen_width, screen_height) # retrieve the layers sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources) # filter layers sprite_layers = [layer for layer in sprite_layers if not layer.is_object_group] # add the hero and enemies to the right layer sprite_layers[1].add_sprite(hero) sprite_layers[1].add_sprite(enm1) sprite_layers[1].add_sprite(enm2) # variables for the main loop clock = pygame.time.Clock() running = True # to run while loop speed = 7 # hero speed mr=ml=md=mu=0 # image variables(change images to show movements of hero) flag_crab = 0 # a variable to check when to add crab's sprite drctn='down' # direction where hero facing right now life = 1 # to keep track wheather hero is alive or not enm =[enm1,enm2] # list of enemy sprites dx_enm = [1,1] # list of direction in x of enemies dy_enm = [-1,-1] # list of direction in y of enemies left_enm = [1*32,9*32] # left collision coordinate for enemies(after which it will return back) right_enm = [12*32,21*32] # right collision coordinate for enemies(after which it will return back) weap_dir = [] # directions of weapons weap_list = [] # contains a list of weapons (currently on screen) arrow_list = [] # contains a list of arrows (currently on screen) arrow_dir = [] # directions of arrows # contains a list of enemy images enmimages = ['./images/slime_d1.png','./images/slime_d2.png','./images/slime_d3.png','./images/slime_u1.png','./images/slime_u2.png','./images/slime_u3.png'] gold = [] # contains a list of gold (currently on screen) heart = [] # contains a list of heart (currently on screen) image_variable = [0,0] flag_ring = 0 tic_flag = 0 x = 0 portal1 = pygame.Rect(58*32,28*32,2*32,8*32) portal=False point_weapon = 2.0 # decrease in health of hero when weapon of enemy collides with hero point_arrow = 15.0 # decrease in health of enemy when arrow of hero collides with enemy crab_attack = 10.0 # attack of crab point_heal = 20.0 # increase in health after healing HP_CRAB = 30.0 # health of crab gold_increase = 5 # increase in health of hero after getting gold heart_increase = 5.0 # increase in health of hero after getting heart speed_enm = 6 # speed of enemy HP_ENM = [100.0,100.0] # health of enemies hero_attack = 10 # hero attack defense_hero = 5 # hero defense #old variables to restore if hero dies in this map savegame=pk.load(open("./save.p","rb")) old_xp = savegame['xp'] old_level = savegame['h_level'] old_gold = savegame['gold'] old_armor = savegame['sheild_hp'] old_arrow = savegame['arrow_count'] # interface code c_pos=[cam_world_pos_x, cam_world_pos_y] interface=menu.create_interface(renderer,sprite_layers,screen,c_pos) hp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/hp_bar.png') # hp bar interface l_g=menu.create_l_g(renderer,sprite_layers,screen,c_pos) # level and gold interface [hp_sprite,hp]=menu.create_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos) xp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/exp_bar.png') # xp interface xp_sprite=menu.create_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos) f_i=menu.create_f_i(renderer,sprite_layers,screen,c_pos) # arrow,armor,sword interface interf_fight=menu.create_interface_fight(renderer,sprite_layers,screen,c_pos) # set up timer for fps printing pygame.time.set_timer(pygame.USEREVENT, 1000) # mainloop while running: dt = clock.tick(50) savegame=pk.load(open("./save.p","rb")) #set coordinates on which turn based fighting appears if(hero_pos_x > 24*32 and flag_crab == 0 and len(enm)==0): flag_crab = 1 if(hero_pos_x > 43*32 and flag_crab == 2): flag_crab = 3 # event handling for event in pygame.event.get(): if (event.type == pygame.QUIT): running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_w and savegame['arrow_count'] > 0: enemy_attack.create_arrow(arrow_list,arrow_dir,drctn,hero,sprite_layers) # create arrow on pressing 'w' enemy_attack.remove_enm(HP_ENM,enm,dx_enm,right_enm,left_enm,dy_enm,sprite_layers,gold,heart) # remove enemy sprite if health bacomes zero savegame['arrow_count'] -=1 # decrease the arrow count and update in the dictionary pk.dump(savegame,open("./save.p","wb")) # save the changes of dictionary elif event.key == pygame.K_a: # attack with sword on pressing 'a' enemy_attack.attack(drctn,hero,enm,HP_ENM) enemy_attack.remove_enm(HP_ENM,enm,dx_enm,right_enm,left_enm,dy_enm,sprite_layers,gold,heart) # remove enemy sprite if health bacomes zero # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) menu.update_f_i(f_i,c_pos) # if one enemy dies then other enemy's walking area increases(other enemy covers the region of previous enemy) if len(enm)==1: right_enm[0] = 19*32 left_enm[0] = 2*32 # add crab sprite for turn based fighting if(flag_crab == 1): sprite_layers[1].add_sprite(crab) flag_crab = 2 x = 35 (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) elif(flag_crab == 3): crab = crab1 x = 54 sprite_layers[1].add_sprite(crab) HP_CRAB = 30.0 flag_crab = 4 (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) # loop for turn based fighting until crab doesn't die while(HP_CRAB > 0 and (flag_crab==2 or flag_crab==4)): for event in pygame.event.get(): # heal on pressing 'h' if event.type == pygame.KEYDOWN and event.key == pygame.K_h and tic_flag==0: heal_sound = sound.create_soundfx('./sounds/heal.ogg') sound.volume(heal_sound,0.4) heal = person.create_person(hero.rect.centerx,hero.rect.top+11,'./images/heal_1.png') # create the heal sprite sprite_layers[2].add_sprite(heal) # add the sprite to the right layer savegame['hp'] += point_heal # increase the health and upadte in the dictionary if savegame['hp'] > savegame['max_hp']: savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) # save the changes in dictionary # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) # image update while tic_flag<=13: heal.image=pygame.image.load('./images/heal_1.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) heal.image=pygame.image.load('./images/heal_2.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) heal.image=pygame.image.load('./images/heal_3.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) tic_flag += 1 tic_flag = 25 # remove the heal sprite sprite_layers[2].remove_sprite(heal) # rendering (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) elif event.type == pygame.KEYDOWN and event.key == pygame.K_a and tic_flag==0: # hero attacks on pressing 'a' sword_sound = sound.create_soundfx('./sounds/sword.ogg') sound.volume(sword_sound,0.4) var_attack_hero = random.randint(hero_attack-2.0, hero_attack+2.0) HP_CRAB -= var_attack_hero # crab's health decreases enemy_attack.create_score(var_attack_hero) # crab's image changes after hero attacks crab.image=pygame.image.load('./images/crab_1.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) crab.image=pygame.image.load('./images/crab_2.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) crab.image=pygame.image.load('./images/crab_3.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) crab.image=pygame.image.load('./images/crab_3.png') # create the score sprite and add it score = person.create_person(x*32,32*32,'./images/sample-out.png') sprite_layers[1].add_sprite(score) while(tic_flag<25): score.rect.centery -= 2 tic_flag += 1 if(tic_flag == 25): sprite_layers[1].remove_sprite(score) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) crab.image=pygame.image.load('./images/crab.png') if tic_flag == 25: pygame.time.delay(1000) # crab attacks var_attack_crab = random.randint(crab_attack-2.0,crab_attack+2.0) var_defense_hero = random.randint(defense_hero-3.0,defense_hero) sh = (var_attack_crab - var_defense_hero) if sh<0 : sh = 0 enemy_attack.create_score(sh) savegame['hp'] -= (sh) pk.dump(savegame,open("./save.p","wb")) # crab's image changes when it attacks shine = 0 while shine < 10 : crab.image=pygame.image.load('./images/crab(1).png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) crab.image=pygame.image.load('./images/crab.png') (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) shine += 1 turnbase_sound = sound.create_soundfx('./sounds/turnbase_touch.ogg') sound.volume(turnbase_sound,0.4) # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) # create the score sprite and add it score = person.create_person(hero_pos_x-32,hero_pos_y-32,'./images/sample-out.png') sprite_layers[1].add_sprite(score) while(tic_flag>0): score.rect.centery += 2 tic_flag -= 1 if(tic_flag == 0): sprite_layers[1].remove_sprite(score) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) # if crab's HP is zero then remove its sprite if(HP_CRAB <= 0) : sprite_layers[1].remove_sprite(crab) # add the ring sprite when second crab dies if(x==54): sprite_layers[1].add_sprite(ring) # increase the xp of player after killing the crab if(x==35 or x==54): savegame['xp'] +=15 pk.dump(savegame,open("./save.p","wb")) x = 30 # remove sprite when hero collides ring if pygame.sprite.collide_rect(hero,ring) and flag_ring!=1: ring_sound = sound.create_soundfx('./sounds/gold_heart.ogg') sound.volume(ring_sound,0.5) sprite_layers[1].remove_sprite(ring) flag_ring = 1 ring_take=person.create_person(hero.rect.centerx,hero.rect.centery+200,'./images/ring_take.png') sprite_layers[2].add_sprite(ring_take) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) i=0 while(i!=1): # Infinite loop until menu is to be removed for event in pygame.event.get(): if (event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE): i=1 continue sprite_layers[2].remove_sprite(ring_take) # calling hero_move() function for hero's movements mov = movements1.hero_move(mr,ml,md,mu,hero_pos_x,hero_pos_y,hero,speed,sprite_layers[3]) mr = mov[0] ml = mov[1] md = mov[2] mu = mov[3] hero_pos_x = mov[4] hero_pos_y = mov[5] # to detect the direction of hero if(mu>1): drctn='up' elif(mr>1): drctn='right' elif(md>1): drctn='down' elif(ml>1): drctn='left' enemy_attack.create_weapons(weap_list,sprite_layers,enm,weap_dir,dy_enm,0) # create the weapons enemy_attack.move_enemy(enm,right_enm,left_enm,dx_enm,speed_enm) # random movement of enemy enemy_attack.dir_update(enm,hero,dy_enm) # direction update of enemy enemy_attack.image_update(enm,image_variable,dy_enm,enmimages) # image update of enemy enemy_attack.remove_arrow(arrow_list,enm,HP_ENM,arrow_dir,sprite_layers,point_arrow) # remove arrow if it goes out of the screen enemy_attack.remove_enm(HP_ENM,enm,dx_enm,right_enm,left_enm,dy_enm,sprite_layers,gold,heart) # remove enemy if health of enemy becomes zero enemy_attack.remove_weapon(hero,enm,weap_list,weap_dir,point_weapon,sprite_layers) # remove weapon # interface update menu.update_f_i(f_i,c_pos) i = 0 l = len(gold) # if hero collides with gold sprite then increase the gold count and remove the sprite while(i>=0 and i<l): if pygame.sprite.collide_rect(hero,gold[i]): savegame=pk.load(open("./save.p","rb")) savegame['gold'] += gold_increase pk.dump(savegame,open("./save.p","wb")) gold_sound = sound.create_soundfx('./sounds/gold_heart.ogg') sound.volume(gold_sound,0.3) sprite_layers[1].remove_sprite(gold[i]) gold.pop(i) else: i+= 1 l = len(gold) # if hero collides with heart sprite then increase the hp and remove the sprite i = 0 l = len(heart) while(i>=0 and i<l): if pygame.sprite.collide_rect(hero,heart[i]): savegame=pk.load(open("./save.p","rb")) savegame['hp'] += heart_increase if savegame['hp'] > savegame['max_hp']: savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) heart_sound = sound.create_soundfx('./sounds/gold_heart.ogg') sound.volume(heart_sound,0.3) sprite_layers[1].remove_sprite(heart[i]) heart.pop(i) else: i+= 1 l = len(heart) # game starts again if player dies if savegame['hp'] <= 0: sound.stop_soundfx(tunnel3_sound) savegame['xp'] = old_xp savegame['h_level'] = old_level savegame['gold'] = old_gold savegame['sheild_hp'] = old_armor savegame['arrow_count'] = old_arrow savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) life = 0 running = False # another map starts if (flag_ring==1 and pygame.Rect.colliderect(hero.rect,portal1)): portal=True sound.stop_soundfx(tunnel3_sound) running=False # adjust camera according to the hero's position, follow him cam_pos_x = hero.rect.centerx cam_pos_y = hero.rect.centery if hero.rect.centerx <= 524 : cam_pos_x = 524 elif hero.rect.centerx >=1459: cam_pos_x = 1459 if hero.rect.centery <=406: cam_pos_y = 406 renderer.set_camera_position(cam_pos_x,cam_pos_y) # interface update c_pos=(cam_pos_x,cam_pos_y) interface.rect.topleft=(c_pos[0]-512,c_pos[1]-384) hp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-382) xp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-345) l_g.rect.topleft=(c_pos[0]-508,c_pos[1]-381) f_i.rect.bottomright=(c_pos[0]+500,c_pos[1]+350) interf_fight.rect.bottomright=(c_pos[0]+512,c_pos[1]+384) # clear screen, might be left out if every pixel is redrawn anyway screen.fill((0, 0, 0)) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) if life == 0: demo_pygame('./maps/tunnel3.tmx',0) elif portal==True: shifty1.demo_pygame('./maps/ship.tmx',0)
def demo_pygame(file_name,frm): # parser the map (it is done here to initialize the # window the same size as the map if it is small enough) world_map = tiledtmxloader.tmxreader.TileMapParser().parse_decode(file_name) # init pygame and set up a screen pygame.display.set_caption("tiledtmxloader - " + file_name + \ " - keys: arrows, 0-9") screen_width = min(1024, world_map.pixel_width) screen_height = min(768, world_map.pixel_height) screen = pygame.display.set_mode((screen_width, screen_height)) # load the images using pygame resources = tiledtmxloader.helperspygame.ResourceLoaderPygame() resources.load(world_map) # prepare map rendering assert world_map.orientation == "orthogonal" # renderer renderer = tiledtmxloader.helperspygame.RendererPygame() #background music battlefield_sound = sound.create_music(file_name) # create hero hero_pos_x = 10*32 hero_pos_y = 41*32 hero = person.create_person(hero_pos_x, hero_pos_y,'./images/hero_u2.png') # create enemy sprites enm = [] enm.append(person.create_person(10*32, 3*32,'./images/haabu_d1.png')) enm.append(person.create_person(14*32, 3*32,'./images/haabu_d1.png')) enm.append(person.create_person(10*32, 7*32,'./images/haabu_d1.png')) enm.append(person.create_person(14*32, 7*32,'./images/haabu_d1.png')) # create dragon sprite dragon = person.create_person(10*32, 27*32,'./images/dragon_d1.png') # cam_offset is for scrolling cam_world_pos_x = 520 cam_world_pos_y = 1050 # set initial cam position and size renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \ screen_width, screen_height) # retrieve the layers sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources) # filter layers sprite_layers = [layer for layer in sprite_layers if not layer.is_object_group] # add the hero and enemies the the right layer sprite_layers[1].add_sprite(hero) sprite_layers[1].add_sprite(dragon) i = 0 for i in range(len(enm)): sprite_layers[1].add_sprite(enm[i]) # variables for the main loop clock = pygame.time.Clock() running = True # to run while loop speed = 7 # hero speed mr=ml=md=mu=0 # image variables(change images to show movements of hero) tic_flag = 0 drctn='down' # direction where hero facing right now life = 1 # to keep track wheather hero is alive or not arrow_list = [] # contains a list of arrows (currently on screen) arrow_dir = [] # directions of arrows enm_dir = [0,0,0,0] drag = [dragon] dx_dragon = [0] image_variable = [0] weap_dir = [] # directions of weapons weap_list = [] # contains a list of weapons (currently on screen) color_change = 0 # a color variable for dragon dy_dragon = [1] # dragon direction specialarrow_list = [] # contains a list of specialarrows (currently on screen) specialarrow_dir = [] # directions of specialarrows dragonimages = ['./images/dragon_d1.png','./images/dragon_d2.png','./images/dragon_d3.png','./images/dragon_u1.png','./images/dragon_u2.png','./images/dragon_u3.png'] enemyimages = ['./images/haabu_d1.png','./images/haabu_d2.png','./images/haabu_d3.png','./images/haabu_u1.png','./images/haabu_u2.png','./images/haabu_u3.png'] chk = 0 HP_ENM = [100.0,100.0,100.0,100.0] # health of enemies HP_DRAGON = [100.0] # health of dragon SPECIALARROW = 5 point_weapon = 1.0 # decrease in health of hero when weapon OR haabu collides with hero ## HAABU MEANS ENEMY (in HINDI) point_arrow = 20.0 # arrow enemy collision point_dragon = 5.0 # hero dragon collision point_specialarrow = 10.0 # specialarrow hero collision haabu_speed = 6 dragon_speed = 8 #old variables to restore if hero dies in this map savegame=pk.load(open("./save.p","rb")) old_xp = savegame['xp'] old_level = savegame['h_level'] old_armor = savegame['sheild_hp'] old_arrow = savegame['arrow_count'] # interface code c_pos=[cam_world_pos_x, cam_world_pos_y] interface=menu.create_interface(renderer,sprite_layers,screen,c_pos) hp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/hp_bar.png') # hp bar interface l_g=menu.create_l_g(renderer,sprite_layers,screen,c_pos) # level and gold interface [hp_sprite,hp]=menu.create_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos) xp_sprite=person.create_person(c_pos[0],c_pos[1],'./images/exp_bar.png') # xp interface xp_sprite=menu.create_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos) s_a=menu.create_s_a(renderer,sprite_layers,screen,c_pos) f_i=menu.create_f_i(renderer,sprite_layers,screen,c_pos) # arrow,armor,sword interface interf_fight=menu.create_interface_fight(renderer,sprite_layers,screen,c_pos) spec_interf=menu.create_interface_spec_arrow(renderer,sprite_layers,screen,c_pos) d_hp=menu.create_interface_dragon(renderer,sprite_layers,screen,c_pos) dhp_sprite=person.create_person(c_pos[0]-400,c_pos[1],'./images/dragon_health.png') #dragon health interface dhp_sprite=menu.create_dragon_hp(renderer,sprite_layers,screen,dhp_sprite,c_pos,HP_DRAGON) # set up timer for fps printing pygame.time.set_timer(pygame.USEREVENT, 1000) # mainloop while running: dt = clock.tick(70) savegame=pk.load(open("./save.p","rb")) # keep adding a group of four enemies after dying previous group if len(enm)==0: color_change += 1 enm.append(person.create_person(10*32, 3*32,'./images/haabu_d1.png')) enm.append(person.create_person(14*32, 3*32,'./images/haabu_d1.png')) enm.append(person.create_person(10*32, 7*32,'./images/haabu_d1.png')) enm.append(person.create_person(14*32, 7*32,'./images/haabu_d1.png')) HP_ENM = [100.0,100.0,100.0,100.0] enm_dir = [0,0,0,0] i = 0 for i in range(len(enm)): sprite_layers[1].add_sprite(enm[i]) # dragon can be killed only if it is of orange color and only by specialarrows # five specialarrows are provided when dragon becomes orange if color_change%2==0: SPECIALARROW = 0 chk = 0 dragonimages = ['./images/dragon_d1.png','./images/dragon_d2.png','./images/dragon_d3.png','./images/dragon_u1.png','./images/dragon_u2.png','./images/dragon_u3.png'] else: if chk == 0: SPECIALARROW = 5 chk = 1 dragonimages = ['./images/orange_d1.png','./images/orange_d2.png','./images/orange_d3.png','./images/orange_u1.png','./images/orange_u2.png','./images/orange_u3.png'] #spec_arrow_interf menu.update_s_a(s_a,c_pos,SPECIALARROW) # event handling for event in pygame.event.get(): if (event.type == pygame.QUIT): running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_w and savegame['arrow_count'] > 0: enemy_attack.create_arrow(arrow_list,arrow_dir,drctn,hero,sprite_layers) # create arrow on pressing 'w' haabu_remove(enm,HP_ENM,sprite_layers,enm_dir) # remove enemy sprite if health bacomes zero savegame['arrow_count'] -=1 # decrease the arrow count and update in the dictionary pk.dump(savegame,open("./save.p","wb")) # save the changes of dictionary elif event.key == pygame.K_a: # attack with sword on pressing 'a' enemy_attack.attack(drctn,hero,enm,HP_ENM) haabu_remove(enm,HP_ENM,sprite_layers,enm_dir) elif event.key == pygame.K_s and color_change%2!=0 and SPECIALARROW > 0: # attack with specialarrow on pressing 's' enemy_attack.create_specialarrow(specialarrow_list,specialarrow_dir,drctn,hero,sprite_layers) SPECIALARROW -= 1 # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) menu.update_f_i(f_i,c_pos) menu.update_dhp_bar(renderer,sprite_layers,screen,dhp_sprite,c_pos,HP_DRAGON) if(color_change%2==1): menu.update_s_a(s_a,c_pos,SPECIALARROW) # calling hero_move() function for hero's movements mov = movements1.hero_move(mr,ml,md,mu,hero_pos_x,hero_pos_y,hero,speed,sprite_layers[3]) mr = mov[0] ml = mov[1] md = mov[2] mu = mov[3] hero_pos_x = mov[4] hero_pos_y = mov[5] # to detect the direction of hero if(mu>1): drctn='up' elif(mr>1): drctn='right' elif(md>1): drctn='down' elif(ml>1): drctn='left' haabu_move(hero,enm,enm_dir,enemyimages,haabu_speed) # random movement of enemy haabu_remove(enm,HP_ENM,sprite_layers,enm_dir)# remove enemy if health of enemy becomes zero if(color_change%2==0):enemy_attack.create_weapons(weap_list,sprite_layers,drag,weap_dir,dy_dragon,1) # create the weapons only if it is not of orange color enemy_attack.remove_weapon(hero,enm,weap_list,weap_dir,point_weapon,sprite_layers) # interface update menu.update_lg(l_g,c_pos) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) menu.update_xp_bar(renderer,sprite_layers,screen,xp_sprite,c_pos,0) menu.update_f_i(f_i,c_pos) menu.update_dhp_bar(renderer,sprite_layers,screen,dhp_sprite,c_pos,HP_DRAGON) enemy_attack.move_enemy(drag,[19*32],[1*32],dx_dragon,dragon_speed)# random movement of dragon enemy_attack.dir_update(drag,hero,dy_dragon) # direction update of enemy enemy_attack.image_update(drag,image_variable,dy_dragon,dragonimages) # image update of enemy enemy_attack.remove_arrow(arrow_list,enm,HP_ENM,arrow_dir,sprite_layers,point_arrow) # removes arrow from the screen enemy_attack.remove_arrow(specialarrow_list,drag,HP_DRAGON,specialarrow_dir,sprite_layers,point_specialarrow) # removes specialarrow from the screen # decrease health of hero if it colldes with dragon if pygame.sprite.collide_rect(hero,drag[0]): weap_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(weap_sound,0.4) savegame=pk.load(open("./save.p","rb")) savegame['hp'] -= point_dragon pk.dump(savegame,open("./save.p","wb")) # game starts again if player dies if savegame['hp'] <= 0: sound.stop_soundfx(battlefield_sound) savegame['xp'] = old_xp savegame['h_level'] = old_level savegame['sheild_hp'] = old_armor savegame['arrow_count'] = old_arrow savegame['hp'] = savegame['max_hp'] pk.dump(savegame,open("./save.p","wb")) menu.update_hp_bar(renderer,sprite_layers,screen,hp_sprite,c_pos,0) life = 0 running = False # Game ends! # clean the screen! if HP_DRAGON[0] <= 0: i = 0 l = len(enm) while(i>=0 and i<l): sprite_layers[1].remove_sprite(enm[i]) enm.pop(i) l = len(enm) i = 0 l = len(weap_list) while(i>=0 and i<l): sprite_layers[2].remove_sprite(weap_list[i]) weap_list.pop(i) l = len(weap_list) i = 0 l = len(arrow_list) while(i>=0 and i<l): sprite_layers[1].remove_sprite(arrow_list[i]) arrow_list.pop(i) l = len(arrow_list) i = 0 l = len(specialarrow_list) while(i>=0 and i<l): sprite_layers[1].remove_sprite(specialarrow_list[i]) specialarrow_list.pop(i) l = len(specialarrow_list) sprite_layers[1].remove_sprite(drag[0]) render_update(renderer,sprite_layers,screen) sound.stop_soundfx(battlefield_sound) running = False life = 2 # adjust camera according to the hero's position, follow him cam_pos_x = hero.rect.centerx cam_pos_y = hero.rect.centery if hero.rect.centerx <= 520 : cam_pos_x = 520 if hero.rect.centery >=1050: cam_pos_y = 1050 elif hero.rect.centery <=407: cam_pos_y = 407 renderer.set_camera_position(cam_pos_x, cam_pos_y) # interface update c_pos=(cam_pos_x,cam_pos_y) interface.rect.topleft=(c_pos[0]-512,c_pos[1]-384) hp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-382) xp_sprite.rect.topleft=(c_pos[0]-400,c_pos[1]-345) l_g.rect.topleft=(c_pos[0]-508,c_pos[1]-381) f_i.rect.bottomright=(c_pos[0]+500,c_pos[1]+350) interf_fight.rect.bottomright=(c_pos[0]+512,c_pos[1]+384) dhp_sprite.rect.midbottom=(c_pos[0],c_pos[1]+350) d_hp.rect.midbottom=(c_pos[0],c_pos[1]+350) spec_interf.rect.topleft=(c_pos[0]-512,c_pos[1]-240) s_a.rect.topleft=(c_pos[0]-540,c_pos[1]-280) # clear screen, might be left out if every pixel is redrawn anyway screen.fill((0, 0, 0)) (renderer,sprite_layers,screen) = render_update(renderer,sprite_layers,screen) if life == 0: demo_pygame('./maps/palace_final.tmx',0) elif life ==2: # Game ends! # Quit the window after pressing 'Esc' running = True ending_sound = sound.create_soundfx('./sounds/ending.ogg') sound.volume(ending_sound,1) while running : for event in pygame.event.get(): if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE ): sound.stop_soundfx(ending_sound) running = False pygame.quit()
def demo_pygame(file_name, frm): # parser the map (it is done here to initialize the # window the same size as the map if it is small enough) world_map = tiledtmxloader.tmxreader.TileMapParser().parse_decode( file_name) # init pygame and set up a screen pygame.display.set_caption("tiledtmxloader - " + file_name + \ " - keys: arrows, 0-9") screen_width = min(1024, world_map.pixel_width) screen_height = min(768, world_map.pixel_height) screen = pygame.display.set_mode((screen_width, screen_height)) # load the images using pygame resources = tiledtmxloader.helperspygame.ResourceLoaderPygame() resources.load(world_map) # prepare map rendering assert world_map.orientation == "orthogonal" # renderer renderer = tiledtmxloader.helperspygame.RendererPygame() #background music tunnel2_4_sound = sound.create_music(file_name) sound.volume(tunnel2_4_sound, 0.2) # create hero sprite if frm == 0: hero_pos_x = 5 * 32 hero_pos_y = 95 * 32 else: hero_pos_x = 81 * 32 hero_pos_y = 62 * 32 hero = person.create_person(hero_pos_x, hero_pos_y, './images/hero_u2.png') # create monster sprites mon = [] mon_dir = [] mon_image = [] mon.append(person.create_person(16 * 32, 82 * 32, './images/slime14.png')) mon.append(person.create_person(39 * 32, 82 * 32, './images/slime14.png')) mon.append(person.create_person(50 * 32, 82 * 32, './images/slime14.png')) mon.append(person.create_person(60 * 32, 82 * 32, './images/slime14.png')) mon.append(person.create_person(67 * 32, 82 * 32, './images/slime14.png')) mon.append(person.create_person(5 * 32, 74 * 32, './images/slime14.png')) mon.append(person.create_person(5 * 32, 47 * 32, './images/slime14.png')) mon.append(person.create_person(14 * 32, 47 * 32, './images/slime14.png')) mon.append(person.create_person(23 * 32, 33 * 32, './images/slime14.png')) mon.append(person.create_person(23 * 32, 20 * 32, './images/slime14.png')) mon.append(person.create_person(20 * 32, 8 * 32, './images/slime14.png')) mon.append(person.create_person(43 * 32, 8 * 32, './images/slime14.png')) mon.append(person.create_person(43 * 32, 24 * 32, './images/slime14.png')) mon.append(person.create_person(43 * 32, 41 * 32, './images/slime14.png')) mon.append(person.create_person(49 * 32, 47 * 32, './images/slime14.png')) mon.append(person.create_person(66 * 32, 47 * 32, './images/slime14.png')) mon.append(person.create_person(72 * 32, 32 * 32, './images/slime14.png')) mon.append(person.create_person(72 * 32, 23 * 32, './images/slime14.png')) mon.append(person.create_person(72 * 32, 8 * 32, './images/slime14.png')) mon.append(person.create_person(81 * 32, 8 * 32, './images/slime14.png')) mon.append(person.create_person(81 * 32, 19 * 32, './images/slime14.png')) mon.append(person.create_person(81 * 32, 36 * 32, './images/slime14.png')) mon.append(person.create_person(81 * 32, 49 * 32, './images/slime14.png')) mon.append(person.create_person(67 * 32, 64 * 32, './images/slime14.png')) mon.append(person.create_person(82 * 32, 64 * 32, './images/slime14.png')) #list to save the direcion constraints for enemy's movements mon_dir = [ -1, 0, -1, 0, 1, 0, 1, 0, 1, 0, 0, -1, 1, 0, -1, 0, 0, -1, 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 1, 0, -1, 0, 0, -1, 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 0, 1, 1, 0, -1, 0 ] # cam_offset is for scrolling cam_world_pos_x = screen_width / 2 cam_world_pos_y = screen_height / 2 # set initial cam position and size renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \ screen_width, screen_height) # retrieve the layers sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources) # filter layers sprite_layers = [ layer for layer in sprite_layers if not layer.is_object_group ] # add the hero and monsters to the the right layer sprite_layers[1].add_sprite(hero) i = 0 for i in range(len(mon)): mon_image.append(1) sprite_layers[1].add_sprite(mon[i]) # portal for entry and exit of hero portal1 = pygame.Rect(4 * 32, 96 * 32, 3 * 32, 2 * 32) portal2 = pygame.Rect(85 * 32, 61 * 32, 1 * 32, 3 * 32) # variables for the main loop clock = pygame.time.Clock() running = True # to run while loop speed = 10 # hero speed mr = ml = md = mu = 0 # image variables(change images to show movements of hero) h_drctn = 'up' # direction where hero facing right now life = 1 # to keep track wheather hero is alive or not arrow_list = [] # contains a list of arrows (currently on screen) arrow_dir = [] # directions of arrows HP_MON = [10.0] * (len(mon)) # health of monster point_arrow = 10.0 # decrease in health of enemy when arrow of hero collides with enemy point_enm = 1.0 # decrease in health of hero after collision with enemy enm_speed = 8.0 # speed of enemy savegame = pk.load(open("./save.p", "rb")) old_xp = savegame['xp'] old_level = savegame['h_level'] old_armor = savegame['sheild_hp'] old_arrow = savegame['arrow_count'] # interface code c_pos = [cam_world_pos_x, cam_world_pos_y] interface = menu.create_interface(renderer, sprite_layers, screen, c_pos) hp_sprite = person.create_person(c_pos[0], c_pos[1], './images/hp_bar.png') # hp bar interface l_g = menu.create_l_g(renderer, sprite_layers, screen, c_pos) # level and gold interface [hp_sprite, hp] = menu.create_hp_bar(renderer, sprite_layers, screen, hp_sprite, c_pos) xp_sprite = person.create_person(c_pos[0], c_pos[1], './images/exp_bar.png') # x interface xp_sprite = menu.create_xp_bar(renderer, sprite_layers, screen, xp_sprite, c_pos) f_i = menu.create_f_i(renderer, sprite_layers, screen, c_pos) # arrow,armor,sword interface interf_fight = menu.create_interface_fight(renderer, sprite_layers, screen, c_pos) # set up timer for fps printing pygame.time.set_timer(pygame.USEREVENT, 1000) # mainloop while running: dt = clock.tick(50) savegame = pk.load(open("./save.p", "rb")) # event handling for event in pygame.event.get(): if (event.type == pygame.QUIT): running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_w and savegame['arrow_count'] > 0: enemy_attack.create_arrow( arrow_list, arrow_dir, h_drctn, hero, sprite_layers) # create arrow on pressing 'w' remove_mon( HP_MON, mon, mon_dir, mon_image, sprite_layers ) # remove monster sprite if health bacomes zero savegame[ 'arrow_count'] -= 1 # decrease the arrow count and update in the dictionary pk.dump(savegame, open("./save.p", "wb")) # save the changes of dictionary elif event.key == pygame.K_a: # attack with sword on pressing 'a' enemy_attack.attack(h_drctn, hero, mon, HP_MON) remove_mon(HP_MON, mon, mon_dir, mon_image, sprite_layers) # interface update menu.update_lg(l_g, c_pos) menu.update_hp_bar(renderer, sprite_layers, screen, hp_sprite, c_pos, 0) menu.update_xp_bar(renderer, sprite_layers, screen, xp_sprite, c_pos, 0) menu.update_f_i(f_i, c_pos) # calling hero_move() function for hero's movements mov = movements1.hero_move(mr, ml, md, mu, hero_pos_x, hero_pos_y, hero, speed, sprite_layers[3]) mr = mov[0] ml = mov[1] md = mov[2] mu = mov[3] hero_pos_x = mov[4] hero_pos_y = mov[5] # to detect the direction of hero if (mu > 1): h_drctn = 'up' elif (mr > 1): h_drctn = 'right' elif (md > 1): h_drctn = 'down' elif (ml > 1): h_drctn = 'left' # decresing the HP of player if collides any of the monsters # if HP becomes zero then start the game again for i in range(len(mon)): if pygame.sprite.collide_rect(hero, mon[i]): ouch_sound = sound.create_soundfx('./sounds/weapon_touch.ogg') sound.volume(ouch_sound, 0.4) if savegame['sheild_hp'] > 0: savegame['sheild_hp'] -= (point_enm / 2) savegame['hp'] -= (point_enm / 2) if savegame['sheild_hp'] < 0: savegame['sheild_hp'] = 0 else: savegame['hp'] -= point_enm if savegame['hp'] <= 0: sound.stop_soundfx(tunnel2_4_sound) savegame['xp'] = old_xp savegame['h_level'] = old_level savegame['sheild_hp'] = old_armor savegame['arrow_count'] = old_arrow savegame['hp'] = savegame['max_hp'] pk.dump(savegame, open("./save.p", "wb")) menu.update_hp_bar(renderer, sprite_layers, screen, hp_sprite, c_pos, 0) life = 0 running = False pk.dump(savegame, open("./save.p", "wb")) (mon_dir, mon, mon_image) = move_mon(mon_dir, mon, mon_image, sprite_layers, enm_speed) # monsters's movement enemy_attack.remove_arrow( arrow_list, mon, HP_MON, arrow_dir, sprite_layers, point_arrow) # remove arrow if it goes out of the screen remove_mon(HP_MON, mon, mon_dir, mon_image, sprite_layers) # remove monsters menu.update_lg(l_g, c_pos) #interface update # adjust camera according to the hero's position, follow him # (don't make the hero follow the cam, maybe later you want different # objects to be followed by the cam) cam_pos_x = hero.rect.centerx cam_pos_y = hero.rect.centery if hero.rect.centerx <= 520: cam_pos_x = 520 elif hero.rect.centerx >= 2336: cam_pos_x = 2336 if hero.rect.centery >= 2875: cam_pos_y = 2875 elif hero.rect.centery <= 408: cam_pos_y = 408 renderer.set_camera_position(cam_pos_x, cam_pos_y) # interface update c_pos = (cam_pos_x, cam_pos_y) interface.rect.topleft = (c_pos[0] - 512, c_pos[1] - 384) hp_sprite.rect.topleft = (c_pos[0] - 400, c_pos[1] - 382) xp_sprite.rect.topleft = (c_pos[0] - 400, c_pos[1] - 345) l_g.rect.topleft = (c_pos[0] - 508, c_pos[1] - 381) f_i.rect.bottomright = (c_pos[0] + 500, c_pos[1] + 350) interf_fight.rect.bottomright = (c_pos[0] + 512, c_pos[1] + 384) # next map if pygame.Rect.colliderect(hero.rect, portal1) or pygame.Rect.colliderect( hero.rect, portal2): portal = True sound.stop_soundfx(tunnel2_4_sound) running = False # clear screen, might be left out if every pixel is redrawn anyway screen.fill((0, 0, 0)) (renderer, sprite_layers, screen) = render_update(renderer, sprite_layers, screen) # Game restarts if life == 0: demo_pygame('./maps/tunnel2_4.tmx', 0) elif portal == True: shifty1.demo_pygame('./maps/village1.tmx', 1)