Advertisement
ZEdKasat

Untitled

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