Advertisement
Guest User

game

a guest
Nov 12th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. import random, pygame, sys
  2. from pygame.locals import *
  3.  
  4. fps = 30
  5. window_width = 704
  6. window_height = 704
  7. fpsClock = pygame.time.Clock()
  8. map_width = 30
  9. map_height = 15
  10.  
  11. green = (100, 255, 0)
  12.  
  13. display_surf = pygame.display.set_mode((window_height, window_width), 0, 32)
  14. pygame.display.set_caption("Rl")
  15.  
  16.  
  17. class Object:
  18. # this is a generic object: the player, a monster, an item, the stairs...
  19. # it's always represented by a character on screen.
  20. def __init__(self, x, y, image):
  21. self.x = x
  22. self.y = y
  23. self.image = pygame.image.load(image)
  24.  
  25. def move(self, dx, dy):
  26. if find_title(world[self.y+dy][self.x+dx]).solid is False: #this line checks if move is possible
  27. self.x += dx
  28. self.y += dy
  29.  
  30. def draw(self):
  31. display_surf.blit(self.image, (self.x, self.y))
  32.  
  33.  
  34. class Title:
  35. # this is a class for titles like grass, road and so on
  36. def __init__(self, title_number, solid, image):
  37. self.title_number = title_number
  38. self.solid = solid
  39. self.image = pygame.image.load(image)
  40.  
  41.  
  42. def find_title(n):
  43. if n == 0:
  44. return nothing
  45. if n == 1:
  46. return grass
  47. if n == 2:
  48. return fance
  49.  
  50.  
  51. def world_gen():
  52. camera_x = scrolling_map(hero.x, 5, 10, map_width)
  53. camera_y = scrolling_map(hero.y, 5, 10, map_height)
  54. for x in range(camera_x, camera_x+11):
  55. for y in range(camera_y, camera_y+11):
  56. if world[y][x] != 0:
  57. display_surf.blit(find_title(world[y][x]).image, (64 * x, 64 * y))
  58. display_surf.blit(hero.image, (64 * 5, 64 * 5))
  59.  
  60. def scrolling_map(p, hs, s, m):
  61. """
  62. Get the position of the camera in a scrolling map:
  63.  
  64. - p is the position of the player.
  65. - hs is half of the screen size, and s is the full screen size.
  66. - m is the size of the map.
  67. """
  68.  
  69. if p < hs:
  70. return 0
  71. elif p >= m - hs:
  72. return m - s
  73. else:
  74. return p - hs
  75.  
  76.  
  77. hero = Object(5, 5, "hero.png")
  78. grass = Title(1, False, "grass.png")
  79. fance = Title(2, True, "fance.png")
  80. nothing = Title(0, False, "road.png")
  81.  
  82. world = [[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  83. [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  84. [2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  85. [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, 0, 0, 0],
  86. [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, 0, 0, 0],
  87. [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, 0, 0, 0],
  88. [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, 0, 0, 0],
  89. [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, 0, 0, 0],
  90. [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, 0, 0, 0],
  91. [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, 0],
  92. [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, 0, 0, 0],
  93. [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, 0, 0, 0],
  94. [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, 0, 0, 0],
  95. [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, 0, 0, 0]]
  96.  
  97. while True:
  98. display_surf.fill(green)
  99.  
  100. for event in pygame.event.get():
  101. if event.type == QUIT:
  102. pygame.quit()
  103. sys.exit()
  104.  
  105. if event.type == KEYDOWN:
  106. if event.key == pygame.K_LEFT:
  107. if hero.x - 1 >= 0:
  108. hero.move(-1, 0)
  109. if event.key == pygame.K_RIGHT:
  110. if hero.x + 1 < map_width:
  111. hero.move(1, 0)
  112. if event.key == pygame.K_UP:
  113. if hero.y - 1 >= 0:
  114. hero.move(0, -1)
  115. if event.key == pygame.K_DOWN:
  116. if hero.y + 1 < map_height:
  117. hero.move(0, 1)
  118.  
  119. world_gen()
  120.  
  121. pygame.display.update()
  122. pygame.time.delay(50)
  123. fpsClock.tick(fps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement