Guest User

Untitled

a guest
Aug 18th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. import random as random
  2. import pygame as pygame
  3.  
  4. pygame.init()
  5. clock = pygame.time.Clock()
  6. Screen = pygame.display.set_mode([650, 650])
  7. Done = False
  8. MapSize = 25
  9.  
  10. TileWidth = 20
  11. TileHeight = 20
  12. TileMargin = 4
  13.  
  14. BLACK = (0, 0, 0)
  15. WHITE = (255, 255, 255)
  16. GREEN = (0, 255, 0)
  17. RED = (255, 0, 0)
  18. BLUE = (0, 0, 255)
  19.  
  20.  
  21.  
  22. class MapTile(object):
  23. def __init__(self, Name, Column, Row):
  24. self.Name = Name
  25. self.Column = Column
  26. self.Row = Row
  27.  
  28.  
  29. class Character(object):
  30. def __init__(self, Name, HP, Column, Row):
  31. self.Name = Name
  32. self.HP = HP
  33. self.Column = Column
  34. self.Row = Row
  35.  
  36. def Move(self, Direction):
  37.  
  38. if Direction == "UP":
  39. if self.Row > 0:
  40. if self.CollisionCheck("UP") == False:
  41. self.Row -= 1
  42.  
  43. elif Direction == "LEFT":
  44. if self.Column > 0:
  45. if self.CollisionCheck("LEFT") == False:
  46. self.Column -= 1
  47.  
  48. elif Direction == "RIGHT":
  49. if self.Column < MapSize-1:
  50. if self.CollisionCheck("RIGHT") == False:
  51. self.Column += 1
  52.  
  53. elif Direction == "DOWN":
  54. if self.Row < MapSize-1:
  55. if self.CollisionCheck("DOWN") == False:
  56. self.Row += 1
  57. print("BOOP")
  58.  
  59. Map.update()
  60.  
  61. def CollisionCheck(self, Direction):
  62. if Direction == "UP":
  63. if len(Map.Grid[self.Column][(self.Row)-1]) > 1:
  64. return True
  65. elif Direction == "LEFT":
  66. if len(Map.Grid[self.Column-1][(self.Row)]) > 1:
  67. return True
  68. elif Direction == "RIGHT":
  69. if len(Map.Grid[self.Column+1][(self.Row)]) > 1:
  70. return True
  71. elif Direction == "DOWN":
  72. if len(Map.Grid[self.Column][(self.Row)+1]) > 1:
  73. return True
  74. return False
  75.  
  76. def Location(self):
  77. print("Coordinates: " + str(self.Column) + ", " + str(self.Row))
  78.  
  79.  
  80. class Map(object):
  81. global MapSize
  82.  
  83. Grid = []
  84.  
  85. for Row in range(MapSize): # Creating grid
  86. Grid.append([])
  87. for Column in range(MapSize):
  88. Grid[Row].append([])
  89.  
  90. for Row in range(MapSize): #Filling grid with grass
  91. for Column in range(MapSize):
  92. TempTile = MapTile("Grass", Column, Row)
  93. Grid[Column][Row].append(TempTile)
  94.  
  95. for Row in range(MapSize): #Rocks
  96. for Column in range(MapSize):
  97. TempTile = MapTile("Rock", Column, Row)
  98. if Row == 1:
  99. Grid[Column][Row].append(TempTile)
  100.  
  101. for i in range(10): #Random trees
  102. RandomRow = random.randint(0, MapSize - 1)
  103. RandomColumn = random.randint(0, MapSize - 1)
  104. TempTile = MapTile("Tree", RandomColumn, RandomRow)
  105. Grid[RandomColumn][RandomRow].append(TempTile)
  106.  
  107. RandomRow = random.randint(0, MapSize - 1)
  108. RandomColumn = random.randint(0, MapSize - 1)
  109. Hero = Character("Hero", 10, RandomColumn, RandomRow)
  110.  
  111. def update(self):
  112. for Column in range(MapSize):
  113. for Row in range(MapSize):
  114. for i in range(len(Map.Grid[Column][Row])):
  115. if Map.Grid[Column][Row][i].Column != Column:
  116. Map.Grid[Column][Row].remove(Map.Grid[Column][Row][i])
  117. elif Map.Grid[Column][Row][i].Name == "Hero":
  118. Map.Grid[Column][Row].remove(Map.Grid[Column][Row][i])
  119. Map.Grid[int(Map.Hero.Column)][int(Map.Hero.Row)].append(Map.Hero)
  120.  
  121. Map = Map()
  122.  
  123. while not Done:
  124.  
  125. for event in pygame.event.get():
  126. if event.type == pygame.QUIT:
  127.  
  128. Done = True
  129.  
  130. elif event.type == pygame.MOUSEBUTTONDOWN:
  131. Pos = pygame.mouse.get_pos()
  132. Column = Pos[0] // (TileWidth + TileMargin)
  133. Row = Pos[1] // (TileHeight + TileMargin)
  134. print(str(Row) + ", " + str(Column))
  135.  
  136. for i in range(len(Map.Grid[Column][Row])):
  137. print(str(Map.Grid[Column][Row][i].Name))
  138.  
  139. elif event.type == pygame.KEYDOWN:
  140. if event.key == pygame.K_LEFT:
  141. Map.Hero.Move("LEFT")
  142. if event.key == pygame.K_RIGHT:
  143. Map.Hero.Move("RIGHT")
  144. if event.key == pygame.K_UP:
  145. Map.Hero.Move("UP")
  146. if event.key == pygame.K_DOWN:
  147. Map.Hero.Move("DOWN")
  148.  
  149. Screen.fill(BLACK)
  150.  
  151. for Row in range(MapSize): # Drawing grid
  152. for Column in range(MapSize):
  153. for i in range(0, len(Map.Grid[Column][Row])):
  154. Color = WHITE
  155. if len(Map.Grid[Column][Row]) == 2:
  156. Color = RED
  157. if Map.Grid[Column][Row][i].Name == "Hero":
  158. Color = GREEN
  159.  
  160.  
  161. pygame.draw.rect(Screen, Color, [(TileMargin + TileWidth) * Column + TileMargin,
  162. (TileMargin + TileHeight) * Row + TileMargin,
  163. TileWidth,
  164. TileHeight])
  165.  
  166. clock.tick(60)
  167.  
  168. pygame.display.flip()
  169. Map.update()
  170.  
  171. pygame.quit()
Add Comment
Please, Sign In to add comment