Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import pygame, sys
  2.  
  3.  
  4. #constants representing colors
  5. BLACK = (0,0,0)
  6. BROWN = (153,76,0)
  7. GREEN = (0,255,0)
  8. BLUE = (0,0,255)
  9.  
  10. #constants representing resources
  11. DIRT = 0
  12. GRASS = 1
  13. WATER = 2
  14. COAL = 3
  15.  
  16. #a dictionary linking resources to colors
  17. colors = {
  18.             DIRT : BROWN,
  19.             GRASS : GREEN,
  20.             WATER : BLUE,
  21.             COAL : BLACK
  22.          }
  23.  
  24. #a list representing the tilemap
  25. tilemap = [
  26.             [GRASS,COAL,DIRT],
  27.             [WATER,WATER,GRASS],
  28.             [COAL,GRASS,WATER],
  29.             [DIRT,GRASS,COAL],
  30.             [GRASS,WATER,DIRT],
  31.           ]
  32.  
  33. #useful game dimensions
  34. TILESIZE = 40
  35. MAPWIDTH = 3
  36. MAPHEIGHT = 5
  37.  
  38. #set up the display
  39. pygame.init()
  40. DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))
  41.  
  42. while True:
  43.  
  44.     #get all user events
  45.     for event in pygame.event.get():
  46.         #if user wants to quit
  47.         if event.type == pygame.QUIT:
  48.             #close the game and window
  49.             pygame.quit()
  50.             sys.exit()
  51.    
  52.     #loop thru each row
  53.     for row in range(MAPHEIGHT):
  54.         #loop thru each column in row
  55.         for column in range(MAPWIDTH):
  56.             #draw the resource at that pos in tilemap, using correct color
  57.             pygame.draw.rect(DISPLAYSURF, colors[tilemap[row][column]], (column*TILESIZE,row*TILESIZE,TILESIZE,TILESIZE))
  58.    
  59.     #update display
  60.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement