Advertisement
Karp_xD

main

Jun 16th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3. win = pygame.display.set_mode((800, 800))
  4. pygame.display.set_caption("Scrolling game")
  5. #declare variables
  6. #   for a player
  7. moving_left = False
  8. moving_right = False
  9. jump = False
  10. TILE_SIZE = 32
  11. #   for a map
  12. scrollx = 0
  13. #creating_world
  14. with open('world.txt') as world:
  15.     reader = world.read()
  16.     world_data = reader.split("\n")
  17.     world.close()
  18. class Square(pygame.sprite.Sprite):
  19.     def __init__(self, x, y):
  20.         pygame.sprite.Sprite.__init__(self)
  21.         self.rect = pygame.Rect(x, y, 32, 32)
  22.         self.can_jump = True
  23.         self.vely = -30
  24.     def draw(self):
  25.         pygame.draw.rect(win, (255, 80, 80), self.rect)
  26.     def move(self):
  27.         global scrollx, jump
  28.         Gravity = 3
  29.         dx = 0
  30.         dy = 0
  31.         dy += Gravity
  32.         if moving_left:
  33.             dx -= 4
  34.         elif moving_right:
  35.             dx += 4
  36.         if jump:
  37.             dy += self.vely
  38.             self.vely += 1
  39.             if self.vely == 0:
  40.                 jump = False
  41.                 self.can_jump = True
  42.                 self.vely = -30
  43.         for tile in obstacle_list:
  44.             if self.rect.bottom >= tile.y and jump == False:
  45.                 dy = 0
  46.         scrollx = -dx
  47.         self.rect.x += dx
  48.         self.rect.y += dy
  49. class Block(pygame.sprite.Sprite):
  50.     def __init__(self, x, y, width, height, color, name_file):
  51.         pygame.sprite.Sprite.__init__(self)
  52.         self.rect = pygame.Rect(x, y, width, height)
  53.         self.color = color
  54.         self.image = pygame.transform.scale(pygame.image.load(f'blocks/{name_file}'), (width, height))
  55.     def move(self):
  56.         self.rect.x += scrollx
  57. Player = Square(400, 400)
  58. run = True
  59. Dirt_group = pygame.sprite.Group()
  60. Grass_group = pygame.sprite.Group()
  61. Clock = pygame.time.Clock()
  62. obstacle_list = []
  63. count_loads = 0
  64. while run:
  65.     Clock.tick(80)
  66.     win.fill((255, 124, 255))
  67.     if count_loads != 1:
  68.         y = 0
  69.         for row in world_data:
  70.             x = 0
  71.             for tile in row:
  72.                 if tile == '1':
  73.                     dirt = Block(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE, (70, 27, 2), 'dirt.png')
  74.                     Dirt_group.add(dirt)
  75.                 if tile == '2':
  76.                     grass = Block(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE, (29, 194, 12), 'grass.png')
  77.                     Grass_group.add(grass)
  78.                 if tile != '0':
  79.                     obstacle_list.append(pygame.Rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
  80.                 x += 1
  81.             y += 1
  82.         count_loads += 1
  83.     for event in pygame.event.get():
  84.         if event.type == pygame.QUIT:
  85.             run = False
  86.         if event.type == pygame.KEYDOWN:
  87.             if event.key == pygame.K_d:
  88.                 moving_right = True
  89.             if event.key == pygame.K_a:
  90.                 moving_left = True
  91.             if event.key == pygame.K_SPACE and Player.can_jump:
  92.                 jump = True
  93.                 Player.can_jump = False
  94.         if event.type == pygame.KEYUP:
  95.             if event.key == pygame.K_d:
  96.                 moving_right = False
  97.             if event.key == pygame.K_a:
  98.                 moving_left = False
  99.     Grass_group.update()
  100.     Dirt_group.update()
  101.     Grass_group.draw(win)
  102.     Dirt_group.draw(win)
  103.     Player.draw()
  104.     Player.move()
  105.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement