ZEdKasat

Tile Game

Sep 17th, 2021 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.82 KB | None | 0 0
  1. import pygame
  2. vector = pygame.math.Vector2
  3. BackgroundColor = 0
  4. playercolor = 0
  5. length = 60
  6. walls = []
  7.  
  8.  
  9. class Player(pygame.sprite.Sprite):
  10.     def __init__(self):
  11.         super().__init__()
  12.         self.i = pygame.image.load("manBlue_gun.png").convert_alpha()
  13.         self.i = pygame.transform.scale(self.i, (length,length))
  14.         self.image = self.i
  15.         self.rect = self.image.get_rect()
  16.         self.rect.topleft = vector(length, length)
  17.         self.rot = 0
  18.         self.position = vector(length, length)
  19.         self.move = (0,0)
  20.    
  21.     def update(self):
  22.         keys = pygame.key.get_pressed()
  23.         if keys[pygame.K_RIGHT]:
  24.             self.rot = (self.rot + 10) % 360
  25.             print("Rotating right")
  26.             # self.image = pygame.transform.rotate(self.i, self.rot)
  27.         elif keys[pygame.K_LEFT]:
  28.             self.rot = (self.rot - 10) % 360
  29.             print("Rotating left")
  30.         self.image = pygame.transform.rotate(self.i, self.rot)
  31.         pos = self.rect.center
  32.         self.rect = self.image.get_rect()
  33.         self.rect.center = pos
  34.         # self.rect.move_ip(0,0)
  35.         self.move = (0, 0)
  36.         if keys[pygame.K_UP]:
  37.             self.move = vector(17, 0).rotate(-self.rot)
  38.         elif keys[pygame.K_DOWN]:
  39.             self.move = vector(-17, 0).rotate(-self.rot)
  40.  
  41.         if not check_collision(self.rect, self.move):
  42.             self.rect.move_ip(self.move)
  43.         else:
  44.             self.move = (0,0)
  45.         self.position = vector(self.rect.center)
  46.  
  47. class bullets(pygame.sprite.Sprite):
  48.     def __init__(self, angle, pos):
  49.         super().__init__()
  50.         self.image = pygame.Surface((10, 10))
  51.         self.image.fill((0,255,0))
  52.         self.rect = self.image.get_rect()
  53.         self.bulletspeed = 0
  54.         self.vector = vector(20, 0).rotate(-angle)
  55.         self.rect.center = pos
  56.  
  57.     def update(self):
  58.         self.rect.move_ip(self.vector)
  59.  
  60. class wall(pygame.sprite.Sprite):
  61.     def __init__(self, c, r):
  62.         super().__init__()
  63.         # print(c, r)
  64.         self.image = pygame.Surface((length, length))
  65.         self.image.fill((0, 125, 125))
  66.         self.rect = self.image.get_rect()
  67.         self.row = r
  68.         self.column = c
  69.         self.rect.topleft = (self.column*length, self.row*length)
  70.        
  71.  
  72. class enemies(pygame.sprite.Sprite):
  73.     def __init__(self, c, r):
  74.         super().__init__()
  75.         self.i = pygame.image.load("robot1_hold.png").convert_alpha()
  76.         self.i = pygame.transform.scale(self.i, (length, length))
  77.         self.image = self.i
  78.         self.image.fill((0,255,0))
  79.         self.rect = self.image.get_rect()
  80.         self.row = r
  81.         self.column = c
  82.         self.rect.topleft = (self.column*length, self.row*length)
  83.         self.rot = 0
  84.  
  85.     # def update(self):
  86.  
  87. def check_collision(rect, movement):
  88.     new_pos_rect = rect.move(movement)
  89.     for w in walls:
  90.         if w.rect.colliderect(new_pos_rect):
  91.             return True
  92.     return False
  93.  
  94.  
  95.  
  96. def setupmap():
  97.     global walls
  98.     with open("map.txt", "r") as file:
  99.         for r, row in enumerate(file):
  100.             for c, element in enumerate(row):
  101.                 if element == "1":
  102.                     print(c, r)
  103.                     w = wall(c, r)
  104.                     walls.append(w)
  105.                 elif element == "M":
  106.                     print(c, r)
  107.                     e = enemies(c, r)
  108.                     enemies_grp.add(e)
  109.                     allelements.add(e)
  110.     total_rows = r + 1
  111.     total_columns = c + 1
  112.     return total_rows, total_columns
  113.  
  114. def update_camera(camera, player, mapwidth, mapheight):
  115.     camera.x = player.position.x - (WIDTH // 2)
  116.     camera.y = player.position.y - (HEIGHT // 2)
  117.     # if camera.x < 0:
  118.     camera.x = player.move[0]
  119.     # elif camera.x > mapwidth - WIDTH:
  120.     #     camera.x = mapwidth - WIDTH
  121.     # if camera.y < 0:
  122.     camera.y = player.move[1]
  123.     # elif camera.y > mapheight - HEIGHT:
  124.     #     camera.y = mapheight - HEIGHT
  125.     # print("camera", camera.x)
  126.     # print("Player position", player.position.x)
  127.     # for e in elements:
  128.     #     e.rect.move_ip(-camera.x, -camera.y)
  129.     # print("Move")
  130.    
  131.  
  132. # def apply_camera(camera, element):
  133. #     rect = element.rect.move(-camera.x, -camera.y)
  134. #     return rect  
  135.  
  136. WIDTH, HEIGHT = 800, 600
  137. pygame.init()
  138. window = pygame.display.set_mode((800, 600))
  139. camera = pygame.Rect(-400, -300, WIDTH, HEIGHT)
  140.  
  141. run = True
  142. clock = pygame.time.Clock()
  143. elements = pygame.sprite.Group()
  144. allelements = pygame.sprite.Group()
  145. wall_grp = pygame.sprite.Group()
  146. enemies_grp = pygame.sprite.Group()
  147. mapsize = setupmap()
  148. for w in walls:
  149.     wall_grp.add(w)
  150.     allelements.add(w)
  151. player = Player()
  152. elements.add(player)
  153. allelements.add(player)
  154. while run:
  155.     clock.tick(30)
  156.     window.fill(BackgroundColor)
  157.     for event in pygame.event.get():
  158.         if event.type == pygame.QUIT:
  159.             run = False
  160.         if event.type == pygame.KEYDOWN:
  161.             if event.key == pygame.K_SPACE:
  162.                 b = bullets(player.rot, player.rect.center)
  163.                 elements.add(b)
  164.                 allelements.add(b)
  165.     update_camera(camera, player, mapsize[1]*length, mapsize[0]*length)
  166.  
  167.     for e in allelements:
  168.         # print("camera x,y ", camera.x, camera.y)
  169.         e.rect.move_ip(-camera.x, -camera.y)
  170.         # window.blit(e.image, apply_camera(camera, e))
  171.     # for e in enemies_grp:
  172.     #     # e.rect.move_ip(-camera.x, -camera.y)
  173.     #     window.blit(e.image, apply_camera(camera, e))
  174.     # for e in elements:
  175.     #     # e.rect.move_ip(-camera.x, -camera.y)
  176.     #     window.blit(e.image, apply_camera(camera, e))
  177.  
  178.     # enemies_grp.update()
  179.     # enemies_grp.draw(window)
  180.     # wall_grp.update()
  181.     # wall_grp.draw(window)
  182.     allelements.update()
  183.     allelements.draw(window)
  184.     pygame.display.update()
  185. pygame.quit()
  186.  
Add Comment
Please, Sign In to add comment