[text] main.py

Viewer

  1. from CONSTANTS import *
  2. import  pygame
  3. from pygame.locals import *
  4. #importing the basic values from a different file
  5.  
  6.  
  7. pygame.init()
  8. clock = pygame.time.Clock()
  9.  
  10. screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
  11. pygame.display.set_caption(GAME_TITLE)
  12.  
  13. #adding all my images to use later on
  14.  
  15. COIN_img = pygame.image.load('Coin.png')
  16.  
  17.  
  18. DOOR_img = pygame.image.load('DOOR.png')
  19. WALL_img = pygame.image.load('WALL.png')
  20. BACKGROUND_img = pygame.image.load('GAMEBACKGROUND.png')
  21. PLATFORM_img = pygame.image.load('PLATFORM.png')
  22.  
  23. class Player():
  24.     def __init__(self,x,y):
  25.         self.images_MoveRight = []
  26.         self.images_MoveLeft = []
  27.         self.animation_counter = 0
  28.         self.index = 0
  29.         
  30.         for num in range(1, 5):
  31.             img_MoveRight = pygame.image.load(f'SPRITE{num}.png')
  32.             img_MoveRight =pygame.transform.scale(img_MoveRight , (30, 70))
  33.             img_MoveLeft = pygame.transform.flip(img_MoveRight, True, False)
  34.             self.images_MoveRight.append(img_MoveRight)
  35.             self.images_MoveLeft.append(img_MoveLeft)
  36.         self.image = self.images_MoveRight[self.index]
  37.         
  38.         self.rect = self.image.get_rect()
  39.         self.rect.x = x
  40.         self.vel_y = 0
  41.         self.rect.y =y
  42.         self.width = self.image.get_width()
  43.         self.height = self.image.get_height()
  44.         self.playerjump = False
  45.         self.direction = 0
  46.     def update(self):
  47.         #all relations to things that need to be updated such as movement
  48.         #seeing if keys are pressed to check for movement
  49.         #using standard movement with change in x and y 
  50.         cooldown_walk = 4
  51.         dy = 0 
  52.         dx = 0
  53.         key = pygame.key.get_pressed()
  54.         if key[pygame.K_UP] and self.playerjump == False:
  55.             self.vel_y = -12
  56.             self.playerjump = True
  57.         if key[pygame.K_UP] == False:
  58.             self.playerjump = False
  59.             
  60.         if key[pygame.K_LEFT]:
  61.             dx -= 4 #means that player can not speed up past the max vertical velocity of 4
  62.             self.animation_counter += 1
  63.             self.direction = -1
  64.         if key[pygame.K_RIGHT]:
  65.             dx += 4
  66.             self.animation_counter += 1
  67.             self.direction = 1
  68.         if key[pygame.K_LEFT] == False and key[pygame.K_RIGHT] == False:
  69.             self.animation_counter = 0
  70.             self.index = 0
  71.             if self.direction == 1:
  72.                 
  73.                 self.image = self.images_MoveRight[self.index]
  74.             if self.direction == -1:
  75.                 self.image = self.images_MoveLeft[self.index]
  76.         
  77.         self.animation_counter += 1 
  78.         
  79.         
  80.         if self.animation_counter > cooldown_walk:
  81.             self.animation_counter = 0
  82.             self.index += 1
  83.             self.index += 1 #increases by one each iteration
  84.             if self.index >= len(self.images_MoveRight):
  85.                 self.index = 0 #if the index reaches the last one then go back to the first index
  86.             self.image = self.images_MoveRight[self.index] #increases index by one in the moveRight list
  87.         
  88.         
  89.         for tile in world.tile_list:
  90.             if tile[1].colliderect(self.rect.x, self.rect.y + dy,self.width, self.height ):
  91.                 if self.vel_y < 0:
  92.                     dy = tile[1].bottom = self.rect.top
  93.                 if self.vel_y >= 0:
  94.                     dy = tile[1].top - self.rect.bottom
  95.                 
  96.             
  97.         
  98.         
  99.         
  100.         
  101.         
  102.          #gravity that basically takes in the velocity the player is falling
  103.         #and ensures it doesnt go past a velocity of in this case 9
  104.         self.vel_y += 2
  105.         if self.vel_y > 9:
  106.             self.vel_y = 9  #max velocity is 9
  107.         dy += self.vel_y
  108.         self.rect.x += dx
  109.         self.rect.y += dy
  110.         
  111.         screen.blit(self.image, self.rect)
  112.     
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. class GAME_WORLD():
  120.     def __init__(self, data):
  121.         self.tile_list = []
  122.         #need to make a system that basially splits areas of pixels into blocks
  123.         
  124.         row_count = 0
  125.         for row in data:
  126.             col_count = 0 
  127.             for tile in row:
  128.                 if tile == 1:
  129.                     #pygame funciton to transform the size of a given image
  130.                     img = pygame.transform.scale(WALL_img, (size_tile, size_tile))
  131.                     img_rect = img.get_rect()
  132.                     #x axis will use column
  133.                     img_rect.x = col_count * size_tile
  134.                     #y axis will use rows
  135.                     img_rect.y = row_count * size_tile
  136.                     tile = (img, img_rect)
  137.                     self.tile_list.append(tile)
  138.                 #I can copy this line of code for any kind of block that i want to
  139.                 if tile == 2:
  140.                     #pygame funciton to transform the size of a given image
  141.                     img = pygame.transform.scale(PLATFORM_img, (size_tile, size_tile))
  142.                     img_rect = img.get_rect()
  143.                     #x axis will use column
  144.                     img_rect.x = col_count * size_tile
  145.                     #y axis will use rows
  146.                     img_rect.y = row_count * size_tile
  147.                     tile = (img, img_rect)
  148.                     self.tile_list.append(tile)
  149.                 if tile == 3:
  150.                     #pygame funciton to transform the size of a given image
  151.                     img = pygame.transform.scale(DOOR_img, (size_tile, size_tile))
  152.                     img_rect = img.get_rect()
  153.                     #x axis will use column
  154.                     img_rect.x = col_count * size_tile
  155.                     #y axis will use rows
  156.                     img_rect.y = row_count * size_tile
  157.                     tile = (img, img_rect)
  158.                     self.tile_list.append(tile)
  159.                
  160.                 col_count += 1
  161.             row_count += 1
  162.     def draw(self):
  163.         for tile in self.tile_list:
  164.             screen.blit(tile[0], tile[1])
  165.             
  166. #Making the map using the previous code that uses numbers to signify different images/tiles
  167. # 1 = Block
  168. # 2 = Platform
  169. # 3 = Door
  170. game_map_data = [
  171. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  172. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1],
  173. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,1],
  174. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  175. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  176. [1,0,0,0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  191. [1,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  192. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  193. [1,0,0,0,0,0,2,2,0,0,0,0,0,0,0,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,1],
  194. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  195. [1,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,1],
  196. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  197. [1,0,0,0,0,0,0,0,2,2,2,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,1],
  198. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
  199. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  200. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  201. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  202. [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  203. [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
  204. ]
  205.  
  206.  
  207.  
  208. world = GAME_WORLD(game_map_data)
  209. player = Player(100,SCREENHEIGHT - 164)
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. run = True
  219. while run:
  220.     clock.tick(FPS)
  221.     screen.blit(BACKGROUND_img, (0, 0))
  222.     world.draw()
  223.     player.update()
  224.     
  225.     
  226.     for event in pygame.event.get():
  227.         if event.type == pygame.QUIT:
  228.             run = False
  229.     pygame.display.update()
  230.     
  231. pygame.quit

Editor

You can edit this paste and save as new:


File Description
  • main.py
  • Paste Code
  • 27 Feb-2021
  • 8.73 Kb
You can Share it: