Advertisement
Maluia

functions.py

Oct 28th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | Gaming | 0 0
  1. import pygame
  2. from os import listdir
  3. from os.path import isfile, join
  4.  
  5. WIDTH, HEIGHT = 1000, 800
  6.  
  7. PLAYER_VEL = 5
  8.  
  9. def flip(sprites):      #flip sprite so it can face the other way
  10.     return [pygame.transform.flip(sprite, True, False) for sprite in sprites]
  11.  
  12. def load_sprite_sheets(dir1, dir2, width, height, direction=False):
  13.     path = join("assets", dir1, dir2)
  14.     images = [f for f in listdir(path) if isfile(join(path, f))]
  15.  
  16.     all_sprites = {}
  17.  
  18.     for image in images:
  19.         sprite_sheet = pygame.image.load(join(path, image)).convert_alpha()
  20.  
  21.         sprites = []
  22.         for i in range(sprite_sheet.get_width() // width):
  23.             surface = pygame.Surface((width, height), pygame.SRCALPHA, 32)
  24.             rect = pygame.Rect(i * width, 0, width, height)
  25.             surface.blit(sprite_sheet, (0, 0), rect)
  26.             sprites.append(pygame.transform.scale2x(surface))
  27.  
  28.         if direction:
  29.             all_sprites[image.replace(".png", "") + "_right"] = sprites
  30.             all_sprites[image.replace(".png", "") + "_left"] = flip(sprites)
  31.         else:
  32.             all_sprites[image.replace(".png", "")] = sprites
  33.  
  34.     return all_sprites
  35.  
  36. def load_sprite_sheet(dir1, dir2, dir3, width, height, direction=False):
  37.     path = join("assets", dir1, dir2, dir3)
  38.     images = [f for f in listdir(path) if isfile(join(path, f))]
  39.  
  40.     all_sprites = {}
  41.  
  42.     for image in images:
  43.         sprite_sheet = pygame.image.load(join(path, image)).convert_alpha()
  44.  
  45.         sprites = []
  46.         for i in range(sprite_sheet.get_width() // width):
  47.             surface = pygame.Surface((width, height), pygame.SRCALPHA, 32)
  48.             rect = pygame.Rect(i * width, 0, width, height)
  49.             surface.blit(sprite_sheet, (0, 0), rect)
  50.             sprites.append(pygame.transform.scale2x(surface))
  51.  
  52.         if direction:
  53.             all_sprites[image.replace(".png", "") + "_right"] = sprites
  54.             all_sprites[image.replace(".png", "") + "_left"] = flip(sprites)
  55.         else:
  56.             all_sprites[image.replace(".png", "")] = sprites
  57.  
  58.     return all_sprites
  59.  
  60. def get_background(name):   #get the background image
  61.     image = pygame.image.load(join("assets", "Background", name)).convert()
  62.     _, _, width, height = image.get_rect()
  63.     tiles = []
  64.  
  65.     for i in range(1920 // width + 1):                 #WIDTH replaced with 1920        #tile the image across the whole screen
  66.         for j in range(1080 // height + 1):           #HEIGHT replaced with 1080
  67.             pos = (i * width, j * height)
  68.             tiles.append(pos)
  69.  
  70.     return tiles, image
  71.  
  72. def handle_vertical_collision(player, objects, dy): #veritcal collision
  73.     collided_objects = []
  74.     for obj in objects:
  75.         if pygame.sprite.collide_mask(player, obj):
  76.             if dy > 0:  #if moving down
  77.                 player.rect.bottom = obj.rect.top   #bottom of player = top of block
  78.                 player.landed()
  79.             elif dy < 0:    #if moving up
  80.                 player.rect.top = obj.rect.bottom   #top of player = bottom of block
  81.                 player.hit_head()
  82.  
  83.         collided_objects.append(obj)
  84.  
  85.     return collided_objects
  86.  
  87. def collide(player, objects, dx):       #horizontal collision
  88.     player.move(dx, 0)
  89.     player.update()
  90.     collided_object = None
  91.     for obj in objects:
  92.         if pygame.sprite.collide_mask(player, obj):
  93.             collided_object = obj
  94.             break
  95.  
  96.  
  97.     player.move(-dx, 0)
  98.     player.update()
  99.     return collided_object
  100.  
  101. def handle_move(player, objects):
  102.     keys = pygame.key.get_pressed()
  103.  
  104.     player.x_vel = 0    #only move when holding down the key
  105.     collide_left = collide(player, objects, -PLAYER_VEL * 2)
  106.     collide_right = collide(player, objects, PLAYER_VEL * 2)
  107.  
  108.     if (keys[pygame.K_LEFT] or keys[pygame.K_a]) and not collide_left and player.healthbar.health > 0:      #move left if left arrow or a is pressed
  109.         player.move_left(PLAYER_VEL)
  110.     if (keys[pygame.K_RIGHT] or keys[pygame.K_d]) and not collide_right and player.healthbar.health > 0:    #move right if right arrow or d is pressed
  111.         player.move_right(PLAYER_VEL)
  112.  
  113.     vertical_collide = handle_vertical_collision(player, objects, player.y_vel)
  114.     to_check = [collide_left, collide_right, *vertical_collide]
  115.  
  116.     for obj in to_check:
  117.         if obj and obj.name == "flame" or obj and obj.name == "blade" or obj and obj.name == "spikes":      #take damage when hit a trap
  118.             player.make_hit()
  119.             player.player_hit(1)
  120.  
  121.         from fruitclass import Fruit  
  122.         if obj and obj.name == "fruit":     #heal when hit a fruit
  123.             player.player_heal(1)
  124.             Fruit.kill
  125.  
  126.         if obj and obj.name == "flag":      #level over when hit the flag
  127.             player.finished = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement