Advertisement
magneto903

platformer

Jun 11th, 2021
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.70 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. pygame.init()
  5.  
  6. clock = pygame.time.Clock()
  7. fps = 60
  8.  
  9. screen_width = 1000
  10. screen_height = 1000
  11.  
  12. screen = pygame.display.set_mode((screen_width, screen_height))
  13. pygame.display.set_caption('Platformer')
  14.  
  15. # define game variables
  16. tile_size = 50
  17. game_over = 0
  18.  
  19. # load images
  20. sun_img = pygame.image.load('sun.png')
  21. bg_img = pygame.image.load('sky.png')
  22.  
  23.  
  24. class Player():
  25.     def __init__(self, x, y):
  26.         self.images_right = []
  27.         self.images_left = []
  28.         self.index = 0
  29.         self.counter = 0
  30.         for num in range(1, 5):
  31.             img_right = pygame.image.load(f'guy{num}.png')
  32.             img_right = pygame.transform.scale(img_right, (40, 80))
  33.             img_left = pygame.transform.flip(img_right, True, False)
  34.             self.images_right.append(img_right)
  35.             self.images_left.append(img_left)
  36.         self.dead_image = pygame.image.load('ghost.png')
  37.         self.image = self.images_right[self.index]
  38.         self.rect = self.image.get_rect()
  39.         self.rect.x = x
  40.         self.rect.y = y
  41.         self.width = self.image.get_width()
  42.         self.height = self.image.get_height()
  43.         self.vel_y = 0
  44.         self.jumped = False
  45.         self.direction = 0
  46.  
  47.     def update(self, game_over):
  48.         dx = 0
  49.         dy = 0
  50.         walk_cooldown = 5
  51.  
  52.         if game_over == 0:
  53.             # get keypresses
  54.             key = pygame.key.get_pressed()
  55.             if key[pygame.K_SPACE] and self.jumped == False:
  56.                 self.vel_y = -15
  57.                 self.jumped = True
  58.             if key[pygame.K_SPACE] == False:
  59.                 self.jumped = False
  60.             if key[pygame.K_LEFT]:
  61.                 dx -= 5
  62.                 self.counter += 1
  63.                 self.direction = -1
  64.             if key[pygame.K_RIGHT]:
  65.                 dx += 5
  66.                 self.counter += 1
  67.                 self.direction = 1
  68.             if key[pygame.K_LEFT] == False and key[pygame.K_RIGHT] == False:
  69.                 self.counter = 0
  70.                 self.index = 0
  71.                 if self.direction == 1:
  72.                     self.image = self.images_right[self.index]
  73.                 if self.direction == -1:
  74.                     self.image = self.images_left[self.index]
  75.  
  76.             # handle animation
  77.             if self.counter > walk_cooldown:
  78.                 self.counter = 0
  79.                 self.index += 1
  80.                 if self.index >= len(self.images_right):
  81.                     self.index = 0
  82.                 if self.direction == 1:
  83.                     self.image = self.images_right[self.index]
  84.                 if self.direction == -1:
  85.                     self.image = self.images_left[self.index]
  86.  
  87.             # add gravity
  88.             self.vel_y += 1
  89.             if self.vel_y > 10:
  90.                 self.vel_y = 10
  91.             dy += self.vel_y
  92.  
  93.             # check for collision
  94.             for tile in world.tile_list:
  95.                 # check for collision in x direction
  96.                 if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
  97.                     dx = 0
  98.                 # check for collision in y direction
  99.                 if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
  100.                     # check if below the ground i.e. jumping
  101.                     if self.vel_y < 0:
  102.                         dy = tile[1].bottom - self.rect.top
  103.                         self.vel_y = 0
  104.                     # check if above the ground i.e. falling
  105.                     elif self.vel_y >= 0:
  106.                         dy = tile[1].top - self.rect.bottom
  107.                         self.vel_y = 0
  108.  
  109.             # check for collision with enemies
  110.             if pygame.sprite.spritecollide(self, blob_group, False):
  111.                 game_over = -1
  112.  
  113.             # check for collision with lava
  114.             if pygame.sprite.spritecollide(self, lava_group, False):
  115.                 game_over = -1
  116.  
  117.             # update player coordinates
  118.             self.rect.x += dx
  119.             self.rect.y += dy
  120.  
  121.  
  122.         elif game_over == -1:
  123.             self.image = self.dead_image
  124.             if self.rect.y > 200:
  125.                 self.rect.y -= 5
  126.  
  127.         # draw player onto screen
  128.         screen.blit(self.image, self.rect)
  129.         pygame.draw.rect(screen, (255, 255, 255), self.rect, 2)
  130.  
  131.         return game_over
  132.  
  133.  
  134. class World():
  135.     def __init__(self, data):
  136.         self.tile_list = []
  137.  
  138.         # load images
  139.         dirt_img = pygame.image.load('dirt.png')
  140.         grass_img = pygame.image.load('grass.png')
  141.  
  142.         row_count = 0
  143.         for row in data:
  144.             col_count = 0
  145.             for tile in row:
  146.                 if tile == 1:
  147.                     img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
  148.                     img_rect = img.get_rect()
  149.                     img_rect.x = col_count * tile_size
  150.                     img_rect.y = row_count * tile_size
  151.                     tile = (img, img_rect)
  152.                     self.tile_list.append(tile)
  153.                 if tile == 2:
  154.                     img = pygame.transform.scale(grass_img, (tile_size, tile_size))
  155.                     img_rect = img.get_rect()
  156.                     img_rect.x = col_count * tile_size
  157.                     img_rect.y = row_count * tile_size
  158.                     tile = (img, img_rect)
  159.                     self.tile_list.append(tile)
  160.                 if tile == 3:
  161.                     blob = Enemy(col_count * tile_size, row_count * tile_size + 15)
  162.                     blob_group.add(blob)
  163.                 if tile == 6:
  164.                     lava = Lava(col_count * tile_size, row_count * tile_size + (tile_size // 2))
  165.                     lava_group.add(lava)
  166.  
  167.                 col_count += 1
  168.             row_count += 1
  169.  
  170.     def draw(self):
  171.         for tile in self.tile_list:
  172.             screen.blit(tile[0], tile[1])
  173.             pygame.draw.rect(screen, (255, 255, 255), tile[1], 2)
  174.  
  175.  
  176. class Enemy(pygame.sprite.Sprite):
  177.     def __init__(self, x, y):
  178.         pygame.sprite.Sprite.__init__(self)
  179.         self.image = pygame.image.load('blob.png')
  180.         self.rect = self.image.get_rect()
  181.         self.rect.x = x
  182.         self.rect.y = y
  183.         self.move_direction = 1
  184.         self.move_counter = 0
  185.  
  186.     def update(self):
  187.         self.rect.x += self.move_direction
  188.         self.move_counter += 1
  189.         if abs(self.move_counter) > 50:
  190.             self.move_direction *= -1
  191.             self.move_counter *= -1
  192.  
  193.  
  194. class Lava(pygame.sprite.Sprite):
  195.     def __init__(self, x, y):
  196.         pygame.sprite.Sprite.__init__(self)
  197.         img = pygame.image.load('lava.png')
  198.         self.image = pygame.transform.scale(img, (tile_size, tile_size // 2))
  199.         self.rect = self.image.get_rect()
  200.         self.rect.x = x
  201.         self.rect.y = y
  202.  
  203.  
  204. world_data = [
  205.     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  206.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  207.     [1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
  208.     [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1],
  209.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 7, 0, 5, 0, 0, 0, 1],
  210.     [1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 1],
  211.     [1, 7, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  212.     [1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  213.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 7, 0, 0, 0, 0, 1],
  214.     [1, 0, 2, 0, 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  215.     [1, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 1],
  216.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1],
  217.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  218.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 2, 0, 1],
  219.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  220.     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 2, 2, 2, 1],
  221.     [1, 0, 0, 0, 0, 0, 2, 2, 2, 6, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1],
  222.     [1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  223.     [1, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  224.     [1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  225. ]
  226.  
  227. player = Player(100, screen_height - 130)
  228.  
  229. blob_group = pygame.sprite.Group()
  230. lava_group = pygame.sprite.Group()
  231.  
  232. world = World(world_data)
  233.  
  234. run = True
  235. while run:
  236.  
  237.     clock.tick(fps)
  238.  
  239.     screen.blit(bg_img, (0, 0))
  240.     screen.blit(sun_img, (100, 100))
  241.  
  242.     world.draw()
  243.  
  244.     if game_over == 0:
  245.         blob_group.update()
  246.  
  247.     blob_group.draw(screen)
  248.     lava_group.draw(screen)
  249.  
  250.     game_over = player.update(game_over)
  251.  
  252.     for event in pygame.event.get():
  253.         if event.type == pygame.QUIT:
  254.             run = False
  255.  
  256.     pygame.display.update()
  257.  
  258. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement